diff --git a/apps/web/components/playlist-view.tsx b/apps/web/components/playlist-view.tsx index 1a9a430..57ceaa8 100644 --- a/apps/web/components/playlist-view.tsx +++ b/apps/web/components/playlist-view.tsx @@ -6,7 +6,7 @@ import * as React from "react" import Image from "next/image" import { useRouter } from "next/navigation" import { useMutation } from "convex/react" -import { Play, Share2, Shuffle, Star, Trash2 } from "lucide-react" +import { Play, SearchIcon, Share2, Shuffle, Star, Trash2 } from "lucide-react" import { toast } from "sonner" import { useAddToPlaylist } from "@/components/add-to-playlist-provider" @@ -305,6 +305,19 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { const songs = isFavorites ? playlist.songs.filter((song) => !hiddenFavoriteIds.has(song.id)) : playlistSongs + const [searchQuery, setSearchQuery] = React.useState("") + const normalizedQuery = searchQuery.trim().toLowerCase() + const filteredSongs = React.useMemo( + () => + normalizedQuery + ? songs.filter((song) => + [song.title, song.artist.name, song.album.title].some((value) => + value.toLowerCase().includes(normalizedQuery) + ) + ) + : songs, + [songs, normalizedQuery] + ) const playlistSongIds = React.useMemo( () => songs.map((song) => song.id as Id<"songs">), [songs] @@ -327,7 +340,8 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { handleSelect, handlePointerDown, prepareContextMenu, - } = useTrackSelection(songs) + clearSelection, + } = useTrackSelection(filteredSongs) const selectedStationArtistId = React.useMemo( () => stationArtistId(songs, selectedIds), @@ -335,8 +349,12 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { ) const selectionPositions = React.useMemo( - () => getSelectionRangePositions(songs.map((song) => song.id), selectedIds), - [songs, selectedIds] + () => + getSelectionRangePositions( + filteredSongs.map((song) => song.id), + selectedIds + ), + [filteredSongs, selectedIds] ) const totalDurationSeconds = React.useMemo( () => sumDurationSeconds(songs), @@ -461,7 +479,13 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { } function handleDrop(targetIndex: number) { - if (isFavorites || !isOwner || dragIndex === null || dragIndex === targetIndex) { + if ( + normalizedQuery || + isFavorites || + !isOwner || + dragIndex === null || + dragIndex === targetIndex + ) { setDragIndex(null) return } @@ -633,13 +657,31 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { - {songs.length > 0 ? ( - songs.map((song, index) => ( +
+
+ + { + setSearchQuery(event.target.value) + clearSelection() + setActiveTrackId("") + setDragIndex(null) + }} + className="h-10 w-full min-w-0 rounded-full border border-input bg-input/30 pr-3 pl-9 text-sm text-foreground transition-colors outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50" + /> +
+
+ {filteredSongs.length > 0 ? ( + filteredSongs.map((song, index) => ( handleRemoveSong(song.id)} onDragStart={(event) => { - if (isFavorites || !isOwner) { + if (normalizedQuery || isFavorites || !isOwner) { return } setDragIndex(index) event.dataTransfer.effectAllowed = "move" }} onDragOver={(event) => { - if (isFavorites || !isOwner) { + if (normalizedQuery || isFavorites || !isOwner) { return } event.preventDefault() @@ -707,7 +749,9 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) { )) ) : (

- This playlist has no songs yet. + {normalizedQuery + ? "No songs found." + : "This playlist has no songs yet."}

)}