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:
+27
-10
@@ -1,6 +1,7 @@
|
||||
import { createQuery } from '@tanstack/svelte-query';
|
||||
import { createInfiniteQuery, createQuery } from '@tanstack/svelte-query';
|
||||
import { api } from './client';
|
||||
import { qk } from './queries';
|
||||
import { pageGetNextPageParam } from './paging';
|
||||
import type { AlbumRef, Page } from './types';
|
||||
|
||||
export const BROWSE_PAGE_SIZE = 50;
|
||||
@@ -22,10 +23,8 @@ export async function listAlbumYears(): Promise<YearCount[]> {
|
||||
return api.get<YearCount[]>('/api/library/years');
|
||||
}
|
||||
|
||||
// The indexes use svelte-query: they're fetched once per page mount, so static
|
||||
// options are enough, and the cache means bouncing between browse and a
|
||||
// drill-down doesn't refetch. The drill-down lists below deliberately do NOT —
|
||||
// see the note on listAlbumsByGenre.
|
||||
// The indexes are cached, so bouncing between browse and a drill-down doesn't
|
||||
// refetch them.
|
||||
export function createGenresQuery() {
|
||||
return createQuery({
|
||||
queryKey: qk.genres(),
|
||||
@@ -47,11 +46,6 @@ export function createAlbumYearsQuery() {
|
||||
// contain slashes ("Rock/Pop" is a real tag), which a path segment cannot
|
||||
// carry — the server would see two segments, and a hard reload of such a URL
|
||||
// would not survive the SPA fallback either.
|
||||
//
|
||||
// Called directly rather than through createInfiniteQuery because the selected
|
||||
// genre comes from the URL and changes without remounting the page. This
|
||||
// codebase has no reactive-query-options pattern, and introducing one here
|
||||
// would be a larger change than the feature warrants.
|
||||
export async function listAlbumsByGenre(
|
||||
genre: string,
|
||||
limit: number,
|
||||
@@ -80,3 +74,26 @@ export async function listAlbumsByYear(
|
||||
});
|
||||
return api.get<Page<AlbumRef>>(`/api/library/albums?${params}`);
|
||||
}
|
||||
|
||||
// The drill-down lists, a page at a time. The selection comes from the URL and
|
||||
// changes without remounting the page, so a page creates its query from the
|
||||
// selection: `$derived(genre ? createAlbumsByGenreInfiniteQuery(genre) : null)`.
|
||||
// Each selection has its own cache key, so a late page for the previous one
|
||||
// never lands in the current list.
|
||||
export function createAlbumsByGenreInfiniteQuery(genre: string) {
|
||||
return createInfiniteQuery({
|
||||
queryKey: qk.albumsByGenre(genre),
|
||||
queryFn: ({ pageParam = 0 }) => listAlbumsByGenre(genre, BROWSE_PAGE_SIZE, pageParam),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: pageGetNextPageParam<AlbumRef>()
|
||||
});
|
||||
}
|
||||
|
||||
export function createAlbumsByYearInfiniteQuery(year: number) {
|
||||
return createInfiniteQuery({
|
||||
queryKey: qk.albumsByYear(year),
|
||||
queryFn: ({ pageParam = 0 }) => listAlbumsByYear(year, BROWSE_PAGE_SIZE, pageParam),
|
||||
initialPageParam: 0,
|
||||
getNextPageParam: pageGetNextPageParam<AlbumRef>()
|
||||
});
|
||||
}
|
||||
|
||||
@@ -74,6 +74,8 @@ export const qk = {
|
||||
// svelte-query because their selection comes from the URL.
|
||||
genres: () => ['genres'] as const,
|
||||
albumYears: () => ['albumYears'] as const,
|
||||
albumsByGenre: (genre: string) => ['albumsByGenre', genre] as const,
|
||||
albumsByYear: (year: number) => ['albumsByYear', year] as const,
|
||||
};
|
||||
|
||||
export function createArtistsQuery(sort: ArtistSort) {
|
||||
|
||||
@@ -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