- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
106 lines
2.5 KiB
TypeScript
106 lines
2.5 KiB
TypeScript
import { Ionicons } from "@expo/vector-icons"
|
|
import { Image, type ImageContentFit, type ImageStyle } from "expo-image"
|
|
import { memo } from "react"
|
|
import {
|
|
PixelRatio,
|
|
StyleProp,
|
|
StyleSheet,
|
|
View,
|
|
type ViewStyle,
|
|
} from "react-native"
|
|
|
|
import { useThemeColors } from "@/lib/theme"
|
|
import {
|
|
coverImageSizes,
|
|
type CoverImageSize,
|
|
withCoverImageSize,
|
|
} from "@/lib/urls"
|
|
|
|
const DEFAULT_SOURCE_SIZE: CoverImageSize = 1024
|
|
|
|
function sourceSize(style: ImageStyle): CoverImageSize {
|
|
const layoutSizes = [
|
|
style.width,
|
|
style.height,
|
|
style.maxWidth,
|
|
style.maxHeight,
|
|
].filter((value): value is number => typeof value === "number")
|
|
|
|
if (layoutSizes.length === 0) return DEFAULT_SOURCE_SIZE
|
|
|
|
const requiredPixels = Math.max(...layoutSizes) * PixelRatio.get()
|
|
return (
|
|
coverImageSizes.find((size) => size >= requiredPixels) ??
|
|
DEFAULT_SOURCE_SIZE
|
|
)
|
|
}
|
|
|
|
function placeholderIconSize(style: ImageStyle): number {
|
|
const { width, height, maxWidth } = style
|
|
if (typeof width === "number" && typeof height === "number") {
|
|
return Math.min(width, height) * 0.38
|
|
}
|
|
if (typeof width === "number") {
|
|
return width * 0.38
|
|
}
|
|
if (typeof maxWidth === "number") {
|
|
return maxWidth * 0.38
|
|
}
|
|
return 48
|
|
}
|
|
|
|
export const CoverImage = memo(function CoverImage({
|
|
uri,
|
|
hasCover,
|
|
style,
|
|
contentFit = "cover",
|
|
blurRadius,
|
|
placeholderVariant = "icon",
|
|
}: {
|
|
uri: string
|
|
hasCover: boolean
|
|
style?: StyleProp<ImageStyle>
|
|
contentFit?: ImageContentFit
|
|
blurRadius?: number
|
|
placeholderVariant?: "icon" | "solid"
|
|
}) {
|
|
const colors = useThemeColors()
|
|
const flatStyle = StyleSheet.flatten(style) ?? {}
|
|
|
|
if (!hasCover) {
|
|
const placeholderStyle: ViewStyle = {
|
|
backgroundColor: colors.coverPlaceholder,
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
overflow: "hidden",
|
|
}
|
|
|
|
return (
|
|
<View style={[style, placeholderStyle]}>
|
|
{placeholderVariant === "icon" ? (
|
|
<Ionicons
|
|
name="musical-note"
|
|
size={placeholderIconSize(flatStyle)}
|
|
color={colors.coverPlaceholderIcon}
|
|
/>
|
|
) : null}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
const sourceUri = withCoverImageSize(uri, sourceSize(flatStyle))
|
|
|
|
return (
|
|
<Image
|
|
source={{ uri: sourceUri }}
|
|
style={[style, { backgroundColor: colors.coverPlaceholder }]}
|
|
contentFit={contentFit}
|
|
transition={150}
|
|
blurRadius={blurRadius}
|
|
cachePolicy="memory-disk"
|
|
recyclingKey={sourceUri}
|
|
enforceEarlyResizing
|
|
/>
|
|
)
|
|
})
|