T3code/ios pull to search (#5)

Co-authored-by: Rem Helper <remhelper@abunchofknowitalls.com>
Reviewed-on: https://git.abunchofknowitalls.com/joshuafhiggins/otunes/pulls/5
This commit was merged in pull request #5.
This commit is contained in:
joshuafhigginsandremhelper committed 2026-07-07 03:43:54 +00:00
1 parent 22a60ccb4c
commit 48c592acd9
3 files changed
+190 -80

No files matched your search

@@ -1,30 +1,67 @@
import { usePaginatedQuery } from "convex/react"
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
if (results === undefined) {
return <LoadingView />
}
const handleChangeText = useCallback(
(event: { nativeEvent: { text: string } }) => {
onQueryChange(event.nativeEvent.text)
},
[onQueryChange]
)
return (
<MediaGridList
content={results}
paddingBottom={contentBottomInset}
onEndReached={() => {
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
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)
}}
/>
</>
)
}
@@ -1,4 +1,6 @@
import { usePaginatedQuery } from "convex/react"
import { usePaginatedQuery, useQuery } from "convex/react"
import { Stack } from "expo-router"
import { useCallback } from "react"
import { FlatList, Pressable, StyleSheet, Text } from "react-native"
import { MediaContextMenu } from "@/components/media-context-menu"
@@ -6,6 +8,7 @@ import { CoverImage } from "@/components/cover-image"
import { LoadingView } from "@/components/ui/primitives"
import { contentBottomInset } from "@/components/player"
import { useServerConfig } from "@/contexts/server-config"
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
import { useAppNavigation } from "@/lib/app-navigation"
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
import { api } from "@otunes/convex/_generated/api"
@@ -19,48 +22,82 @@ export default function ArtistsScreen() {
const { push } = useAppNavigation()
const colors = useThemeColors()
const { config } = useServerConfig()
const { searchQuery, onQueryChange, onSearchSubmit } = useDebouncedSearch()
const { results, status, loadMore } = usePaginatedQuery(
api.library.browseArtistsPaginated,
{},
{ initialNumItems: PAGE_SIZE }
)
const searchResults = useQuery(
api.library.searchLibrary,
searchQuery ? { query: searchQuery } : "skip"
)
const { handleArtistAction, isArtistFavorite } = useMediaMenuActions()
const artists = searchQuery ? searchResults?.artists : results
if (results === undefined || !config) {
return <LoadingView />
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 (artists === undefined || !config) {
return (
<>
{searchBar}
<LoadingView />
</>
)
}
return (
<FlatList
data={results}
keyExtractor={(artist) => artist.id}
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
onEndReached={() => {
if (status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
onEndReachedThreshold={0.5}
renderItem={({ item: artist }: { item: BrowseArtist }) => (
<MediaContextMenu
favorited={isArtistFavorite(artist.id)}
share={{ type: "artist", artistId: artist.id }}
onAction={(action) => void handleArtistAction(artist, action)}
>
<Pressable
style={[styles.row, { borderBottomColor: colors.border }]}
onPress={() => push(`/artist/${artist.id}`)}
<>
{searchBar}
<FlatList
data={artists}
keyExtractor={(artist) => artist.id}
style={{ flex: 1, backgroundColor: colors.background }}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
onEndReached={() => {
if (!searchQuery && status === "CanLoadMore") loadMore(PAGE_SIZE)
}}
onEndReachedThreshold={0.5}
renderItem={({ item: artist }: { item: BrowseArtist }) => (
<MediaContextMenu
favorited={isArtistFavorite(artist.id)}
share={{ type: "artist", artistId: artist.id }}
onAction={(action) => void handleArtistAction(artist, action)}
>
<CoverImage
uri={artistCoverUrl(config.serverUrl, artist.id)}
hasCover={artist.photo != null}
style={styles.avatar}
/>
<Text style={[styles.name, { color: colors.foreground }]}>{artist.name}</Text>
</Pressable>
</MediaContextMenu>
)}
/>
<Pressable
style={[styles.row, { borderBottomColor: colors.border }]}
onPress={() => push(`/artist/${artist.id}`)}
>
<CoverImage
uri={artistCoverUrl(config.serverUrl, artist.id)}
hasCover={artist.photo != null}
style={styles.avatar}
/>
<Text style={[styles.name, { color: colors.foreground }]}>{artist.name}</Text>
</Pressable>
</MediaContextMenu>
)}
/>
</>
)
}
@@ -1,11 +1,13 @@
import { usePaginatedQuery } from "convex/react"
import { useState } from "react"
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 { usePlayback } 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"
@@ -17,6 +19,7 @@ export default function SongsScreen() {
const { playFromLibrarySong } = usePlayback()
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")
@@ -25,46 +28,79 @@ export default function SongsScreen() {
{ sortKey, sortDirection },
{ initialNumItems: PAGE_SIZE }
)
const searchResults = useQuery(
api.library.searchLibrary,
searchQuery ? { query: searchQuery } : "skip"
)
const songs = searchQuery ? searchResults?.songs : results
if (results === undefined) {
return <LoadingView />
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 (
<FlatList
data={results}
keyExtractor={(item) => item.id}
contentContainerStyle={{ paddingBottom: contentBottomInset }}
contentInsetAdjustmentBehavior="automatic"
onEndReached={() => {
if (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),
}}
/>
)}
/>
<>
{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),
}}
/>
)}
/>
</>
)
}