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

350 lines
9.0 KiB
TypeScript

import { useMediaGridLayout } from "@/hooks/use-media-grid-layout"
import {
CollectionHero,
useCollectionHeroLayout,
} from "@/components/collection-hero"
import { Link } from "expo-router"
import { useHeaderHeight } from "expo-router/react-navigation"
import Reanimated from "react-native-reanimated"
import { type ReactNode, type ComponentProps } from "react"
import {
Platform,
ScrollView,
StyleSheet,
View,
type DimensionValue,
type StyleProp,
type ViewStyle,
} from "react-native"
import { contentBottomInset } from "@/components/player"
import { carouselCardWidth, radius, spacing, useThemeColors } from "@/lib/theme"
/** Reserve the transparent native header explicitly while skeletons cannot scroll. */
function SkeletonContent({
children,
embedded = false,
}: {
children: ReactNode
embedded?: boolean
}) {
const colors = useThemeColors()
const headerHeight = useHeaderHeight()
if (embedded) return <>{children}</>
return (
<ScrollView
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{
paddingTop: Platform.OS === "ios" ? headerHeight : 0,
paddingBottom: contentBottomInset,
}}
contentInsetAdjustmentBehavior="never"
scrollEnabled={false}
accessibilityLabel="Loading"
>
{children}
</ScrollView>
)
}
export function Skeleton({ style }: { style?: StyleProp<ViewStyle> }) {
const colors = useThemeColors()
return (
<View
style={[
{ backgroundColor: colors.muted, borderRadius: radius.sm },
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, columns, onLayout, horizontalPadding, gap, ready } =
useMediaGridLayout()
return (
<SkeletonContent embedded={embedded}>
<View
onLayout={onLayout}
accessibilityLabel="Loading"
accessibilityRole="progressbar"
style={[
styles.grid,
contentInset ? styles.gridInset : styles.gridRowInset,
{
paddingHorizontal: horizontalPadding,
paddingTop: contentInset ? horizontalPadding : 0,
gap,
},
]}
>
{Array.from(
{ length: ready ? Math.max(items, columns * 3) : 0 },
(_, 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 headerHeight = useHeaderHeight()
const colors = useThemeColors()
return (
<ScrollView
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={[
styles.home,
{ paddingTop: (Platform.OS === "ios" ? headerHeight : 0) + spacing.lg },
]}
contentInsetAdjustmentBehavior="never"
scrollEnabled={false}
accessibilityLabel="Loading"
>
<CarouselSkeleton />
<CarouselSkeleton />
</ScrollView>
)
}
/** Album and playlist pages: cover, title, play buttons, then tracks. */
export function CollectionDetailSkeleton({
zoomCoverSize,
style,
round = false,
backgroundColor,
onLayout,
}: {
zoomCoverSize?: number
style?: ComponentProps<typeof Reanimated.ScrollView>["style"]
round?: boolean
backgroundColor?: string
onLayout?: () => void
}) {
const colors = useThemeColors()
const headerHeight = useHeaderHeight()
const { landscape, coverSize: tabletCoverSize } = useCollectionHeroLayout()
const coverSize =
!round && landscape
? tabletCoverSize!
: (zoomCoverSize ?? (round ? 180 : 210))
const actions = (
<View style={styles.actions}>
<Skeleton style={styles.iconAction} />
<Skeleton style={styles.playButton} />
<Skeleton style={styles.iconAction} />
</View>
)
const artwork = (
<Skeleton
style={{
width: coverSize,
height: coverSize,
borderRadius: round ? coverSize / 2 : radius.lg,
}}
/>
)
return (
<Reanimated.ScrollView
onLayout={onLayout}
style={[
{ flex: 1, backgroundColor: backgroundColor ?? colors.background },
style,
]}
contentContainerStyle={{
paddingTop: Platform.OS === "ios" ? headerHeight : 0,
paddingBottom: contentBottomInset,
}}
contentInsetAdjustmentBehavior="never"
scrollEnabled={false}
accessibilityLabel="Loading"
>
{round ? (
<View style={styles.artistHero}>
{zoomCoverSize ? (
<Link.AppleZoomTarget>
<View collapsable={false}>{artwork}</View>
</Link.AppleZoomTarget>
) : (
artwork
)}
<TextLine width={200} height={24} />
<TextLine width={120} height={16} />
{actions}
</View>
) : (
<CollectionHero
artwork={
zoomCoverSize ? (
<Link.AppleZoomTarget>
<View collapsable={false}>{artwork}</View>
</Link.AppleZoomTarget>
) : (
artwork
)
}
actions={actions}
>
<TextLine width={landscape && !round ? "85%" : 200} height={24} />
<TextLine
width={landscape && !round ? "60%" : 120}
height={landscape && !round ? 22 : 16}
/>
<TextLine width={landscape && !round ? "70%" : 150} height={14} />
</CollectionHero>
)}
{round ? (
<MediaGridSkeleton items={4} contentInset={false} embedded />
) : (
<SongListSkeleton rows={8} embedded />
)}
</Reanimated.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 },
artistHero: { 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 },
})