Files
otunes/apps/mobile/app/(app)/(tabs)/home/queue.tsx
T
joshuafhiggins 9da758b196 perf: optimize app rendering and data loading (#18)
- Virtualize mobile lists and defer queue loading
- Split playback state from actions to reduce rerenders
- Skip invalid or unnecessary Convex queries

Reviewed-on: #18
2026-09-05 16:35:37 -04:00

172 lines
5.1 KiB
TypeScript

import { usePaginatedQuery, useQuery } from "convex/react"
import { Stack, useRouter } from "expo-router"
import { useState } from "react"
import { FlatList, Pressable, StyleSheet, Text, View } from "react-native"
import { Button, LoadingView } from "@/components/ui/primitives"
import { contentBottomInset, SongRow } from "@/components/player"
import { usePlaybackActions, usePlaybackQueue } from "@/contexts/playback"
import { api } from "@otunes/convex/_generated/api"
import type { BrowseSong } from "@otunes/convex/types"
import { spacing, useThemeColors } from "@/lib/theme"
import { showError } from "@/lib/toast"
const PAGE_SIZE = 50
export default function QueueScreen() {
const router = useRouter()
const colors = useThemeColors()
const { queueSongIds, currentSong } = usePlaybackQueue()
const { skipToQueueIndex, clearQueue, reorderQueue } = usePlaybackActions()
const [reordering, setReordering] = useState(false)
const queueVersion = queueSongIds.length
? `${queueSongIds.length}:${queueSongIds[0]}:${queueSongIds[queueSongIds.length - 1]}`
: "empty"
const {
results: queueSongs,
status,
loadMore,
} = usePaginatedQuery(
api.playback.getQueuePaginated,
queueSongIds.length > 0 ? { queueVersion } : "skip",
{ initialNumItems: PAGE_SIZE }
)
const historySongs = useQuery(api.playback.getQueueHistory)
async function moveSong(fromIndex: number, direction: -1 | 1) {
if (reordering) return
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)
setReordering(true)
try {
await reorderQueue(next)
} catch (error) {
showError(
error instanceof Error ? error.message : "Failed to reorder queue"
)
} finally {
setReordering(false)
}
}
function handleClearQueue() {
if (router.canGoBack()) {
router.back()
setTimeout(() => {
void clearQueue()
}, 0)
return
}
void clearQueue()
}
if (queueSongIds.length > 0 && queueSongs === undefined) {
return <LoadingView />
}
return (
<>
<Stack.Screen
options={{
headerRight: () =>
queueSongIds.length > 0 ? (
<Button
title="Clear"
variant="ghost"
onPress={handleClearQueue}
/>
) : null,
}}
/>
<FlatList
style={{ flex: 1, backgroundColor: colors.background }}
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 }}
contentInsetAdjustmentBehavior="automatic"
onEndReached={() => {
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
renderItem={({ item }) => (
<View>
<SongRow
song={item}
onPress={() => void skipToQueueIndex(item.queueIndex)}
/>
<View style={styles.queueActions}>
<Pressable
disabled={reordering || item.queueIndex === 0}
onPress={() => void moveSong(item.queueIndex, -1)}
>
<Text style={{ color: colors.foreground }}>Move up</Text>
</Pressable>
<Pressable
disabled={
reordering || item.queueIndex === queueSongIds.length - 1
}
onPress={() => void moveSong(item.queueIndex, 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
}
/>
</>
)
}
const styles = StyleSheet.create({
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 },
})