refactor(web): genre and year albums page like every other list (DRY W6)
The genre and year drill-downs hand-rolled their own pager (a request token to drop late answers, loading, failed and later-page-failed flags), on the note that the codebase had no way to make a query from a changing URL. The search pages have since done exactly that: $derived(q ? createXInfiniteQuery(q) : null). browse.ts gains createAlbumsByGenreInfiniteQuery and createAlbumsByYearInfiniteQuery, keyed per selection so a late page for the previous one never lands in the current list. Both pages draw their albums through one AlbumDrillDown view (back link, count, grid, ListContinuation), and the out-of-date note is gone. Tests drive the pages through mockInfiniteQuery, as the search pages' do, with the page() fixture now in test-utils. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
<script lang="ts">
|
||||
import { ChevronLeft } from 'lucide-svelte';
|
||||
import { plural } from '#lib/utils/plural.js';
|
||||
import AlbumCard from './AlbumCard.svelte';
|
||||
import EmptyState from './EmptyState.svelte';
|
||||
import ListContinuation from './ListContinuation.svelte';
|
||||
import type { AlbumRef, Page } from '#lib/api/types.js';
|
||||
|
||||
// One browse axis's albums (a genre, a year): a back link to the axis, the
|
||||
// count, and the paged grid. A failed first page offers Try again; a failed
|
||||
// later page keeps the grid and offers it at the end.
|
||||
type AlbumPages = {
|
||||
data?: { pages: Page<AlbumRef>[] };
|
||||
isPending: boolean;
|
||||
isError: boolean;
|
||||
hasNextPage: boolean;
|
||||
isFetchingNextPage: boolean;
|
||||
isFetchNextPageError: boolean;
|
||||
fetchNextPage: () => unknown;
|
||||
refetch: () => unknown;
|
||||
};
|
||||
|
||||
let {
|
||||
query,
|
||||
backHref,
|
||||
backLabel,
|
||||
title,
|
||||
failText,
|
||||
emptyTitle,
|
||||
emptyHint,
|
||||
endLabel
|
||||
}: {
|
||||
query: AlbumPages | null;
|
||||
backHref: string;
|
||||
backLabel: string;
|
||||
title: string;
|
||||
failText: string;
|
||||
emptyTitle: string;
|
||||
emptyHint: string;
|
||||
endLabel: string;
|
||||
} = $props();
|
||||
|
||||
const albums = $derived(query?.data?.pages.flatMap((p) => p.items) ?? []);
|
||||
const total = $derived(query?.data?.pages[0]?.total ?? 0);
|
||||
const failed = $derived(!!query?.isError && albums.length === 0);
|
||||
</script>
|
||||
|
||||
<div class="space-y-4">
|
||||
<header class="space-y-2">
|
||||
<a href={backHref} class="inline-flex items-center gap-1 text-sm text-accent-fg hover:underline">
|
||||
<ChevronLeft size={14} aria-hidden="true" />
|
||||
{backLabel}
|
||||
</a>
|
||||
<div>
|
||||
<h1 class="font-display text-2xl font-medium text-text-primary">{title}</h1>
|
||||
{#if query && !query.isPending && !failed}
|
||||
<p class="text-sm text-text-secondary">{plural(total, 'album', 'albums')}</p>
|
||||
{/if}
|
||||
</div>
|
||||
</header>
|
||||
|
||||
{#if failed}
|
||||
<p class="text-sm text-action-destructive">
|
||||
{failText}
|
||||
<button type="button" class="underline hover:no-underline" onclick={() => query?.refetch()}
|
||||
>Try again</button
|
||||
>
|
||||
</p>
|
||||
{:else if !query || query.isPending}
|
||||
<p class="text-text-secondary">Loading…</p>
|
||||
{:else if albums.length === 0}
|
||||
<EmptyState title={emptyTitle} hint={emptyHint} />
|
||||
{:else}
|
||||
<div class="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6">
|
||||
{#each albums as album (album.id)}
|
||||
<AlbumCard {album} />
|
||||
{/each}
|
||||
</div>
|
||||
<ListContinuation
|
||||
hasMore={query.hasNextPage}
|
||||
loading={query.isFetchingNextPage}
|
||||
failed={query.isFetchNextPageError}
|
||||
onMore={() => query?.fetchNextPage()}
|
||||
{endLabel}
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
Reference in New Issue
Block a user