feat(web): search within playlists
This commit is contained in:
1 parent
52d5958ac2
commit
3c9bdd2eab
1 file changed
+55
-11
@@ -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 }) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{songs.length > 0 ? (
|
||||
songs.map((song, index) => (
|
||||
<div className="px-6 pb-4">
|
||||
<div className="relative max-w-sm">
|
||||
<SearchIcon className="pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted-foreground" />
|
||||
<input
|
||||
type="search"
|
||||
aria-label="Search playlist"
|
||||
placeholder="Search playlist"
|
||||
value={searchQuery}
|
||||
onChange={(event) => {
|
||||
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"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
{filteredSongs.length > 0 ? (
|
||||
filteredSongs.map((song, index) => (
|
||||
<PlaylistSongRow
|
||||
key={song.id}
|
||||
song={song}
|
||||
index={index}
|
||||
isOwner={isOwner && !isFavorites}
|
||||
isOwner={isOwner && !isFavorites && !normalizedQuery}
|
||||
canRemove={isFavorites || isOwner}
|
||||
showFavoriteInMenu={!isFavorites}
|
||||
removeLabel={
|
||||
@@ -679,14 +721,14 @@ export function PlaylistView({ playlist }: { playlist: BrowsePlaylistDetail }) {
|
||||
onMediaAction={handleMediaAction}
|
||||
onRemove={() => 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 }) {
|
||||
))
|
||||
) : (
|
||||
<p className="px-6 py-8 text-sm text-muted-foreground">
|
||||
This playlist has no songs yet.
|
||||
{normalizedQuery
|
||||
? "No songs found."
|
||||
: "This playlist has no songs yet."}
|
||||
</p>
|
||||
)}
|
||||
<MediaCollectionFooter
|
||||
|
||||
Reference in new issue
Block a user