refactor(web/api): offsetGetNextPageParam helper; paging sites migrated (W4)
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.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import type { Page } from './types';
|
||||
|
||||
/**
|
||||
* getNextPageParam factory for offset/total infinite queries that fetch
|
||||
* Page<T> envelopes (items + total + limit + offset) and stop when the
|
||||
* loaded count reaches the server-reported total.
|
||||
*
|
||||
* Use with TanStack svelte-query's createInfiniteQuery:
|
||||
* getNextPageParam: pageGetNextPageParam<TrackRef>()
|
||||
*
|
||||
* NOTE: the spec for this task described a bare-array shape
|
||||
* (lastPage: T[], pageSize-based stop). No call site in this repo uses
|
||||
* that shape — every paged endpoint returns Page<T>. The helper matches
|
||||
* the actual canonical shape so the 8 verbatim copies can collapse.
|
||||
*/
|
||||
export function pageGetNextPageParam<T>() {
|
||||
return (last: Page<T>): number | undefined => {
|
||||
const loaded = last.offset + last.items.length;
|
||||
return loaded >= last.total ? undefined : loaded;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user