-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathusePromptVersionsByIdInfinite.ts
More file actions
61 lines (57 loc) · 2.28 KB
/
Copy pathusePromptVersionsByIdInfinite.ts
File metadata and controls
61 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { useInfiniteQuery } from "@tanstack/react-query";
import { Sorting } from "@/types/sorting";
import { Filter } from "@/types/filters";
import {
getPromptVersionsById,
PromptVersionsByIdResponse,
} from "./getPromptVersionsById";
const PAGE_SIZE = 25;
type UsePromptVersionsByIdInfiniteParams = {
promptId: string;
sorting?: Sorting;
filters?: Filter[];
search?: string;
};
type UsePromptVersionsByIdInfiniteOptions = {
enabled?: boolean;
refetchInterval?: number;
refetchOnWindowFocus?: boolean;
};
export default function usePromptVersionsByIdInfinite(
params: UsePromptVersionsByIdInfiniteParams,
options?: UsePromptVersionsByIdInfiniteOptions,
) {
return useInfiniteQuery<PromptVersionsByIdResponse>({
// Shares the "prompt-versions" key prefix with usePromptVersionsById so
// the mutation hooks that invalidate that prefix (create/delete/deploy a
// version) also invalidate this list — otherwise the sidebar goes stale
// after every write. The explicit `view: "infinite"` marker keeps the two
// hooks' cache entries apart without relying on the fragile "this one
// never has page/size" invariant, which a future change to either hook
// could silently break.
queryKey: ["prompt-versions", { ...params, view: "infinite" as const }],
queryFn: (context) =>
getPromptVersionsById(context, {
...params,
size: PAGE_SIZE,
page: context.pageParam as number,
}),
// `size` on the response is the actual item count returned (not the
// requested page size), so it shrinks on the last page and hits 0 past
// it — `page * size` is not a valid "items seen so far" once that
// happens. Sum each page's real content length instead. An empty page
// always ends pagination outright: trusting `total` past that point can
// loop forever if it's out of sync with the actual row count (e.g. a
// concurrent delete between the count and the page query).
getNextPageParam: (lastPage, allPages) => {
if (lastPage.content.length === 0) return undefined;
const fetchedCount = allPages.reduce(
(sum, p) => sum + p.content.length,
0,
);
return fetchedCount < lastPage.total ? allPages.length + 1 : undefined;
},
initialPageParam: 1,
...options,
});
}