Skip to content

Commit 4257fdc

Browse files
NicolappsConvex, Inc.
authored andcommitted
Expose the internal state of usePaginatedQuery through an internal hook (#41552)
GitOrigin-RevId: 68406e0ae95cd472a76bad1690b2358be420ca23
1 parent ceb3aba commit 4257fdc

File tree

2 files changed

+93
-4
lines changed

2 files changed

+93
-4
lines changed

src/react/use_paginated_query.test.tsx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @vitest-environment custom-vitest-environment.ts
33
*/
4-
import { expect, vi, test, describe, beforeEach } from "vitest";
4+
import { expect, vi, test, describe, beforeEach, it } from "vitest";
55
import { act, renderHook } from "@testing-library/react";
66
import React from "react";
77

@@ -26,6 +26,9 @@ import {
2626
insertAtTop,
2727
insertAtPosition,
2828
PaginatedQueryReference,
29+
usePaginatedQueryInternal,
30+
includePage,
31+
page,
2932
} from "./use_paginated_query.js";
3033
import { OptimisticLocalStore } from "../browser/index.js";
3134

@@ -400,6 +403,54 @@ describe("usePaginatedQuery pages", () => {
400403
"item4S",
401404
]);
402405
});
406+
407+
describe("usePaginatedQueryInternal", () => {
408+
it("doesn’t expose the page keys by default", () => {
409+
const { result } = renderHook(
410+
() => usePaginatedQueryInternal(query, {}, { initialNumItems: 1 }),
411+
{ wrapper },
412+
);
413+
mockPage(
414+
{
415+
numItems: 1,
416+
cursor: null,
417+
},
418+
{
419+
page: [{ name: "item1" }],
420+
continueCursor: "abc",
421+
isDone: true,
422+
},
423+
);
424+
const item = result.current.user.results[0];
425+
expect(page in item).to.be.false;
426+
expect("0" in result.current.internal.state.queries).to.be.true;
427+
});
428+
429+
it("exposes the page keys when asked", () => {
430+
const { result } = renderHook(
431+
() =>
432+
usePaginatedQueryInternal(
433+
query,
434+
{},
435+
{ initialNumItems: 1, [includePage]: true },
436+
),
437+
{ wrapper },
438+
);
439+
mockPage(
440+
{
441+
numItems: 1,
442+
cursor: null,
443+
},
444+
{
445+
page: [{ name: "item1" }],
446+
continueCursor: "abc",
447+
isDone: true,
448+
},
449+
);
450+
const item = result.current.user.results[0];
451+
expect(item[page]).to.equal("0");
452+
});
453+
});
403454
});
404455

405456
describe("PaginatedQueryArgs", () => {

src/react/use_paginated_query.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,32 @@ export function usePaginatedQuery<Query extends PaginatedQueryReference>(
164164
args: PaginatedQueryArgs<Query> | "skip",
165165
options: { initialNumItems: number },
166166
): UsePaginatedQueryReturnType<Query> {
167+
const { user } = usePaginatedQueryInternal(query, args, options);
168+
return user;
169+
}
170+
171+
/** @internal */
172+
export const includePage = Symbol("includePageKeys");
173+
174+
/** @internal */
175+
export const page = Symbol("page");
176+
177+
/**
178+
* @internal
179+
*/
180+
export function usePaginatedQueryInternal<
181+
Query extends PaginatedQueryReference,
182+
>(
183+
query: Query,
184+
args: PaginatedQueryArgs<Query> | "skip",
185+
options: {
186+
initialNumItems: number;
187+
[includePage]?: boolean;
188+
},
189+
): {
190+
user: UsePaginatedQueryReturnType<Query>;
191+
internal: { state: UsePaginatedQueryState };
192+
} {
167193
if (
168194
typeof options?.initialNumItems !== "number" ||
169195
options.initialNumItems < 0
@@ -234,6 +260,7 @@ export function usePaginatedQuery<Query extends PaginatedQueryReference>(
234260

235261
const resultsObject = useQueries(currState.queries);
236262

263+
const isIncludingPageKeys = options[includePage] ?? false;
237264
const [results, maybeLastResult]: [
238265
Value[],
239266
undefined | PaginationResult<Value>,
@@ -303,7 +330,14 @@ export function usePaginatedQuery<Query extends PaginatedQueryReference>(
303330
// page and return 'LoadingMore' while the page is splitting.
304331
return [allItems, undefined];
305332
}
306-
allItems.push(...currResult.page);
333+
allItems.push(
334+
...(isIncludingPageKeys
335+
? currResult.page.map((i: any) => ({
336+
...i,
337+
[page]: pageKey.toString(),
338+
}))
339+
: currResult.page),
340+
);
307341
}
308342
return [allItems, currResult];
309343
}, [
@@ -313,6 +347,7 @@ export function usePaginatedQuery<Query extends PaginatedQueryReference>(
313347
options.initialNumItems,
314348
createInitialState,
315349
logger,
350+
isIncludingPageKeys,
316351
]);
317352

318353
const statusObject = useMemo(() => {
@@ -379,8 +414,11 @@ export function usePaginatedQuery<Query extends PaginatedQueryReference>(
379414
}, [maybeLastResult, currState.nextPageKey]);
380415

381416
return {
382-
results,
383-
...statusObject,
417+
user: {
418+
results,
419+
...statusObject,
420+
},
421+
internal: { state: currState },
384422
};
385423
}
386424

0 commit comments

Comments
 (0)