497 lines
15 KiB
TypeScript
497 lines
15 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import {
|
|
MediaContextMenuArea,
|
|
type MediaContextAction,
|
|
} from "@/components/media-context-menu"
|
|
import { PlaybackSongMeta } from "@/components/playback-song-meta"
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { TrackCover, TrackRow } from "@/components/track-row"
|
|
import { Skeleton } from "@/components/ui/skeleton"
|
|
import { Spinner } from "@/components/ui/spinner"
|
|
import { useSongDownloadState } from "@/hooks/use-downloads"
|
|
import { useMediaActions } from "@/hooks/use-media-actions"
|
|
import { useTrackSelection } from "@/hooks/use-track-selection"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { BrowseSong, QueueSong } from "@otunes/convex/types"
|
|
import {
|
|
getContiguousSelectedIndices,
|
|
moveQueueBlock,
|
|
} from "@/lib/move-queue-block"
|
|
import { getSelectionRangePositions } from "@/lib/selection-range-styles"
|
|
import { stationArtistId } from "@/lib/station-artist"
|
|
import { useSongs } from "@/lib/song-cache"
|
|
import { cn } from "@/lib/utils"
|
|
|
|
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
|
|
}
|
|
|
|
/** 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.slice(1).map((songId, offset) => {
|
|
const queueIndex = offset + 1
|
|
const selectionId = `${songId}-${queueIndex}`
|
|
const song = songs[queueIndex]
|
|
return {
|
|
selectionId,
|
|
queueIndex,
|
|
song: song ? { ...song, queueIndex, selectionId } : null,
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Opens the song's right-click menu over `children`, for the queue rows that
|
|
* stand alone (history and the current song) rather than in the selection.
|
|
*/
|
|
function SongMenuArea({
|
|
song,
|
|
children,
|
|
}: {
|
|
song: BrowseSong
|
|
children: React.ReactElement
|
|
}) {
|
|
const { isSongFavorite, toggleSongFavorite } = useFavorites()
|
|
const { runSongsAction } = useMediaActions()
|
|
const downloaded = useSongDownloadState(song.id) === "here"
|
|
|
|
function handleAction(action: MediaContextAction) {
|
|
if (action === "favorite") {
|
|
void toggleSongFavorite(song.id)
|
|
return
|
|
}
|
|
runSongsAction(action, [song.id], song.artist.id)
|
|
}
|
|
|
|
return (
|
|
<MediaContextMenuArea
|
|
menu={{
|
|
label: `More options for ${song.title}`,
|
|
stationArtistId: song.artist.id,
|
|
selectedCount: 1,
|
|
onOpenMenu: () => {},
|
|
onAction: handleAction,
|
|
favorited: isSongFavorite(song.id),
|
|
downloaded,
|
|
navigation: {
|
|
artist: { id: song.artist.id },
|
|
album: { id: song.album.id },
|
|
},
|
|
share: { type: "song", albumId: song.album.id, songId: song.id },
|
|
}}
|
|
>
|
|
{children}
|
|
</MediaContextMenuArea>
|
|
)
|
|
}
|
|
|
|
/** 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 (
|
|
<SongMenuArea song={song}>
|
|
<li
|
|
role="button"
|
|
tabIndex={0}
|
|
className="flex cursor-pointer items-center gap-3 px-4 py-2 text-sm hover:bg-foreground/5 data-[state=open]: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>
|
|
</SongMenuArea>
|
|
)
|
|
}
|
|
|
|
function QueueSongRowSkeleton() {
|
|
return (
|
|
<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>
|
|
</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 scrollRef = React.useRef<HTMLDivElement>(null)
|
|
const currentRef = React.useRef<HTMLDivElement>(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 historySongs = useSongs(queueHistorySongIds)
|
|
const currentSong = visibleQueueSongs[0]
|
|
const hasMore = queueSongIds.length > visibleCount
|
|
const hasUpcoming = queueSongIds.length > 1
|
|
|
|
const selectableQueue = React.useMemo(
|
|
() => toSelectableQueue(visibleQueueIds, visibleQueueSongs),
|
|
[visibleQueueIds, visibleQueueSongs]
|
|
)
|
|
|
|
const [dragIndex, setDragIndex] = React.useState<number | null>(null)
|
|
const currentQueueSongId = queueSongIds[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
|
|
}
|
|
scroller.scrollTo({
|
|
top: current.offsetTop,
|
|
behavior: hasScrolledRef.current ? "smooth" : "instant",
|
|
})
|
|
hasScrolledRef.current = true
|
|
}, [queueOpen, currentQueueSongId, queueHistorySongIds.length])
|
|
|
|
React.useEffect(() => {
|
|
if (!hasMore) {
|
|
return
|
|
}
|
|
|
|
const sentinel = loadMoreRef.current
|
|
const root = scrollRef.current
|
|
if (!sentinel || !root) {
|
|
return
|
|
}
|
|
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
if (entries[0]?.isIntersecting) {
|
|
setVisibleCount((count) => count + QUEUE_PAGE_SIZE)
|
|
}
|
|
},
|
|
{ root, rootMargin: "240px" }
|
|
)
|
|
|
|
observer.observe(sentinel)
|
|
return () => observer.disconnect()
|
|
}, [hasMore])
|
|
|
|
const selectionItems = React.useMemo(
|
|
() => selectableQueue.map((item) => ({ id: item.selectionId })),
|
|
[selectableQueue]
|
|
)
|
|
const {
|
|
selectedIds,
|
|
selectedCount,
|
|
handleSelect,
|
|
handlePointerDown,
|
|
prepareContextMenu,
|
|
clearSelection,
|
|
} = useTrackSelection(selectionItems)
|
|
const { isSongFavorite } = useFavorites()
|
|
const { runSongsAction } = useMediaActions()
|
|
|
|
/** The rows a menu acts on: the selection, else just the clicked row. */
|
|
const menuTargets = React.useCallback(
|
|
(selectionId: string) => {
|
|
const ids = selectedIds.has(selectionId)
|
|
? selectedIds
|
|
: new Set([selectionId])
|
|
return selectableQueue.filter((item) => ids.has(item.selectionId))
|
|
},
|
|
[selectableQueue, selectedIds]
|
|
)
|
|
|
|
const selectedStationArtistId = React.useMemo(
|
|
() =>
|
|
stationArtistId(
|
|
selectableQueue.flatMap((item) =>
|
|
item.song ? [{ id: item.selectionId, artist: item.song.artist }] : []
|
|
),
|
|
selectedIds
|
|
),
|
|
[selectableQueue, selectedIds]
|
|
)
|
|
|
|
const handleMediaAction = React.useCallback(
|
|
(action: MediaContextAction, selectionId: string) => {
|
|
const songs = menuTargets(selectionId).flatMap((item) =>
|
|
item.song ? [item.song] : []
|
|
)
|
|
const artistId = songs[0]?.artist.id
|
|
runSongsAction(
|
|
action,
|
|
songs.map((song) => song.id),
|
|
songs.every((song) => song.artist.id === artistId)
|
|
? artistId
|
|
: undefined
|
|
)
|
|
},
|
|
[menuTargets, runSongsAction]
|
|
)
|
|
|
|
const handleRemove = React.useCallback(
|
|
(selectionId: string) => {
|
|
const removed = new Set(
|
|
menuTargets(selectionId).map((item) => item.queueIndex)
|
|
)
|
|
clearSelection()
|
|
void reorderQueue(queueSongIds.filter((_, index) => !removed.has(index)))
|
|
},
|
|
[clearSelection, menuTargets, queueSongIds, reorderQueue]
|
|
)
|
|
|
|
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 handleHistoryPlay = React.useCallback(
|
|
(historyIndex: number) => void skipToHistoryIndex(historyIndex),
|
|
[skipToHistoryIndex]
|
|
)
|
|
|
|
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(
|
|
"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"
|
|
)}
|
|
>
|
|
<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()}
|
|
>
|
|
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">
|
|
{currentSong ? (
|
|
<SongMenuArea song={currentSong}>
|
|
<div className="flex items-center gap-3 px-4 py-3">
|
|
<TrackCover
|
|
albumId={currentSong.album.id}
|
|
alt={currentSong.album.title}
|
|
inRange={false}
|
|
size="lg"
|
|
/>
|
|
<PlaybackSongMeta
|
|
song={currentSong}
|
|
align="start"
|
|
showAlbum={false}
|
|
/>
|
|
</div>
|
|
</SongMenuArea>
|
|
) : currentQueueSongId ? (
|
|
<div className="px-4 py-3">
|
|
<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}
|
|
selectionId={item.selectionId}
|
|
isFavorite={isSongFavorite(item.song.id)}
|
|
selectedCount={selectedCount}
|
|
selectionStationArtistId={selectedStationArtistId}
|
|
removeLabel="Remove from Queue"
|
|
selectionPosition={selectionPositions[index]}
|
|
isDragging={draggingIndices.has(index)}
|
|
onPointerDown={handlePointerDown}
|
|
onSelect={handleRowSelect}
|
|
onPlay={handleRowPlay}
|
|
onDragStart={setDragIndex}
|
|
onDrop={handleDrop}
|
|
onPrepareContextMenu={prepareContextMenu}
|
|
onMediaAction={handleMediaAction}
|
|
onRemove={handleRemove}
|
|
/>
|
|
) : (
|
|
<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>
|
|
)
|
|
}
|