feat(web): match the queue sidebar to the mobile queue (#119)

This commit was merged in pull request #119.
This commit is contained in:
joshuafhiggins committed 2026-11-19 19:25:55 -05:00
1 parent 4a87d782d1
commit 0d644869ea
3 files changed
+219 -132

No files matched your search

+18
View File
@@ -103,8 +103,11 @@ type PlaybackContextValue = {
toggleShuffle: () => Promise<void>
cycleRepeat: () => Promise<void>
clearQueue: () => Promise<void>
clearHistory: () => Promise<void>
reorderQueue: (songIds: Id<"songs">[]) => Promise<void>
skipToQueueIndex: (index: number) => Promise<void>
/** Plays `history[index]`, moving it and every later song back into the queue. */
skipToHistoryIndex: (index: number) => Promise<void>
skipNext: () => Promise<void>
skipPrevious: () => Promise<void>
dolbyAtmosSupported: boolean | null
@@ -1455,6 +1458,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
setIsPlaying(false)
}, [store])
const clearHistory = useCallback(async () => {
store.update(playbackQueue.clearHistory)
}, [store])
const reorderQueue = useCallback(
async (songIds: Id<"songs">[]) => {
store.update((state) => playbackQueue.reorderQueue(state, songIds))
@@ -1469,6 +1476,13 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
[store]
)
const skipToHistoryIndex = useCallback(
async (index: number) => {
store.update((state) => playbackQueue.skipToHistoryIndex(state, index))
},
[store]
)
const skipNext = useCallback(async () => {
const before = store.getState()
if (before.repeatEnabled === "none" && before.queue.length <= 1) {
@@ -1550,8 +1564,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
toggleShuffle,
cycleRepeat,
clearQueue,
clearHistory,
reorderQueue,
skipToQueueIndex,
skipToHistoryIndex,
skipNext,
skipPrevious,
}),
@@ -1573,8 +1589,10 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
toggleShuffle,
cycleRepeat,
clearQueue,
clearHistory,
reorderQueue,
skipToQueueIndex,
skipToHistoryIndex,
skipNext,
skipPrevious,
]
+180 -120
View File
@@ -5,13 +5,11 @@ import * as React from "react"
import { PlaybackSongMeta } from "@/components/playback-song-meta"
import { usePlayback } from "@/components/playback-provider"
import { TrackCover, TrackRow } from "@/components/track-row"
import { Button } from "@/components/ui/button"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Skeleton } from "@/components/ui/skeleton"
import { Spinner } from "@/components/ui/spinner"
import { useTrackSelection } from "@/hooks/use-track-selection"
import type { Id } from "@otunes/convex/_generated/dataModel"
import type { BrowseSong, HistorySong, QueueSong } from "@otunes/convex/types"
import type { BrowseSong, QueueSong } from "@otunes/convex/types"
import {
getContiguousSelectedIndices,
moveQueueBlock,
@@ -19,7 +17,6 @@ import {
import { getSelectionRangePositions } from "@/lib/selection-range-styles"
import { useSongs } from "@/lib/song-cache"
import { cn } from "@/lib/utils"
import { formatDuration } from "@/lib/format-duration"
const QUEUE_INITIAL_VISIBLE_COUNT = 100
const QUEUE_PAGE_SIZE = 50
@@ -32,11 +29,13 @@ type QueueItem = {
song: QueueSelectable | null
}
/** Upcoming songs only: queue index 0 is the current song, shown on its own. */
function toSelectableQueue(
songIds: Id<"songs">[],
songs: (BrowseSong | undefined)[]
): QueueItem[] {
return songIds.map((songId, queueIndex) => {
return songIds.slice(1).map((songId, offset) => {
const queueIndex = offset + 1
const selectionId = `${songId}-${queueIndex}`
const song = songs[queueIndex]
return {
@@ -47,57 +46,75 @@ function toSelectableQueue(
})
}
function HistorySongRow({ song }: { song: HistorySong }) {
/** A played song; clicking it plays it again from that point in history. */
function HistorySongRow({
song,
historyIndex,
onPlay,
}: {
song: BrowseSong | undefined
historyIndex: number
onPlay: (historyIndex: number) => void
}) {
if (!song) {
return <QueueSongRowSkeleton />
}
return (
<li className="relative w-full py-2 text-left text-sm opacity-60">
<div className="relative z-10 flex items-center gap-3 px-2">
<TrackCover
albumId={song.album.id}
alt={song.album.title}
inRange={false}
square
/>
<PlaybackSongMeta
song={song}
isCurrent={false}
inRange={false}
align="start"
showAlbum={false}
/>
<span className="shrink-0 text-xs text-muted-foreground tabular-nums">
{formatDuration(song.durationSeconds)}
</span>
</div>
<li
role="button"
tabIndex={0}
className="flex cursor-pointer items-center gap-3 px-4 py-2 text-sm hover:bg-foreground/5"
onClick={() => onPlay(historyIndex)}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
onPlay(historyIndex)
}
}}
>
<TrackCover
albumId={song.album.id}
alt={song.album.title}
inRange={false}
/>
<PlaybackSongMeta song={song} align="start" showAlbum={false} />
</li>
)
}
function QueueSongRowSkeleton() {
return (
<li className="flex items-center gap-3 px-2 py-2" aria-hidden>
<Skeleton className="size-10 shrink-0 rounded-none" />
<li className="flex items-center gap-3 px-4 py-2" aria-hidden>
<Skeleton className="size-10 shrink-0 rounded" />
<div className="flex min-w-0 flex-1 flex-col gap-1.5">
<Skeleton className="h-3.5 w-3/4" />
<Skeleton className="h-3 w-1/2" />
</div>
<Skeleton className="h-3 w-8 shrink-0" />
</li>
)
}
/**
* The queue sidebar, laid out like the mobile now playing queue: history above
* (scrolled out of view at first), then the current song and the upcoming
* songs, which can be dragged to reorder. Shuffle and repeat live in the
* player bar.
*/
export function QueuePanel() {
const {
queueSongIds,
queueHistorySongIds,
queueOpen,
clearHistory,
clearQueue,
reorderQueue,
skipToQueueIndex,
skipToHistoryIndex,
} = usePlayback()
const loadMoreRef = React.useRef<HTMLDivElement>(null)
const nowPlayingRef = React.useRef<HTMLElement>(null)
const scrollRef = React.useRef<HTMLDivElement>(null)
const currentRef = React.useRef<HTMLDivElement>(null)
const [visibleCount, setVisibleCount] = React.useState(
QUEUE_INITIAL_VISIBLE_COUNT
)
@@ -107,15 +124,10 @@ export function QueuePanel() {
[queueSongIds, visibleCount]
)
const visibleQueueSongs = useSongs(visibleQueueIds)
const historySongMetadata = useSongs(queueHistorySongIds)
const historySongs = React.useMemo(
() =>
historySongMetadata.flatMap((song, historyIndex) =>
song ? [{ ...song, historyIndex }] : []
),
[historySongMetadata]
)
const historySongs = useSongs(queueHistorySongIds)
const currentSong = visibleQueueSongs[0]
const hasMore = queueSongIds.length > visibleCount
const hasUpcoming = queueSongIds.length > 1
const selectableQueue = React.useMemo(
() => toSelectableQueue(visibleQueueIds, visibleQueueSongs),
@@ -125,17 +137,21 @@ export function QueuePanel() {
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
const currentQueueSongId = queueSongIds[0]
React.useEffect(() => {
if (!queueOpen || queueSongIds.length === 0) {
// Keep the current song at the top as playback moves through the queue:
// jump there on open, then scroll smoothly as songs change.
const hasScrolledRef = React.useRef(false)
React.useLayoutEffect(() => {
const scroller = scrollRef.current
const current = currentRef.current
if (!queueOpen || !scroller || !current) {
return
}
const frame = requestAnimationFrame(() => {
nowPlayingRef.current?.scrollIntoView({ block: "center" })
scroller.scrollTo({
top: current.offsetTop,
behavior: hasScrolledRef.current ? "smooth" : "instant",
})
return () => cancelAnimationFrame(frame)
}, [queueOpen, queueSongIds.length, currentQueueSongId])
hasScrolledRef.current = true
}, [queueOpen, currentQueueSongId, queueHistorySongIds.length])
React.useEffect(() => {
if (!hasMore) {
@@ -143,12 +159,8 @@ export function QueuePanel() {
}
const sentinel = loadMoreRef.current
if (!sentinel) {
return
}
const viewport = sentinel.closest('[data-slot="scroll-area-viewport"]')
if (!viewport) {
const root = scrollRef.current
if (!sentinel || !root) {
return
}
@@ -158,7 +170,7 @@ export function QueuePanel() {
setVisibleCount((count) => count + QUEUE_PAGE_SIZE)
}
},
{ root: viewport, rootMargin: "240px" }
{ root, rootMargin: "240px" }
)
observer.observe(sentinel)
@@ -186,6 +198,11 @@ export function QueuePanel() {
[selectableQueue, skipToQueueIndex]
)
const handleHistoryPlay = React.useCallback(
(historyIndex: number) => void skipToHistoryIndex(historyIndex),
[skipToHistoryIndex]
)
const selectionPositions = React.useMemo(
() =>
getSelectionRangePositions(
@@ -240,76 +257,119 @@ export function QueuePanel() {
return (
<aside
className={cn(
"flex h-svh w-72 shrink-0 flex-col border-l bg-background",
"relative flex h-svh w-72 shrink-0 flex-col border-l bg-background",
"max-md:fixed max-md:inset-y-0 max-md:right-0 max-md:z-20 max-md:shadow-xl"
)}
>
<header className="flex items-center justify-between gap-2 border-b px-4 py-3">
<h1 className="text-lg font-semibold">Up Next</h1>
<Button
variant="ghost"
size="sm"
className="text-primary hover:text-primary"
onClick={() => void clearQueue()}
disabled={queueSongIds.length === 0}
>
Clear
</Button>
</header>
<ScrollArea className="min-h-0 flex-1">
{queueSongIds.length === 0 ? (
<p className="px-4 py-8 text-sm text-muted-foreground">
Nothing in the queue.
</p>
) : (
<>
{historySongs.length > 0 ? (
<ul className="flex flex-col border-b py-1">
{historySongs.map((song) => (
<HistorySongRow
key={`history-${song.id}-${song.historyIndex}`}
song={song}
/>
))}
</ul>
) : null}
<ul className="flex flex-col py-1">
{selectableQueue.map((item, index) =>
item.song ? (
<TrackRow
key={item.selectionId}
ref={item.queueIndex === 0 ? nowPlayingRef : undefined}
variant="queue"
song={item.song}
index={index}
isActive={item.queueIndex === 0}
isNowPlaying={item.queueIndex === 0}
selectionPosition={selectionPositions[index]}
isDragging={draggingIndices.has(index)}
onPointerDown={handlePointerDown}
onSelect={handleRowSelect}
onPlay={handleRowPlay}
onDragStart={setDragIndex}
onDrop={handleDrop}
/>
) : (
<QueueSongRowSkeleton key={item.selectionId} />
)
)}
</ul>
{hasMore ? (
<div
ref={loadMoreRef}
className="flex items-center justify-center py-4"
aria-hidden
<div
ref={scrollRef}
className="relative min-h-0 flex-1 [scrollbar-width:none] overflow-y-auto overscroll-contain"
>
{queueHistorySongIds.length > 0 ? (
<section>
<header className="flex items-center justify-between px-4 pt-3 pb-1">
<h2 className="text-base font-bold">History</h2>
<button
type="button"
className="text-sm text-muted-foreground hover:text-foreground"
onClick={() => void clearHistory()}
>
<Spinner className="size-5 text-muted-foreground" />
</div>
) : null}
</>
)}
</ScrollArea>
Clear
</button>
</header>
<ul className="flex flex-col">
{queueHistorySongIds.map((songId, historyIndex) => (
<HistorySongRow
key={`history-${songId}-${historyIndex}`}
song={historySongs[historyIndex]}
historyIndex={historyIndex}
onPlay={handleHistoryPlay}
/>
))}
</ul>
</section>
) : null}
{/* Fills the viewport so the current song can always scroll to the top. */}
<div ref={currentRef} className="flex min-h-full flex-col">
{currentQueueSongId ? (
<div className="flex items-center gap-3 px-4 py-3">
{currentSong ? (
<>
<TrackCover
albumId={currentSong.album.id}
alt={currentSong.album.title}
inRange={false}
size="lg"
/>
<PlaybackSongMeta
song={currentSong}
align="start"
showAlbum={false}
/>
</>
) : (
<Skeleton className="size-14 shrink-0 rounded-md" />
)}
</div>
) : null}
<header className="sticky top-0 z-20 flex items-center justify-between bg-background px-4 py-2">
<h2 className="text-base font-bold">Continue Playing</h2>
<button
type="button"
className="text-sm text-muted-foreground hover:text-foreground disabled:pointer-events-none disabled:opacity-50"
onClick={() => void clearQueue()}
disabled={queueSongIds.length === 0}
>
Clear
</button>
</header>
{hasUpcoming ? (
<>
<ul className="flex flex-col pb-8">
{selectableQueue.map((item, index) =>
item.song ? (
<TrackRow
key={item.selectionId}
variant="queue"
song={item.song}
index={index}
selectionPosition={selectionPositions[index]}
isDragging={draggingIndices.has(index)}
onPointerDown={handlePointerDown}
onSelect={handleRowSelect}
onPlay={handleRowPlay}
onDragStart={setDragIndex}
onDrop={handleDrop}
/>
) : (
<QueueSongRowSkeleton key={item.selectionId} />
)
)}
</ul>
{hasMore ? (
<div
ref={loadMoreRef}
className="flex items-center justify-center py-4"
aria-hidden
>
<Spinner className="size-5 text-muted-foreground" />
</div>
) : null}
</>
) : (
<p className="flex flex-1 items-center justify-center px-4 pb-24 text-center text-lg font-semibold text-muted-foreground">
Nothing else is queued.
</p>
)}
</div>
</div>
<div
aria-hidden
className="pointer-events-none absolute inset-x-0 bottom-0 h-9 bg-linear-to-b from-transparent to-background"
/>
</aside>
)
}
+21 -12
View File
@@ -3,7 +3,7 @@
import * as React from "react"
import Image from "next/image"
import Link from "next/link"
import { ArrowDownToLine, Music2, Play, Star, Trash2 } from "lucide-react"
import { ArrowDownToLine, Menu, Music2, Play, Star, Trash2 } from "lucide-react"
import {
MediaContextMenu,
@@ -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`: square cover, title and artist, no menu.
* - `queue`: cover, title and artist, duration, and a reorder grip. No menu.
*/
export type TrackRowVariant = "library" | "album" | "playlist" | "queue"
@@ -89,13 +89,14 @@ export function TrackCover({
albumId,
alt,
inRange,
square = false,
size = "md",
children,
}: {
albumId: string | undefined
alt: string
inRange: boolean
square?: boolean
/** `lg` is the larger current-song cover in the queue. */
size?: "md" | "lg"
/** Overlay shown above the cover image. */
children?: React.ReactNode
}) {
@@ -105,8 +106,8 @@ export function TrackCover({
return (
<div
className={cn(
"flex size-10 shrink-0 items-center justify-center bg-muted ring-1",
!square && "rounded",
"flex shrink-0 items-center justify-center bg-muted ring-1",
size === "lg" ? "size-14 rounded-md" : "size-10 rounded",
ringClass
)}
>
@@ -118,8 +119,8 @@ export function TrackCover({
return (
<div
className={cn(
"group/cover relative size-10 shrink-0 overflow-hidden ring-1",
!square && "rounded",
"group/cover relative shrink-0 overflow-hidden ring-1",
size === "lg" ? "size-14 rounded-md" : "size-10 rounded",
ringClass
)}
>
@@ -127,7 +128,7 @@ export function TrackCover({
src={albumCoverUrl(albumId)}
alt={alt}
fill
sizes="40px"
sizes={size === "lg" ? "56px" : "40px"}
className="object-cover"
/>
{children}
@@ -277,7 +278,9 @@ function TrackRowComponent({
const inRange = isInSelectionRange(selectionPosition)
const downloadState = useSongDownloadState(song.id, downloadCollection)
const isDownloaded = downloadState === "here"
const showDownloaded = isDownloaded || downloadState === "device"
// The queue row only shows the duration, like the mobile queue.
const showDownloaded =
variant !== "queue" && (isDownloaded || downloadState === "device")
const flush = variant === "library" || variant === "queue"
const play = () => onPlay(index)
const toggleFavorite = () => onToggleFavorite?.(song.id)
@@ -459,12 +462,11 @@ function TrackRowComponent({
break
case "queue":
content = (
<div className="relative z-10 flex items-center gap-3 px-2">
<div className="relative z-10 flex items-center gap-3 pr-2 pl-4">
<TrackCover
albumId={song.album.id}
alt={song.album.title}
inRange={inRange}
square
/>
<PlaybackSongMeta
song={song}
@@ -474,6 +476,13 @@ function TrackRowComponent({
showAlbum={false}
/>
{duration}
<Menu
aria-hidden
className={cn(
"size-4 shrink-0 cursor-grab",
inRange ? "text-primary-foreground" : "text-muted-foreground"
)}
/>
</div>
)
break