feat(mobile): search within playlists using native search #68
No files matched your search
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
PlaylistSearchBar,
|
||||
usePlaylistSearch,
|
||||
} from "@/components/playlist-search-bar"
|
||||
import { useAndroidScreenPalette } from "@/contexts/android-tab-theme"
|
||||
import { MusicEntityView } from "@/components/music-entity-view"
|
||||
import { useMutation } from "convex/react"
|
||||
@@ -158,6 +162,8 @@ function PlaylistContent({
|
||||
const isSubscribed = playlist.isSubscribed
|
||||
const canEdit = playlist.isOwner && playlistId !== null
|
||||
const songIds = useMemo(() => songs.map((song) => song.id), [songs])
|
||||
const { filteredSongs, onQueryChange, onSearchSubmit } =
|
||||
usePlaylistSearch(songs)
|
||||
const swipeGroup = useSwipeRowGroup()
|
||||
|
||||
const handlePlaySong = useCallback(
|
||||
@@ -312,10 +318,14 @@ function PlaylistContent({
|
||||
<StatusBar style={palette.isDark ? "light" : "dark"} />
|
||||
) : null}
|
||||
<Stack.Screen options={screenOptions} />
|
||||
<PlaylistSearchBar
|
||||
onQueryChange={onQueryChange}
|
||||
onSearchSubmit={onSearchSubmit}
|
||||
/>
|
||||
<FlatList
|
||||
onLayout={onLayout}
|
||||
{...longListProps}
|
||||
data={songs}
|
||||
data={filteredSongs}
|
||||
keyExtractor={songKey}
|
||||
renderItem={renderItem}
|
||||
style={{ flex: 1, backgroundColor: palette.background }}
|
||||
@@ -323,6 +333,16 @@ function PlaylistContent({
|
||||
contentContainerStyle={styles.listContent}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
onScrollBeginDrag={swipeGroup.closeOpenRow}
|
||||
ListEmptyComponent={
|
||||
<Text
|
||||
style={[
|
||||
styles.emptyResults,
|
||||
{ color: palette.secondaryForeground },
|
||||
]}
|
||||
>
|
||||
{songs.length ? "No songs found." : "No songs in this playlist."}
|
||||
</Text>
|
||||
}
|
||||
ListHeaderComponent={
|
||||
<PlaylistHero
|
||||
entityId={playlistId ?? "otunes:favorites"}
|
||||
@@ -518,6 +538,7 @@ const PlaylistHero = memo(function PlaylistHero({
|
||||
})
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
emptyResults: { padding: spacing.lg, textAlign: "center" },
|
||||
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
||||
listContent: { paddingBottom: contentBottomInset },
|
||||
hero: { alignItems: "center", padding: spacing.lg, gap: spacing.sm },
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
PlaylistSearchBar,
|
||||
usePlaylistSearch,
|
||||
} from "@/components/playlist-search-bar"
|
||||
import { memo, useCallback, useEffect, useMemo } from "react"
|
||||
import {
|
||||
FlatList,
|
||||
@@ -37,6 +41,8 @@ export default function FavoritesScreen() {
|
||||
const { isSongFavorite } = useFavorites()
|
||||
const { handleSongAction } = useMediaMenuActions()
|
||||
const songIds = useMemo(() => songs.map((song) => song.id), [songs])
|
||||
const { filteredSongs, onQueryChange, onSearchSubmit } =
|
||||
usePlaylistSearch(songs)
|
||||
|
||||
useEffect(() => {
|
||||
if (favorites) rememberSongs(favorites.songs)
|
||||
@@ -99,14 +105,25 @@ export default function FavoritesScreen() {
|
||||
return (
|
||||
<>
|
||||
<Stack.Screen options={SCREEN_OPTIONS} />
|
||||
<PlaylistSearchBar
|
||||
onQueryChange={onQueryChange}
|
||||
onSearchSubmit={onSearchSubmit}
|
||||
/>
|
||||
<FlatList
|
||||
{...longListProps}
|
||||
data={songs}
|
||||
data={filteredSongs}
|
||||
keyExtractor={songKey}
|
||||
renderItem={renderItem}
|
||||
style={{ flex: 1, backgroundColor: colors.background }}
|
||||
contentContainerStyle={styles.listContent}
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
ListEmptyComponent={
|
||||
<Text
|
||||
style={[styles.emptyResults, { color: colors.mutedForeground }]}
|
||||
>
|
||||
{songs.length ? "No songs found." : "No songs in this playlist."}
|
||||
</Text>
|
||||
}
|
||||
ListHeaderComponent={
|
||||
<FavoritesHero
|
||||
title={favorites.title}
|
||||
@@ -173,6 +190,7 @@ const FavoritesHero = memo(function FavoritesHero({
|
||||
})
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
emptyResults: { padding: spacing.lg, textAlign: "center" },
|
||||
empty: { flex: 1, alignItems: "center", justifyContent: "center" },
|
||||
listContent: { paddingBottom: contentBottomInset },
|
||||
hero: { alignItems: "center", padding: spacing.lg, gap: spacing.sm },
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { useCallback, useMemo } from "react"
|
||||
import { Platform } from "react-native"
|
||||
import { Stack } from "expo-router"
|
||||
|
||||
import { useDebouncedSearch } from "@/hooks/use-debounced-search"
|
||||
import { useThemeColors } from "@/lib/theme"
|
||||
import type { BrowseSong } from "@otunes/convex/types"
|
||||
|
||||
export function usePlaylistSearch(songs: BrowseSong[]) {
|
||||
const search = useDebouncedSearch()
|
||||
const filteredSongs = useMemo(() => {
|
||||
const query = search.searchQuery.toLowerCase()
|
||||
if (!query) return songs
|
||||
return songs.filter((song) =>
|
||||
[song.title, song.artist.name, song.album.title].some((value) =>
|
||||
value.toLowerCase().includes(query)
|
||||
)
|
||||
)
|
||||
}, [songs, search.searchQuery])
|
||||
|
||||
return { ...search, filteredSongs }
|
||||
}
|
||||
|
||||
export function PlaylistSearchBar({
|
||||
onQueryChange,
|
||||
onSearchSubmit,
|
||||
}: Pick<
|
||||
ReturnType<typeof useDebouncedSearch>,
|
||||
"onQueryChange" | "onSearchSubmit"
|
||||
>) {
|
||||
const colors = useThemeColors()
|
||||
const handleChangeText = useCallback(
|
||||
(event: { nativeEvent: { text: string } }) => {
|
||||
onQueryChange(event.nativeEvent.text)
|
||||
},
|
||||
[onQueryChange]
|
||||
)
|
||||
|
||||
return (
|
||||
<Stack.SearchBar
|
||||
placeholder="Search playlist"
|
||||
onChangeText={handleChangeText}
|
||||
onSearchButtonPress={onSearchSubmit}
|
||||
autoCapitalize="none"
|
||||
{...(Platform.OS === "android"
|
||||
? {
|
||||
barTintColor: colors.background,
|
||||
textColor: colors.foreground,
|
||||
tintColor: colors.primary,
|
||||
hintTextColor: colors.mutedForeground,
|
||||
headerIconColor: colors.foreground,
|
||||
}
|
||||
: {})}
|
||||
/>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user