Skip to content

Commit a72e3cd

Browse files
committed
feat: Add useSeamInfiniteQuery
1 parent fa6438d commit a72e3cd

File tree

4 files changed

+74
-5
lines changed

4 files changed

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

0 commit comments

Comments
 (0)