82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import { memo } from "react"
|
|
import { ScrollView, StyleSheet } from "react-native"
|
|
|
|
import { ContentCarousel } from "@/components/media-cards"
|
|
import { HomeSkeleton } from "@/components/skeletons"
|
|
import { tabContentBottomInset } from "@/components/player"
|
|
import { useCachedPaginatedQuery } from "@/hooks/use-cached-query"
|
|
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 recentlyAdded = useCachedPaginatedQuery(
|
|
api.library.browseRecentlyAddedPaginated,
|
|
{},
|
|
{ initialNumItems: CAROUSEL_ITEM_LIMIT }
|
|
)
|
|
const recentlyPlayed = useCachedPaginatedQuery(
|
|
api.library.browseRecentlyPlayedPaginated,
|
|
{},
|
|
{ initialNumItems: CAROUSEL_ITEM_LIMIT }
|
|
)
|
|
|
|
const isLoading = [recentlyAdded, recentlyPlayed].some(
|
|
(result) => result.status === "LoadingFirstPage" && !result.isCached
|
|
)
|
|
if (isLoading) {
|
|
return <HomeSkeleton />
|
|
}
|
|
|
|
const added = recentlyAdded.results.slice(0, CAROUSEL_ITEM_LIMIT)
|
|
const played = recentlyPlayed.results.slice(0, CAROUSEL_ITEM_LIMIT)
|
|
|
|
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,
|
|
},
|
|
})
|