diff --git a/apps/web/components/playback-provider.tsx b/apps/web/components/playback-provider.tsx index 95ccc42..7849447 100644 --- a/apps/web/components/playback-provider.tsx +++ b/apps/web/components/playback-provider.tsx @@ -103,8 +103,11 @@ type PlaybackContextValue = { toggleShuffle: () => Promise cycleRepeat: () => Promise clearQueue: () => Promise + clearHistory: () => Promise reorderQueue: (songIds: Id<"songs">[]) => Promise skipToQueueIndex: (index: number) => Promise + /** Plays `history[index]`, moving it and every later song back into the queue. */ + skipToHistoryIndex: (index: number) => Promise skipNext: () => Promise skipPrevious: () => Promise 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, ] diff --git a/apps/web/components/queue-panel.tsx b/apps/web/components/queue-panel.tsx index a08984b..996c994 100644 --- a/apps/web/components/queue-panel.tsx +++ b/apps/web/components/queue-panel.tsx @@ -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 + } + return ( -
  • -
    - - - - - - {formatDuration(song.durationSeconds)} - -
    +
  • onPlay(historyIndex)} + onKeyDown={(event) => { + if (event.key === "Enter" || event.key === " ") { + event.preventDefault() + onPlay(historyIndex) + } + }} + > + +
  • ) } function QueueSongRowSkeleton() { return ( -
  • - +
  • +
    -
  • ) } +/** + * 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(null) - const nowPlayingRef = React.useRef(null) + const scrollRef = React.useRef(null) + const currentRef = React.useRef(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(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 ( ) } diff --git a/apps/web/components/track-row.tsx b/apps/web/components/track-row.tsx index 32eb139..9dbee0e 100644 --- a/apps/web/components/track-row.tsx +++ b/apps/web/components/track-row.tsx @@ -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 (
    @@ -118,8 +119,8 @@ export function TrackCover({ return (
    @@ -127,7 +128,7 @@ export function TrackCover({ src={albumCoverUrl(albumId)} alt={alt} fill - sizes="40px" + sizes={size === "lg" ? "56px" : "40px"} className="object-cover" /> {children} @@ -459,12 +460,11 @@ function TrackRowComponent({ break case "queue": content = ( -
    +
    {duration} +
    ) break