@convex-dev/agent 0.1.6-alpha.0 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,647 @@
1
+ import { useMemo, useState } from "react";
2
+ import { ConvexError, convexToJson, } from "convex/values";
3
+ import { useQueries } from "convex/react";
4
+ import { getFunctionName, } from "convex/server";
5
+ import { useConvex } from "convex/react";
6
+ import { compareValues } from "convex/values";
7
+ const splitQuery = (key, splitCursor, continueCursor) => (prevState) => {
8
+ console.log("splitQuery", key, splitCursor, continueCursor);
9
+ const queries = { ...prevState.queries };
10
+ const splitKey1 = prevState.nextPageKey;
11
+ const splitKey2 = prevState.nextPageKey + 1;
12
+ const nextPageKey = prevState.nextPageKey + 2;
13
+ queries[splitKey1] = {
14
+ query: prevState.query,
15
+ args: {
16
+ ...prevState.args,
17
+ paginationOpts: {
18
+ ...prevState.queries[key].args.paginationOpts,
19
+ endCursor: splitCursor,
20
+ },
21
+ },
22
+ };
23
+ queries[splitKey2] = {
24
+ query: prevState.query,
25
+ args: {
26
+ ...prevState.args,
27
+ paginationOpts: {
28
+ ...prevState.queries[key].args.paginationOpts,
29
+ cursor: splitCursor,
30
+ endCursor: continueCursor,
31
+ },
32
+ },
33
+ };
34
+ const ongoingSplits = { ...prevState.ongoingSplits };
35
+ ongoingSplits[key] = [splitKey1, splitKey2];
36
+ return {
37
+ ...prevState,
38
+ nextPageKey,
39
+ queries,
40
+ ongoingSplits,
41
+ };
42
+ };
43
+ const completeSplitQuery = (key) => (prevState) => {
44
+ console.log("completeSplitQuery", key);
45
+ const completedSplit = prevState.ongoingSplits[key];
46
+ if (completedSplit === undefined) {
47
+ return prevState;
48
+ }
49
+ const queries = { ...prevState.queries };
50
+ delete queries[key];
51
+ const ongoingSplits = { ...prevState.ongoingSplits };
52
+ delete ongoingSplits[key];
53
+ let pageKeys = prevState.pageKeys.slice();
54
+ const pageIndex = prevState.pageKeys.findIndex((v) => v === key);
55
+ if (pageIndex >= 0) {
56
+ pageKeys = [
57
+ ...prevState.pageKeys.slice(0, pageIndex),
58
+ ...completedSplit,
59
+ ...prevState.pageKeys.slice(pageIndex + 1),
60
+ ];
61
+ }
62
+ return {
63
+ ...prevState,
64
+ queries,
65
+ pageKeys,
66
+ ongoingSplits,
67
+ };
68
+ };
69
+ /**
70
+ * This is a clone of the `usePaginatedQuery` hook from `convex-helpers`.
71
+ * The difference is that we lazily pin the end cursor on loading a new page.
72
+ * This will be standard behavior in a future version of `convex/react`, but
73
+ * for now it allows the agent to have correct behavior when loading a new page
74
+ * where new items are still coming in.
75
+ */
76
+ export function usePaginatedQuery(query, args, options) {
77
+ console.log("usePaginatedQuery", query, args, options);
78
+ if (typeof options?.initialNumItems !== "number" ||
79
+ options.initialNumItems < 0) {
80
+ throw new Error(`\`options.initialNumItems\` must be a positive number. Received \`${options?.initialNumItems}\`.`);
81
+ }
82
+ const skip = args === "skip";
83
+ const argsObject = skip ? {} : args;
84
+ const queryName = getFunctionName(query);
85
+ const createInitialState = useMemo(() => {
86
+ return () => {
87
+ const id = nextPaginationId();
88
+ console.log("createInitialState", id, queryName, argsObject);
89
+ return {
90
+ query,
91
+ args: argsObject,
92
+ id,
93
+ nextPageKey: 1,
94
+ pageKeys: skip ? [] : [0],
95
+ queries: skip
96
+ ? {}
97
+ : {
98
+ 0: {
99
+ query,
100
+ args: {
101
+ ...argsObject,
102
+ paginationOpts: {
103
+ numItems: options.initialNumItems,
104
+ cursor: null,
105
+ id,
106
+ },
107
+ },
108
+ },
109
+ },
110
+ ongoingSplits: {},
111
+ skip,
112
+ };
113
+ };
114
+ // ESLint doesn't like that we're stringifying the args. We do this because
115
+ // we want to avoid rerendering if the args are a different
116
+ // object that serializes to the same result.
117
+ // eslint-disable-next-line react-hooks/exhaustive-deps
118
+ }, [
119
+ // eslint-disable-next-line react-hooks/exhaustive-deps
120
+ JSON.stringify(convexToJson(argsObject)),
121
+ queryName,
122
+ options.initialNumItems,
123
+ skip,
124
+ ]);
125
+ const [state, setState] = useState(createInitialState);
126
+ // `currState` is the state that we'll render based on.
127
+ let currState = state;
128
+ if (getFunctionName(query) !== getFunctionName(state.query) ||
129
+ JSON.stringify(convexToJson(argsObject)) !==
130
+ JSON.stringify(convexToJson(state.args)) ||
131
+ skip !== state.skip) {
132
+ currState = createInitialState();
133
+ console.log("resetting state", currState);
134
+ setState(currState);
135
+ }
136
+ const convexClient = useConvex();
137
+ const logger = convexClient.logger;
138
+ const resultsObject = useQueries(currState.queries);
139
+ const [results, maybeLastResult] = useMemo(() => {
140
+ let currResult = undefined;
141
+ const allItems = [];
142
+ for (const pageKey of currState.pageKeys) {
143
+ currResult = resultsObject[pageKey];
144
+ console.log({
145
+ pageKey,
146
+ continue: currResult?.continueCursor,
147
+ cursor: currState.queries[pageKey]?.args?.paginationOpts?.cursor,
148
+ split: currResult?.splitCursor,
149
+ status: currResult?.pageStatus,
150
+ length: currResult?.page?.length,
151
+ });
152
+ if (currResult === undefined) {
153
+ break;
154
+ }
155
+ if (currResult instanceof Error) {
156
+ if (currResult.message.includes("InvalidCursor") ||
157
+ (currResult instanceof ConvexError &&
158
+ typeof currResult.data === "object" &&
159
+ currResult.data?.isConvexSystemError === true &&
160
+ currResult.data?.paginationError === "InvalidCursor")) {
161
+ // - InvalidCursor: If the cursor is invalid, probably the paginated
162
+ // database query was data-dependent and changed underneath us. The
163
+ // cursor in the params or journal no longer matches the current
164
+ // database query.
165
+ // In all cases, we want to restart pagination to throw away all our
166
+ // existing cursors.
167
+ logger.warn("usePaginatedQuery hit error, resetting pagination state: " +
168
+ currResult.message);
169
+ setState(createInitialState);
170
+ return [[], undefined];
171
+ }
172
+ else {
173
+ throw currResult;
174
+ }
175
+ }
176
+ const ongoingSplit = currState.ongoingSplits[pageKey];
177
+ if (ongoingSplit !== undefined) {
178
+ console.log("ongoingSplit", pageKey, ongoingSplit);
179
+ if (resultsObject[ongoingSplit[0]] !== undefined &&
180
+ resultsObject[ongoingSplit[1]] !== undefined) {
181
+ // Both pages of the split have results now. Swap them in.
182
+ console.log("completeSplitQuery", pageKey);
183
+ setState(completeSplitQuery(pageKey));
184
+ }
185
+ }
186
+ else if (currResult.splitCursor &&
187
+ (currResult.pageStatus === "SplitRecommended" ||
188
+ currResult.pageStatus === "SplitRequired" ||
189
+ currResult.page.length > options.initialNumItems * 2)) {
190
+ console.log("splitQuery", pageKey, currResult.pageStatus, currResult.page.length);
191
+ // If a single page has more than double the expected number of items,
192
+ // or if the server requests a split, split the page into two.
193
+ setState(splitQuery(pageKey, currResult.splitCursor, currResult.continueCursor));
194
+ }
195
+ if (currResult.pageStatus === "SplitRequired") {
196
+ // If pageStatus is 'SplitRequired', it means the server was not able to
197
+ // fetch the full page. So we stop results before the incomplete
198
+ // page and return 'LoadingMore' while the page is splitting.
199
+ return [allItems, undefined];
200
+ }
201
+ allItems.push(...currResult.page);
202
+ }
203
+ return [allItems, currResult];
204
+ // eslint-disable-next-line react-hooks/exhaustive-deps
205
+ }, [
206
+ resultsObject,
207
+ currState.pageKeys,
208
+ currState.ongoingSplits,
209
+ options.initialNumItems,
210
+ createInitialState,
211
+ logger,
212
+ ]);
213
+ const statusObject = useMemo(() => {
214
+ if (maybeLastResult === undefined) {
215
+ if (currState.nextPageKey === 1) {
216
+ return {
217
+ status: "LoadingFirstPage",
218
+ isLoading: true,
219
+ loadMore: (_numItems) => {
220
+ // Intentional noop.
221
+ },
222
+ };
223
+ }
224
+ else {
225
+ console.log("LoadingMore", maybeLastResult);
226
+ return {
227
+ status: "LoadingMore",
228
+ isLoading: true,
229
+ loadMore: (_numItems) => {
230
+ // Intentional noop.
231
+ },
232
+ };
233
+ }
234
+ }
235
+ if (maybeLastResult.isDone) {
236
+ console.log("Exhausted", maybeLastResult);
237
+ return {
238
+ status: "Exhausted",
239
+ isLoading: false,
240
+ loadMore: (_numItems) => {
241
+ // Intentional noop.
242
+ },
243
+ };
244
+ }
245
+ const continueCursor = maybeLastResult.continueCursor;
246
+ let alreadyLoadingMore = false;
247
+ return {
248
+ status: "CanLoadMore",
249
+ isLoading: false,
250
+ loadMore: (numItems) => {
251
+ if (!alreadyLoadingMore) {
252
+ alreadyLoadingMore = true;
253
+ setState((prevState) => {
254
+ const lastPageKey = prevState.pageKeys.at(-1);
255
+ const replaceKey = prevState.nextPageKey;
256
+ const newKey = prevState.nextPageKey + 1;
257
+ const nextPageKey = prevState.nextPageKey + 2;
258
+ const pageKeys = prevState.pageKeys; //[...prevState.pageKeys, prevState.nextPageKey];
259
+ const queries = { ...prevState.queries };
260
+ console.log("loading more", lastPageKey, replaceKey, newKey, nextPageKey, pageKeys);
261
+ queries[replaceKey] = {
262
+ query: prevState.query,
263
+ args: {
264
+ ...prevState.args,
265
+ paginationOpts: {
266
+ ...queries[prevState.pageKeys.at(-1)].args
267
+ .paginationOpts,
268
+ endCursor: continueCursor,
269
+ },
270
+ },
271
+ };
272
+ queries[newKey] = {
273
+ query: prevState.query,
274
+ args: {
275
+ ...prevState.args,
276
+ paginationOpts: {
277
+ numItems,
278
+ cursor: continueCursor,
279
+ id: prevState.id,
280
+ },
281
+ },
282
+ };
283
+ return {
284
+ ...prevState,
285
+ nextPageKey,
286
+ pageKeys,
287
+ queries,
288
+ ongoingSplits: {
289
+ ...prevState.ongoingSplits,
290
+ [lastPageKey]: [replaceKey, newKey],
291
+ },
292
+ };
293
+ });
294
+ }
295
+ },
296
+ };
297
+ }, [maybeLastResult, currState.nextPageKey]);
298
+ return {
299
+ results,
300
+ ...statusObject,
301
+ };
302
+ }
303
+ let paginationId = 0;
304
+ /**
305
+ * Generate a new, unique ID for a pagination session.
306
+ *
307
+ * Every usage of {@link usePaginatedQuery} puts a unique ID into the
308
+ * query function arguments as a "cache-buster". This serves two purposes:
309
+ *
310
+ * 1. All calls to {@link usePaginatedQuery} have independent query
311
+ * journals.
312
+ *
313
+ * Every time we start a new pagination session, we'll load the first page of
314
+ * results and receive a fresh journal. Without the ID, we might instead reuse
315
+ * a query subscription already present in our client. This isn't desirable
316
+ * because the existing query function result may have grown or shrunk from the
317
+ * requested `initialNumItems`.
318
+ *
319
+ * 2. We can restart the pagination session on some types of errors.
320
+ *
321
+ * Sometimes we want to restart pagination from the beginning if we hit an error.
322
+ * Similar to (1), we'd like to ensure that this new session actually requests
323
+ * its first page from the server and doesn't reuse a query result already
324
+ * present in the client that may have hit the error.
325
+ *
326
+ * @returns The pagination ID.
327
+ */
328
+ function nextPaginationId() {
329
+ paginationId++;
330
+ return paginationId;
331
+ }
332
+ /**
333
+ * Reset pagination id for tests only, so tests know what it is.
334
+ */
335
+ export function resetPaginationId() {
336
+ paginationId = 0;
337
+ }
338
+ /**
339
+ * Optimistically update the values in a paginated list.
340
+ *
341
+ * This optimistic update is designed to be used to update data loaded with
342
+ * {@link usePaginatedQuery}. It updates the list by applying
343
+ * `updateValue` to each element of the list across all of the loaded pages.
344
+ *
345
+ * This will only apply to queries with a matching names and arguments.
346
+ *
347
+ * Example usage:
348
+ * ```ts
349
+ * const myMutation = useMutation(api.myModule.myMutation)
350
+ * .withOptimisticUpdate((localStore, mutationArg) => {
351
+ *
352
+ * // Optimistically update the document with ID `mutationArg`
353
+ * // to have an additional property.
354
+ *
355
+ * optimisticallyUpdateValueInPaginatedQuery(
356
+ * localStore,
357
+ * api.myModule.paginatedQuery
358
+ * {},
359
+ * currentValue => {
360
+ * if (mutationArg === currentValue._id) {
361
+ * return {
362
+ * ...currentValue,
363
+ * "newProperty": "newValue",
364
+ * };
365
+ * }
366
+ * return currentValue;
367
+ * }
368
+ * );
369
+ *
370
+ * });
371
+ * ```
372
+ *
373
+ * @param localStore - An {@link OptimisticLocalStore} to update.
374
+ * @param query - A {@link FunctionReference} for the paginated query to update.
375
+ * @param args - The arguments object to the query function, excluding the
376
+ * `paginationOpts` property.
377
+ * @param updateValue - A function to produce the new values.
378
+ *
379
+ * @public
380
+ */
381
+ export function optimisticallyUpdateValueInPaginatedQuery(localStore, query, args, updateValue) {
382
+ const expectedArgs = JSON.stringify(convexToJson(args));
383
+ for (const queryResult of localStore.getAllQueries(query)) {
384
+ if (queryResult.value !== undefined) {
385
+ const { paginationOpts: _, ...innerArgs } = queryResult.args;
386
+ if (JSON.stringify(convexToJson(innerArgs)) === expectedArgs) {
387
+ const value = queryResult.value;
388
+ if (typeof value === "object" &&
389
+ value !== null &&
390
+ Array.isArray(value.page)) {
391
+ localStore.setQuery(query, queryResult.args, {
392
+ ...value,
393
+ page: value.page.map(updateValue),
394
+ });
395
+ }
396
+ }
397
+ }
398
+ }
399
+ }
400
+ /**
401
+ * Updates a paginated query to insert an element at the top of the list.
402
+ *
403
+ * This is regardless of the sort order, so if the list is in descending order,
404
+ * the inserted element will be treated as the "biggest" element, but if it's
405
+ * ascending, it'll be treated as the "smallest".
406
+ *
407
+ * Example:
408
+ * ```ts
409
+ * const createTask = useMutation(api.tasks.create)
410
+ * .withOptimisticUpdate((localStore, mutationArgs) => {
411
+ * insertAtTop({
412
+ * paginatedQuery: api.tasks.list,
413
+ * argsToMatch: { listId: mutationArgs.listId },
414
+ * localQueryStore: localStore,
415
+ * item: { _id: crypto.randomUUID() as Id<"tasks">, title: mutationArgs.title, completed: false },
416
+ * });
417
+ * });
418
+ * ```
419
+ *
420
+ * @param options.paginatedQuery - A function reference to the paginated query.
421
+ * @param options.argsToMatch - Optional arguments that must be in each relevant paginated query.
422
+ * This is useful if you use the same query function with different arguments to load
423
+ * different lists.
424
+ * @param options.localQueryStore
425
+ * @param options.item The item to insert.
426
+ * @returns
427
+ */
428
+ export function insertAtTop(options) {
429
+ const { paginatedQuery, argsToMatch, localQueryStore, item } = options;
430
+ const queries = localQueryStore.getAllQueries(paginatedQuery);
431
+ const queriesThatMatch = queries.filter((q) => {
432
+ if (argsToMatch === undefined) {
433
+ return true;
434
+ }
435
+ return Object.keys(argsToMatch).every(
436
+ // @ts-expect-error -- This should be safe since both should be plain objects
437
+ (k) => compareValues(argsToMatch[k], q.args[k]) === 0);
438
+ });
439
+ const firstPage = queriesThatMatch.find((q) => q.args.paginationOpts.cursor === null);
440
+ if (firstPage === undefined || firstPage.value === undefined) {
441
+ // first page is not loaded, so don't update it until it loads
442
+ return;
443
+ }
444
+ localQueryStore.setQuery(paginatedQuery, firstPage.args, {
445
+ ...firstPage.value,
446
+ page: [item, ...firstPage.value.page],
447
+ });
448
+ }
449
+ /**
450
+ * Updates a paginated query to insert an element at the bottom of the list.
451
+ *
452
+ * This is regardless of the sort order, so if the list is in descending order,
453
+ * the inserted element will be treated as the "smallest" element, but if it's
454
+ * ascending, it'll be treated as the "biggest".
455
+ *
456
+ * This only has an effect if the last page is loaded, since otherwise it would result
457
+ * in the element being inserted at the end of whatever is loaded (which is the middle of the list)
458
+ * and then popping out once the optimistic update is over.
459
+ *
460
+ * @param options.paginatedQuery - A function reference to the paginated query.
461
+ * @param options.argsToMatch - Optional arguments that must be in each relevant paginated query.
462
+ * This is useful if you use the same query function with different arguments to load
463
+ * different lists.
464
+ * @param options.localQueryStore
465
+ * @param options.element The element to insert.
466
+ * @returns
467
+ */
468
+ export function insertAtBottomIfLoaded(options) {
469
+ const { paginatedQuery, localQueryStore, item, argsToMatch } = options;
470
+ const queries = localQueryStore.getAllQueries(paginatedQuery);
471
+ const queriesThatMatch = queries.filter((q) => {
472
+ if (argsToMatch === undefined) {
473
+ return true;
474
+ }
475
+ return Object.keys(argsToMatch).every(
476
+ // @ts-expect-error -- This should be safe since both should be plain objects
477
+ (k) => compareValues(argsToMatch[k], q.args[k]) === 0);
478
+ });
479
+ const lastPage = queriesThatMatch.find((q) => q.value !== undefined && q.value.isDone);
480
+ if (lastPage === undefined) {
481
+ // last page is not loaded, so don't update it since the item would immediately pop out
482
+ // when the server updates
483
+ return;
484
+ }
485
+ localQueryStore.setQuery(paginatedQuery, lastPage.args, {
486
+ ...lastPage.value,
487
+ page: [...lastPage.value.page, item],
488
+ });
489
+ }
490
+ /**
491
+ * This is a helper function for inserting an item at a specific position in a paginated query.
492
+ *
493
+ * You must provide the sortOrder and a function for deriving the sort key (an array of values) from an item in the list.
494
+ *
495
+ * This will only work if the server query uses the same sort order and sort key as the optimistic update.
496
+ *
497
+ * Example:
498
+ * ```ts
499
+ * const createTask = useMutation(api.tasks.create)
500
+ * .withOptimisticUpdate((localStore, mutationArgs) => {
501
+ * insertAtPosition({
502
+ * paginatedQuery: api.tasks.listByPriority,
503
+ * argsToMatch: { listId: mutationArgs.listId },
504
+ * sortOrder: "asc",
505
+ * sortKeyFromItem: (item) => [item.priority, item._creationTime],
506
+ * localQueryStore: localStore,
507
+ * item: {
508
+ * _id: crypto.randomUUID() as Id<"tasks">,
509
+ * _creationTime: Date.now(),
510
+ * title: mutationArgs.title,
511
+ * completed: false,
512
+ * priority: mutationArgs.priority,
513
+ * },
514
+ * });
515
+ * });
516
+ * ```
517
+ * @param options.paginatedQuery - A function reference to the paginated query.
518
+ * @param options.argsToMatch - Optional arguments that must be in each relevant paginated query.
519
+ * This is useful if you use the same query function with different arguments to load
520
+ * different lists.
521
+ * @param options.sortOrder - The sort order of the paginated query ("asc" or "desc").
522
+ * @param options.sortKeyFromItem - A function for deriving the sort key (an array of values) from an element in the list.
523
+ * Including a tie-breaker field like `_creationTime` is recommended.
524
+ * @param options.localQueryStore
525
+ * @param options.item - The item to insert.
526
+ * @returns
527
+ */
528
+ export function insertAtPosition(options) {
529
+ const { paginatedQuery, sortOrder, sortKeyFromItem, localQueryStore, item, argsToMatch, } = options;
530
+ const queries = localQueryStore.getAllQueries(paginatedQuery);
531
+ // Group into sets of pages for the same usePaginatedQuery. Grouping is by all
532
+ // args except paginationOpts, but including paginationOpts.id.
533
+ const queryGroups = {};
534
+ for (const query of queries) {
535
+ if (argsToMatch !== undefined &&
536
+ !Object.keys(argsToMatch).every((k) =>
537
+ // @ts-expect-error why is this not working?
538
+ argsToMatch[k] === query.args[k])) {
539
+ continue;
540
+ }
541
+ const key = JSON.stringify(Object.fromEntries(Object.entries(query.args).map(([k, v]) => [
542
+ k,
543
+ k === "paginationOpts" ? v.id : v,
544
+ ])));
545
+ queryGroups[key] ??= [];
546
+ queryGroups[key].push(query);
547
+ }
548
+ for (const pageQueries of Object.values(queryGroups)) {
549
+ insertAtPositionInPages({
550
+ pageQueries,
551
+ paginatedQuery,
552
+ sortOrder,
553
+ sortKeyFromItem,
554
+ localQueryStore,
555
+ item,
556
+ });
557
+ }
558
+ }
559
+ function insertAtPositionInPages(options) {
560
+ const { pageQueries, sortOrder, sortKeyFromItem, localQueryStore, item, paginatedQuery, } = options;
561
+ const insertedKey = sortKeyFromItem(item);
562
+ const loadedPages = pageQueries.filter((q) => q.value !== undefined && q.value.page.length > 0);
563
+ const sortedPages = loadedPages.sort((a, b) => {
564
+ const aKey = sortKeyFromItem(a.value.page[0]);
565
+ const bKey = sortKeyFromItem(b.value.page[0]);
566
+ if (sortOrder === "asc") {
567
+ return compareValues(aKey, bKey);
568
+ }
569
+ else {
570
+ return compareValues(bKey, aKey);
571
+ }
572
+ });
573
+ // check if the inserted element is before the first page
574
+ const firstLoadedPage = sortedPages[0];
575
+ if (firstLoadedPage === undefined) {
576
+ // no pages, so don't update until they load
577
+ return;
578
+ }
579
+ const firstPageKey = sortKeyFromItem(firstLoadedPage.value.page[0]);
580
+ const isBeforeFirstPage = sortOrder === "asc"
581
+ ? compareValues(insertedKey, firstPageKey) <= 0
582
+ : compareValues(insertedKey, firstPageKey) >= 0;
583
+ if (isBeforeFirstPage) {
584
+ if (firstLoadedPage.args.paginationOpts.cursor === null) {
585
+ localQueryStore.setQuery(paginatedQuery, firstLoadedPage.args, {
586
+ ...firstLoadedPage.value,
587
+ page: [item, ...firstLoadedPage.value.page],
588
+ });
589
+ }
590
+ else {
591
+ // if the very first page is not loaded
592
+ return;
593
+ }
594
+ return;
595
+ }
596
+ const lastLoadedPage = sortedPages[sortedPages.length - 1];
597
+ if (lastLoadedPage === undefined) {
598
+ // no pages, so don't update until they load
599
+ return;
600
+ }
601
+ const lastPageKey = sortKeyFromItem(lastLoadedPage.value.page[lastLoadedPage.value.page.length - 1]);
602
+ const isAfterLastPage = sortOrder === "asc"
603
+ ? compareValues(insertedKey, lastPageKey) >= 0
604
+ : compareValues(insertedKey, lastPageKey) <= 0;
605
+ if (isAfterLastPage) {
606
+ // Only update if the last page is done loading, otherwise it will pop out
607
+ // when the server updates the query
608
+ if (lastLoadedPage.value.isDone) {
609
+ localQueryStore.setQuery(paginatedQuery, lastLoadedPage.args, {
610
+ ...lastLoadedPage.value,
611
+ page: [...lastLoadedPage.value.page, item],
612
+ });
613
+ }
614
+ return;
615
+ }
616
+ // if sorted in ascending order, find the first page that starts with a key greater than the inserted element,
617
+ // and update the page before it
618
+ // if sorted in descending order, find the first page that starts with a key less than the inserted element,
619
+ // and update the page before it
620
+ const successorPageIndex = sortedPages.findIndex((p) => sortOrder === "asc"
621
+ ? compareValues(sortKeyFromItem(p.value.page[0]), insertedKey) > 0
622
+ : compareValues(sortKeyFromItem(p.value.page[0]), insertedKey) < 0);
623
+ const pageToUpdate = successorPageIndex === -1
624
+ ? sortedPages[sortedPages.length - 1]
625
+ : sortedPages[successorPageIndex - 1];
626
+ if (pageToUpdate === undefined) {
627
+ // no pages, so don't update until they load
628
+ return;
629
+ }
630
+ // If ascending, find the first element that is greater than or equal to the inserted element
631
+ // If descending, find the first element that is less than or equal to the inserted element
632
+ const indexWithinPage = pageToUpdate.value.page.findIndex((e) => sortOrder === "asc"
633
+ ? compareValues(sortKeyFromItem(e), insertedKey) >= 0
634
+ : compareValues(sortKeyFromItem(e), insertedKey) <= 0);
635
+ const newPage = indexWithinPage === -1
636
+ ? [...pageToUpdate.value.page, item]
637
+ : [
638
+ ...pageToUpdate.value.page.slice(0, indexWithinPage),
639
+ item,
640
+ ...pageToUpdate.value.page.slice(indexWithinPage),
641
+ ];
642
+ localQueryStore.setQuery(paginatedQuery, pageToUpdate.args, {
643
+ ...pageToUpdate.value,
644
+ page: newPage,
645
+ });
646
+ }
647
+ //# sourceMappingURL=usePaginatedQuery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usePaginatedQ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