122 lines
3.4 KiB
TypeScript
122 lines
3.4 KiB
TypeScript
import { Stack } from "expo-router"
|
|
import { useCallback, useState } from "react"
|
|
import { FlatList, StyleSheet, type ListRenderItemInfo } from "react-native"
|
|
|
|
import type { MediaAction } from "@/components/media-context-menu"
|
|
import { contentBottomInset, SongListRow } from "@/components/player"
|
|
import { SongListSkeleton } from "@/components/skeletons"
|
|
import { useFavorites } from "@/contexts/favorites"
|
|
import { usePlaybackActions } from "@/contexts/playback"
|
|
import {
|
|
useCachedPaginatedQuery,
|
|
useCachedQuery,
|
|
} from "@/hooks/use-cached-query"
|
|
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
|
|
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { BrowseSong } from "@otunes/convex/types"
|
|
import { longListProps } from "@/lib/list"
|
|
|
|
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, isCached } = useCachedPaginatedQuery(
|
|
api.library.browseSongsPaginated,
|
|
{ sortKey, sortDirection },
|
|
{ initialNumItems: PAGE_SIZE }
|
|
)
|
|
const searchResults = useCachedQuery(
|
|
api.library.searchLibrary,
|
|
searchQuery ? { query: searchQuery } : "skip"
|
|
)
|
|
const songs = searchQuery
|
|
? searchResults?.songs
|
|
: status === "LoadingFirstPage" && !isCached
|
|
? undefined
|
|
: results
|
|
|
|
const handlePlaySong = useCallback(
|
|
(song: BrowseSong) =>
|
|
void playFromLibrarySong(song.id, { sortKey, sortDirection }),
|
|
[playFromLibrarySong, sortDirection, sortKey]
|
|
)
|
|
const handleSongMenuAction = useCallback(
|
|
(song: BrowseSong, action: MediaAction) =>
|
|
void handleSongAction(song, action),
|
|
[handleSongAction]
|
|
)
|
|
const renderItem = useCallback(
|
|
({ item }: ListRenderItemInfo<BrowseSong>) => (
|
|
<SongListRow
|
|
song={item}
|
|
favorited={isSongFavorite(item.id)}
|
|
onPlay={handlePlaySong}
|
|
onAction={handleSongMenuAction}
|
|
/>
|
|
),
|
|
[handlePlaySong, handleSongMenuAction, isSongFavorite]
|
|
)
|
|
|
|
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}
|
|
<SongListSkeleton />
|
|
</>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{searchBar}
|
|
|
|
<FlatList
|
|
{...longListProps}
|
|
data={songs}
|
|
keyExtractor={songKey}
|
|
renderItem={renderItem}
|
|
contentContainerStyle={styles.listContent}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
onEndReached={() => {
|
|
if (!searchQuery && status === "CanLoadMore") loadMore(PAGE_SIZE)
|
|
}}
|
|
onEndReachedThreshold={0.5}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
function songKey(song: BrowseSong) {
|
|
return song.id
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
listContent: { paddingBottom: contentBottomInset },
|
|
})
|