Files
minstrel/web/src/lib/api/history.ts
T
bvandeusenandClaude Opus 5.5 be384c8efe
release / go (push) Failing after 1m10s
release / web (push) Failing after 10s
release / android (push) Failing after 1m2s
release / govulncheck (push) Successful in 17s
release / integration (push) Successful in 2m31s
release / Build signed APK (push) Failing after 1m43s
release / Build + push container image (push) Skipped
web: svelte-query 6 — queries are reactive objects read directly, not stores (#8532)
@tanstack/svelte-query 6 is built on runes:
- createQuery and createInfiniteQuery take their options as a function
  and return a reactive object that components read as query.data.
- createMutation takes its options the same way.

What this commit changes:
- Every query creator in src/lib/api now takes its arguments as getters
  and reads them inside the options, so a query follows the values it
  depends on. Before, each component rebuilt it in a $derived.
- Constant arguments are passed as getters too, so the rule has no
  exceptions.
- Components call a creator once at init: const query =
  createArtistQuery(() => id). The $derived(create…) and $store pairs
  are gone.
- A query that loads only on demand (a chosen genre or year, the
  Handled view) has an enabled getter. The page keeps its `cond ? q :
  null` alias, because a switched-off query reports isPending and the
  page expects null when nothing is chosen.
- Tests: the query mocks return plain objects. liveQuery
  (test-utils/liveQuery.svelte.ts) gives a test a result it can change
  mid-test. Argument checks call the getter the page passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 10:55:05 -05:00

44 lines
1.2 KiB
TypeScript

import { createInfiniteQuery } from '@tanstack/svelte-query';
import { api } from './client';
import { qk } from './queries';
import type { TrackRef } from './types';
export const HISTORY_PAGE_SIZE = 50;
export type HistoryEvent = {
id: string;
played_at: string; // RFC3339
track: TrackRef;
};
export type HistoryResponse = {
events: HistoryEvent[];
has_more: boolean;
};
// Exposed for direct testability (the createInfiniteQuery wrapper is harder
// to drive in unit tests without a full QueryClient harness).
export async function fetchHistoryPage(offset = 0): Promise<HistoryResponse> {
return api.get<HistoryResponse>(
`/api/me/history?offset=${offset}&limit=${HISTORY_PAGE_SIZE}`
);
}
export function getNextPageParam(
lastPage: HistoryResponse,
_allPages: HistoryResponse[],
lastPageParam: number
): number | undefined {
return lastPage.has_more ? lastPageParam + HISTORY_PAGE_SIZE : undefined;
}
export function createHistoryQuery() {
return createInfiniteQuery(() => ({
queryKey: qk.history(),
queryFn: ({ pageParam }) => fetchHistoryPage(pageParam as number),
initialPageParam: 0,
getNextPageParam: (lastPage, allPages, lastPageParam) =>
getNextPageParam(lastPage, allPages, lastPageParam as number)
}));
}