The track list menu showed "Unfavorite" for favorited songs but always favorited. runSongsAction now unfavorites when every target song is already favorited, and rows in a selection label the item by that same rule. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
448 lines
15 KiB
TypeScript
448 lines
15 KiB
TypeScript
"use client"
|
|
|
|
import { PageHeader } from "@/components/library-skeletons"
|
|
import * as React from "react"
|
|
import Image from "next/image"
|
|
import Link from "next/link"
|
|
import { Play, Share2, Shuffle } from "lucide-react"
|
|
|
|
import { AudioFormatBadge } from "@/components/audio-format-badge"
|
|
import { MediaCollectionFooter } from "@/components/media-collection-footer"
|
|
|
|
import { DownloadButton, DownloadedQuality } from "@/components/download-button"
|
|
import { useDownloads } from "@/components/downloads-provider"
|
|
import { FavoriteMediaButton } from "@/components/favorite-media-button"
|
|
import {
|
|
useIsCollectionDownloaded,
|
|
useOpenedFromDownloads,
|
|
} from "@/hooks/use-downloads"
|
|
import { downloadsQueueOrigin } from "@otunes/convex/download-quality"
|
|
import { useFavorites } from "@/components/favorites-provider"
|
|
import {
|
|
useCurrentSongId,
|
|
usePlaybackActions,
|
|
} from "@/components/playback-provider"
|
|
import { copyAlbumLink } from "@/lib/media-links"
|
|
|
|
import {
|
|
MediaContextMenu,
|
|
type MediaContextAction,
|
|
} from "@/components/media-context-menu"
|
|
import { TrackRow } from "@/components/track-row"
|
|
import { Button } from "@/components/ui/button"
|
|
import { ScrollArea } from "@/components/ui/scroll-area"
|
|
import {
|
|
useMediaActions,
|
|
useTrackListMediaAction,
|
|
} from "@/hooks/use-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,
|
|
BrowseSong,
|
|
} from "@otunes/convex/types"
|
|
import {
|
|
getSelectionRangePositions,
|
|
TRACK_LIST_CONTAINER,
|
|
} 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 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
|
|
}
|
|
|
|
export function AlbumView({
|
|
album,
|
|
highlightedSongId,
|
|
}: {
|
|
album: BrowseAlbumDetail
|
|
highlightedSongId?: string
|
|
}) {
|
|
const {
|
|
isSongFavorite,
|
|
areSongsFavorite,
|
|
isAlbumFavorite,
|
|
toggleSongFavorite,
|
|
toggleAlbumFavorite,
|
|
} = useFavorites()
|
|
const currentSongId = useCurrentSongId()
|
|
const {
|
|
playFromSong,
|
|
playAlbum,
|
|
playAlbumShuffled,
|
|
playInOrder,
|
|
playShuffled,
|
|
} = usePlaybackActions()
|
|
const { runSongsAction } = useMediaActions()
|
|
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,
|
|
handleSelect,
|
|
handlePointerDown,
|
|
prepareContextMenu,
|
|
} = useTrackSelection(album.tracks, validHighlightedSongId)
|
|
const selectionFavorited = areSongsFavorite(selectedIds)
|
|
|
|
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])
|
|
|
|
const trackSongs = React.useMemo(
|
|
() =>
|
|
new Map<string, BrowseSong>(
|
|
album.tracks.map((track) => [
|
|
track.id,
|
|
{
|
|
id: track.id,
|
|
title: track.title,
|
|
durationSeconds: track.durationSeconds,
|
|
artist: album.artist,
|
|
album: { id: album.id, title: album.title, cover: album.cover },
|
|
},
|
|
])
|
|
),
|
|
[album.tracks, album.artist, album.id, album.title, album.cover]
|
|
)
|
|
|
|
const downloadRef = React.useMemo(
|
|
() => ({ kind: "album" as const, id: album.id }),
|
|
[album.id]
|
|
)
|
|
const fromDownloads = useOpenedFromDownloads()
|
|
const playOrigin = fromDownloads
|
|
? downloadsQueueOrigin(`album:${album.id}`)
|
|
: `album:${album.id}`
|
|
const handleMediaAction = useTrackListMediaAction({
|
|
selectedIds,
|
|
artistId: album.artist.id,
|
|
downloadFrom: downloadRef,
|
|
})
|
|
const { requestDownload, removeDownload } = useDownloads()
|
|
const downloadTarget = React.useMemo(
|
|
() => ({
|
|
kind: "album" as const,
|
|
id: album.id,
|
|
title: album.title,
|
|
songIds: albumSongIds,
|
|
}),
|
|
[album.id, album.title, albumSongIds]
|
|
)
|
|
const albumDownloaded = useIsCollectionDownloaded(downloadTarget)
|
|
|
|
const handleRowSelect = React.useCallback(
|
|
(index: number, event: React.MouseEvent) => {
|
|
const trackId = album.tracks[index].id
|
|
handleSelect(trackId, index, event)
|
|
setActiveTrackId(trackId)
|
|
},
|
|
[album.tracks, handleSelect]
|
|
)
|
|
|
|
const handleRowPlay = React.useCallback(
|
|
(index: number) => {
|
|
void playFromSong(album.tracks[index].id, albumSongIds, playOrigin)
|
|
},
|
|
[album.tracks, albumSongIds, playOrigin, playFromSong]
|
|
)
|
|
|
|
const handleToggleFavorite = React.useCallback(
|
|
(songId: string) => void toggleSongFavorite(songId as Id<"songs">),
|
|
[toggleSongFavorite]
|
|
)
|
|
|
|
function handleAlbumMediaAction(action: MediaContextAction) {
|
|
if (action === "favorite") {
|
|
void toggleAlbumFavorite(album.id as Id<"albums">)
|
|
return
|
|
}
|
|
if (action === "download") {
|
|
void requestDownload(downloadTarget)
|
|
return
|
|
}
|
|
if (action === "remove-download") {
|
|
void removeDownload(downloadTarget)
|
|
return
|
|
}
|
|
runSongsAction(action, albumSongIds, album.artist.id)
|
|
}
|
|
|
|
return (
|
|
<div className="flex h-svh flex-col">
|
|
<PageHeader openApp />
|
|
<ScrollArea className="min-h-0 flex-1 [&_[data-slot=scroll-area-viewport]>div]:block!">
|
|
<div className="pb-28">
|
|
<div className="mx-auto w-full max-w-9/10 min-w-0">
|
|
<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>
|
|
<h1 className="text-3xl font-bold tracking-tight sm:text-4xl">
|
|
{album.title}
|
|
</h1>
|
|
<Link
|
|
href={"/artist/" + album.artist.id}
|
|
className="inline-block text-lg font-semibold text-primary hover:underline"
|
|
>
|
|
{album.artist.name}
|
|
</Link>
|
|
<div className="flex flex-wrap items-center justify-center gap-2 text-sm text-muted-foreground sm:justify-start">
|
|
{metadata ? <span>{metadata}</span> : null}
|
|
{album.hasLossless ? (
|
|
<>
|
|
{metadata ? <span aria-hidden>·</span> : null}
|
|
<AudioFormatBadge format="lossless" />
|
|
</>
|
|
) : null}
|
|
{album.hasAtmos ? (
|
|
<>
|
|
{metadata || album.hasLossless ? (
|
|
<span aria-hidden>·</span>
|
|
) : null}
|
|
<AudioFormatBadge format="atmos" />
|
|
</>
|
|
) : null}
|
|
<DownloadedQuality collection={downloadRef} />
|
|
</div>
|
|
</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 (fromDownloads
|
|
? playShuffled(albumSongIds, playOrigin)
|
|
: 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 (fromDownloads
|
|
? playInOrder(albumSongIds, playOrigin)
|
|
: 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}`}
|
|
/>
|
|
<DownloadButton target={downloadTarget} />
|
|
<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)}
|
|
downloaded={albumDownloaded}
|
|
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}
|
|
variant="album"
|
|
song={trackSongs.get(track.id)!}
|
|
index={listIndex}
|
|
trackNumber={
|
|
track.track > 0
|
|
? track.track
|
|
: sectionTrackIndex + 1
|
|
}
|
|
subtitle={track.artists?.join(", ")}
|
|
downloadCollection={downloadRef}
|
|
isActive={track.id === activeTrackId}
|
|
selectionPosition={selectionPositions[listIndex]}
|
|
isFavorite={isSongFavorite(track.id)}
|
|
selectedCount={selectedCount}
|
|
selectionStationArtistId={album.artist.id}
|
|
selectionFavorited={selectionFavorited}
|
|
isNowPlaying={currentSongId === track.id}
|
|
onPointerDown={handlePointerDown}
|
|
onSelect={handleRowSelect}
|
|
onPlay={handleRowPlay}
|
|
onToggleFavorite={handleToggleFavorite}
|
|
onPrepareContextMenu={prepareContextMenu}
|
|
onMediaAction={handleMediaAction}
|
|
/>
|
|
)
|
|
})}
|
|
</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>
|
|
)
|
|
}
|