- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
335 lines
8.1 KiB
TypeScript
335 lines
8.1 KiB
TypeScript
import { Ionicons } from "@expo/vector-icons"
|
|
import { memo, useCallback, type ReactElement } from "react"
|
|
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
|
|
|
|
import { CoverImage } from "@/components/cover-image"
|
|
import { MediaContextMenu } from "@/components/media-context-menu"
|
|
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
|
import { useServerConfig } from "@/contexts/server-config"
|
|
import { useAppNavigation } from "@/lib/app-navigation"
|
|
import type {
|
|
BrowseAlbum,
|
|
BrowseArtist,
|
|
PlaylistSummary,
|
|
} from "@otunes/convex/types"
|
|
import { albumCoverUrl, artistCoverUrl, playlistCoverUrl } from "@/lib/urls"
|
|
import {
|
|
carouselCardWidth,
|
|
radius,
|
|
spacing,
|
|
useThemeColors,
|
|
useTwoColumnCardWidth,
|
|
} from "@/lib/theme"
|
|
|
|
export type MediaItem = BrowseAlbum | BrowseArtist | PlaylistSummary
|
|
|
|
export const AlbumCard = memo(function AlbumCard({
|
|
album,
|
|
width = carouselCardWidth,
|
|
}: {
|
|
album: BrowseAlbum
|
|
width?: number
|
|
}) {
|
|
const { push } = useAppNavigation()
|
|
const { config } = useServerConfig()
|
|
const colors = useThemeColors()
|
|
const { handleAlbumAction, isAlbumFavorite } = useMediaMenuActions()
|
|
const onPress = useCallback(() => {
|
|
push(`/album/${album.id}`)
|
|
}, [push, album.id])
|
|
if (!config) return null
|
|
|
|
const card = (
|
|
<Pressable style={[styles.card, { width }]} onPress={onPress}>
|
|
<CoverImage
|
|
uri={albumCoverUrl(config.serverUrl, album.id)}
|
|
hasCover={album.cover != null}
|
|
style={{ width, height: width, borderRadius: radius.md }}
|
|
/>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={2}
|
|
>
|
|
{album.title}
|
|
</Text>
|
|
<Text
|
|
style={[styles.subtitle, { color: colors.mutedForeground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{album.artist.name}
|
|
</Text>
|
|
</Pressable>
|
|
)
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
favorited={isAlbumFavorite(album.id)}
|
|
navigation={{ artist: { id: album.artist.id } }}
|
|
share={{ type: "album", albumId: album.id }}
|
|
onAction={(action) => void handleAlbumAction(album, action)}
|
|
>
|
|
{card}
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
export const ArtistCard = memo(function ArtistCard({
|
|
artist,
|
|
width = carouselCardWidth,
|
|
}: {
|
|
artist: BrowseArtist
|
|
width?: number
|
|
}) {
|
|
const { push } = useAppNavigation()
|
|
const { config } = useServerConfig()
|
|
const colors = useThemeColors()
|
|
const { handleArtistAction, isArtistFavorite } = useMediaMenuActions()
|
|
const onPress = useCallback(() => {
|
|
push(`/artist/${artist.id}`)
|
|
}, [push, artist.id])
|
|
if (!config) return null
|
|
|
|
const card = (
|
|
<Pressable style={[styles.card, { width }]} onPress={onPress}>
|
|
<CoverImage
|
|
uri={artistCoverUrl(config.serverUrl, artist.id)}
|
|
hasCover={artist.photo != null}
|
|
style={{ width, height: width, borderRadius: width / 2 }}
|
|
/>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={2}
|
|
>
|
|
{artist.name}
|
|
</Text>
|
|
</Pressable>
|
|
)
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
favorited={isArtistFavorite(artist.id)}
|
|
share={{ type: "artist", artistId: artist.id }}
|
|
onAction={(action) => void handleArtistAction(artist, action)}
|
|
>
|
|
{card}
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
export const PlaylistCard = memo(function PlaylistCard({
|
|
playlist,
|
|
width = carouselCardWidth,
|
|
}: {
|
|
playlist: PlaylistSummary
|
|
width?: number
|
|
}) {
|
|
const { push } = useAppNavigation()
|
|
const { config } = useServerConfig()
|
|
const colors = useThemeColors()
|
|
const { handlePlaylistAction } = useMediaMenuActions()
|
|
const onPress = useCallback(() => {
|
|
push(`/playlist/${playlist.id}`)
|
|
}, [push, playlist.id])
|
|
if (!config) return null
|
|
|
|
const card = (
|
|
<Pressable style={[styles.card, { width }]} onPress={onPress}>
|
|
<CoverImage
|
|
uri={playlistCoverUrl(config.serverUrl, playlist.id)}
|
|
hasCover={playlist.cover != null}
|
|
style={{ width, height: width, borderRadius: radius.md }}
|
|
/>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={2}
|
|
>
|
|
{playlist.title}
|
|
</Text>
|
|
<Text
|
|
style={[styles.subtitle, { color: colors.mutedForeground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{playlist.owner}
|
|
</Text>
|
|
</Pressable>
|
|
)
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
showFavorite={false}
|
|
share={{ type: "playlist", playlistId: playlist.id }}
|
|
onAction={(action) => void handlePlaylistAction(playlist, action)}
|
|
>
|
|
{card}
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
const CarouselCard = memo(function CarouselCard({
|
|
item,
|
|
width,
|
|
}: {
|
|
item: MediaItem
|
|
width: number
|
|
}) {
|
|
if ("owner" in item) return <PlaylistCard playlist={item} width={width} />
|
|
if ("artist" in item) return <AlbumCard album={item} width={width} />
|
|
return <ArtistCard artist={item} width={width} />
|
|
})
|
|
|
|
export function MediaGrid({ content }: { content: MediaItem[] }) {
|
|
const cardWidth = useTwoColumnCardWidth()
|
|
|
|
return (
|
|
<View style={styles.grid}>
|
|
{content.map((item) => (
|
|
<View key={item.id} style={{ width: cardWidth }}>
|
|
<CarouselCard item={item} width={cardWidth} />
|
|
</View>
|
|
))}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
export function MediaGridList({
|
|
content,
|
|
paddingBottom,
|
|
onEndReached,
|
|
ListHeaderComponent,
|
|
contentInset = true,
|
|
}: {
|
|
content: MediaItem[]
|
|
paddingBottom: number
|
|
onEndReached?: () => void
|
|
ListHeaderComponent?: ReactElement | null
|
|
contentInset?: boolean
|
|
}) {
|
|
const cardWidth = useTwoColumnCardWidth()
|
|
const colors = useThemeColors()
|
|
|
|
return (
|
|
<FlatList
|
|
data={content}
|
|
keyExtractor={(item) => item.id}
|
|
numColumns={2}
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={[
|
|
contentInset ? styles.gridListContent : styles.gridListContentInset,
|
|
{ paddingBottom },
|
|
]}
|
|
columnWrapperStyle={[
|
|
styles.gridListRow,
|
|
!contentInset && styles.gridListRowInset,
|
|
]}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
ListHeaderComponent={ListHeaderComponent}
|
|
onEndReached={onEndReached}
|
|
onEndReachedThreshold={0.5}
|
|
renderItem={({ item }) => (
|
|
<View style={{ width: cardWidth }}>
|
|
<CarouselCard item={item} width={cardWidth} />
|
|
</View>
|
|
)}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function ContentCarouselBody({
|
|
title,
|
|
href,
|
|
content,
|
|
}: {
|
|
title: string
|
|
href?: string
|
|
content: MediaItem[]
|
|
}) {
|
|
const { push } = useAppNavigation()
|
|
const colors = useThemeColors()
|
|
const onHeaderPress = useCallback(() => {
|
|
if (href) {
|
|
push(href)
|
|
}
|
|
}, [href, push])
|
|
|
|
return (
|
|
<View style={styles.section}>
|
|
<Pressable
|
|
disabled={!href}
|
|
onPress={onHeaderPress}
|
|
style={styles.sectionHeader}
|
|
>
|
|
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
|
|
{title}
|
|
</Text>
|
|
{href ? (
|
|
<Ionicons
|
|
name="chevron-forward"
|
|
size={18}
|
|
color={colors.mutedForeground}
|
|
/>
|
|
) : null}
|
|
</Pressable>
|
|
<FlatList
|
|
data={content}
|
|
keyExtractor={(item) => item.id}
|
|
horizontal
|
|
showsHorizontalScrollIndicator={false}
|
|
contentContainerStyle={styles.carouselContent}
|
|
renderItem={({ item }) => (
|
|
<View>
|
|
<CarouselCard item={item} width={carouselCardWidth} />
|
|
</View>
|
|
)}
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
export const ContentCarousel = memo(ContentCarouselBody)
|
|
|
|
const styles = StyleSheet.create({
|
|
card: {
|
|
gap: spacing.xs,
|
|
},
|
|
title: {
|
|
fontSize: 14,
|
|
fontWeight: "600",
|
|
},
|
|
subtitle: {
|
|
fontSize: 12,
|
|
},
|
|
section: {
|
|
gap: spacing.md,
|
|
},
|
|
sectionHeader: {
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: spacing.xs,
|
|
},
|
|
sectionTitle: {
|
|
fontSize: 22,
|
|
fontWeight: "700",
|
|
},
|
|
carouselContent: {
|
|
gap: spacing.md,
|
|
},
|
|
grid: {
|
|
flexDirection: "row",
|
|
flexWrap: "wrap",
|
|
gap: spacing.md,
|
|
},
|
|
gridListContent: {
|
|
padding: spacing.lg,
|
|
gap: spacing.md,
|
|
},
|
|
gridListContentInset: {
|
|
gap: spacing.md,
|
|
},
|
|
gridListRow: {
|
|
gap: spacing.md,
|
|
},
|
|
gridListRowInset: {
|
|
paddingHorizontal: spacing.lg,
|
|
},
|
|
})
|