From 216c6a807b330433603ba80e364a3b4e2ea6271d Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Fri, 20 Nov 2026 00:19:41 -0500 Subject: [PATCH] feat(web): right click menu for queue songs (#131) --- apps/web/components/queue-panel.tsx | 74 ++++++++++++++++++++++++++++- apps/web/components/track-row.tsx | 26 ++++++---- 2 files changed, 89 insertions(+), 11 deletions(-) diff --git a/apps/web/components/queue-panel.tsx b/apps/web/components/queue-panel.tsx index 996c994..b318ffa 100644 --- a/apps/web/components/queue-panel.tsx +++ b/apps/web/components/queue-panel.tsx @@ -2,11 +2,14 @@ import * as React from "react" +import { useFavorites } from "@/components/favorites-provider" +import type { MediaContextAction } from "@/components/media-context-menu" import { PlaybackSongMeta } from "@/components/playback-song-meta" import { usePlayback } from "@/components/playback-provider" import { TrackCover, TrackRow } from "@/components/track-row" import { Skeleton } from "@/components/ui/skeleton" import { Spinner } from "@/components/ui/spinner" +import { useMediaActions } from "@/hooks/use-media-actions" import { useTrackSelection } from "@/hooks/use-track-selection" import type { Id } from "@otunes/convex/_generated/dataModel" import type { BrowseSong, QueueSong } from "@otunes/convex/types" @@ -15,6 +18,7 @@ import { moveQueueBlock, } from "@/lib/move-queue-block" import { getSelectionRangePositions } from "@/lib/selection-range-styles" +import { stationArtistId } from "@/lib/station-artist" import { useSongs } from "@/lib/song-cache" import { cn } from "@/lib/utils" @@ -181,8 +185,66 @@ export function QueuePanel() { () => selectableQueue.map((item) => ({ id: item.selectionId })), [selectableQueue] ) - const { selectedIds, handleSelect, handlePointerDown } = - useTrackSelection(selectionItems) + const { + selectedIds, + selectedCount, + handleSelect, + handlePointerDown, + prepareContextMenu, + clearSelection, + } = useTrackSelection(selectionItems) + const { isSongFavorite } = useFavorites() + const { runSongsAction } = useMediaActions() + + /** The rows a menu acts on: the selection, else just the clicked row. */ + const menuTargets = React.useCallback( + (selectionId: string) => { + const ids = selectedIds.has(selectionId) + ? selectedIds + : new Set([selectionId]) + return selectableQueue.filter((item) => ids.has(item.selectionId)) + }, + [selectableQueue, selectedIds] + ) + + const selectedStationArtistId = React.useMemo( + () => + stationArtistId( + selectableQueue.flatMap((item) => + item.song ? [{ id: item.selectionId, artist: item.song.artist }] : [] + ), + selectedIds + ), + [selectableQueue, selectedIds] + ) + + const handleMediaAction = React.useCallback( + (action: MediaContextAction, selectionId: string) => { + const songs = menuTargets(selectionId).flatMap((item) => + item.song ? [item.song] : [] + ) + const artistId = songs[0]?.artist.id + runSongsAction( + action, + songs.map((song) => song.id), + songs.every((song) => song.artist.id === artistId) + ? artistId + : undefined + ) + }, + [menuTargets, runSongsAction] + ) + + const handleRemove = React.useCallback( + (selectionId: string) => { + const removed = new Set( + menuTargets(selectionId).map((item) => item.queueIndex) + ) + clearSelection() + void reorderQueue(queueSongIds.filter((_, index) => !removed.has(index))) + }, + [clearSelection, menuTargets, queueSongIds, reorderQueue] + ) const handleRowSelect = React.useCallback( (index: number, event: React.MouseEvent) => { @@ -336,6 +398,11 @@ export function QueuePanel() { variant="queue" song={item.song} index={index} + selectionId={item.selectionId} + isFavorite={isSongFavorite(item.song.id)} + selectedCount={selectedCount} + selectionStationArtistId={selectedStationArtistId} + removeLabel="Remove from Queue" selectionPosition={selectionPositions[index]} isDragging={draggingIndices.has(index)} onPointerDown={handlePointerDown} @@ -343,6 +410,9 @@ export function QueuePanel() { onPlay={handleRowPlay} onDragStart={setDragIndex} onDrop={handleDrop} + onPrepareContextMenu={prepareContextMenu} + onMediaAction={handleMediaAction} + onRemove={handleRemove} /> ) : ( diff --git a/apps/web/components/track-row.tsx b/apps/web/components/track-row.tsx index c732512..48048d4 100644 --- a/apps/web/components/track-row.tsx +++ b/apps/web/components/track-row.tsx @@ -38,7 +38,7 @@ import type { DownloadSource } from "@otunes/convex/client/download-queue" * columns (use with `LibraryTrackListHeader`). * - `album`: favorite star, track number, title with optional credits. * - `playlist`: cover with favorite overlay, track number, title. - * - `queue`: cover, title and artist, duration, and a reorder grip. No menu. + * - `queue`: cover, title and artist, duration, and a reorder grip. */ export type TrackRowVariant = "library" | "album" | "playlist" | "queue" @@ -47,6 +47,12 @@ export type TrackRowProps = { song: BrowseSong /** Position in the list the selection hook was given. */ index: number + /** + * The id the selection hook knows this row by, passed to the menu and + * remove callbacks. Defaults to the song id; the queue uses a per-position + * id since a song can appear more than once. + */ + selectionId?: string /** Number shown in the track number column (`album` and `playlist`). */ trackNumber?: number /** Secondary line under the title, e.g. album track credits. */ @@ -71,10 +77,10 @@ export type TrackRowProps = { onPointerDown: (event: React.MouseEvent) => void onPlay: (index: number) => void onToggleFavorite?: (songId: string) => void - onPrepareContextMenu?: (songId: string) => void + onPrepareContextMenu?: (selectionId: string) => void /** Renders the "more options" and right-click menus when provided. */ - onMediaAction?: (action: MediaContextAction, songId: string) => void - onRemove?: (songId: string) => void + onMediaAction?: (action: MediaContextAction, selectionId: string) => void + onRemove?: (selectionId: string) => void /** Makes the row draggable when provided. */ onDragStart?: (index: number) => void onDrop?: (index: number) => void @@ -251,6 +257,7 @@ function TrackRowComponent({ variant, song, index, + selectionId, trackNumber, subtitle, downloadCollection, @@ -284,6 +291,7 @@ function TrackRowComponent({ const flush = variant === "library" || variant === "queue" const play = () => onPlay(index) const toggleFavorite = () => onToggleFavorite?.(song.id) + const rowId = selectionId ?? song.id const favoriteStar = (
@@ -333,8 +341,8 @@ function TrackRowComponent({ stationArtistId: inRange ? selectionStationArtistId : song.artist.id, selectedCount: inRange && selectedCount > 0 ? selectedCount : 1, inSelectionRange: inRange, - onOpenMenu: () => onPrepareContextMenu?.(song.id), - onAction: (action) => onMediaAction(action, song.id), + onOpenMenu: () => onPrepareContextMenu?.(rowId), + onAction: (action) => onMediaAction(action, rowId), playlistLabel, showFavorite: showFavoriteInMenu, favorited: isFavorite, @@ -351,7 +359,7 @@ function TrackRowComponent({ onRemove(song.id)} + onClick={() => onRemove(rowId)} > {removeLabel} @@ -555,7 +563,7 @@ function TrackRowComponent({ } /** - * Pass stable callbacks: they receive the row's `index` or song id, so one - * handler can serve every row. + * Pass stable callbacks: they receive the row's `index`, song id or + * `selectionId`, so one handler can serve every row. */ export const TrackRow = React.memo(TrackRowComponent)