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:
2026-11-27 03:21:55 -05:00
co-authored by Claude Opus 5.5
parent 7314101f91
commit c290667b8a
8 changed files with 257 additions and 308 deletions
+27 -10
View File
@@ -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>()
});
}