- Replace mobile placeholder with Expo Router app (tabs, playback, auth, library) - Move Convex backend to @otunes/convex for web and mobile reuse - Update web app imports and API routes to use shared package
440 lines
12 KiB
TypeScript
440 lines
12 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import Image from "next/image"
|
|
import { useMutation, usePaginatedQuery, useQuery } 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 { Spinner } from "@/components/ui/spinner"
|
|
import { useTrackSelection } from "@/hooks/use-track-selection"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { HistorySong, QueueSong } from "@otunes/convex/types"
|
|
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"
|
|
|
|
const QUEUE_PAGINATION_THRESHOLD = 100
|
|
const QUEUE_PAGE_SIZE = 50
|
|
|
|
type QueueSelectable = QueueSong & { selectionId: string }
|
|
|
|
function getQueueVersion(queueSongIds: Id<"songs">[]) {
|
|
if (queueSongIds.length === 0) {
|
|
return "empty"
|
|
}
|
|
return `${queueSongIds.length}:${queueSongIds[0]}:${queueSongIds[queueSongIds.length - 1]}`
|
|
}
|
|
|
|
function formatDuration(seconds: number): string {
|
|
const minutes = Math.floor(seconds / 60)
|
|
const remainingSeconds = seconds % 60
|
|
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
|
|
}
|
|
|
|
function toSelectableQueue(songs: QueueSong[]): QueueSelectable[] {
|
|
return songs.map((song) => ({
|
|
...song,
|
|
selectionId: `${song.id}-${song.queueIndex}`,
|
|
}))
|
|
}
|
|
|
|
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 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">
|
|
{song.album.id ? (
|
|
<QueueSongCover
|
|
albumId={song.album.id}
|
|
title={song.album.title}
|
|
inRange={false}
|
|
/>
|
|
) : (
|
|
<div className="flex size-10 shrink-0 items-center justify-center bg-muted ring-1 ring-foreground/10">
|
|
<Music2 className="size-4 text-muted-foreground" />
|
|
</div>
|
|
)}
|
|
|
|
<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>
|
|
)
|
|
}
|
|
|
|
const QueueSongRow = React.forwardRef<
|
|
HTMLLIElement,
|
|
{
|
|
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
|
|
}
|
|
>(function QueueSongRow(
|
|
{
|
|
song,
|
|
isNowPlaying,
|
|
selectionPosition,
|
|
isDragging,
|
|
onSelect,
|
|
onPointerDown,
|
|
onSkipTo,
|
|
onDragStart,
|
|
onDragOver,
|
|
onDrop,
|
|
},
|
|
ref
|
|
) {
|
|
const inRange = isInSelectionRange(selectionPosition)
|
|
|
|
return (
|
|
<li
|
|
ref={ref}
|
|
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 { queueSongIds, queueOpen, clearQueue, skipToQueueIndex } =
|
|
usePlayback()
|
|
const reorderQueue = useMutation(api.playback.reorderQueue)
|
|
const loadMoreRef = React.useRef<HTMLDivElement>(null)
|
|
const nowPlayingRef = React.useRef<HTMLLIElement>(null)
|
|
|
|
const historySongs = useQuery(
|
|
api.playback.getQueueHistory,
|
|
queueOpen ? {} : "skip"
|
|
)
|
|
|
|
const queueVersion = React.useMemo(
|
|
() => getQueueVersion(queueSongIds),
|
|
[queueSongIds]
|
|
)
|
|
const shouldPaginate = queueSongIds.length > QUEUE_PAGINATION_THRESHOLD
|
|
const initialPageSize = shouldPaginate
|
|
? QUEUE_PAGE_SIZE
|
|
: queueSongIds.length
|
|
|
|
const { results, status, loadMore } = usePaginatedQuery(
|
|
api.playback.getQueuePaginated,
|
|
queueOpen && queueSongIds.length > 0
|
|
? { queueVersion }
|
|
: "skip",
|
|
{ initialNumItems: initialPageSize }
|
|
)
|
|
|
|
const queueSongs = results ?? []
|
|
const selectableQueue = React.useMemo(
|
|
() => toSelectableQueue(queueSongs),
|
|
[queueSongs]
|
|
)
|
|
|
|
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
|
|
|
|
React.useEffect(() => {
|
|
if (!queueOpen || queueSongIds.length === 0) {
|
|
return
|
|
}
|
|
|
|
const frame = requestAnimationFrame(() => {
|
|
nowPlayingRef.current?.scrollIntoView({ block: "center" })
|
|
})
|
|
|
|
return () => cancelAnimationFrame(frame)
|
|
}, [queueOpen, queueSongIds[0]])
|
|
|
|
React.useEffect(() => {
|
|
if (!shouldPaginate) {
|
|
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 && status === "CanLoadMore") {
|
|
loadMore(QUEUE_PAGE_SIZE)
|
|
}
|
|
},
|
|
{ root: viewport, rootMargin: "240px" }
|
|
)
|
|
|
|
observer.observe(sentinel)
|
|
return () => observer.disconnect()
|
|
}, [shouldPaginate, status, loadMore])
|
|
|
|
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: Id<"songs">[]) {
|
|
try {
|
|
await reorderQueue({ songIds: nextQueue })
|
|
} catch (error) {
|
|
toast.error(
|
|
error instanceof Error ? error.message : "Failed to reorder queue"
|
|
)
|
|
}
|
|
}
|
|
|
|
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 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={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>
|
|
) : status === "LoadingFirstPage" ? (
|
|
<div className="flex items-center justify-center py-8">
|
|
<Spinner className="size-5 text-muted-foreground" />
|
|
</div>
|
|
) : (
|
|
<>
|
|
{historySongs && 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((song, index) => (
|
|
<QueueSongRow
|
|
key={song.selectionId}
|
|
ref={song.queueIndex === 0 ? nowPlayingRef : undefined}
|
|
song={song}
|
|
isNowPlaying={song.queueIndex === 0}
|
|
selectionPosition={selectionPositions[index]}
|
|
isDragging={draggingIndices.has(index)}
|
|
onPointerDown={handlePointerDown}
|
|
onSelect={(event) => {
|
|
handleSelect(song.selectionId, index, event)
|
|
}}
|
|
onSkipTo={() => void skipToQueueIndex(song.queueIndex)}
|
|
onDragStart={(event) => {
|
|
setDragIndex(index)
|
|
event.dataTransfer.effectAllowed = "move"
|
|
}}
|
|
onDragOver={(event) => event.preventDefault()}
|
|
onDrop={(event) => {
|
|
event.preventDefault()
|
|
handleDrop(index)
|
|
}}
|
|
/>
|
|
))}
|
|
</ul>
|
|
{shouldPaginate ? (
|
|
<div
|
|
ref={loadMoreRef}
|
|
className="flex items-center justify-center py-4"
|
|
aria-hidden={status !== "LoadingMore"}
|
|
>
|
|
{status === "LoadingMore" ? (
|
|
<Spinner className="size-5 text-muted-foreground" />
|
|
) : null}
|
|
</div>
|
|
) : null}
|
|
</>
|
|
)}
|
|
</ScrollArea>
|
|
</aside>
|
|
)
|
|
}
|