@@ -28,6 +28,8 @@ import type {
2828 FunctionReference ,
2929 FunctionReturnType ,
3030 OptionalRestArgs ,
31+ PaginationOptions ,
32+ PaginationResult ,
3133} from "convex/server" ;
3234import {
3335 useQuery ,
@@ -38,6 +40,9 @@ import {
3840 type MutationOptions ,
3941 useConvex ,
4042 type ReactMutation ,
43+ usePaginatedQuery ,
44+ type PaginatedQueryArgs ,
45+ type UsePaginatedQueryReturnType ,
4146} from "convex/react" ;
4247import type { SessionId } from "../server/sessions.js" ;
4348import type { EmptyObject , BetterOmit } from "../index.js" ;
@@ -86,6 +91,21 @@ export type SessionArgsAndOptions<
8691 ? [ args ?: EmptyObject , options ?: Options ]
8792 : [ args : BetterOmit < FunctionArgs < Fn > , "sessionId" > , options ?: Options ] ;
8893
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+
89109/**
90110 * Context for a Convex session, creating a server session and providing the id.
91111 *
@@ -203,6 +223,37 @@ export function useSessionQuery<Query extends SessionFunction<"query">>(
203223 return useQuery ( query , ...( [ newArgs ] as OptionalRestArgs < Query > ) ) ;
204224}
205225
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+
206257type SessionMutation < Mutation extends FunctionReference < "mutation" > > = (
207258 ...args : SessionArgsArray < Mutation >
208259) => Promise < FunctionReturnType < Mutation > > ;
0 commit comments