294 lines
7.9 KiB
TypeScript
294 lines
7.9 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import Image from "next/image"
|
|
import { useMutation } from "convex/react"
|
|
import { Music2 } from "lucide-react"
|
|
import { toast } from "sonner"
|
|
|
|
import { PlaybackSongMeta } from "@/components/playback-song-meta"
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { Button } from "@/components/ui/button"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import { useTrackSelection } from "@/hooks/use-track-selection"
|
|
import { api } from "@/convex/_generated/api"
|
|
import type { Id } from "@/convex/_generated/dataModel"
|
|
import type { BrowseSong } from "@/convex/library"
|
|
import { albumCoverUrl } from "@/lib/cover-urls"
|
|
import {
|
|
getContiguousSelectedIndices,
|
|
moveQueueBlock,
|
|
} from "@/lib/move-queue-block"
|
|
import {
|
|
getSelectionRangePositions,
|
|
isInSelectionRange,
|
|
QueueSelectionRangeHighlight,
|
|
QueueTrackRowActiveHighlight,
|
|
QueueTrackRowHoverHighlight,
|
|
type SelectionRangePosition,
|
|
} from "@/lib/selection-range-styles"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
type QueueSelectable = BrowseSong & { selectionId: string }
|
|
|
|
function formatDuration(seconds: number): string {
|
|
const minutes = Math.floor(seconds / 60)
|
|
const remainingSeconds = seconds % 60
|
|
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
|
|
}
|
|
|
|
function toSelectableQueue(queue: BrowseSong[]): QueueSelectable[] {
|
|
return queue.map((song, index) => ({
|
|
...song,
|
|
selectionId: `${song.id}-${index}`,
|
|
}))
|
|
}
|
|
|
|
function QueueSongCover({
|
|
albumId,
|
|
title,
|
|
inRange,
|
|
}: {
|
|
albumId: string
|
|
title: string
|
|
inRange: boolean
|
|
}) {
|
|
return (
|
|
<div
|
|
className={cn(
|
|
"relative size-10 shrink-0 overflow-hidden ring-1",
|
|
inRange ? "ring-white/25" : "ring-foreground/10"
|
|
)}
|
|
>
|
|
<Image
|
|
src={albumCoverUrl(albumId)}
|
|
alt={title}
|
|
fill
|
|
sizes="40px"
|
|
className="object-cover"
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function QueueSongRow({
|
|
song,
|
|
isNowPlaying,
|
|
selectionPosition,
|
|
isDragging,
|
|
onSelect,
|
|
onPointerDown,
|
|
onSkipTo,
|
|
onDragStart,
|
|
onDragOver,
|
|
onDrop,
|
|
}: {
|
|
song: QueueSelectable
|
|
isNowPlaying: boolean
|
|
selectionPosition: SelectionRangePosition
|
|
isDragging: boolean
|
|
onSelect: (event: React.MouseEvent) => void
|
|
onPointerDown: (event: React.MouseEvent) => void
|
|
onSkipTo: () => void
|
|
onDragStart: (event: React.DragEvent) => void
|
|
onDragOver: (event: React.DragEvent) => void
|
|
onDrop: (event: React.DragEvent) => void
|
|
}) {
|
|
const inRange = isInSelectionRange(selectionPosition)
|
|
|
|
return (
|
|
<li
|
|
role="button"
|
|
tabIndex={0}
|
|
draggable
|
|
onDragStart={onDragStart}
|
|
onDragOver={onDragOver}
|
|
onDrop={onDrop}
|
|
onMouseDown={onPointerDown}
|
|
onClick={onSelect}
|
|
onDoubleClick={(event) => {
|
|
event.preventDefault()
|
|
onSkipTo()
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "Enter" || event.key === " ") {
|
|
event.preventDefault()
|
|
onSelect(event as unknown as React.MouseEvent)
|
|
}
|
|
}}
|
|
className={cn(
|
|
"group relative w-full cursor-pointer select-none py-2 text-left text-sm transition-colors",
|
|
inRange && "text-primary-foreground",
|
|
isDragging && "opacity-50"
|
|
)}
|
|
>
|
|
<QueueSelectionRangeHighlight position={selectionPosition} />
|
|
{!inRange && isNowPlaying ? <QueueTrackRowActiveHighlight /> : null}
|
|
{!inRange && !isNowPlaying ? <QueueTrackRowHoverHighlight /> : null}
|
|
<div className="relative z-10 flex items-center gap-3 px-2">
|
|
{song.album.id ? (
|
|
<QueueSongCover
|
|
albumId={song.album.id}
|
|
title={song.album.title}
|
|
inRange={inRange}
|
|
/>
|
|
) : (
|
|
<div
|
|
className={cn(
|
|
"flex size-10 shrink-0 items-center justify-center bg-muted ring-1",
|
|
inRange ? "ring-white/25" : "ring-foreground/10"
|
|
)}
|
|
>
|
|
<Music2 className="size-4 text-muted-foreground" />
|
|
</div>
|
|
)}
|
|
|
|
<PlaybackSongMeta
|
|
song={song}
|
|
isCurrent={isNowPlaying}
|
|
inRange={inRange}
|
|
align="start"
|
|
showAlbum={false}
|
|
/>
|
|
|
|
<span
|
|
className={cn(
|
|
"shrink-0 text-xs tabular-nums",
|
|
inRange ? "text-primary-foreground" : "text-muted-foreground"
|
|
)}
|
|
>
|
|
{formatDuration(song.durationSeconds)}
|
|
</span>
|
|
</div>
|
|
</li>
|
|
)
|
|
}
|
|
|
|
export function QueuePanel() {
|
|
const { queue, queueOpen, clearQueue, skipToQueueIndex } = usePlayback()
|
|
const reorderQueue = useMutation(api.playback.reorderQueue)
|
|
|
|
const selectableQueue = React.useMemo(() => toSelectableQueue(queue), [queue])
|
|
|
|
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
|
|
|
|
const {
|
|
selectedIds,
|
|
handleSelect,
|
|
handlePointerDown,
|
|
} = useTrackSelection(
|
|
selectableQueue.map((item) => ({ id: item.selectionId }))
|
|
)
|
|
|
|
const selectionPositions = React.useMemo(
|
|
() =>
|
|
getSelectionRangePositions(
|
|
selectableQueue.map((item) => item.selectionId),
|
|
selectedIds
|
|
),
|
|
[selectableQueue, selectedIds]
|
|
)
|
|
|
|
const draggingIndices = React.useMemo(() => {
|
|
if (dragIndex === null) {
|
|
return new Set<number>()
|
|
}
|
|
return new Set(
|
|
getContiguousSelectedIndices(selectableQueue, selectedIds, dragIndex)
|
|
)
|
|
}, [dragIndex, selectableQueue, selectedIds])
|
|
|
|
async function persistOrder(nextQueue: BrowseSong[]) {
|
|
try {
|
|
await reorderQueue({
|
|
songIds: nextQueue.map((song) => song.id as Id<"songs">),
|
|
})
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to reorder queue"
|
|
)
|
|
}
|
|
}
|
|
|
|
function handleDrop(targetIndex: number) {
|
|
if (dragIndex === null) {
|
|
return
|
|
}
|
|
|
|
const fromIndices = getContiguousSelectedIndices(
|
|
selectableQueue,
|
|
selectedIds,
|
|
dragIndex
|
|
)
|
|
const next = moveQueueBlock(queue, fromIndices, targetIndex)
|
|
|
|
setDragIndex(null)
|
|
|
|
if (next === queue) {
|
|
return
|
|
}
|
|
|
|
void persistOrder(next)
|
|
}
|
|
|
|
if (!queueOpen) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<aside
|
|
className={cn(
|
|
"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={queue.length === 0}
|
|
>
|
|
Clear
|
|
</Button>
|
|
</header>
|
|
|
|
<ScrollArea className="min-h-0 flex-1">
|
|
{queue.length === 0 ? (
|
|
<p className="px-4 py-8 text-sm text-muted-foreground">
|
|
Nothing in the queue.
|
|
</p>
|
|
) : (
|
|
<ul className="flex flex-col py-1">
|
|
{selectableQueue.map((song, index) => (
|
|
<QueueSongRow
|
|
key={song.selectionId}
|
|
song={song}
|
|
isNowPlaying={index === 0}
|
|
selectionPosition={selectionPositions[index]}
|
|
isDragging={draggingIndices.has(index)}
|
|
onPointerDown={handlePointerDown}
|
|
onSelect={(event) => {
|
|
handleSelect(song.selectionId, index, event)
|
|
}}
|
|
onSkipTo={() => void skipToQueueIndex(index)}
|
|
onDragStart={(event) => {
|
|
setDragIndex(index)
|
|
event.dataTransfer.effectAllowed = "move"
|
|
}}
|
|
onDragOver={(event) => event.preventDefault()}
|
|
onDrop={(event) => {
|
|
event.preventDefault()
|
|
handleDrop(index)
|
|
}}
|
|
/>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</ScrollArea>
|
|
</aside>
|
|
)
|
|
}
|