-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
376 lines (357 loc) · 9.76 KB
/
index.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import {
type PaginatedQueryArgs,
type PaginatedQueryReference,
useQuery,
} from "convex/react";
import {
type Cursor,
type FunctionReturnType,
type PaginationOptions,
getFunctionName,
} from "convex/server";
import { type Value, convexToJson } from "convex/values";
import { type Dispatch, useEffect, useReducer } from "react";
/**
* Load data reactively from a paginated query, one page at a time.
*
* Use `loadNext` and `loadPrev` to navigate through pages.
*
* @param query A reference to the public paginated query function.
* @param args The arguments object for the query function, excluding the `paginationOpts` property. That property is injected by this hook.
* @param options An object specifying the `initialNumItems` to be loaded in the first page.
* @returns The paginated query result, in the form of a discriminated union. Use the `_tag` property to determine the current state of the query.
*/
export const useNextPrevPaginatedQuery = <
Query extends PaginatedQueryReference,
>(
query: Query,
args: PaginatedQueryArgs<Query> | "skip",
options: { initialNumItems: number },
): Result<Query> => {
if (options.initialNumItems <= 0)
throw new Error("Initial number of items must be greater than zero");
const queryName = getFunctionName(query);
const [state, dispatch] = useReducer(
reducer,
{ queryName, args, options },
initialState,
);
useEffect(() => {
if (
(state._tag !== "Skipped" &&
(JSON.stringify(state.args) !== JSON.stringify(args) ||
state.initialNumItems !== options.initialNumItems ||
state.queryName !== queryName)) ||
(state._tag === "Skipped" && args !== "skip")
) {
dispatch({
_tag: "ArgsChanged",
queryName,
args,
options,
});
}
}, [args, options, state, queryName]);
const mergedArgs = mergeArgs(state);
// NOTE: Is it possible to remove this `any`?
const queryResults = useQuery(query, mergedArgs as any);
// biome-ignore lint/correctness/useExhaustiveDependencies:
useEffect(() => {
if (queryResults) {
dispatch({
_tag: "GotResults",
results: queryResults,
nextCursor: queryResults.isDone ? null : queryResults.continueCursor,
});
}
}, [
queryResults
? JSON.stringify(convexToJson(queryResults as unknown as Value))
: null,
]);
const result: Result<Query> = makeResult(state, dispatch);
return result;
};
/**
* The result of a paginated query, modeled as a discriminated union.
*
* You should use `if` or `switch` statements to conditionally render
* based on the `_tag` property, which indicates the current state of the
* paginated query.
*/
export type Result<Query extends PaginatedQueryReference> =
| { _tag: "Skipped" }
| {
_tag: "LoadingInitialResults";
}
| {
_tag: "Loaded";
/** @deprecated Use `page` instead. This will be removed in the next major release. */
results: FunctionReturnType<Query>["page"];
/** The current page of results. */
page: FunctionReturnType<Query>["page"];
/** The number of the current page (1-indexed). */
pageNum: number;
/** A function which loads the next page of results, or null if this is the last page. */
loadNext: (() => void) | null;
/** A function which loads the previous page of results, or null if this is the first page. */
loadPrev: (() => void) | null;
}
| {
_tag: "LoadingNextResults";
}
| {
_tag: "LoadingPrevResults";
};
type State<Query extends PaginatedQueryReference> =
| { _tag: "Skipped" }
| {
_tag: "LoadingInitialResults";
queryName: string;
args: PaginatedQueryArgs<Query>;
initialNumItems: number;
}
| {
_tag: "LoadingNextResults";
queryName: string;
args: PaginatedQueryArgs<Query>;
initialNumItems: number;
loadingCursor: Cursor | null;
prevCursors: Cursor[];
}
| {
_tag: "LoadingPrevResults";
queryName: string;
args: PaginatedQueryArgs<Query>;
initialNumItems: number;
loadingCursor: Cursor | null;
prevCursors: Cursor[];
}
| {
_tag: "Loaded";
queryName: string;
args: PaginatedQueryArgs<Query>;
initialNumItems: number;
currentResults: FunctionReturnType<Query>;
currentCursor: Cursor | null;
prevCursors: Cursor[];
nextCursor: Cursor | null;
};
type Action<Query extends PaginatedQueryReference> =
| {
_tag: "GotResults";
results: FunctionReturnType<Query>;
nextCursor: Cursor | null;
}
| { _tag: "NextPageRequested" }
| { _tag: "PrevPageRequested" }
| {
_tag: "ArgsChanged";
queryName: string;
args: PaginatedQueryArgs<Query> | "skip";
options: { initialNumItems: number };
};
const reducer = <Query extends PaginatedQueryReference>(
state: State<Query>,
action: Action<Query>,
): State<Query> => {
switch (action._tag) {
case "ArgsChanged":
return initialState({
queryName: action.queryName,
args: action.args,
options: action.options,
});
case "PrevPageRequested":
if (state._tag === "Loaded") {
const loadingCursor =
state.prevCursors[state.prevCursors.length - 1] ?? null;
const prevCursors = state.prevCursors.slice(0, -1);
return {
_tag: "LoadingPrevResults",
queryName: state.queryName,
args: state.args,
initialNumItems: state.initialNumItems,
loadingCursor,
prevCursors,
};
}
throw new Error(
"Cannot load previous page unless the current page is loaded",
);
case "NextPageRequested":
if (state._tag === "Loaded") {
return {
_tag: "LoadingNextResults",
queryName: state.queryName,
args: state.args,
initialNumItems: state.initialNumItems,
loadingCursor: state.nextCursor,
prevCursors: [
...state.prevCursors,
...(state.currentCursor ? [state.currentCursor] : []),
],
};
} else {
throw new Error(
"Cannot load next page unless the current page is loaded",
);
}
case "GotResults":
if (state._tag === "LoadingInitialResults") {
return {
_tag: "Loaded",
queryName: state.queryName,
args: state.args,
initialNumItems: state.initialNumItems,
currentResults: action.results,
currentCursor: null,
prevCursors: [],
nextCursor: action.nextCursor,
};
} else if (state._tag === "LoadingNextResults") {
return {
_tag: "Loaded",
queryName: state.queryName,
args: state.args,
initialNumItems: state.initialNumItems,
currentResults: action.results,
currentCursor: state.loadingCursor,
prevCursors: state.prevCursors,
nextCursor: action.nextCursor,
};
} else if (state._tag === "LoadingPrevResults") {
return {
_tag: "Loaded",
queryName: state.queryName,
args: state.args,
initialNumItems: state.initialNumItems,
currentResults: action.results,
currentCursor: state.loadingCursor,
prevCursors: state.prevCursors,
nextCursor: action.nextCursor,
};
} else if (state._tag === "Loaded") {
return {
...state,
currentResults: action.results,
currentCursor: state.currentCursor,
prevCursors: state.prevCursors,
nextCursor: action.nextCursor,
};
} else {
throw new Error("Got results in impossible state");
}
default:
throw new Error("Impossible action");
}
};
const mergeArgs = <Query extends PaginatedQueryReference>(
state: State<Query>,
): PaginatedQueryArgs<Query> | "skip" => {
if (state._tag === "Skipped") {
return "skip" as const;
} else {
switch (state._tag) {
case "LoadingInitialResults":
return {
...state.args,
paginationOpts: {
numItems: state.initialNumItems,
cursor: null,
} satisfies PaginationOptions,
};
case "LoadingNextResults":
case "LoadingPrevResults":
return {
...state.args,
paginationOpts: {
numItems: state.initialNumItems,
cursor: state.loadingCursor,
} satisfies PaginationOptions,
};
case "Loaded":
return {
...state.args,
paginationOpts: {
numItems: state.initialNumItems,
cursor: state.currentCursor,
} satisfies PaginationOptions,
};
default:
throw new Error(`Invalid state: ${state}`);
}
}
};
const makeResult = <Query extends PaginatedQueryReference>(
state: State<Query>,
dispatch: Dispatch<Action<Query>>,
): Result<Query> => {
switch (state._tag) {
case "Skipped":
return { _tag: "Skipped" };
case "LoadingInitialResults":
return {
_tag: "LoadingInitialResults",
};
case "Loaded":
return {
_tag: "Loaded",
results: state.currentResults.page,
page: state.currentResults.page,
pageNum: 1 + state.prevCursors.length + (state.currentCursor ? 1 : 0),
loadNext: makeLoadNext(state, dispatch),
loadPrev: makeLoadPrev(state, dispatch),
};
case "LoadingNextResults":
return {
_tag: "LoadingNextResults",
};
case "LoadingPrevResults":
return {
_tag: "LoadingPrevResults",
};
default:
throw new Error(`Invalid state: ${state}`);
}
};
const makeLoadPrev = <Query extends PaginatedQueryReference>(
state: State<Query>,
dispatch: Dispatch<Action<Query>>,
): (() => void) | null => {
if (
state._tag === "Loaded" &&
(state.prevCursors.length > 0 || state.currentCursor !== null)
) {
return () => dispatch({ _tag: "PrevPageRequested" });
} else {
return null;
}
};
const makeLoadNext = <Query extends PaginatedQueryReference>(
state: State<Query>,
dispatch: Dispatch<Action<Query>>,
): (() => void) | null => {
if (state._tag === "Loaded" && state.nextCursor !== null) {
return () => dispatch({ _tag: "NextPageRequested" });
} else {
return null;
}
};
const initialState = <Query extends PaginatedQueryReference>({
queryName,
args,
options,
}: {
queryName: string;
args: PaginatedQueryArgs<Query> | "skip";
options: { initialNumItems: number };
}): State<Query> =>
args === "skip"
? { _tag: "Skipped" }
: {
_tag: "LoadingInitialResults" as const,
queryName,
args,
initialNumItems: options.initialNumItems,
};