Files
otunes/apps/mobile/components/skeletons.tsx
T

296 lines
7.3 KiB
TypeScript

import { useEffect, useRef, type ReactNode } from "react"
import {
Animated,
ScrollView,
StyleSheet,
View,
type DimensionValue,
type StyleProp,
type ViewStyle,
} from "react-native"
import { contentBottomInset } from "@/components/player"
import {
carouselCardWidth,
radius,
spacing,
useThemeColors,
useTwoColumnCardWidth,
} from "@/lib/theme"
/** Match the inset-adjusted lists used once screen data is available. */
function SkeletonContent({
children,
embedded = false,
}: {
children: ReactNode
embedded?: boolean
}) {
const colors = useThemeColors()
if (embedded) return <>{children}</>
return (
<ScrollView
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
scrollEnabled={false}
accessibilityLabel="Loading"
>
{children}
</ScrollView>
)
}
export function Skeleton({ style }: { style?: StyleProp<ViewStyle> }) {
const colors = useThemeColors()
const opacity = useRef(new Animated.Value(1)).current
useEffect(() => {
const pulse = Animated.loop(
Animated.sequence([
Animated.timing(opacity, {
toValue: 0.5,
duration: 800,
useNativeDriver: true,
}),
Animated.timing(opacity, {
toValue: 1,
duration: 800,
useNativeDriver: true,
}),
])
)
pulse.start()
return () => pulse.stop()
}, [opacity])
return (
<Animated.View
style={[
{ backgroundColor: colors.muted, borderRadius: radius.sm, opacity },
style,
]}
/>
)
}
function TextLine({
width,
height = 14,
}: {
width: DimensionValue
height?: number
}) {
return <Skeleton style={{ width, height, borderRadius: 4 }} />
}
export function SongRowSkeleton() {
const colors = useThemeColors()
return (
<View style={[styles.songRow, { borderBottomColor: colors.border }]}>
<Skeleton style={styles.songArtwork} />
<View style={styles.songMeta}>
<TextLine width="60%" height={16} />
<TextLine width="40%" />
</View>
</View>
)
}
export function SongListSkeleton({
rows = 10,
embedded = false,
}: {
rows?: number
embedded?: boolean
}) {
return (
<SkeletonContent embedded={embedded}>
<View accessibilityLabel="Loading" accessibilityRole="progressbar">
{Array.from({ length: rows }, (_, index) => (
<SongRowSkeleton key={index} />
))}
</View>
</SkeletonContent>
)
}
function CardSkeleton({
width,
round = false,
}: {
width: number
round?: boolean
}) {
return (
<View style={[styles.card, { width }]}>
<Skeleton
style={{
width,
height: width,
borderRadius: round ? width / 2 : radius.md,
}}
/>
<TextLine width="80%" />
{round ? null : <TextLine width="50%" height={12} />}
</View>
)
}
export function MediaGridSkeleton({
items = 6,
contentInset = true,
embedded = false,
}: {
items?: number
contentInset?: boolean
embedded?: boolean
}) {
const cardWidth = useTwoColumnCardWidth()
return (
<SkeletonContent embedded={embedded}>
<View
accessibilityLabel="Loading"
accessibilityRole="progressbar"
style={[
styles.grid,
contentInset ? styles.gridInset : styles.gridRowInset,
]}
>
{Array.from({ length: items }, (_, index) => (
<CardSkeleton key={index} width={cardWidth} />
))}
</View>
</SkeletonContent>
)
}
export function CarouselSkeleton() {
return (
<View style={styles.carousel}>
<TextLine width={160} height={22} />
<View style={styles.carouselRow}>
{Array.from({ length: 3 }, (_, index) => (
<CardSkeleton key={index} width={carouselCardWidth} />
))}
</View>
</View>
)
}
export function HomeSkeleton() {
const colors = useThemeColors()
return (
<ScrollView
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={styles.home}
contentInsetAdjustmentBehavior="automatic"
scrollEnabled={false}
accessibilityLabel="Loading"
>
<CarouselSkeleton />
<CarouselSkeleton />
</ScrollView>
)
}
/** Album and playlist pages: cover, title, play buttons, then tracks. */
export function CollectionDetailSkeleton({
round = false,
}: {
round?: boolean
}) {
const colors = useThemeColors()
const coverSize = round ? 180 : 210
return (
<ScrollView
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
scrollEnabled={false}
accessibilityLabel="Loading"
>
<View style={styles.hero}>
<Skeleton
style={{
width: coverSize,
height: coverSize,
borderRadius: round ? coverSize / 2 : radius.lg,
}}
/>
<TextLine width={200} height={24} />
<TextLine width={120} height={16} />
<View style={styles.actions}>
<Skeleton style={styles.iconAction} />
<Skeleton style={styles.playButton} />
<Skeleton style={styles.iconAction} />
</View>
</View>
{round ? (
<MediaGridSkeleton items={4} contentInset={false} embedded />
) : (
<SongListSkeleton rows={8} embedded />
)}
</ScrollView>
)
}
export function ArtistListSkeleton({ rows = 12 }: { rows?: number }) {
const colors = useThemeColors()
return (
<SkeletonContent>
<View accessibilityLabel="Loading" accessibilityRole="progressbar">
{Array.from({ length: rows }, (_, index) => (
<View
key={index}
style={[styles.artistRow, { borderBottomColor: colors.border }]}
>
<Skeleton style={styles.artistAvatar} />
<TextLine width="50%" height={16} />
</View>
))}
</View>
</SkeletonContent>
)
}
const styles = StyleSheet.create({
songRow: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
minHeight: 64,
paddingVertical: 10,
paddingHorizontal: spacing.lg,
borderBottomWidth: StyleSheet.hairlineWidth,
},
songArtwork: { width: 44, height: 44, borderRadius: 6 },
songMeta: { flex: 1, gap: 6 },
card: { gap: spacing.xs },
grid: { flexDirection: "row", flexWrap: "wrap", gap: spacing.md },
gridInset: { padding: spacing.lg },
gridRowInset: { paddingHorizontal: spacing.lg },
carousel: { gap: spacing.md, overflow: "hidden" },
carouselRow: { flexDirection: "row", gap: spacing.md },
home: { padding: spacing.lg, gap: spacing.xl },
hero: { alignItems: "center", padding: spacing.lg, gap: spacing.sm },
actions: {
flexDirection: "row",
alignItems: "center",
gap: spacing.sm,
marginTop: spacing.sm,
},
iconAction: { width: 44, height: 44, borderRadius: 22 },
playButton: { width: 160, height: 44, borderRadius: 999 },
artistRow: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingHorizontal: spacing.lg,
paddingVertical: spacing.md,
borderBottomWidth: StyleSheet.hairlineWidth,
},
artistAvatar: { width: 44, height: 44, borderRadius: 22 },
})