- 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
134 lines
4.4 KiB
TypeScript
134 lines
4.4 KiB
TypeScript
import { usePaginatedQuery, useQuery } from "convex/react"
|
|
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
|
|
import { SafeAreaView } from "react-native-safe-area-context"
|
|
|
|
import { ScreenHeader } from "@/components/screen-header"
|
|
import { Button, LoadingView } from "@/components/ui/primitives"
|
|
import { contentBottomInset, SongRow } from "@/components/player"
|
|
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 { spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
const PAGE_SIZE = 50
|
|
|
|
export default function QueueScreen() {
|
|
const colors = useThemeColors()
|
|
const {
|
|
queueSongIds,
|
|
currentSong,
|
|
skipToQueueIndex,
|
|
clearQueue,
|
|
reorderQueue,
|
|
} = usePlayback()
|
|
|
|
const queueVersion = queueSongIds.length
|
|
? `${queueSongIds.length}:${queueSongIds[0]}:${queueSongIds[queueSongIds.length - 1]}`
|
|
: "empty"
|
|
|
|
const { results: queueSongs, status, loadMore } = usePaginatedQuery(
|
|
api.playback.getQueuePaginated,
|
|
{ queueVersion },
|
|
{ initialNumItems: PAGE_SIZE }
|
|
)
|
|
|
|
const historySongs = useQuery(api.playback.getQueueHistory)
|
|
|
|
function moveSong(fromIndex: number, direction: -1 | 1) {
|
|
const toIndex = fromIndex + direction
|
|
if (toIndex < 0 || toIndex >= queueSongIds.length) return
|
|
const next = [...queueSongIds]
|
|
const [item] = next.splice(fromIndex, 1)
|
|
next.splice(toIndex, 0, item)
|
|
void reorderQueue(next)
|
|
}
|
|
|
|
return (
|
|
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]} edges={["top"]}>
|
|
<ScreenHeader
|
|
title="Queue"
|
|
right={
|
|
queueSongIds.length > 0 ? (
|
|
<Button title="Clear" variant="ghost" onPress={() => void clearQueue()} />
|
|
) : null
|
|
}
|
|
/>
|
|
|
|
{queueSongs === undefined ? (
|
|
<LoadingView />
|
|
) : (
|
|
<FlatList
|
|
ListHeaderComponent={
|
|
<View>
|
|
<Text style={[styles.section, { color: colors.mutedForeground }]}>Up Next</Text>
|
|
{queueSongIds.length === 0 ? (
|
|
<Text style={[styles.empty, { color: colors.mutedForeground }]}>
|
|
Queue is empty
|
|
</Text>
|
|
) : null}
|
|
</View>
|
|
}
|
|
data={queueSongs}
|
|
keyExtractor={(item) => `${item.id}-${item.queueIndex}`}
|
|
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
|
onEndReached={() => {
|
|
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
|
|
}}
|
|
renderItem={({ item, index }) => (
|
|
<View>
|
|
<SongRow
|
|
song={item}
|
|
onPress={() => void skipToQueueIndex(item.queueIndex)}
|
|
/>
|
|
<View style={styles.queueActions}>
|
|
<Pressable disabled={index === 0} onPress={() => moveSong(index, -1)}>
|
|
<Text style={{ color: colors.foreground }}>Move up</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
disabled={index === queueSongIds.length - 1}
|
|
onPress={() => moveSong(index, 1)}
|
|
>
|
|
<Text style={{ color: colors.foreground }}>Move down</Text>
|
|
</Pressable>
|
|
{currentSong?.id === item.id ? (
|
|
<Text style={{ color: colors.primary, fontWeight: "700" }}>Now playing</Text>
|
|
) : null}
|
|
</View>
|
|
</View>
|
|
)}
|
|
ListFooterComponent={
|
|
historySongs && historySongs.length > 0 ? (
|
|
<View style={styles.history}>
|
|
<Text style={[styles.section, { color: colors.mutedForeground }]}>History</Text>
|
|
{historySongs.map((song: BrowseSong) => (
|
|
<SongRow key={song.id} song={song} />
|
|
))}
|
|
</View>
|
|
) : null
|
|
}
|
|
/>
|
|
)}
|
|
</SafeAreaView>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
safe: { flex: 1 },
|
|
section: {
|
|
paddingHorizontal: spacing.lg,
|
|
paddingTop: spacing.md,
|
|
fontSize: 12,
|
|
fontWeight: "700",
|
|
textTransform: "uppercase",
|
|
},
|
|
empty: { padding: spacing.lg },
|
|
queueActions: {
|
|
flexDirection: "row",
|
|
gap: spacing.lg,
|
|
paddingHorizontal: spacing.lg,
|
|
paddingBottom: spacing.sm,
|
|
},
|
|
history: { marginTop: spacing.lg },
|
|
})
|