Final task of PR2 DRY pass. Extracts the verbatim `offset + items.length >= total` math repeated across 8 createInfiniteQuery sites into a single Page<T>-shape helper. The task spec described a bare-array helper signature (lastPage: T[], pageSize-based stop), but no call site in this repo uses that shape — every paged endpoint returns a Page<T> envelope. The helper is named pageGetNextPageParam and matches the actual canonical shape so the 8 copies could collapse. Migrated: - likes.ts: 3 sites (TrackRef, AlbumRef, ArtistRef) - albums.ts: 1 site (AlbumRef) - queries.ts: 4 sites (artists + 3 search facets) history.ts left alone — uses has_more/total, different shape.
26 lines
929 B
TypeScript
26 lines
929 B
TypeScript
import { createInfiniteQuery } from '@tanstack/svelte-query';
|
|
import { api } from './client';
|
|
import { qk } from './queries';
|
|
import { pageGetNextPageParam } from './paging';
|
|
import type { AlbumRef, AlbumDetail, Page, TrackRef } from './types';
|
|
|
|
export const ALBUM_PAGE_SIZE = 50;
|
|
|
|
export async function listAlbumsAlpha(limit: number, offset: number): Promise<Page<AlbumRef>> {
|
|
return api.get<Page<AlbumRef>>(`/api/library/albums?limit=${limit}&offset=${offset}`);
|
|
}
|
|
|
|
export function createAlbumsAlphaInfiniteQuery() {
|
|
return createInfiniteQuery({
|
|
queryKey: qk.albumsAlpha(),
|
|
queryFn: ({ pageParam = 0 }) => listAlbumsAlpha(ALBUM_PAGE_SIZE, pageParam),
|
|
initialPageParam: 0,
|
|
getNextPageParam: pageGetNextPageParam<AlbumRef>()
|
|
});
|
|
}
|
|
|
|
export async function listAlbumTracks(albumId: string): Promise<TrackRef[]> {
|
|
const detail = await api.get<AlbumDetail>(`/api/albums/${albumId}`);
|
|
return detail.tracks;
|
|
}
|