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

131 lines
3.7 KiB
TypeScript

import { usePaginatedQuery } from "convex/react"
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 { LoadingView } from "@/components/ui/primitives"
import { useAppNavigation } from "@/lib/app-navigation"
import { api } from "@otunes/convex/_generated/api"
import { appleMusic, spacing, useThemeColors } from "@/lib/theme"
const PAGE_SIZE = 16
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: "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",
}
return <Ionicons name={ionMap[name] ?? "musical-notes"} size={22} color={appleMusic.pink} />
}
export default function LibraryScreen() {
const { push } = useAppNavigation()
const colors = useThemeColors()
const { results, status, loadMore } = usePaginatedQuery(
api.library.browseRecentlyAddedPaginated,
{},
{ initialNumItems: PAGE_SIZE }
)
const listHeader = useMemo(
() => (
<View>
{links.map((link) => (
<Pressable
key={link.href}
style={[styles.row, { borderBottomColor: colors.border }]}
onPress={() => push(link.href)}
>
<LibraryIcon name={link.icon} />
<Text style={[styles.rowTitle, { color: colors.foreground }]}>{link.title}</Text>
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
</Pressable>
))}
<Pressable
style={[styles.section, { paddingHorizontal: spacing.lg }]}
onPress={() => push("/recently-added")}
>
<Text style={[styles.sectionTitle, { color: colors.foreground }]}>
Recently Added
</Text>
</Pressable>
</View>
),
[colors.border, colors.foreground, colors.mutedForeground, push]
)
if (results === undefined) {
return (
<View style={{ flex: 1, backgroundColor: colors.background }}>
{listHeader}
<LoadingView />
</View>
)
}
return (
<MediaGridList
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,
paddingVertical: 14,
borderBottomWidth: StyleSheet.hairlineWidth,
},
rowTitle: {
flex: 1,
fontSize: 20,
fontWeight: "400",
},
section: {
paddingTop: spacing.xl,
paddingBottom: spacing.md,
},
sectionTitle: {
fontSize: 22,
fontWeight: "700",
},
})