refactor(web): every cover thumbnail goes through Cover, so a failed load falls back (DRY W2)
Seven places drew their own cover image. The Missing, Suspect sources, Hidden and Reports rows showed a Music2 icon only when the track had no album; a cover that failed to load (not fetched yet, or gone) showed a broken image. The queue row, play bar and Now Playing each set the placeholder in their own onerror, and the playlist card's images had no fallback at all. Cover gains fallback="track" (the Music2 icon a track row uses), and all seven go through it, so each one handles a missing src and a failed load the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { FALLBACK_COVER } from '#lib/media/covers.js';
|
||||
import { Disc3 } from 'lucide-svelte';
|
||||
import { Disc3, Music2 } from 'lucide-svelte';
|
||||
|
||||
// Shared cover-artwork renderer. The web counterpart of Android's
|
||||
// CoverTile/ServerImage: one place that owns the loading placeholder and
|
||||
@@ -19,9 +19,10 @@
|
||||
// 'round' clips to a circle (artist avatars); 'square' leaves clipping
|
||||
// to the parent so existing rounded-md / rounded corners still apply.
|
||||
shape?: 'square' | 'round';
|
||||
// 'artist' degrades to a Disc3 icon; 'image' to the static placeholder
|
||||
// cover. A null/empty src is treated the same as a failed load.
|
||||
fallback?: 'image' | 'artist';
|
||||
// 'artist' degrades to a Disc3 icon, 'track' to a Music2 icon (a track
|
||||
// row's thumbnail), 'image' to the static placeholder cover. A
|
||||
// null/empty src is treated the same as a failed load.
|
||||
fallback?: 'image' | 'artist' | 'track';
|
||||
class?: string;
|
||||
} = $props();
|
||||
|
||||
@@ -35,7 +36,8 @@
|
||||
failed = false;
|
||||
});
|
||||
|
||||
const useIcon = $derived(fallback === 'artist' && (failed || !src));
|
||||
const FallbackIcon = $derived(fallback === 'artist' ? Disc3 : fallback === 'track' ? Music2 : null);
|
||||
const useIcon = $derived(FallbackIcon !== null && (failed || !src));
|
||||
// On failure for the image fallback, swap to the static placeholder asset.
|
||||
// src doesn't change on the second error, so there's no reload loop.
|
||||
const imgSrc = $derived(failed ? FALLBACK_COVER : src);
|
||||
@@ -46,15 +48,18 @@
|
||||
? 'rounded-full'
|
||||
: ''}"
|
||||
>
|
||||
{#if useIcon}
|
||||
{#if useIcon && FallbackIcon}
|
||||
<div class="flex h-full w-full items-center justify-center">
|
||||
<Disc3 strokeWidth={1} class="h-4/5 w-4/5 text-text-muted" />
|
||||
<FallbackIcon
|
||||
strokeWidth={1}
|
||||
class="{fallback === 'track' ? 'h-1/2 w-1/2' : 'h-4/5 w-4/5'} text-text-muted"
|
||||
/>
|
||||
</div>
|
||||
{:else if imgSrc}
|
||||
<img
|
||||
src={imgSrc}
|
||||
{alt}
|
||||
class="h-full w-full object-cover transition-opacity duration-200 {loaded
|
||||
class="h-full w-full object-cover transition-[opacity,transform] duration-200 {loaded
|
||||
? 'opacity-100'
|
||||
: 'opacity-0'} {klass}"
|
||||
loading="lazy"
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import { render, fireEvent } from '@testing-library/svelte';
|
||||
import Cover from './Cover.svelte';
|
||||
|
||||
describe('Cover', () => {
|
||||
test('a track row with no cover shows the track icon, not an image', () => {
|
||||
const { container } = render(Cover, { props: { src: undefined, fallback: 'track' } });
|
||||
expect(container.querySelector('img')).toBeNull();
|
||||
expect(container.querySelector('svg')).not.toBeNull();
|
||||
});
|
||||
|
||||
test('a track cover that fails to load gives way to the track icon', async () => {
|
||||
const { container } = render(Cover, { props: { src: '/api/albums/a1/cover', fallback: 'track' } });
|
||||
const img = container.querySelector('img');
|
||||
expect(img).not.toBeNull();
|
||||
await fireEvent.error(img!);
|
||||
expect(container.querySelector('img')).toBeNull();
|
||||
expect(container.querySelector('svg')).not.toBeNull();
|
||||
});
|
||||
|
||||
test('an image cover that fails to load shows the placeholder cover', async () => {
|
||||
const { container } = render(Cover, { props: { src: '/api/albums/a1/cover' } });
|
||||
await fireEvent.error(container.querySelector('img')!);
|
||||
expect(container.querySelector('img')!.getAttribute('src')).not.toBe('/api/albums/a1/cover');
|
||||
});
|
||||
});
|
||||
@@ -12,7 +12,8 @@
|
||||
toggleQueueDrawer
|
||||
} from '#lib/player/store.svelte.js';
|
||||
import { formatDuration } from '#lib/media/duration.js';
|
||||
import { FALLBACK_COVER, coverUrl } from '#lib/media/covers.js';
|
||||
import { coverUrl } from '#lib/media/covers.js';
|
||||
import Cover from './Cover.svelte';
|
||||
import { dominantColorFromUrl, rgbToCssString } from '#lib/media/dominantColor.js';
|
||||
import { useSmoothPosition } from '#lib/player/smoothPosition.svelte.js';
|
||||
import LikeButton from './LikeButton.svelte';
|
||||
@@ -32,9 +33,6 @@
|
||||
|
||||
const onSeekInput = (e: Event) => seekTo(inputNumber(e));
|
||||
const onVolumeInput = (e: Event) => setVolume(inputNumber(e));
|
||||
function onCoverError(e: Event) {
|
||||
(e.currentTarget as HTMLImageElement).src = FALLBACK_COVER;
|
||||
}
|
||||
|
||||
// Dominant-color accent. Sample the current track's cover and
|
||||
// pipe it into a CSS custom property; a 2px strip above the bar
|
||||
@@ -105,12 +103,9 @@
|
||||
class="flex min-w-0 items-center gap-2 focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-accent"
|
||||
style="flex: 1 1 35%;"
|
||||
>
|
||||
<img
|
||||
src={coverUrl(current.album_id)}
|
||||
alt=""
|
||||
class="h-12 w-12 shrink-0 rounded-md object-cover"
|
||||
onerror={onCoverError}
|
||||
/>
|
||||
<div class="h-12 w-12 shrink-0 overflow-hidden rounded-md">
|
||||
<Cover src={coverUrl(current.album_id)} />
|
||||
</div>
|
||||
<div class="min-w-0">
|
||||
<div class="truncate text-sm font-medium text-text-primary">{current.title}</div>
|
||||
<div class="truncate text-xs text-text-secondary">{current.artist_name}</div>
|
||||
@@ -267,12 +262,9 @@
|
||||
aria-label="Open now playing"
|
||||
class="shrink-0 focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-accent"
|
||||
>
|
||||
<img
|
||||
src={coverUrl(current.album_id)}
|
||||
alt=""
|
||||
class="h-24 w-24 rounded-md object-cover"
|
||||
onerror={onCoverError}
|
||||
/>
|
||||
<div class="h-24 w-24 overflow-hidden rounded-md">
|
||||
<Cover src={coverUrl(current.album_id)} />
|
||||
</div>
|
||||
</a>
|
||||
<!-- Sized to the 96px cover beside it: the title reads at body size,
|
||||
as Android's mini player gives it title weight. -->
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
import { systemPlaylistRefetch } from '#lib/playlists/systemRefetch.js';
|
||||
import { refreshedLabel, songsLikeArtist, washTone } from '#lib/playlists/shelf.js';
|
||||
import { coverUrl } from '#lib/media/covers.js';
|
||||
import Cover from './Cover.svelte';
|
||||
import { dominantColorFromUrl, rgbToCssString } from '#lib/media/dominantColor.js';
|
||||
import { errCode } from '#lib/api/errors.js';
|
||||
import { qk } from '#lib/api/queries.js';
|
||||
@@ -225,7 +226,7 @@
|
||||
|
||||
{#snippet leadArt(rounded: string)}
|
||||
{#if lead}
|
||||
<img src={lead} alt="" class="h-full w-full object-cover {rounded}" />
|
||||
<Cover src={lead} class={rounded} />
|
||||
{:else}
|
||||
<div class="flex h-full w-full items-center justify-center text-text-muted">
|
||||
<span class="text-xs">No tracks yet</span>
|
||||
@@ -312,11 +313,7 @@
|
||||
group-hover:shadow-lg group-hover:ring-1 group-hover:ring-accent/40"
|
||||
>
|
||||
{#if playlist.cover_url}
|
||||
<img
|
||||
src={playlist.cover_url}
|
||||
alt=""
|
||||
class="h-full w-full object-cover transition-transform group-hover:scale-105"
|
||||
/>
|
||||
<Cover src={playlist.cover_url} class="group-hover:scale-105" />
|
||||
{:else}
|
||||
<div class="flex h-full w-full items-center justify-center text-text-muted">
|
||||
<span class="text-xs">No tracks yet</span>
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
import { GripVertical, X } from 'lucide-svelte';
|
||||
import type { TrackRef } from '#lib/api/types.js';
|
||||
import { playFromQueueIndex, removeFromQueue, moveQueueItem } from '#lib/player/store.svelte.js';
|
||||
import { coverUrl, FALLBACK_COVER } from '#lib/media/covers.js';
|
||||
import { coverUrl } from '#lib/media/covers.js';
|
||||
import Cover from './Cover.svelte';
|
||||
import { reorderDrag } from './reorder-drag';
|
||||
import LikeButton from './LikeButton.svelte';
|
||||
|
||||
@@ -58,12 +59,9 @@
|
||||
shadow is what keeps the glyph readable over pale covers without one.
|
||||
-->
|
||||
<div class="relative h-10 w-10 shrink-0">
|
||||
<img
|
||||
src={coverUrl(track.album_id)}
|
||||
alt=""
|
||||
onerror={(e) => ((e.currentTarget as HTMLImageElement).src = FALLBACK_COVER)}
|
||||
class="h-10 w-10 rounded object-cover"
|
||||
/>
|
||||
<div class="h-10 w-10 overflow-hidden rounded">
|
||||
<Cover src={coverUrl(track.album_id)} />
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
aria-label="Reorder track (use arrow keys)"
|
||||
|
||||
@@ -107,7 +107,9 @@ describe('QueueTrackRow', () => {
|
||||
// Sharing a parent is what "overlaid" means structurally. If someone moves
|
||||
// the grip back into its own flex slot, this fails — which is the point:
|
||||
// that slot costs horizontal space in every row.
|
||||
expect(handle.parentElement).toBe(art!.parentElement);
|
||||
// The art sits inside Cover's own frame, so the shared parent is the
|
||||
// frame's parent.
|
||||
expect(handle.parentElement).toBe(art!.parentElement!.parentElement!.parentElement);
|
||||
});
|
||||
|
||||
it('the handle is visible at rest, not hover-revealed', () => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
import { Copy, Download, FolderX, Music2, Trash2 } from 'lucide-svelte';
|
||||
import { Copy, Download, FolderX, Trash2 } from 'lucide-svelte';
|
||||
import Modal from '#lib/components/Modal.svelte';
|
||||
import { createMissingFilesQuery, forgetMissingTracks, MISSING_FILES_CSV_URL } from '#lib/api/admin.js';
|
||||
import { errMessage } from '#lib/api/errors.js';
|
||||
@@ -172,21 +173,9 @@
|
||||
<ul class="divide-y divide-border">
|
||||
{#each group.tracks as t (t.track_id)}
|
||||
<li class="flex items-center gap-3 px-4 py-2" data-testid="missing-track-row">
|
||||
<div
|
||||
class="flex h-10 w-10 shrink-0 items-center justify-center rounded bg-surface-hover"
|
||||
aria-hidden="true"
|
||||
>
|
||||
{#if t.album_id}
|
||||
<img
|
||||
src={coverUrl(t.album_id)}
|
||||
alt=""
|
||||
class="h-full w-full rounded object-cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
{:else}
|
||||
<Music2 size={18} strokeWidth={1} class="text-text-muted" />
|
||||
{/if}
|
||||
</div>
|
||||
<div class="h-10 w-10 shrink-0 overflow-hidden rounded" aria-hidden="true">
|
||||
<Cover src={t.album_id ? coverUrl(t.album_id) : undefined} fallback="track" />
|
||||
</div>
|
||||
|
||||
<div class="min-w-0 flex-1">
|
||||
<div class="truncate text-sm text-text-primary">{t.title}</div>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
import { Music2, CheckCircle2, Trash2, Play, Copy, ChevronRight, FileX } from 'lucide-svelte';
|
||||
import { CheckCircle2, Trash2, Play, Copy, ChevronRight, FileX } from 'lucide-svelte';
|
||||
import { useQueryClient } from '@tanstack/svelte-query';
|
||||
import RowActionsMenu, { type RowAction } from '#lib/components/RowActionsMenu.svelte';
|
||||
import TabStrip from '#lib/components/TabStrip.svelte';
|
||||
@@ -208,13 +209,9 @@
|
||||
{@const a = actionsFor(r)}
|
||||
{@const open = !!expanded[r.track_id]}
|
||||
<li class="flex items-start gap-4 p-3" data-testid="report-row">
|
||||
<div class="flex h-12 w-12 shrink-0 items-center justify-center rounded-md bg-surface-hover" aria-hidden="true">
|
||||
{#if r.album_id}
|
||||
<img src={coverUrl(r.album_id)} alt="" class="h-full w-full rounded-md object-cover" loading="lazy" />
|
||||
{:else}
|
||||
<Music2 size={20} strokeWidth={1} class="text-text-muted" />
|
||||
{/if}
|
||||
</div>
|
||||
<div class="h-12 w-12 shrink-0 overflow-hidden rounded-md" aria-hidden="true">
|
||||
<Cover src={r.album_id ? coverUrl(r.album_id) : undefined} fallback="track" />
|
||||
</div>
|
||||
<div class="min-w-0 flex-1 space-y-1">
|
||||
<div class="truncate text-sm font-medium text-text-primary">{r.title}</div>
|
||||
<div class="truncate text-xs text-text-secondary">
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
import { FileCheck, Music2 } from 'lucide-svelte';
|
||||
import { FileCheck } from 'lucide-svelte';
|
||||
import { createSuspectSourcesQuery, removeSuspectFile, setSuspectVerdict } from '#lib/api/admin.js';
|
||||
import { errMessage } from '#lib/api/errors.js';
|
||||
import { pushToast } from '#lib/stores/toast.svelte.js';
|
||||
@@ -158,21 +159,9 @@
|
||||
<ul class="divide-y divide-border">
|
||||
{#each group.tracks as t (t.track_id)}
|
||||
<li class="flex items-center gap-3 px-4 py-2" data-testid="suspect-track-row">
|
||||
<div
|
||||
class="flex h-10 w-10 shrink-0 items-center justify-center rounded bg-surface-hover"
|
||||
aria-hidden="true"
|
||||
>
|
||||
{#if t.album_id}
|
||||
<img
|
||||
src={coverUrl(t.album_id)}
|
||||
alt=""
|
||||
class="h-full w-full rounded object-cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
{:else}
|
||||
<Music2 size={18} strokeWidth={1} class="text-text-muted" />
|
||||
{/if}
|
||||
</div>
|
||||
<div class="h-10 w-10 shrink-0 overflow-hidden rounded" aria-hidden="true">
|
||||
<Cover src={t.album_id ? coverUrl(t.album_id) : undefined} fallback="track" />
|
||||
</div>
|
||||
|
||||
<div class="min-w-0 flex-1 space-y-0.5">
|
||||
<div class="truncate text-sm text-text-primary">{t.title}</div>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
<script lang="ts">
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import KindPill from '#lib/components/KindPill.svelte';
|
||||
import { pageTitle } from '#lib/branding.js';
|
||||
import { Music2, RotateCcw } from 'lucide-svelte';
|
||||
import { RotateCcw } from 'lucide-svelte';
|
||||
import { useQueryClient } from '@tanstack/svelte-query';
|
||||
import { createMyQuarantineQuery, unflagTrack } from '#lib/api/quarantine.js';
|
||||
import { qk } from '#lib/api/queries.js';
|
||||
@@ -50,17 +51,8 @@
|
||||
{#each rows as row (row.track_id)}
|
||||
<li class="flex items-start gap-3 rounded-md border border-border bg-surface p-3">
|
||||
<!-- Album art / fallback -->
|
||||
<div class="flex h-14 w-14 shrink-0 items-center justify-center rounded-md bg-surface-hover">
|
||||
{#if row.album_cover_art_path}
|
||||
<img
|
||||
src={coverUrl(row.album_id)}
|
||||
alt=""
|
||||
class="h-full w-full rounded-md object-cover"
|
||||
loading="lazy"
|
||||
/>
|
||||
{:else}
|
||||
<Music2 size={24} strokeWidth={1} class="text-text-muted" />
|
||||
{/if}
|
||||
<div class="h-14 w-14 shrink-0 overflow-hidden rounded-md" aria-hidden="true">
|
||||
<Cover src={row.album_cover_art_path ? coverUrl(row.album_id) : undefined} fallback="track" />
|
||||
</div>
|
||||
<!-- Body -->
|
||||
<div class="flex flex-1 flex-col gap-1">
|
||||
|
||||
@@ -13,7 +13,8 @@
|
||||
toggleQueueDrawer
|
||||
} from '#lib/player/store.svelte.js';
|
||||
import { formatDuration } from '#lib/media/duration.js';
|
||||
import { FALLBACK_COVER, coverUrl } from '#lib/media/covers.js';
|
||||
import { coverUrl } from '#lib/media/covers.js';
|
||||
import Cover from '#lib/components/Cover.svelte';
|
||||
import { dominantColorFromUrl, rgbToCssString } from '#lib/media/dominantColor.js';
|
||||
import { useSmoothPosition } from '#lib/player/smoothPosition.svelte.js';
|
||||
import LikeButton from '#lib/components/LikeButton.svelte';
|
||||
@@ -50,9 +51,6 @@
|
||||
|
||||
const onSeekInput = (e: Event) => seekTo(inputNumber(e));
|
||||
const onVolumeInput = (e: Event) => setVolume(inputNumber(e));
|
||||
function onCoverError(e: Event) {
|
||||
(e.currentTarget as HTMLImageElement).src = FALLBACK_COVER;
|
||||
}
|
||||
function goBack() {
|
||||
// Most users land here from the PlayerBar — history.back() returns
|
||||
// them to whatever surface they were on. If there's nothing to go
|
||||
@@ -128,12 +126,7 @@
|
||||
data-testid="now-playing-column"
|
||||
>
|
||||
<div class="aspect-square w-full overflow-hidden rounded-lg bg-surface-hover shadow-lg">
|
||||
<img
|
||||
src={coverUrl(current.album_id)}
|
||||
alt=""
|
||||
class="h-full w-full object-cover"
|
||||
onerror={onCoverError}
|
||||
/>
|
||||
<Cover src={coverUrl(current.album_id)} />
|
||||
</div>
|
||||
|
||||
<div class="w-full text-center">
|
||||
|
||||
Reference in New Issue
Block a user