- Virtualize mobile lists and defer queue loading - Split playback state from actions to reduce rerenders - Skip invalid or unnecessary Convex queries Reviewed-on: #18
133 lines
4.0 KiB
TypeScript
133 lines
4.0 KiB
TypeScript
import { useQuery } from "convex/react"
|
|
import { memo, useMemo } from "react"
|
|
import { FlatList, StyleSheet, Text, View } from "react-native"
|
|
|
|
import { ContentCarousel } from "@/components/media-cards"
|
|
import { tabContentBottomInset, SongRow } from "@/components/player"
|
|
import { LoadingView } from "@/components/ui/primitives"
|
|
import { useFavorites } from "@/contexts/favorites"
|
|
import { usePlaybackActions } from "@/contexts/playback"
|
|
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { BrowseSong } from "@otunes/convex/types"
|
|
import { spacing, useThemeColors } from "@/lib/theme"
|
|
|
|
function SearchResultsBody({ searchQuery }: { searchQuery: string }) {
|
|
const colors = useThemeColors()
|
|
const { playFromSong } = usePlaybackActions()
|
|
const { isSongFavorite } = useFavorites()
|
|
const { handleSongAction } = useMediaMenuActions()
|
|
const hasQuery = searchQuery.length > 0
|
|
|
|
const results = useQuery(
|
|
api.library.searchLibrary,
|
|
hasQuery ? { query: searchQuery } : "skip"
|
|
)
|
|
const playlistResults = useQuery(
|
|
api.playlists.searchPlaylists,
|
|
hasQuery ? { query: searchQuery } : "skip"
|
|
)
|
|
|
|
const isSearching =
|
|
hasQuery && (results === undefined || playlistResults === undefined)
|
|
|
|
const hasResults = useMemo(
|
|
() =>
|
|
Boolean(
|
|
(results &&
|
|
(results.topResults.length > 0 ||
|
|
results.artists.length > 0 ||
|
|
results.albums.length > 0 ||
|
|
results.songs.length > 0)) ||
|
|
(playlistResults && playlistResults.length > 0)
|
|
),
|
|
[playlistResults, results]
|
|
)
|
|
const songIds = useMemo(
|
|
() => results?.songs.map((song) => song.id) ?? [],
|
|
[results?.songs]
|
|
)
|
|
|
|
if (isSearching) {
|
|
return <LoadingView />
|
|
}
|
|
|
|
if (!hasQuery) {
|
|
return <></>
|
|
}
|
|
|
|
return (
|
|
<FlatList
|
|
data={results?.songs ?? []}
|
|
keyExtractor={(song) => song.id}
|
|
style={{ flex: 1, backgroundColor: colors.background }}
|
|
contentContainerStyle={{ paddingBottom: tabContentBottomInset }}
|
|
contentInsetAdjustmentBehavior="automatic"
|
|
keyboardShouldPersistTaps="handled"
|
|
ListHeaderComponent={
|
|
<>
|
|
{!hasResults ? (
|
|
<View style={styles.empty}>
|
|
<Text
|
|
style={[styles.emptyText, { color: colors.mutedForeground }]}
|
|
>
|
|
No results for "{searchQuery}"
|
|
</Text>
|
|
</View>
|
|
) : null}
|
|
|
|
{results?.artists.length ? (
|
|
<View style={styles.section}>
|
|
<ContentCarousel title="Artists" content={results.artists} />
|
|
</View>
|
|
) : null}
|
|
|
|
{results?.albums.length ? (
|
|
<View style={styles.section}>
|
|
<ContentCarousel title="Albums" content={results.albums} />
|
|
</View>
|
|
) : null}
|
|
</>
|
|
}
|
|
renderItem={({ item: song }: { item: BrowseSong }) => (
|
|
<SongRow
|
|
song={song}
|
|
onPress={() => void playFromSong(song.id, songIds)}
|
|
contextMenu={{
|
|
favorited: isSongFavorite(song.id),
|
|
navigation: {
|
|
artist: { id: song.artist.id },
|
|
album: { id: song.album.id },
|
|
},
|
|
share: {
|
|
type: "song",
|
|
albumId: song.album.id,
|
|
songId: song.id,
|
|
},
|
|
onAction: (action) => void handleSongAction(song, action),
|
|
}}
|
|
/>
|
|
)}
|
|
ListFooterComponent={
|
|
playlistResults && playlistResults.length > 0 ? (
|
|
<View style={styles.section}>
|
|
<ContentCarousel title="Playlists" content={playlistResults} />
|
|
</View>
|
|
) : null
|
|
}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export const SearchResults = memo(SearchResultsBody)
|
|
|
|
const styles = StyleSheet.create({
|
|
empty: { padding: spacing.xl, alignItems: "center" },
|
|
emptyText: { fontSize: 16 },
|
|
section: {
|
|
paddingHorizontal: spacing.lg,
|
|
paddingBottom: spacing.lg,
|
|
gap: spacing.md,
|
|
},
|
|
})
|