- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
116 lines
3.3 KiB
TypeScript
116 lines
3.3 KiB
TypeScript
import { usePaginatedQuery, useQuery } from "convex/react"
|
|
import { Stack } from "expo-router"
|
|
import { useCallback, useState } from "react"
|
|
import { FlatList, StyleSheet } from "react-native"
|
|
|
|
import { contentBottomInset, SongRow } from "@/components/player"
|
|
import { LoadingView } from "@/components/ui/primitives"
|
|
import { useFavorites } from "@/contexts/favorites"
|
|
import { usePlaybackActions } from "@/contexts/playback"
|
|
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
|
|
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import { spacing } from "@/lib/theme"
|
|
|
|
const PAGE_SIZE = 50
|
|
|
|
export default function SongsScreen() {
|
|
const { playFromLibrarySong } = usePlaybackActions()
|
|
const { isSongFavorite } = useFavorites()
|
|
const { handleSongAction } = useMediaMenuActions()
|
|
const { searchQuery, onQueryChange, onSearchSubmit } = useDebouncedSearch()
|
|
const [sortKey, setSortKey] = useState<"title" | "artist" | "album">("title")
|
|
const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc")
|
|
|
|
const { results, status, loadMore } = usePaginatedQuery(
|
|
api.library.browseSongsPaginated,
|
|
{ sortKey, sortDirection },
|
|
{ initialNumItems: PAGE_SIZE }
|
|
)
|
|
const searchResults = useQuery(
|
|
api.library.searchLibrary,
|
|
searchQuery ? { query: searchQuery } : "skip"
|
|
)
|
|
const songs = searchQuery ? searchResults?.songs : results
|
|
|
|
const handleChangeText = useCallback(
|
|
(event: { nativeEvent: { text: string } }) => {
|
|
onQueryChange(event.nativeEvent.text)
|
|
},
|
|
[onQueryChange]
|
|
)
|
|
|
|
const searchBar = (
|
|
<Stack.Screen
|
|
options={{
|
|
headerSearchBarOptions: {
|
|
placeholder: "Search",
|
|
onChangeText: handleChangeText,
|
|
onSearchButtonPress: onSearchSubmit,
|
|
},
|
|
}}
|
|
/>
|
|
)
|
|
|
|
if (songs === undefined) {
|
|
return (
|
|
<>
|
|
{searchBar}
|
|
<LoadingView />
|
|
</>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{searchBar}
|
|
|
|
<FlatList
|
|
data={songs}
|
|
keyExtractor={(item) => item.id}
|
|
contentContainerStyle={{ paddingBottom: contentBottomInset }}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
onEndReached={() => {
|
|
if (!searchQuery && status === "CanLoadMore") loadMore(PAGE_SIZE)
|
|
}}
|
|
onEndReachedThreshold={0.5}
|
|
renderItem={({ item }) => (
|
|
<SongRow
|
|
song={item}
|
|
onPress={() =>
|
|
void playFromLibrarySong(item.id as Id<"songs">, {
|
|
sortKey,
|
|
sortDirection,
|
|
})
|
|
}
|
|
contextMenu={{
|
|
favorited: isSongFavorite(item.id),
|
|
navigation: {
|
|
artist: { id: item.artist.id },
|
|
album: { id: item.album.id },
|
|
},
|
|
share: {
|
|
type: "song",
|
|
albumId: item.album.id,
|
|
songId: item.id,
|
|
},
|
|
onAction: (action) => void handleSongAction(item, action),
|
|
}}
|
|
/>
|
|
)}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
sortRow: {
|
|
flexDirection: "row",
|
|
justifyContent: "space-around",
|
|
paddingVertical: spacing.sm,
|
|
borderBottomWidth: StyleSheet.hairlineWidth,
|
|
},
|
|
sortButton: { padding: spacing.sm },
|
|
})
|