177 lines
6.0 KiB
Svelte
177 lines
6.0 KiB
Svelte
<script lang="ts">
|
|
import { plural } from '#lib/utils/plural.js';
|
|
import PageColumn from '#lib/components/PageColumn.svelte';
|
|
import { page } from '$app/state';
|
|
import { createAlbumQuery } from '#lib/api/queries.js';
|
|
import { qk } from '#lib/api/queries.js';
|
|
import { pageTitle } from '#lib/branding.js';
|
|
import TrackRow from '#lib/components/TrackRow.svelte';
|
|
import TrackList from '#lib/components/TrackList.svelte';
|
|
import CollectionHeader from '#lib/components/CollectionHeader.svelte';
|
|
import { shuffled } from '#lib/utils/shuffle.js';
|
|
import LibrarySkeleton from '#lib/components/LibrarySkeleton.svelte';
|
|
import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte';
|
|
import LikeButton from '#lib/components/LikeButton.svelte';
|
|
import { useDelayed } from '#lib/utils/useDelayed.svelte.js';
|
|
import { formatDuration } from '#lib/media/duration.js';
|
|
import type { ApiError } from '#lib/api/client.js';
|
|
import { playQueue } from '#lib/player/store.svelte.js';
|
|
import { user } from '#lib/auth/store.svelte.js';
|
|
import { refetchAlbumCover } from '#lib/api/admin.js';
|
|
import { errCode } from '#lib/api/errors.js';
|
|
import { useQueryClient } from '@tanstack/svelte-query';
|
|
|
|
const id = $derived(page.params.id ?? '');
|
|
const queryStore = $derived(createAlbumQuery(id));
|
|
const query = $derived($queryStore);
|
|
const showSkeleton = useDelayed(() => query.isPending);
|
|
|
|
const notFound = $derived(
|
|
query.isError && (query.error as unknown as ApiError | undefined)?.code === 'not_found'
|
|
);
|
|
|
|
let isStartingPlay = $state(false);
|
|
|
|
function onPlay() {
|
|
const data = query.data;
|
|
if (isStartingPlay || !data || data.tracks.length === 0) return;
|
|
isStartingPlay = true;
|
|
try {
|
|
playQueue(data.tracks, 0);
|
|
} finally {
|
|
isStartingPlay = false;
|
|
}
|
|
}
|
|
|
|
function onShuffle() {
|
|
const data = query.data;
|
|
if (isStartingPlay || !data || data.tracks.length === 0) return;
|
|
isStartingPlay = true;
|
|
try {
|
|
playQueue(shuffled(data.tracks), 0);
|
|
} finally {
|
|
isStartingPlay = false;
|
|
}
|
|
}
|
|
|
|
const queryClient = useQueryClient();
|
|
let refetching = $state(false);
|
|
let refetchError = $state<string | null>(null);
|
|
|
|
const isAdmin = $derived(user.value?.is_admin === true);
|
|
|
|
async function onRefetchCover() {
|
|
const data = query.data;
|
|
if (!data || refetching) return;
|
|
refetching = true;
|
|
refetchError = null;
|
|
try {
|
|
await refetchAlbumCover(data.id);
|
|
await queryClient.invalidateQueries({ queryKey: qk.album(data.id) });
|
|
} catch (e) {
|
|
const code = errCode(e);
|
|
refetchError = code === 'unknown' ? 'refetch failed' : code;
|
|
} finally {
|
|
refetching = false;
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<svelte:head>
|
|
<title>{pageTitle(query.data?.title ? `Album · ${query.data.title}` : 'Album')}</title>
|
|
</svelte:head>
|
|
|
|
<PageColumn>
|
|
{#if notFound}
|
|
<div role="alert" class="rounded border border-border bg-surface p-4">
|
|
<p>Album not found.</p>
|
|
<a href="/" class="mt-2 inline-block text-sm text-accent-fg">Back to Library</a>
|
|
</div>
|
|
{:else if query.isError}
|
|
<ApiErrorBanner error={query.error} onRetry={query.refetch} />
|
|
{:else if showSkeleton.value && !query.data}
|
|
<LibrarySkeleton variant="album" />
|
|
{:else if query.data}
|
|
{@const album = query.data}
|
|
<a
|
|
href={`/artists/${album.artist_id}`}
|
|
class="text-sm text-text-secondary hover:text-text-primary"
|
|
>
|
|
← {album.artist_name}
|
|
</a>
|
|
|
|
<CollectionHeader
|
|
title={album.title}
|
|
coverUrl={album.cover_url}
|
|
coverAlt={album.title}
|
|
playDisabled={isStartingPlay || album.tracks.length === 0}
|
|
{onPlay}
|
|
{onShuffle}
|
|
>
|
|
{#snippet titleExtra()}
|
|
<LikeButton entityType="album" entityId={album.id} size="lg" />
|
|
{/snippet}
|
|
<p>
|
|
<a href={`/artists/${album.artist_id}`} class="hover:underline">{album.artist_name}</a>
|
|
</p>
|
|
<!-- Year and genre are quick-jumps into the browse axes (#367): from
|
|
an album you like, one click to everything else from that year or
|
|
in that genre. -->
|
|
{#if album.year}
|
|
<p class="text-sm">
|
|
<a href={`/library/years?y=${album.year}`} class="text-accent-fg hover:underline">
|
|
{album.year}
|
|
</a>
|
|
</p>
|
|
{/if}
|
|
{#if album.genres?.length}
|
|
<ul class="flex flex-wrap gap-1.5">
|
|
{#each album.genres as genre (genre)}
|
|
<li>
|
|
<a
|
|
href={`/library/genres?g=${encodeURIComponent(genre)}`}
|
|
class="inline-block rounded-full border border-border px-2.5 py-0.5 text-xs
|
|
text-text-secondary hover:bg-surface-hover hover:text-text-primary
|
|
focus-visible:ring-2 focus-visible:ring-accent"
|
|
>
|
|
{genre}
|
|
</a>
|
|
</li>
|
|
{/each}
|
|
</ul>
|
|
{/if}
|
|
<p class="text-sm text-text-secondary">
|
|
{plural(album.track_count, 'track', 'tracks')}
|
|
· {formatDuration(album.duration_sec)}
|
|
</p>
|
|
{#snippet actions()}
|
|
{#if isAdmin}
|
|
<button
|
|
type="button"
|
|
onclick={onRefetchCover}
|
|
disabled={refetching}
|
|
aria-label={`Refetch cover for ${album.title}`}
|
|
title={album.cover_art_source === 'none' ? 'No cover found previously — try again' : 'Refetch from MusicBrainz'}
|
|
class="flex items-center gap-2 rounded-md border border-border bg-surface px-3 py-2 text-sm font-medium text-text-secondary hover:border-accent disabled:opacity-50"
|
|
>
|
|
Refetch cover
|
|
</button>
|
|
{/if}
|
|
{#if refetchError}
|
|
<span class="text-sm text-error-fg">{refetchError}</span>
|
|
{/if}
|
|
{/snippet}
|
|
</CollectionHeader>
|
|
|
|
{#if album.tracks.length === 0}
|
|
<p class="text-text-secondary">This album has no tracks.</p>
|
|
{:else}
|
|
<TrackList>
|
|
{#each album.tracks as track, i (track.id)}
|
|
<TrackRow tracks={album.tracks} index={i} showAlbum={false} />
|
|
{/each}
|
|
</TrackList>
|
|
{/if}
|
|
{/if}
|
|
</PageColumn>
|