Files
otunes/apps/mobile/app/(app)/(tabs)/library/index.tsx
T

183 lines
4.9 KiB
TypeScript

import { useTabletLayout } from "@/hooks/use-tablet-layout"
import { useMemo } from "react"
import { Pressable, StyleSheet, Text, View } from "react-native"
import { SymbolView } from "expo-symbols"
import { Platform } from "react-native"
import { Ionicons } from "@expo/vector-icons"
import { MediaGridList } from "@/components/media-cards"
import { tabContentBottomInset } from "@/components/player"
import { MediaGridSkeleton } from "@/components/skeletons"
import { ProfileHeaderButton } from "@/components/profile-header-button"
import { useCachedPaginatedQuery } from "@/hooks/use-cached-query"
import { useAppNavigation } from "@/lib/app-navigation"
import { api } from "@otunes/convex/_generated/api"
import { appleMusic, spacing, useThemeColors } from "@/lib/theme"
const PAGE_SIZE = 24
const links = [
{
title: "Playlists",
href: "/browse/playlists",
icon: "music.note.list" as const,
},
{ title: "Artists", href: "/browse/artists", icon: "mic.fill" as const },
{ title: "Albums", href: "/browse/albums", icon: "square.stack" as const },
{ title: "Songs", href: "/browse/songs", icon: "music.note" as const },
{
title: "Downloads",
href: "/downloads",
icon: "arrow.down.circle" as const,
},
{
title: "Recently Played",
href: "/recently-played",
icon: "clock" as const,
},
{ title: "Favorites", href: "/playlist/favorites", icon: "star" as const },
]
function LibraryIcon({ name }: { name: (typeof links)[number]["icon"] }) {
if (Platform.OS === "ios") {
return (
<SymbolView
name={name}
size={22}
tintColor={appleMusic.pink}
weight="semibold"
/>
)
}
const ionMap: Record<string, keyof typeof Ionicons.glyphMap> = {
"music.note.list": "list",
"mic.fill": "mic",
"square.stack": "albums",
"music.note": "musical-notes",
clock: "time",
sparkles: "sparkles",
star: "star",
"arrow.down.circle": "arrow-down-circle",
}
return (
<Ionicons
name={ionMap[name] ?? "musical-notes"}
size={22}
color={appleMusic.pink}
/>
)
}
export default function LibraryScreen() {
const tabletLayout = useTabletLayout()
const { push } = useAppNavigation()
const isTablet = tabletLayout
const colors = useThemeColors()
const title = isTablet ? "Recently Added" : "Library"
const { results, status, loadMore, isCached } = useCachedPaginatedQuery(
api.library.browseRecentlyAddedPaginated,
{},
{ initialNumItems: PAGE_SIZE }
)
const listHeader = useMemo(
() => (
<View>
{!isTablet ? (
<>
{links.map((link) => (
<Pressable
key={link.href}
style={styles.row}
onPress={() => push(link.href)}
>
<LibraryIcon name={link.icon} />
<View
style={[
styles.rowContent,
{ borderBottomColor: colors.border },
]}
>
<Text style={[styles.rowTitle, { color: colors.foreground }]}>
{link.title}
</Text>
<Ionicons
name="chevron-forward"
size={16}
color={colors.mutedForeground}
/>
</View>
</Pressable>
))}
<Pressable
style={[styles.section, { paddingHorizontal: spacing.lg }]}
onPress={() => push("/recently-added")}
>
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
Recently Added
</Text>
</Pressable>
</>
) : null}
</View>
),
[colors.border, colors.foreground, colors.mutedForeground, push, isTablet]
)
if (status === "LoadingFirstPage" && !isCached) {
return (
<MediaGridSkeleton
title={title}
titleAccessory={<ProfileHeaderButton />}
contentInset={false}
header={listHeader}
/>
)
}
return (
<MediaGridList
title={title}
titleAccessory={<ProfileHeaderButton />}
content={results}
paddingBottom={tabContentBottomInset}
contentInset={false}
ListHeaderComponent={listHeader}
onEndReached={() => {
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
/>
)
}
const styles = StyleSheet.create({
row: {
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingHorizontal: spacing.lg,
},
rowContent: {
flex: 1,
flexDirection: "row",
alignItems: "center",
gap: spacing.md,
paddingVertical: 14,
borderBottomWidth: StyleSheet.hairlineWidth,
},
rowTitle: {
flex: 1,
fontSize: 20,
fontWeight: "400",
},
section: {
paddingTop: spacing.xl,
paddingBottom: spacing.md,
},
sectionTitle: {
fontSize: 22,
fontWeight: "700",
},
})