546 lines
18 KiB
TypeScript
546 lines
18 KiB
TypeScript
"use client"
|
|
|
|
import { MobileSidebarTrigger } from "@/components/mobile-sidebar-trigger"
|
|
import * as React from "react"
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
import { Play, Share2, Shuffle, Star } from "lucide-react"
|
|
|
|
import { MediaCollectionFooter } from "@/components/media-collection-footer"
|
|
import { TrackNumberPlayButton } from "@/components/track-number-play-button"
|
|
|
|
import { useAddToPlaylist } from "@/components/add-to-playlist-provider"
|
|
import { FavoriteMediaButton } from "@/components/favorite-media-button"
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import { usePlayback } from "@/components/playback-provider"
|
|
import { copyAlbumLink } from "@/lib/media-links"
|
|
|
|
import {
|
|
MediaContextMenu,
|
|
type MediaContextAction,
|
|
} from "@/components/media-context-menu"
|
|
import { Button } from "@/components/ui/button"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import { useQueueMediaActions } from "@/hooks/use-queue-media-actions"
|
|
import { useTrackSelection } from "@/hooks/use-track-selection"
|
|
import { cn } from "@/lib/utils"
|
|
import type { Id } from "@otunes/convex/_generated/dataModel"
|
|
import type { BrowseAlbumDetail, BrowseAlbumTrack } from "@otunes/convex/types"
|
|
import {
|
|
getSelectionRangePositions,
|
|
isInSelectionRange,
|
|
ALBUM_TRACK_ROW,
|
|
ALBUM_TRACK_TITLE,
|
|
SelectionRangeHighlight,
|
|
TRACK_LIST_CONTAINER,
|
|
TrackRowActiveHighlight,
|
|
TrackRowHoverHighlight,
|
|
TRACK_ROW_PADDING,
|
|
type SelectionRangePosition,
|
|
} from "@/lib/selection-range-styles"
|
|
import { albumCoverUrl } from "@/lib/cover-urls"
|
|
import { sumDurationSeconds } from "@/lib/media-summary"
|
|
import { albumDetailSongs, rememberSongs } from "@/lib/song-cache"
|
|
|
|
function formatDuration(seconds: number): string {
|
|
const minutes = Math.floor(seconds / 60)
|
|
const remainingSeconds = seconds % 60
|
|
return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`
|
|
}
|
|
|
|
function formatMetadata(genre: string | null, year: number | null): string | null {
|
|
const parts: string[] = []
|
|
if (genre) {
|
|
parts.push(genre)
|
|
}
|
|
if (year) {
|
|
parts.push(String(year))
|
|
}
|
|
return parts.length > 0 ? parts.join(" · ") : null
|
|
}
|
|
|
|
type AlbumTrackSection = {
|
|
label: string | null
|
|
tracks: BrowseAlbumTrack[]
|
|
}
|
|
|
|
function groupAlbumTracksByDisk(tracks: BrowseAlbumTrack[]): AlbumTrackSection[] {
|
|
const hasDiskNumbers = tracks.some((track) => track.diskNumber !== null)
|
|
if (!hasDiskNumbers) {
|
|
return [{ label: null, tracks }]
|
|
}
|
|
|
|
const byDisk = new Map<number, BrowseAlbumTrack[]>()
|
|
const withoutDisk: BrowseAlbumTrack[] = []
|
|
|
|
for (const track of tracks) {
|
|
if (track.diskNumber !== null) {
|
|
const group = byDisk.get(track.diskNumber) ?? []
|
|
group.push(track)
|
|
byDisk.set(track.diskNumber, group)
|
|
} else {
|
|
withoutDisk.push(track)
|
|
}
|
|
}
|
|
|
|
const sections: AlbumTrackSection[] = [...byDisk.keys()]
|
|
.sort((a, b) => a - b)
|
|
.map((diskNumber) => ({
|
|
label: `Disc ${diskNumber}`,
|
|
tracks: byDisk.get(diskNumber)!,
|
|
}))
|
|
|
|
if (withoutDisk.length > 0) {
|
|
sections.push({ label: null, tracks: withoutDisk })
|
|
}
|
|
|
|
return sections
|
|
}
|
|
|
|
function TrackRow({
|
|
track,
|
|
sectionIndex,
|
|
artist,
|
|
isActive,
|
|
isSelected,
|
|
selectionPosition,
|
|
isFavorite,
|
|
selectedCount,
|
|
onSelect,
|
|
onPointerDown,
|
|
onToggleFavorite,
|
|
onPrepareContextMenu,
|
|
onMediaAction,
|
|
isNowPlaying,
|
|
onPlay,
|
|
albumId,
|
|
}: {
|
|
track: BrowseAlbumTrack
|
|
sectionIndex: number
|
|
artist: BrowseAlbumDetail["artist"]
|
|
albumId: string
|
|
isActive: boolean
|
|
isSelected: boolean
|
|
selectionPosition: SelectionRangePosition
|
|
isFavorite: boolean
|
|
selectedCount: number
|
|
onSelect: (event: React.MouseEvent) => void
|
|
onPointerDown: (event: React.MouseEvent) => void
|
|
onToggleFavorite: () => void
|
|
onPrepareContextMenu: () => void
|
|
onMediaAction: (action: MediaContextAction, trackId: string) => void
|
|
isNowPlaying: boolean
|
|
onPlay: () => void
|
|
}) {
|
|
const displayTrack =
|
|
track.track > 0 ? track.track : sectionIndex + 1
|
|
const inRange = isInSelectionRange(selectionPosition)
|
|
|
|
return (
|
|
<div
|
|
role="button"
|
|
tabIndex={0}
|
|
onMouseDown={onPointerDown}
|
|
onClick={onSelect}
|
|
onDoubleClick={(event) => {
|
|
event.preventDefault()
|
|
onPlay()
|
|
}}
|
|
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.5 text-left text-sm transition-colors",
|
|
inRange && "text-primary-foreground"
|
|
)}
|
|
>
|
|
<SelectionRangeHighlight position={selectionPosition} />
|
|
{!inRange && isActive ? <TrackRowActiveHighlight /> : null}
|
|
{!inRange && !isActive ? <TrackRowHoverHighlight /> : null}
|
|
<div
|
|
className={cn("relative z-10", ALBUM_TRACK_ROW, TRACK_ROW_PADDING)}
|
|
>
|
|
<div className="flex shrink-0 items-center justify-center">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
className={cn(
|
|
"size-8 opacity-0 transition-opacity group-hover:opacity-100",
|
|
inRange
|
|
? "text-primary-foreground hover:bg-white/15 hover:text-primary-foreground"
|
|
: "text-primary hover:text-primary",
|
|
(isFavorite || isActive || isSelected) && "opacity-100"
|
|
)}
|
|
aria-label={
|
|
isFavorite
|
|
? `Remove ${track.title} from favorites`
|
|
: `Favorite ${track.title}`
|
|
}
|
|
onClick={(event) => {
|
|
event.stopPropagation()
|
|
onToggleFavorite()
|
|
}}
|
|
>
|
|
<Star
|
|
className={cn(
|
|
"size-4",
|
|
isFavorite && (inRange ? "fill-current" : "fill-primary")
|
|
)}
|
|
/>
|
|
</Button>
|
|
</div>
|
|
|
|
<TrackNumberPlayButton
|
|
displayTrack={displayTrack}
|
|
title={track.title}
|
|
isActive={isActive}
|
|
isNowPlaying={isNowPlaying}
|
|
inRange={inRange}
|
|
onPlay={onPlay}
|
|
/>
|
|
|
|
<div className="min-w-0 flex-1">
|
|
<div className={ALBUM_TRACK_TITLE}>{track.title}</div>
|
|
{track.artists ? (
|
|
<p
|
|
className={cn(
|
|
"truncate text-xs",
|
|
inRange
|
|
? "text-primary-foreground/80"
|
|
: "text-muted-foreground"
|
|
)}
|
|
>
|
|
{track.artists.join(", ")}
|
|
</p>
|
|
) : null}
|
|
</div>
|
|
|
|
<span
|
|
className={cn(
|
|
"shrink-0 text-right tabular-nums",
|
|
inRange ? "text-primary-foreground" : "text-muted-foreground"
|
|
)}
|
|
>
|
|
{formatDuration(track.durationSeconds)}
|
|
</span>
|
|
|
|
<MediaContextMenu
|
|
label={`More options for ${track.title}`}
|
|
stationArtistId={artist.id}
|
|
selectedCount={selectedCount}
|
|
inSelectionRange={inRange}
|
|
onOpenMenu={onPrepareContextMenu}
|
|
onAction={(action) => onMediaAction(action, track.id)}
|
|
favorited={isFavorite}
|
|
navigation={{
|
|
artist: { id: artist.id },
|
|
album: { id: albumId },
|
|
}}
|
|
share={{
|
|
type: "song",
|
|
albumId,
|
|
songId: track.id,
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export function AlbumView({
|
|
album,
|
|
highlightedSongId,
|
|
}: {
|
|
album: BrowseAlbumDetail
|
|
highlightedSongId?: string
|
|
}) {
|
|
const { openAddToPlaylist } = useAddToPlaylist()
|
|
const {
|
|
isSongFavorite,
|
|
isAlbumFavorite,
|
|
toggleSongFavorite,
|
|
toggleAlbumFavorite,
|
|
favoriteSongs,
|
|
} = useFavorites()
|
|
const { currentSong, playFromSong, playAlbum, playAlbumShuffled } =
|
|
usePlayback()
|
|
const { runQueueAction, createStation } = useQueueMediaActions()
|
|
const albumSongIds = React.useMemo(
|
|
() => album.tracks.map((track) => track.id as Id<"songs">),
|
|
[album.tracks]
|
|
)
|
|
|
|
React.useEffect(() => {
|
|
rememberSongs(albumDetailSongs(album))
|
|
}, [album])
|
|
|
|
const metadata = formatMetadata(album.genre, album.year)
|
|
const [activeTrackId, setActiveTrackId] = React.useState("")
|
|
const validHighlightedSongId = React.useMemo(() => {
|
|
if (!highlightedSongId) {
|
|
return undefined
|
|
}
|
|
return album.tracks.some((track) => track.id === highlightedSongId)
|
|
? highlightedSongId
|
|
: undefined
|
|
}, [album.tracks, highlightedSongId])
|
|
|
|
const {
|
|
selectedIds,
|
|
selectedCount,
|
|
isSelected,
|
|
handleSelect,
|
|
handlePointerDown,
|
|
prepareContextMenu,
|
|
} = useTrackSelection(album.tracks, validHighlightedSongId)
|
|
|
|
const selectionPositions = React.useMemo(
|
|
() => getSelectionRangePositions(album.tracks.map((track) => track.id), selectedIds),
|
|
[album.tracks, selectedIds]
|
|
)
|
|
|
|
const trackSections = React.useMemo(
|
|
() => groupAlbumTracksByDisk(album.tracks),
|
|
[album.tracks]
|
|
)
|
|
const totalDurationSeconds = React.useMemo(
|
|
() => sumDurationSeconds(album.tracks),
|
|
[album.tracks]
|
|
)
|
|
|
|
const trackListIndexById = React.useMemo(() => {
|
|
const indexById = new Map<string, number>()
|
|
album.tracks.forEach((track, index) => {
|
|
indexById.set(track.id, index)
|
|
})
|
|
return indexById
|
|
}, [album.tracks])
|
|
|
|
function handleMediaAction(
|
|
action: MediaContextAction,
|
|
fallbackTrackId?: string
|
|
) {
|
|
const targetIds =
|
|
selectedIds.size > 0
|
|
? Array.from(selectedIds)
|
|
: fallbackTrackId
|
|
? [fallbackTrackId]
|
|
: activeTrackId
|
|
? [activeTrackId]
|
|
: []
|
|
switch (action) {
|
|
case "create-station":
|
|
void createStation(album.artist.id)
|
|
break
|
|
case "favorite":
|
|
void favoriteSongs(targetIds as Id<"songs">[])
|
|
break
|
|
case "playlist":
|
|
openAddToPlaylist(targetIds as Id<"songs">[])
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle":
|
|
void runQueueAction(action, targetIds as Id<"songs">[])
|
|
break
|
|
}
|
|
}
|
|
|
|
function handleAlbumMediaAction(action: MediaContextAction) {
|
|
const targetIds = album.tracks.map((track) => track.id as Id<"songs">)
|
|
|
|
switch (action) {
|
|
case "create-station":
|
|
void createStation(album.artist.id)
|
|
break
|
|
case "favorite":
|
|
void toggleAlbumFavorite(album.id as Id<"albums">)
|
|
break
|
|
case "playlist":
|
|
openAddToPlaylist(targetIds)
|
|
break
|
|
case "play-next":
|
|
case "play-last":
|
|
case "shuffle":
|
|
void runQueueAction(action, targetIds)
|
|
break
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex h-svh flex-col">
|
|
<ScrollArea className="min-h-0 flex-1">
|
|
<div className="pb-28">
|
|
<div className="mx-auto w-full min-w-0 max-w-9/10">
|
|
<div className="flex flex-col gap-8 pt-6 pb-8 sm:flex-row sm:items-end">
|
|
<div className="relative mx-auto aspect-square w-full max-w-60 shrink-0 overflow-hidden rounded-lg ring-1 ring-foreground/10 sm:mx-0">
|
|
<Image
|
|
src={albumCoverUrl(album.id)}
|
|
alt={album.title}
|
|
fill
|
|
sizes="240px"
|
|
className="object-cover"
|
|
preload
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex min-w-0 flex-1 flex-col gap-4">
|
|
<div className="space-y-1 text-center sm:text-left">
|
|
<p className="text-xs font-medium tracking-wide text-muted-foreground uppercase">
|
|
Album
|
|
</p>
|
|
<div className="flex min-w-0 items-center justify-center gap-2 sm:justify-start md:contents">
|
|
<MobileSidebarTrigger />
|
|
<h1 className="text-3xl font-bold tracking-tight sm:text-4xl">
|
|
{album.title}
|
|
</h1>
|
|
</div>
|
|
<Link
|
|
href={"/artist/" + album.artist.id}
|
|
className="inline-block text-lg font-semibold text-primary hover:underline"
|
|
>
|
|
{album.artist.name}
|
|
</Link>
|
|
{metadata ? (
|
|
<p className="text-sm text-muted-foreground">{metadata}</p>
|
|
) : null}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center justify-center gap-2 sm:justify-start">
|
|
<Button
|
|
variant="secondary"
|
|
size="icon"
|
|
className="rounded-full"
|
|
aria-label="Shuffle album"
|
|
onClick={() =>
|
|
void playAlbumShuffled(
|
|
album.id as Id<"albums">,
|
|
albumSongIds
|
|
)
|
|
}
|
|
>
|
|
<Shuffle />
|
|
</Button>
|
|
<Button
|
|
className="h-10 gap-2 rounded-full bg-foreground px-6 text-background hover:bg-foreground/90"
|
|
aria-label={`Play ${album.title}`}
|
|
onClick={() =>
|
|
void playAlbum(album.id as Id<"albums">, albumSongIds)
|
|
}
|
|
>
|
|
<Play className="fill-current" />
|
|
Play
|
|
</Button>
|
|
<FavoriteMediaButton
|
|
isFavorite={isAlbumFavorite(album.id)}
|
|
onToggle={() =>
|
|
void toggleAlbumFavorite(album.id as Id<"albums">)
|
|
}
|
|
favoritedLabel={`Remove ${album.title} from favorites`}
|
|
unfavoritedLabel={`Favorite ${album.title}`}
|
|
/>
|
|
<Button
|
|
variant="secondary"
|
|
size="icon"
|
|
className="rounded-full"
|
|
aria-label="Share album"
|
|
onClick={() => copyAlbumLink(album.id)}
|
|
>
|
|
<Share2 />
|
|
</Button>
|
|
<MediaContextMenu
|
|
label="More album options"
|
|
stationArtistId={album.artist.id}
|
|
selectedCount={album.tracks.length}
|
|
onOpenMenu={() => {}}
|
|
onAction={handleAlbumMediaAction}
|
|
favorited={isAlbumFavorite(album.id)}
|
|
trackRowTrigger={false}
|
|
triggerVariant="secondary"
|
|
triggerClassName="rounded-full"
|
|
share={{ type: "album", albumId: album.id }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{album.tracks.length > 0 ? (
|
|
<div className={TRACK_LIST_CONTAINER}>
|
|
{trackSections.map((section, sectionIndex) => (
|
|
<div
|
|
key={section.label ?? "other-tracks"}
|
|
className={cn(
|
|
section.label === null &&
|
|
sectionIndex > 0 &&
|
|
"border-t border-border/40"
|
|
)}
|
|
>
|
|
{section.label ? (
|
|
<h2 className="px-8 pt-6 pb-2 text-sm font-semibold text-muted-foreground">
|
|
{section.label}
|
|
</h2>
|
|
) : null}
|
|
{section.tracks.map((track, sectionTrackIndex) => {
|
|
const listIndex = trackListIndexById.get(track.id) ?? 0
|
|
|
|
return (
|
|
<TrackRow
|
|
key={track.id}
|
|
track={track}
|
|
sectionIndex={sectionTrackIndex}
|
|
artist={album.artist}
|
|
albumId={album.id}
|
|
isActive={track.id === activeTrackId}
|
|
isSelected={isSelected(track.id)}
|
|
selectionPosition={selectionPositions[listIndex]}
|
|
isFavorite={isSongFavorite(track.id)}
|
|
selectedCount={
|
|
isSelected(track.id) && selectedCount > 0
|
|
? selectedCount
|
|
: 1
|
|
}
|
|
onPointerDown={handlePointerDown}
|
|
onSelect={(event) => {
|
|
handleSelect(track.id, listIndex, event)
|
|
setActiveTrackId(track.id)
|
|
}}
|
|
onToggleFavorite={() =>
|
|
void toggleSongFavorite(track.id as Id<"songs">)
|
|
}
|
|
onPrepareContextMenu={() =>
|
|
prepareContextMenu(track.id)
|
|
}
|
|
onMediaAction={handleMediaAction}
|
|
isNowPlaying={currentSong?.id === track.id}
|
|
onPlay={() =>
|
|
void playFromSong(
|
|
track.id as Id<"songs">,
|
|
albumSongIds
|
|
)
|
|
}
|
|
/>
|
|
)
|
|
})}
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<p className="py-8 text-sm text-muted-foreground">
|
|
No tracks in this album.
|
|
</p>
|
|
)}
|
|
<MediaCollectionFooter
|
|
releaseDate={album.releaseDate}
|
|
songCount={album.tracks.length}
|
|
totalDurationSeconds={totalDurationSeconds}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</ScrollArea>
|
|
</div>
|
|
)
|
|
}
|