Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com> Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/5
68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
import { usePaginatedQuery, useQuery } from "convex/react"
|
|
import { Stack } from "expo-router"
|
|
import { useCallback } from "react"
|
|
|
|
import { MediaGridList } from "@/components/media-cards"
|
|
import { LoadingView } from "@/components/ui/primitives"
|
|
import { contentBottomInset } from "@/components/player"
|
|
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
|
|
const PAGE_SIZE = 50
|
|
|
|
export default function AlbumsScreen() {
|
|
const { searchQuery, onQueryChange, onSearchSubmit } = useDebouncedSearch()
|
|
const { results, status, loadMore } = usePaginatedQuery(
|
|
api.library.browseAlbumsPaginated,
|
|
{},
|
|
{ initialNumItems: PAGE_SIZE }
|
|
)
|
|
const searchResults = useQuery(
|
|
api.library.searchLibrary,
|
|
searchQuery ? { query: searchQuery } : "skip"
|
|
)
|
|
const albums = searchQuery ? searchResults?.albums : 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 (albums === undefined) {
|
|
return (
|
|
<>
|
|
{searchBar}
|
|
<LoadingView />
|
|
</>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
{searchBar}
|
|
|
|
<MediaGridList
|
|
content={albums}
|
|
paddingBottom={contentBottomInset}
|
|
onEndReached={() => {
|
|
if (!searchQuery && status === "CanLoadMore") loadMore(PAGE_SIZE)
|
|
}}
|
|
/>
|
|
</>
|
|
)
|
|
}
|