Files
minstrel/web/src/lib/api/browse.ts
T
bvandeusenandClaude Opus 5.5 c290667b8a 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>
2026-11-27 03:21:55 -05:00

100 lines
3.4 KiB
TypeScript

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;
// Genres are the raw ID3 strings, split on [;,] server-side but otherwise
// untouched — no case folding, no synonym mapping. So "Rock" and "rock" can
// both appear, as can "Rock/Pop" beside "Rock" and "Pop". Deliberate: the
// raw spread has to be visible before anyone can judge whether it needs
// normalising.
export type GenreCount = { genre: string; track_count: number };
export type YearCount = { year: number; album_count: number };
export async function listGenres(): Promise<GenreCount[]> {
return api.get<GenreCount[]>('/api/library/genres');
}
export async function listAlbumYears(): Promise<YearCount[]> {
return api.get<YearCount[]>('/api/library/years');
}
// The indexes are cached, so bouncing between browse and a drill-down doesn't
// refetch them.
export function createGenresQuery() {
return createQuery({
queryKey: qk.genres(),
queryFn: listGenres,
// Genres only change when the library is rescanned.
staleTime: 5 * 60_000
});
}
export function createAlbumYearsQuery() {
return createQuery({
queryKey: qk.albumYears(),
queryFn: listAlbumYears,
staleTime: 5 * 60_000
});
}
// Genre travels as a QUERY parameter, never a path segment. Raw ID3 genres
// 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.
export async function listAlbumsByGenre(
genre: string,
limit: number,
offset: number
): Promise<Page<AlbumRef>> {
const params = new URLSearchParams({
genre,
limit: String(limit),
offset: String(offset)
});
return api.get<Page<AlbumRef>>(`/api/library/albums?${params}`);
}
// Single-year drill-down. The server takes an inclusive range, so one year is
// expressed as its own degenerate range rather than needing a separate shape.
export async function listAlbumsByYear(
year: number,
limit: number,
offset: number
): Promise<Page<AlbumRef>> {
const params = new URLSearchParams({
year_from: String(year),
year_to: String(year),
limit: String(limit),
offset: String(offset)
});
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>()
});
}