Skip to content

Commit 87e573a

Browse files
authored
Merge pull request #697 from seamapi/evan/cx-409-add-pagination-support-to-useseamquery
feat: Add useSeamInfiniteQuery
2 parents fa6438d + 1a959fd commit 87e573a

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
"@rxfork/r2wc-react-to-web-component": "^2.4.0",
146146
"@seamapi/fake-devicedb": "^1.6.1",
147147
"@seamapi/fake-seam-connect": "^1.76.0",
148-
"@seamapi/http": "^1.40.0",
148+
"@seamapi/http": "^1.40.1",
149149
"@seamapi/types": "^1.395.3",
150150
"@storybook/addon-designs": "^7.0.1",
151151
"@storybook/addon-essentials": "^7.0.2",

src/lib/seam/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './devices/use-device-providers.js'
1212
export * from './devices/use-devices.js'
1313
export * from './SeamProvider.js'
1414
export * from './use-seam-client.js'
15+
export * from './use-seam-infinite-query.js'
1516
export * from './use-seam-mutation.js'
1617
export * from './use-seam-mutation-without-workspace.js'
1718
export * from './use-seam-query.js'
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import type {
2+
SeamHttpApiError,
3+
SeamHttpEndpointPaginatedQueryPaths,
4+
SeamHttpEndpoints,
5+
SeamHttpRequest,
6+
SeamPageCursor,
7+
} from '@seamapi/http/connect'
8+
import {
9+
useInfiniteQuery,
10+
type UseInfiniteQueryOptions,
11+
type UseInfiniteQueryResult,
12+
} from '@tanstack/react-query'
13+
14+
import { useSeamClient } from 'lib/seam/use-seam-client.js'
15+
16+
export type UseSeamInfiniteQueryParameters<
17+
T extends SeamHttpEndpointPaginatedQueryPaths,
18+
> = Parameters<SeamHttpEndpoints[T]>[0]
19+
20+
export type UseSeamInfiniteQueryResult<
21+
T extends SeamHttpEndpointPaginatedQueryPaths,
22+
> = UseInfiniteQueryResult<QueryData<T>, SeamHttpApiError>
23+
24+
export function useSeamInfiniteQuery<
25+
T extends SeamHttpEndpointPaginatedQueryPaths,
26+
>(
27+
endpointPath: T,
28+
parameters?: UseSeamInfiniteQueryParameters<T>,
29+
options: Parameters<SeamHttpEndpoints[T]>[1] &
30+
QueryOptions<QueryData<T>, SeamHttpApiError> = {}
31+
): UseSeamInfiniteQueryResult<T> {
32+
const { endpointClient: client, queryKeyPrefixes } = useSeamClient()
33+
return useInfiniteQuery({
34+
enabled: client != null,
35+
...options,
36+
queryKey: [
37+
...queryKeyPrefixes,
38+
...endpointPath.split('/').filter((v) => v !== ''),
39+
parameters,
40+
],
41+
initialPageParam: null,
42+
getNextPageParam: (lastPage) => lastPage.nextPageCursor,
43+
queryFn: async ({ pageParam }) => {
44+
if (client == null)
45+
return {
46+
data: [] as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
47+
nextPageCursor: null,
48+
}
49+
// Using @ts-expect-error over any is preferred, but not possible here because TypeScript will run out of memory.
50+
// Type assertion is needed here for performance reasons. The types are correct at runtime.
51+
const endpoint = client[endpointPath] as (...args: any) => any
52+
const request = endpoint(parameters, options)
53+
const pages = client.createPaginator(request as SeamHttpRequest<any, any>)
54+
if (pageParam == null) {
55+
const [data, { nextPageCursor }] = await pages.firstPage()
56+
return {
57+
data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
58+
nextPageCursor,
59+
}
60+
}
61+
// Type assertion is needed for pageParam since the Seam API expects a branded PageCursor type.
62+
const [data, { nextPageCursor }] = await pages.nextPage(
63+
pageParam as SeamPageCursor
64+
)
65+
return {
66+
data: data as Awaited<ReturnType<SeamHttpEndpoints[T]>>,
67+
nextPageCursor,
68+
}
69+
},
70+
})
71+
}
72+
73+
interface QueryData<T extends SeamHttpEndpointPaginatedQueryPaths> {
74+
data: Awaited<ReturnType<SeamHttpEndpoints[T]>>
75+
nextPageCursor: SeamPageCursor | null
76+
}
77+
78+
type QueryOptions<X, Y> = Omit<
79+
UseInfiniteQueryOptions<X, Y>,
80+
'queryKey' | 'queryFn' | 'initialPageParam' | 'getNextPageParam'
81+
>

src/lib/seam/use-seam-mutation-without-workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type UseSeamMutationWithoutWorkspaceResult<
2323
UseSeamMutationWithoutWorkspaceVariables<T>
2424
>
2525

26-
export function UseSeamMutationWithoutWorkspace<
26+
export function useSeamMutationWithoutWorkspace<
2727
T extends SeamHttpEndpointWithoutWorkspaceMutationPaths,
2828
>(
2929
endpointPath: T,

0 commit comments

Comments
 (0)