87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { usePaginatedQuery } from "convex/react"
|
|
import { memo, useRef } from "react"
|
|
import { ScrollView, StyleSheet } from "react-native"
|
|
|
|
import { ContentCarousel } from "@/components/media-cards"
|
|
import { LoadingView } from "@/components/ui/primitives"
|
|
import { tabContentBottomInset } from "@/components/player"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { BrowseAlbum, PlaylistSummary } from "@otunes/convex/types"
|
|
import { spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
const CAROUSEL_ITEM_LIMIT = 10
|
|
|
|
const HomeCarousels = memo(function HomeCarousels({
|
|
recentlyAdded,
|
|
recentlyPlayed,
|
|
}: {
|
|
recentlyAdded: BrowseAlbum[]
|
|
recentlyPlayed: (BrowseAlbum | PlaylistSummary)[]
|
|
}) {
|
|
return (
|
|
<>
|
|
{recentlyPlayed.length > 0 ? (
|
|
<ContentCarousel
|
|
title="Recently Played"
|
|
href="/recently-played"
|
|
content={recentlyPlayed}
|
|
/>
|
|
) : null}
|
|
<ContentCarousel
|
|
title="Recently Added"
|
|
href="/recently-added"
|
|
content={recentlyAdded}
|
|
/>
|
|
</>
|
|
)
|
|
})
|
|
|
|
export default function HomeScreen() {
|
|
const colors = useThemeColors()
|
|
const { results: recentlyAdded } = usePaginatedQuery(
|
|
api.library.browseRecentlyAddedPaginated,
|
|
{},
|
|
{ initialNumItems: CAROUSEL_ITEM_LIMIT }
|
|
)
|
|
const { results: recentlyPlayed } = usePaginatedQuery(
|
|
api.library.browseRecentlyPlayedPaginated,
|
|
{},
|
|
{ initialNumItems: CAROUSEL_ITEM_LIMIT }
|
|
)
|
|
const cachedData = useRef<{
|
|
recentlyAdded?: BrowseAlbum[]
|
|
recentlyPlayed?: (BrowseAlbum | PlaylistSummary)[]
|
|
}>({})
|
|
|
|
if (recentlyAdded !== undefined) {
|
|
cachedData.current.recentlyAdded = recentlyAdded
|
|
}
|
|
if (recentlyPlayed !== undefined) {
|
|
cachedData.current.recentlyPlayed = recentlyPlayed
|
|
}
|
|
|
|
const added = recentlyAdded ?? cachedData.current.recentlyAdded
|
|
const played = recentlyPlayed ?? cachedData.current.recentlyPlayed
|
|
|
|
if (added === undefined || played === undefined) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
return (
|
|
<ScrollView
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={[styles.content, { paddingBottom: tabContentBottomInset }]}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
>
|
|
<HomeCarousels recentlyAdded={added} recentlyPlayed={played} />
|
|
</ScrollView>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
content: {
|
|
padding: spacing.lg,
|
|
gap: spacing.xl,
|
|
},
|
|
})
|