@@ -28,6 +28,8 @@ import type {
28
28
FunctionReference ,
29
29
FunctionReturnType ,
30
30
OptionalRestArgs ,
31
+ PaginationOptions ,
32
+ PaginationResult ,
31
33
} from "convex/server" ;
32
34
import {
33
35
useQuery ,
@@ -38,6 +40,9 @@ import {
38
40
type MutationOptions ,
39
41
useConvex ,
40
42
type ReactMutation ,
43
+ usePaginatedQuery ,
44
+ type PaginatedQueryArgs ,
45
+ type UsePaginatedQueryReturnType ,
41
46
} from "convex/react" ;
42
47
import type { SessionId } from "../server/sessions.js" ;
43
48
import type { EmptyObject , BetterOmit } from "../index.js" ;
@@ -86,6 +91,21 @@ export type SessionArgsAndOptions<
86
91
? [ args ?: EmptyObject , options ?: Options ]
87
92
: [ args : BetterOmit < FunctionArgs < Fn > , "sessionId" > , options ?: Options ] ;
88
93
94
+ type SessionPaginatedQueryFunction <
95
+ Args extends { paginationOpts : PaginationOptions } = {
96
+ paginationOpts : PaginationOptions ;
97
+ } ,
98
+ > = FunctionReference <
99
+ "query" ,
100
+ "public" ,
101
+ { sessionId : SessionId } & Args ,
102
+ PaginationResult < any >
103
+ > ;
104
+
105
+ export type SessionPaginatedQueryArgs <
106
+ Fn extends SessionPaginatedQueryFunction ,
107
+ > = BetterOmit < PaginatedQueryArgs < Fn > , "sessionId" > | "skip" ;
108
+
89
109
/**
90
110
* Context for a Convex session, creating a server session and providing the id.
91
111
*
@@ -203,6 +223,37 @@ export function useSessionQuery<Query extends SessionFunction<"query">>(
203
223
return useQuery ( query , ...( [ newArgs ] as OptionalRestArgs < Query > ) ) ;
204
224
}
205
225
226
+ /**
227
+ * Use this in place of {@link usePaginatedQuery} to run a query, passing a sessionId.
228
+ *
229
+ * @param query Query that takes in a sessionId parameter. Like `api.foo.bar`.
230
+ * @param args Args for that query, without the sessionId.
231
+ * @param options - An object specifying the `initialNumItems` to be loaded in
232
+ * the first page.
233
+ * @returns A {@link UsePaginatedQueryRes} that includes the currently loaded
234
+ * items, the status of the pagination, and a `loadMore` function.
235
+ * For SSR, it will skip the query until the second render.
236
+ */
237
+ export function useSessionPaginatedQuery <
238
+ Query extends SessionPaginatedQueryFunction ,
239
+ > (
240
+ query : Query ,
241
+ args : SessionPaginatedQueryArgs < Query > ,
242
+ options : { initialNumItems : number } ,
243
+ ) : UsePaginatedQueryReturnType < Query > | undefined {
244
+ const [ sessionId ] = useSessionId ( ) ;
245
+ const skip = args === "skip" || ! sessionId ;
246
+ const originalArgs = args === "skip" ? { } : ( args ?? { } ) ;
247
+
248
+ const newArgs = skip ? "skip" : { ...originalArgs , sessionId } ;
249
+
250
+ return usePaginatedQuery (
251
+ query ,
252
+ newArgs as PaginatedQueryArgs < Query > | "skip" ,
253
+ options ,
254
+ ) ;
255
+ }
256
+
206
257
type SessionMutation < Mutation extends FunctionReference < "mutation" > > = (
207
258
...args : SessionArgsArray < Mutation >
208
259
) => Promise < FunctionReturnType < Mutation > > ;
0 commit comments