470 lines
12 KiB
TypeScript
470 lines
12 KiB
TypeScript
import { useTabletLayout } from "@/hooks/use-tablet-layout"
|
|
import Animated from "react-native-reanimated"
|
|
import { tabletLayoutTransition } from "@/lib/tablet-motion"
|
|
import { TabletMediaGrid } from "@/components/tablet-media-grid"
|
|
import { useMediaGridLayout } from "@/hooks/use-media-grid-layout"
|
|
import { MusicEntityView } from "@/components/music-entity-view"
|
|
import { Link } from "expo-router"
|
|
import { Ionicons } from "@expo/vector-icons"
|
|
import { memo, useCallback, type ReactElement } from "react"
|
|
import {
|
|
FlatList,
|
|
Platform,
|
|
Pressable,
|
|
ScrollView,
|
|
StyleSheet,
|
|
Text,
|
|
View,
|
|
type ListRenderItemInfo,
|
|
} 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 { longListProps } from "@/lib/list"
|
|
import type {
|
|
BrowseAlbum,
|
|
BrowseArtist,
|
|
PlaylistSummary,
|
|
} from "@otunes/convex/types"
|
|
import { albumCoverUrl, artistCoverUrl, playlistCoverUrl } from "@/lib/urls"
|
|
import { carouselCardWidth, radius, spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
export type MediaItem = BrowseAlbum | BrowseArtist | PlaylistSummary
|
|
|
|
const GRID_INITIAL_ROWS = 5
|
|
const MAX_NON_VIRTUAL_CAROUSEL_ITEMS = 10
|
|
|
|
export const AlbumCard = memo(function AlbumCard({
|
|
album,
|
|
width = carouselCardWidth,
|
|
fluid = false,
|
|
}: {
|
|
album: BrowseAlbum
|
|
width?: number
|
|
fluid?: boolean
|
|
}) {
|
|
const { href } = useAppNavigation()
|
|
const tabletLayout = useTabletLayout()
|
|
const { config } = useServerConfig()
|
|
const colors = useThemeColors()
|
|
const { handleAlbumAction, isAlbumFavorite } = useMediaMenuActions()
|
|
if (!config) return null
|
|
|
|
const card = (
|
|
<Link href={href(`/album/${album.id}`)} push asChild>
|
|
<Pressable
|
|
style={StyleSheet.flatten([
|
|
styles.card,
|
|
{ width: fluid ? "100%" : width },
|
|
])}
|
|
>
|
|
<Link.AppleZoom>
|
|
<View collapsable={false}>
|
|
<CoverImage
|
|
uri={albumCoverUrl(config.serverUrl, album.id)}
|
|
hasCover={album.cover != null}
|
|
style={
|
|
fluid
|
|
? { width: "100%", aspectRatio: 1, borderRadius: radius.md }
|
|
: { width, height: width, borderRadius: radius.md }
|
|
}
|
|
/>
|
|
</View>
|
|
</Link.AppleZoom>
|
|
<MusicEntityView kind="album" id={album.id} style={styles.card}>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={tabletLayout ? 1 : 2}
|
|
>
|
|
{album.title}
|
|
</Text>
|
|
<Text
|
|
style={[styles.subtitle, { color: colors.mutedForeground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{album.artist.name}
|
|
</Text>
|
|
</MusicEntityView>
|
|
</Pressable>
|
|
</Link>
|
|
)
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
favorited={isAlbumFavorite(album.id)}
|
|
stationArtistId={album.artist.id}
|
|
navigation={{ artist: { id: album.artist.id } }}
|
|
share={{ type: "album", albumId: album.id, title: album.title }}
|
|
onAction={(action) => void handleAlbumAction(album, action)}
|
|
>
|
|
{card}
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
export const ArtistCard = memo(function ArtistCard({
|
|
artist,
|
|
width = carouselCardWidth,
|
|
fluid = false,
|
|
}: {
|
|
artist: BrowseArtist
|
|
width?: number
|
|
fluid?: boolean
|
|
}) {
|
|
const { push } = useAppNavigation()
|
|
const tabletLayout = useTabletLayout()
|
|
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: fluid ? "100%" : width }]}
|
|
onPress={onPress}
|
|
>
|
|
<CoverImage
|
|
uri={artistCoverUrl(config.serverUrl, artist.id)}
|
|
hasCover={artist.photo != null}
|
|
style={
|
|
fluid
|
|
? { width: "100%", aspectRatio: 1, borderRadius: 9999 }
|
|
: { width, height: width, borderRadius: width / 2 }
|
|
}
|
|
/>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={tabletLayout ? 1 : 2}
|
|
>
|
|
{artist.name}
|
|
</Text>
|
|
</Pressable>
|
|
)
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
favorited={isArtistFavorite(artist.id)}
|
|
share={{ type: "artist", artistId: artist.id, title: artist.name }}
|
|
onAction={(action) => void handleArtistAction(artist, action)}
|
|
>
|
|
<MusicEntityView kind="artist" id={artist.id}>
|
|
{card}
|
|
</MusicEntityView>
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
export const PlaylistCard = memo(function PlaylistCard({
|
|
playlist,
|
|
width = carouselCardWidth,
|
|
fluid = false,
|
|
}: {
|
|
playlist: PlaylistSummary
|
|
width?: number
|
|
fluid?: boolean
|
|
}) {
|
|
const { href } = useAppNavigation()
|
|
const tabletLayout = useTabletLayout()
|
|
const { config } = useServerConfig()
|
|
const colors = useThemeColors()
|
|
const { handlePlaylistAction } = useMediaMenuActions()
|
|
if (!config) return null
|
|
|
|
const card = (
|
|
<Link href={href(`/playlist/${playlist.id}`)} push asChild>
|
|
<Pressable
|
|
style={StyleSheet.flatten([
|
|
styles.card,
|
|
{ width: fluid ? "100%" : width },
|
|
])}
|
|
>
|
|
<Link.AppleZoom>
|
|
<View collapsable={false}>
|
|
<CoverImage
|
|
uri={playlistCoverUrl(config.serverUrl, playlist.id)}
|
|
hasCover={playlist.cover != null}
|
|
style={
|
|
fluid
|
|
? { width: "100%", aspectRatio: 1, borderRadius: radius.md }
|
|
: { width, height: width, borderRadius: radius.md }
|
|
}
|
|
/>
|
|
</View>
|
|
</Link.AppleZoom>
|
|
<MusicEntityView kind="playlist" id={playlist.id} style={styles.card}>
|
|
<Text
|
|
style={[styles.title, { color: colors.foreground }]}
|
|
numberOfLines={tabletLayout ? 1 : 2}
|
|
>
|
|
{playlist.title}
|
|
</Text>
|
|
<Text
|
|
style={[styles.subtitle, { color: colors.mutedForeground }]}
|
|
numberOfLines={1}
|
|
>
|
|
{playlist.owner}
|
|
</Text>
|
|
</MusicEntityView>
|
|
</Pressable>
|
|
</Link>
|
|
)
|
|
|
|
return (
|
|
<MediaContextMenu
|
|
showFavorite={false}
|
|
share={{
|
|
type: "playlist",
|
|
playlistId: playlist.id,
|
|
title: playlist.title,
|
|
}}
|
|
onAction={(action) => void handlePlaylistAction(playlist, action)}
|
|
>
|
|
{card}
|
|
</MediaContextMenu>
|
|
)
|
|
})
|
|
|
|
const CarouselCard = memo(function CarouselCard({
|
|
item,
|
|
width,
|
|
fluid = false,
|
|
}: {
|
|
item: MediaItem
|
|
width: number
|
|
fluid?: boolean
|
|
}) {
|
|
if ("owner" in item)
|
|
return <PlaylistCard playlist={item} width={width} fluid={fluid} />
|
|
if ("artist" in item)
|
|
return <AlbumCard album={item} width={width} fluid={fluid} />
|
|
return <ArtistCard artist={item} width={width} fluid={fluid} />
|
|
})
|
|
|
|
export function MediaGrid({ content }: { content: MediaItem[] }) {
|
|
const tabletLayout = useTabletLayout()
|
|
const { cardWidth, onLayout, ready } = useMediaGridLayout()
|
|
|
|
return (
|
|
<View style={styles.grid} onLayout={onLayout}>
|
|
{(ready ? content : []).map((item) => (
|
|
<Animated.View
|
|
key={item.id}
|
|
layout={tabletLayout ? tabletLayoutTransition : undefined}
|
|
style={{ width: cardWidth }}
|
|
>
|
|
<CarouselCard item={item} width={cardWidth} fluid={tabletLayout} />
|
|
</Animated.View>
|
|
))}
|
|
</View>
|
|
)
|
|
}
|
|
|
|
function renderTabletCard(item: MediaItem, width: number) {
|
|
return <CarouselCard item={item} width={width} fluid />
|
|
}
|
|
|
|
export function MediaGridList({
|
|
content,
|
|
paddingBottom,
|
|
onEndReached,
|
|
ListHeaderComponent,
|
|
contentInset = true,
|
|
}: {
|
|
content: MediaItem[]
|
|
paddingBottom: number
|
|
onEndReached?: () => void
|
|
ListHeaderComponent?: ReactElement
|
|
contentInset?: boolean
|
|
}) {
|
|
const tabletLayout = useTabletLayout()
|
|
const { columns, cardWidth, onLayout, horizontalPadding, gap } =
|
|
useMediaGridLayout()
|
|
const colors = useThemeColors()
|
|
const renderItem = useCallback(
|
|
({ item }: ListRenderItemInfo<MediaItem>) => (
|
|
<View style={{ width: cardWidth }}>
|
|
<CarouselCard item={item} width={cardWidth} />
|
|
</View>
|
|
),
|
|
[cardWidth]
|
|
)
|
|
|
|
if (tabletLayout) {
|
|
return (
|
|
<TabletMediaGrid
|
|
content={content}
|
|
paddingBottom={paddingBottom}
|
|
onEndReached={onEndReached}
|
|
ListHeaderComponent={ListHeaderComponent}
|
|
contentInset={contentInset}
|
|
renderCard={renderTabletCard}
|
|
/>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<FlatList
|
|
key={columns}
|
|
onLayout={onLayout}
|
|
data={content}
|
|
keyExtractor={mediaItemKey}
|
|
renderItem={renderItem}
|
|
numColumns={columns}
|
|
initialNumToRender={GRID_INITIAL_ROWS}
|
|
maxToRenderPerBatch={GRID_INITIAL_ROWS}
|
|
windowSize={longListProps.windowSize}
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={[
|
|
contentInset ? styles.gridListContent : styles.gridListContentInset,
|
|
{
|
|
paddingBottom,
|
|
gap,
|
|
...(contentInset ? { paddingHorizontal: horizontalPadding } : {}),
|
|
},
|
|
]}
|
|
columnWrapperStyle={[
|
|
styles.gridListRow,
|
|
!contentInset && { paddingHorizontal: horizontalPadding },
|
|
{ gap },
|
|
]}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
ListHeaderComponent={ListHeaderComponent}
|
|
onEndReached={onEndReached}
|
|
onEndReachedThreshold={0.5}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function mediaItemKey(item: MediaItem) {
|
|
return item.id
|
|
}
|
|
|
|
function renderCarouselItem({ item }: ListRenderItemInfo<MediaItem>) {
|
|
return (
|
|
<View>
|
|
<CarouselCard item={item} width={carouselCardWidth} />
|
|
</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>
|
|
{Platform.OS === "android" &&
|
|
content.length <= MAX_NON_VIRTUAL_CAROUSEL_ITEMS ? (
|
|
<ScrollView
|
|
horizontal
|
|
style={styles.androidCarousel}
|
|
showsHorizontalScrollIndicator={false}
|
|
contentContainerStyle={styles.carouselContent}
|
|
>
|
|
{content.map((item) => (
|
|
<View key={mediaItemKey(item)}>
|
|
<CarouselCard item={item} width={carouselCardWidth} />
|
|
</View>
|
|
))}
|
|
</ScrollView>
|
|
) : (
|
|
<FlatList
|
|
data={content}
|
|
keyExtractor={mediaItemKey}
|
|
renderItem={renderCarouselItem}
|
|
horizontal
|
|
showsHorizontalScrollIndicator={false}
|
|
contentContainerStyle={styles.carouselContent}
|
|
/>
|
|
)}
|
|
</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",
|
|
},
|
|
androidCarousel: {
|
|
// Horizontal lists should measure their cards, not stretch the Home sections.
|
|
flexGrow: 0,
|
|
},
|
|
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,
|
|
},
|
|
})
|