Files
otunes/apps/mobile/app/(app)/library/songs.tsx
T
joshuafhiggins 73c7283d4d Add Expo mobile app and extract shared Convex package
- Replace mobile placeholder with Expo Router app (tabs, playback, auth, library)
- Move Convex backend to @otunes/convex for web and mobile reuse
- Update web app imports and API routes to use shared package
2026-08-02 04:14:01 +00:00

137 lines
4.5 KiB
TypeScript

import { usePaginatedQuery } from "convex/react"
import { useState } from "react"
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
import { SafeAreaView } from "react-native-safe-area-context"
import { MediaActionsSheet, type MediaAction } from "@/components/media-actions-sheet"
import { ScreenHeader } from "@/components/screen-header"
import { contentBottomInset, SongRow } from "@/components/player"
import { LoadingView } from "@/components/ui/primitives"
import { useAddToPlaylist } from "@/contexts/add-to-playlist"
import { useFavorites } from "@/contexts/favorites"
import { usePlayback } from "@/contexts/playback"
import { api } from "@otunes/convex/_generated/api"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong } from "@otunes/convex/types"
import { showSuccess } from "@/lib/toast"
import { spacing, useThemeColors } from "@/lib/theme"
const PAGE_SIZE = 50
export default function SongsScreen() {
const colors = useThemeColors()
const { playFromLibrarySong, addToQueueNext, addToQueueLast, playShuffled } = usePlayback()
const { openAddToPlaylist } = useAddToPlaylist()
const { isSongFavorite, toggleSongFavorite } = useFavorites()
const [sortKey, setSortKey] = useState<"title" | "artist" | "album">("title")
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc")
const [actionSong, setActionSong] = useState<BrowseSong | null>(null)
const { results, status, loadMore } = usePaginatedQuery(
api.library.browseSongsPaginated,
{ sortKey, sortDirection },
{ initialNumItems: PAGE_SIZE }
)
function cycleSort(key: "title" | "artist" | "album") {
if (sortKey === key) {
setSortDirection((d) => (d === "asc" ? "desc" : "asc"))
} else {
setSortKey(key)
setSortDirection("asc")
}
}
function handleAction(action: MediaAction) {
if (!actionSong) return
const songIds = [actionSong.id]
switch (action) {
case "playlist":
openAddToPlaylist(songIds)
break
case "play-next":
void addToQueueNext(songIds).then(() => showSuccess("Queued to play next"))
break
case "play-last":
void addToQueueLast(songIds).then(() => showSuccess("Queued to play last"))
break
case "shuffle":
void playShuffled(songIds)
break
case "favorite":
void toggleSongFavorite(actionSong.id)
break
default:
break
}
}
if (results === undefined) {
return (
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
<ScreenHeader title="Songs" />
<LoadingView />
</SafeAreaView>
)
}
return (
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
<ScreenHeader title="Songs" />
<View style={[styles.sortRow, { borderBottomColor: colors.border }]}>
{(["title", "artist", "album"] as const).map((key) => (
<Pressable key={key} onPress={() => cycleSort(key)} style={styles.sortButton}>
<Text
style={{
color: sortKey === key ? colors.foreground : colors.mutedForeground,
fontWeight: sortKey === key ? "700" : "500",
}}
>
{key[0].toUpperCase() + key.slice(1)}
{sortKey === key ? (sortDirection === "asc" ? " ↑" : " ↓") : ""}
</Text>
</Pressable>
))}
</View>
<FlatList
data={results}
keyExtractor={(item) => item.id}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
onEndReached={() => {
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
onEndReachedThreshold={0.5}
renderItem={({ item }) => (
<SongRow
song={item}
onPress={() =>
void playFromLibrarySong(item.id as Id<"songs">, { sortKey, sortDirection })
}
onLongPress={() => setActionSong(item)}
/>
)}
/>
<MediaActionsSheet
visible={Boolean(actionSong)}
title={actionSong?.title ?? ""}
favorited={actionSong ? isSongFavorite(actionSong.id) : false}
onClose={() => setActionSong(null)}
onAction={handleAction}
/>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
safe: { flex: 1 },
sortRow: {
flexDirection: "row",
justifyContent: "space-around",
paddingVertical: spacing.sm,
borderBottomWidth: StyleSheet.hairlineWidth,
},
sortButton: { padding: spacing.sm },
})