309 lines
8.8 KiB
TypeScript
309 lines
8.8 KiB
TypeScript
"use client"
|
|
|
|
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 {
|
|
getContiguousSelectedIndices,
|
|
moveQueueBlock,
|
|
} from "@/lib/move-queue-block"
|
|
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
|
|
|
|
type QueueSelectable = QueueSong & { selectionId: string }
|
|
|
|
type QueueItem = {
|
|
selectionId: string
|
|
queueIndex: number
|
|
song: QueueSelectable | null
|
|
}
|
|
|
|
function toSelectableQueue(
|
|
songIds: Id<"songs">[],
|
|
songs: (BrowseSong | undefined)[]
|
|
): QueueItem[] {
|
|
return songIds.map((songId, queueIndex) => {
|
|
const selectionId = `${songId}-${queueIndex}`
|
|
const song = songs[queueIndex]
|
|
return {
|
|
selectionId,
|
|
queueIndex,
|
|
song: song ? { ...song, queueIndex, selectionId } : null,
|
|
}
|
|
})
|
|
}
|
|
|
|
function HistorySongRow({ song }: { song: HistorySong }) {
|
|
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 tabular-nums text-muted-foreground">
|
|
{formatDuration(song.durationSeconds)}
|
|
</span>
|
|
</div>
|
|
</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" />
|
|
<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>
|
|
)
|
|
}
|
|
|
|
export function QueuePanel() {
|
|
const {
|
|
queueSongIds,
|
|
queueHistorySongIds,
|
|
queueOpen,
|
|
clearQueue,
|
|
reorderQueue,
|
|
skipToQueueIndex,
|
|
} = usePlayback()
|
|
const loadMoreRef = React.useRef<HTMLDivElement>(null)
|
|
const nowPlayingRef = React.useRef<HTMLElement>(null)
|
|
const [visibleCount, setVisibleCount] = React.useState(
|
|
QUEUE_INITIAL_VISIBLE_COUNT
|
|
)
|
|
|
|
const visibleQueueIds = React.useMemo(
|
|
() => queueSongIds.slice(0, visibleCount),
|
|
[queueSongIds, visibleCount]
|
|
)
|
|
const visibleQueueSongs = useSongs(visibleQueueIds)
|
|
const historySongMetadata = useSongs(queueHistorySongIds)
|
|
const historySongs = React.useMemo(
|
|
() =>
|
|
historySongMetadata.flatMap((song, historyIndex) =>
|
|
song ? [{ ...song, historyIndex }] : []
|
|
),
|
|
[historySongMetadata]
|
|
)
|
|
const hasMore = queueSongIds.length > visibleCount
|
|
|
|
const selectableQueue = React.useMemo(
|
|
() => toSelectableQueue(visibleQueueIds, visibleQueueSongs),
|
|
[visibleQueueIds, visibleQueueSongs]
|
|
)
|
|
|
|
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
|
|
const currentQueueSongId = queueSongIds[0]
|
|
|
|
React.useEffect(() => {
|
|
if (!queueOpen || queueSongIds.length === 0) {
|
|
return
|
|
}
|
|
|
|
const frame = requestAnimationFrame(() => {
|
|
nowPlayingRef.current?.scrollIntoView({ block: "center" })
|
|
})
|
|
|
|
return () => cancelAnimationFrame(frame)
|
|
}, [queueOpen, queueSongIds.length, currentQueueSongId])
|
|
|
|
React.useEffect(() => {
|
|
if (!hasMore) {
|
|
return
|
|
}
|
|
|
|
const sentinel = loadMoreRef.current
|
|
if (!sentinel) {
|
|
return
|
|
}
|
|
|
|
const viewport = sentinel.closest('[data-slot="scroll-area-viewport"]')
|
|
if (!viewport) {
|
|
return
|
|
}
|
|
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
if (entries[0]?.isIntersecting) {
|
|
setVisibleCount((count) => count + QUEUE_PAGE_SIZE)
|
|
}
|
|
},
|
|
{ root: viewport, rootMargin: "240px" }
|
|
)
|
|
|
|
observer.observe(sentinel)
|
|
return () => observer.disconnect()
|
|
}, [hasMore])
|
|
|
|
const selectionItems = React.useMemo(
|
|
() => selectableQueue.map((item) => ({ id: item.selectionId })),
|
|
[selectableQueue]
|
|
)
|
|
const { selectedIds, handleSelect, handlePointerDown } =
|
|
useTrackSelection(selectionItems)
|
|
|
|
const handleRowSelect = React.useCallback(
|
|
(index: number, event: React.MouseEvent) => {
|
|
handleSelect(selectableQueue[index].selectionId, index, event)
|
|
},
|
|
[selectableQueue, handleSelect]
|
|
)
|
|
|
|
const handleRowPlay = React.useCallback(
|
|
(index: number) => {
|
|
void skipToQueueIndex(selectableQueue[index].queueIndex)
|
|
},
|
|
[selectableQueue, skipToQueueIndex]
|
|
)
|
|
|
|
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])
|
|
|
|
function handleDrop(targetLocalIndex: number) {
|
|
if (dragIndex === null) {
|
|
return
|
|
}
|
|
|
|
const fromLocalIndices = getContiguousSelectedIndices(
|
|
selectableQueue,
|
|
selectedIds,
|
|
dragIndex
|
|
)
|
|
const fromQueueIndices = fromLocalIndices.map(
|
|
(index) => selectableQueue[index].queueIndex
|
|
)
|
|
const targetQueueIndex = selectableQueue[targetLocalIndex].queueIndex
|
|
const next = moveQueueBlock(queueSongIds, fromQueueIndices, targetQueueIndex)
|
|
|
|
setDragIndex(null)
|
|
|
|
if (next === queueSongIds) {
|
|
return
|
|
}
|
|
|
|
void reorderQueue(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={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
|
|
>
|
|
<Spinner className="size-5 text-muted-foreground" />
|
|
</div>
|
|
) : null}
|
|
</>
|
|
)}
|
|
</ScrollArea>
|
|
</aside>
|
|
)
|
|
}
|