Files
otunes/apps/mobile/components/search-results.tsx
T

142 lines
4.2 KiB
TypeScript

import { memo, useCallback, useMemo } from "react"
import {
FlatList,
StyleSheet,
Text,
View,
type ListRenderItemInfo,
} from "react-native"
import { ContentCarousel } from "@/components/media-cards"
import type { MediaAction } from "@/components/media-context-menu"
import { tabContentBottomInset, SongListRow } from "@/components/player"
import { SongListSkeleton } from "@/components/skeletons"
import { useFavorites } from "@/contexts/favorites"
import { usePlaybackActions } from "@/contexts/playback"
import { useCachedQuery } from "@/hooks/use-cached-query"
import { useMediaMenuActions } from "@/hooks/use-media-menu-actions"
import { api } from "@otunes/convex/_generated/api"
import type { BrowseSong, SearchSong } 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 = useCachedQuery(
api.library.searchLibrary,
hasQuery ? { query: searchQuery } : "skip"
)
const playlistResults = useCachedQuery(
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]
)
const handlePlaySong = useCallback(
(song: BrowseSong) => void playFromSong(song.id, songIds),
[playFromSong, songIds]
)
const handleSongMenuAction = useCallback(
(song: BrowseSong, action: MediaAction) =>
void handleSongAction(song, action),
[handleSongAction]
)
const renderItem = useCallback(
({ item: song }: ListRenderItemInfo<SearchSong>) => (
<SongListRow
song={song}
favorited={isSongFavorite(song.id)}
onPlay={handlePlaySong}
onAction={handleSongMenuAction}
/>
),
[handlePlaySong, handleSongMenuAction, isSongFavorite]
)
if (isSearching) {
return <SongListSkeleton />
}
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={renderItem}
ListFooterComponent={
playlistResults && playlistResults.length > 0 ? (
<View style={styles.section}>
<ContentCarousel title="Playlists" content={playlistResults} />
</View>
) : undefined
}
/>
)
}
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,
},
})