@sanity/sdk-react 0.0.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.d.ts +302 -273
- package/dist/index.js +14 -7
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/src/_exports/sdk-react.ts +8 -8
- package/src/hooks/document/useApplyDocumentActions.ts +4 -4
- package/src/hooks/document/useDocument.test.ts +8 -10
- package/src/hooks/document/useDocument.ts +50 -33
- package/src/hooks/document/useDocumentEvent.ts +2 -2
- package/src/hooks/document/useDocumentSyncStatus.ts +1 -1
- package/src/hooks/document/useEditDocument.ts +15 -15
- package/src/hooks/documents/useDocuments.ts +5 -5
- package/src/hooks/paginatedDocuments/usePaginatedDocuments.ts +5 -5
- package/src/hooks/preview/{usePreview.test.tsx → useDocumentPreview.test.tsx} +5 -5
- package/src/hooks/preview/{usePreview.tsx → useDocumentPreview.tsx} +11 -8
- package/src/hooks/projection/{useProjection.test.tsx → useDocumentProjection.test.tsx} +5 -5
- package/src/hooks/projection/{useProjection.ts → useDocumentProjection.ts} +22 -17
- package/src/hooks/query/useQuery.ts +5 -5
|
@@ -8,7 +8,7 @@ import {act, render, screen} from '@testing-library/react'
|
|
|
8
8
|
import {Suspense, useRef} from 'react'
|
|
9
9
|
import {type Mock} from 'vitest'
|
|
10
10
|
|
|
11
|
-
import {
|
|
11
|
+
import {useDocumentProjection} from './useDocumentProjection'
|
|
12
12
|
|
|
13
13
|
// Mock IntersectionObserver
|
|
14
14
|
const mockIntersectionObserver = vi.fn()
|
|
@@ -61,7 +61,7 @@ function TestComponent({
|
|
|
61
61
|
projection: ValidProjection
|
|
62
62
|
}) {
|
|
63
63
|
const ref = useRef(null)
|
|
64
|
-
const {data, isPending} =
|
|
64
|
+
const {data, isPending} = useDocumentProjection<ProjectionResult>({...document, projection, ref})
|
|
65
65
|
|
|
66
66
|
return (
|
|
67
67
|
<div ref={ref}>
|
|
@@ -72,7 +72,7 @@ function TestComponent({
|
|
|
72
72
|
)
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
describe('
|
|
75
|
+
describe('useDocumentProjection', () => {
|
|
76
76
|
let getCurrent: Mock
|
|
77
77
|
let subscribe: Mock
|
|
78
78
|
|
|
@@ -228,7 +228,7 @@ describe('useProjection', () => {
|
|
|
228
228
|
projection,
|
|
229
229
|
...docHandle
|
|
230
230
|
}: DocumentHandle & {projection: ValidProjection}) {
|
|
231
|
-
const {data} =
|
|
231
|
+
const {data} = useDocumentProjection<ProjectionResult>({...docHandle, projection}) // No ref provided
|
|
232
232
|
return (
|
|
233
233
|
<div>
|
|
234
234
|
<h1>{data.title}</h1>
|
|
@@ -261,7 +261,7 @@ describe('useProjection', () => {
|
|
|
261
261
|
...docHandle
|
|
262
262
|
}: DocumentHandle & {projection: ValidProjection}) {
|
|
263
263
|
const ref = useRef({}) // ref.current is not an HTML element
|
|
264
|
-
const {data} =
|
|
264
|
+
const {data} = useDocumentProjection<ProjectionResult>({...docHandle, projection, ref})
|
|
265
265
|
return (
|
|
266
266
|
<div>
|
|
267
267
|
<h1>{data.title}</h1>
|
|
@@ -14,7 +14,7 @@ import {useSanityInstance} from '../context/useSanityInstance'
|
|
|
14
14
|
* @public
|
|
15
15
|
* @category Types
|
|
16
16
|
*/
|
|
17
|
-
export interface
|
|
17
|
+
export interface useDocumentProjectionOptions<
|
|
18
18
|
TProjection extends ValidProjection = ValidProjection,
|
|
19
19
|
TDocumentType extends string = string,
|
|
20
20
|
TDataset extends string = string,
|
|
@@ -32,7 +32,7 @@ export interface UseProjectionOptions<
|
|
|
32
32
|
* @public
|
|
33
33
|
* @category Types
|
|
34
34
|
*/
|
|
35
|
-
export interface
|
|
35
|
+
export interface useDocumentProjectionResults<TData> {
|
|
36
36
|
/** The projected data */
|
|
37
37
|
data: TData
|
|
38
38
|
/** True if the projection is currently being resolved */
|
|
@@ -59,7 +59,7 @@ export interface UseProjectionResults<TData> {
|
|
|
59
59
|
|
|
60
60
|
// Overload 1: Relies on Typegen
|
|
61
61
|
/**
|
|
62
|
-
* @
|
|
62
|
+
* @public
|
|
63
63
|
* Fetch a projection, relying on Typegen for the return type based on the handle and projection.
|
|
64
64
|
*
|
|
65
65
|
* @category Documents
|
|
@@ -69,7 +69,7 @@ export interface UseProjectionResults<TData> {
|
|
|
69
69
|
* @example Using Typegen for a book preview
|
|
70
70
|
* ```tsx
|
|
71
71
|
* // ProjectionComponent.tsx
|
|
72
|
-
* import {
|
|
72
|
+
* import {useDocumentProjection, type DocumentHandle} from '@sanity/sdk-react'
|
|
73
73
|
* import {useRef} from 'react'
|
|
74
74
|
* import {defineProjection} from 'groq'
|
|
75
75
|
*
|
|
@@ -90,7 +90,7 @@ export interface UseProjectionResults<TData> {
|
|
|
90
90
|
*
|
|
91
91
|
* // Spread the doc handle into the options
|
|
92
92
|
* // Typegen infers the return type based on 'book' and the projection
|
|
93
|
-
* const { data } =
|
|
93
|
+
* const { data } = useDocumentProjection({
|
|
94
94
|
* ...doc, // Pass the handle properties
|
|
95
95
|
* ref,
|
|
96
96
|
* projection: myProjection,
|
|
@@ -114,18 +114,20 @@ export interface UseProjectionResults<TData> {
|
|
|
114
114
|
* // </Suspense>
|
|
115
115
|
* ```
|
|
116
116
|
*/
|
|
117
|
-
export function
|
|
117
|
+
export function useDocumentProjection<
|
|
118
118
|
TProjection extends ValidProjection = ValidProjection,
|
|
119
119
|
TDocumentType extends string = string,
|
|
120
120
|
TDataset extends string = string,
|
|
121
121
|
TProjectId extends string = string,
|
|
122
122
|
>(
|
|
123
|
-
options:
|
|
124
|
-
):
|
|
123
|
+
options: useDocumentProjectionOptions<TProjection, TDocumentType, TDataset, TProjectId>,
|
|
124
|
+
): useDocumentProjectionResults<
|
|
125
|
+
SanityProjectionResult<TProjection, TDocumentType, `${TProjectId}.${TDataset}`>
|
|
126
|
+
>
|
|
125
127
|
|
|
126
128
|
// Overload 2: Explicit type provided
|
|
127
129
|
/**
|
|
128
|
-
* @
|
|
130
|
+
* @public
|
|
129
131
|
* Fetch a projection with an explicitly defined return type `TData`.
|
|
130
132
|
*
|
|
131
133
|
* @param options - Options including the document handle properties (`documentId`, etc.) and the `projection`.
|
|
@@ -133,7 +135,7 @@ export function useProjection<
|
|
|
133
135
|
*
|
|
134
136
|
* @example Explicitly typing the projection result
|
|
135
137
|
* ```tsx
|
|
136
|
-
* import {
|
|
138
|
+
* import {useDocumentProjection, type DocumentHandle} from '@sanity/sdk-react'
|
|
137
139
|
* import {useRef} from 'react'
|
|
138
140
|
*
|
|
139
141
|
* interface SimpleBookPreview {
|
|
@@ -147,7 +149,7 @@ export function useProjection<
|
|
|
147
149
|
*
|
|
148
150
|
* function BookPreview({ doc }: BookPreviewProps) {
|
|
149
151
|
* const ref = useRef(null)
|
|
150
|
-
* const { data } =
|
|
152
|
+
* const { data } = useDocumentProjection<SimpleBookPreview>({
|
|
151
153
|
* ...doc,
|
|
152
154
|
* ref,
|
|
153
155
|
* projection: `{ title, 'authorName': author->name }`
|
|
@@ -169,16 +171,16 @@ export function useProjection<
|
|
|
169
171
|
* // </Suspense>
|
|
170
172
|
* ```
|
|
171
173
|
*/
|
|
172
|
-
export function
|
|
173
|
-
options:
|
|
174
|
-
):
|
|
174
|
+
export function useDocumentProjection<TData extends object>(
|
|
175
|
+
options: useDocumentProjectionOptions, // Uses base options type
|
|
176
|
+
): useDocumentProjectionResults<TData>
|
|
175
177
|
|
|
176
178
|
// Implementation (no JSDoc needed here as it's covered by overloads)
|
|
177
|
-
export function
|
|
179
|
+
export function useDocumentProjection<TData extends object>({
|
|
178
180
|
ref,
|
|
179
181
|
projection,
|
|
180
182
|
...docHandle
|
|
181
|
-
}:
|
|
183
|
+
}: useDocumentProjectionOptions): useDocumentProjectionResults<TData> {
|
|
182
184
|
const instance = useSanityInstance()
|
|
183
185
|
const stateSource = getProjectionState<TData>(instance, {...docHandle, projection})
|
|
184
186
|
|
|
@@ -228,5 +230,8 @@ export function useProjection<TData extends object>({
|
|
|
228
230
|
[stateSource, ref],
|
|
229
231
|
)
|
|
230
232
|
|
|
231
|
-
return useSyncExternalStore(
|
|
233
|
+
return useSyncExternalStore(
|
|
234
|
+
subscribe,
|
|
235
|
+
stateSource.getCurrent,
|
|
236
|
+
) as useDocumentProjectionResults<TData>
|
|
232
237
|
}
|
|
@@ -12,7 +12,7 @@ import {useSanityInstance} from '../context/useSanityInstance'
|
|
|
12
12
|
|
|
13
13
|
// Overload 1: Inferred Type (using Typegen)
|
|
14
14
|
/**
|
|
15
|
-
* @
|
|
15
|
+
* @public
|
|
16
16
|
* Executes a GROQ query, inferring the result type from the query string and options.
|
|
17
17
|
* Leverages Sanity Typegen if configured for enhanced type safety.
|
|
18
18
|
*
|
|
@@ -74,14 +74,14 @@ export function useQuery<
|
|
|
74
74
|
options: QueryOptions<TQuery, TDataset, TProjectId>,
|
|
75
75
|
): {
|
|
76
76
|
/** The query result, typed based on the GROQ query string */
|
|
77
|
-
data: SanityQueryResult<TQuery, TDataset
|
|
77
|
+
data: SanityQueryResult<TQuery, `${TProjectId}.${TDataset}`>
|
|
78
78
|
/** True if a query transition is in progress */
|
|
79
79
|
isPending: boolean
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
// Overload 2: Explicit Type Provided
|
|
83
83
|
/**
|
|
84
|
-
* @
|
|
84
|
+
* @public
|
|
85
85
|
* Executes a GROQ query with an explicitly provided result type `TData`.
|
|
86
86
|
*
|
|
87
87
|
* @param options - Configuration for the query, including `query`, optional `params`, `projectId`, `dataset`, etc.
|
|
@@ -116,7 +116,7 @@ export function useQuery<TData>(options: QueryOptions): {
|
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
|
-
* @
|
|
119
|
+
* @public
|
|
120
120
|
* Fetches data and subscribes to real-time updates using a GROQ query.
|
|
121
121
|
*
|
|
122
122
|
* @remarks
|
|
@@ -182,7 +182,7 @@ export function useQuery(options: QueryOptions): {data: unknown; isPending: bool
|
|
|
182
182
|
// the captured signal remains unchanged for this suspended render.
|
|
183
183
|
// Thus, the promise thrown here uses a stable abort signal, ensuring correct behavior.
|
|
184
184
|
const currentSignal = ref.current.signal
|
|
185
|
-
|
|
185
|
+
|
|
186
186
|
throw resolveQuery(instance, {...deferred, signal: currentSignal})
|
|
187
187
|
}
|
|
188
188
|
|