@sanity/sdk-react 2.10.0 → 2.11.1
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/dist/index.d.ts +257 -200
- package/dist/index.js +368 -257
- package/dist/index.js.map +1 -1
- package/package.json +21 -24
- package/src/_exports/index.ts +2 -0
- package/src/_exports/sdk-react.ts +4 -0
- package/src/components/SDKProvider.test.tsx +5 -12
- package/src/components/SDKProvider.tsx +26 -24
- package/src/components/errors/CorsErrorComponent.tsx +2 -2
- package/src/config/handles.ts +55 -0
- package/src/constants.ts +5 -0
- package/src/context/DefaultResourceContext.ts +10 -0
- package/src/context/PerspectiveContext.ts +12 -0
- package/src/context/ResourceProvider.test.tsx +2 -2
- package/src/context/ResourceProvider.tsx +53 -49
- package/src/hooks/agent/agentActions.ts +55 -38
- package/src/hooks/context/useResource.test.tsx +32 -0
- package/src/hooks/context/useResource.ts +24 -0
- package/src/hooks/context/useSanityInstance.test.tsx +42 -111
- package/src/hooks/context/useSanityInstance.ts +28 -50
- package/src/hooks/dashboard/useDispatchIntent.test.ts +5 -1
- package/src/hooks/dashboard/useDispatchIntent.ts +3 -3
- package/src/hooks/dashboard/useManageFavorite.test.tsx +16 -12
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.ts +1 -5
- package/src/hooks/document/{useApplyDocumentActions.test.ts → useApplyDocumentActions.test.tsx} +42 -77
- package/src/hooks/document/useApplyDocumentActions.ts +29 -63
- package/src/hooks/document/useDocument.ts +5 -7
- package/src/hooks/document/useDocumentEvent.ts +4 -3
- package/src/hooks/document/useDocumentPermissions.test.tsx +58 -150
- package/src/hooks/document/useDocumentPermissions.ts +78 -55
- package/src/hooks/document/useEditDocument.test.tsx +25 -60
- package/src/hooks/document/useEditDocument.ts +1 -1
- package/src/hooks/documents/useDocuments.ts +13 -8
- package/src/hooks/helpers/createStateSourceHook.tsx +1 -2
- package/src/hooks/helpers/useNormalizedResourceOptions.test.tsx +253 -0
- package/src/hooks/helpers/useNormalizedResourceOptions.ts +85 -47
- package/src/hooks/organizations/useOrganization.test-d.ts +53 -0
- package/src/hooks/organizations/useOrganization.test.ts +65 -0
- package/src/hooks/organizations/useOrganization.ts +40 -0
- package/src/hooks/organizations/useOrganizations.test-d.ts +55 -0
- package/src/hooks/organizations/useOrganizations.test.ts +85 -0
- package/src/hooks/organizations/useOrganizations.ts +45 -0
- package/src/hooks/paginatedDocuments/usePaginatedDocuments.ts +23 -9
- package/src/hooks/presence/usePresence.ts +4 -11
- package/src/hooks/preview/useDocumentPreview.tsx +4 -7
- package/src/hooks/projection/useDocumentProjection.ts +5 -7
- package/src/hooks/projects/useProject.test-d.ts +49 -0
- package/src/hooks/projects/useProject.ts +33 -41
- package/src/hooks/projects/useProjects.test-d.ts +49 -0
- package/src/hooks/projects/useProjects.ts +17 -23
- package/src/hooks/query/useQuery.ts +1 -1
- package/src/hooks/releases/useActiveReleases.ts +6 -6
- package/src/hooks/releases/usePerspective.ts +7 -12
- package/src/hooks/users/useUser.ts +1 -1
- package/src/hooks/users/useUsers.ts +1 -1
package/src/hooks/document/{useApplyDocumentActions.test.ts → useApplyDocumentActions.test.tsx}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import {applyDocumentActions,
|
|
1
|
+
import {applyDocumentActions, createSanityInstance} from '@sanity/sdk'
|
|
2
|
+
import {renderHook as reactRenderHook} from '@testing-library/react'
|
|
2
3
|
import {describe, it} from 'vitest'
|
|
3
4
|
|
|
4
5
|
import {renderHook} from '../../../test/test-utils'
|
|
6
|
+
import {SanityInstanceContext} from '../../context/SanityInstanceContext'
|
|
5
7
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
6
8
|
import {useApplyDocumentActions} from './useApplyDocumentActions'
|
|
7
9
|
|
|
@@ -12,18 +14,7 @@ vi.mock('@sanity/sdk', async (importOriginal) => {
|
|
|
12
14
|
|
|
13
15
|
vi.mock('../context/useSanityInstance')
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
const instances: Record<string, SanityInstance | undefined> = {
|
|
17
|
-
'p123.d': {__id: 'p123.d'} as unknown as SanityInstance,
|
|
18
|
-
'p.d123': {__id: 'p.d123'} as unknown as SanityInstance,
|
|
19
|
-
'p123.d123': {__id: 'p123.d123'} as unknown as SanityInstance,
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const instance = {
|
|
23
|
-
match({projectId = 'p', dataset = 'd'}): SanityInstance | undefined {
|
|
24
|
-
return instances[`${projectId}.${dataset}`]
|
|
25
|
-
},
|
|
26
|
-
} as unknown as SanityInstance
|
|
17
|
+
const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
|
|
27
18
|
|
|
28
19
|
describe('useApplyDocumentActions', () => {
|
|
29
20
|
beforeEach(() => {
|
|
@@ -31,7 +22,7 @@ describe('useApplyDocumentActions', () => {
|
|
|
31
22
|
vi.mocked(useSanityInstance).mockReturnValueOnce(instance)
|
|
32
23
|
})
|
|
33
24
|
|
|
34
|
-
it('uses the
|
|
25
|
+
it('uses the effective context resource', async () => {
|
|
35
26
|
const {result} = renderHook(() => useApplyDocumentActions())
|
|
36
27
|
result.current({
|
|
37
28
|
type: 'document.edit',
|
|
@@ -45,43 +36,28 @@ describe('useApplyDocumentActions', () => {
|
|
|
45
36
|
type: 'document.edit',
|
|
46
37
|
documentType: 'post',
|
|
47
38
|
documentId: 'abc',
|
|
39
|
+
// resource named in test-utils
|
|
40
|
+
resource: {projectId: 'test', dataset: 'test'},
|
|
48
41
|
},
|
|
49
42
|
],
|
|
43
|
+
resource: {projectId: 'test', dataset: 'test'},
|
|
50
44
|
})
|
|
51
45
|
})
|
|
52
46
|
|
|
53
|
-
it('uses SanityInstance
|
|
54
|
-
const {result} =
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
documentId: 'abc',
|
|
59
|
-
|
|
60
|
-
projectId: 'p123',
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instances['p123.d'], {
|
|
64
|
-
actions: [
|
|
65
|
-
{
|
|
66
|
-
type: 'document.edit',
|
|
67
|
-
documentType: 'post',
|
|
68
|
-
documentId: 'abc',
|
|
69
|
-
|
|
70
|
-
projectId: 'p123',
|
|
71
|
-
},
|
|
72
|
-
],
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
it('uses SanityInstance when dataset is overrideen', async () => {
|
|
77
|
-
const {result} = renderHook(() => useApplyDocumentActions())
|
|
78
|
-
result.current({
|
|
79
|
-
type: 'document.edit',
|
|
80
|
-
documentType: 'post',
|
|
81
|
-
documentId: 'abc',
|
|
82
|
-
|
|
83
|
-
dataset: 'd123',
|
|
47
|
+
it('uses the SanityInstance when resource is not provided', async () => {
|
|
48
|
+
const {result} = reactRenderHook(() => useApplyDocumentActions(), {
|
|
49
|
+
wrapper: ({children}) => (
|
|
50
|
+
<SanityInstanceContext.Provider value={instance}>{children}</SanityInstanceContext.Provider>
|
|
51
|
+
),
|
|
84
52
|
})
|
|
53
|
+
result.current(
|
|
54
|
+
{
|
|
55
|
+
type: 'document.edit',
|
|
56
|
+
documentType: 'post',
|
|
57
|
+
documentId: 'abc',
|
|
58
|
+
},
|
|
59
|
+
{},
|
|
60
|
+
)
|
|
85
61
|
|
|
86
62
|
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instance, {
|
|
87
63
|
actions: [
|
|
@@ -89,51 +65,36 @@ describe('useApplyDocumentActions', () => {
|
|
|
89
65
|
type: 'document.edit',
|
|
90
66
|
documentType: 'post',
|
|
91
67
|
documentId: 'abc',
|
|
92
|
-
|
|
93
|
-
dataset: 'd123',
|
|
68
|
+
resource: {projectId: 'p', dataset: 'd'},
|
|
94
69
|
},
|
|
95
70
|
],
|
|
71
|
+
resource: {projectId: 'p', dataset: 'd'},
|
|
96
72
|
})
|
|
97
73
|
})
|
|
98
74
|
|
|
99
|
-
it('
|
|
75
|
+
it('resolves resource from projectId and dataset in action', async () => {
|
|
100
76
|
const {result} = renderHook(() => useApplyDocumentActions())
|
|
101
77
|
result.current({
|
|
102
78
|
type: 'document.edit',
|
|
103
79
|
documentType: 'post',
|
|
104
80
|
documentId: 'abc',
|
|
105
|
-
|
|
106
|
-
projectId: 'p123',
|
|
81
|
+
projectId: 'p',
|
|
107
82
|
dataset: 'd123',
|
|
108
83
|
})
|
|
109
84
|
|
|
110
|
-
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(
|
|
85
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instance, {
|
|
111
86
|
actions: [
|
|
112
87
|
{
|
|
113
88
|
type: 'document.edit',
|
|
114
89
|
documentType: 'post',
|
|
115
90
|
documentId: 'abc',
|
|
116
|
-
|
|
117
|
-
projectId: 'p123',
|
|
118
|
-
dataset: 'd123',
|
|
91
|
+
resource: {projectId: 'p', dataset: 'd123'},
|
|
119
92
|
},
|
|
120
93
|
],
|
|
94
|
+
resource: {projectId: 'p', dataset: 'd123'},
|
|
121
95
|
})
|
|
122
96
|
})
|
|
123
97
|
|
|
124
|
-
it("throws if SanityInstance.match doesn't find anything", async () => {
|
|
125
|
-
const {result} = renderHook(() => useApplyDocumentActions())
|
|
126
|
-
expect(() => {
|
|
127
|
-
result.current({
|
|
128
|
-
type: 'document.edit',
|
|
129
|
-
documentType: 'post',
|
|
130
|
-
documentId: 'abc',
|
|
131
|
-
|
|
132
|
-
projectId: 'other',
|
|
133
|
-
})
|
|
134
|
-
}).toThrow()
|
|
135
|
-
})
|
|
136
|
-
|
|
137
98
|
it('throws when actions have mismatched project IDs', async () => {
|
|
138
99
|
const {result} = renderHook(() => useApplyDocumentActions())
|
|
139
100
|
expect(() => {
|
|
@@ -143,15 +104,17 @@ describe('useApplyDocumentActions', () => {
|
|
|
143
104
|
documentType: 'post',
|
|
144
105
|
documentId: 'abc',
|
|
145
106
|
projectId: 'p123',
|
|
107
|
+
dataset: 'd',
|
|
146
108
|
},
|
|
147
109
|
{
|
|
148
110
|
type: 'document.edit',
|
|
149
111
|
documentType: 'post',
|
|
150
112
|
documentId: 'def',
|
|
151
113
|
projectId: 'p456',
|
|
114
|
+
dataset: 'd',
|
|
152
115
|
},
|
|
153
116
|
])
|
|
154
|
-
}).toThrow(/Mismatched
|
|
117
|
+
}).toThrow(/Mismatched resources found in actions/)
|
|
155
118
|
})
|
|
156
119
|
|
|
157
120
|
it('throws when actions have mismatched datasets', async () => {
|
|
@@ -173,7 +136,7 @@ describe('useApplyDocumentActions', () => {
|
|
|
173
136
|
dataset: 'd2',
|
|
174
137
|
},
|
|
175
138
|
])
|
|
176
|
-
}).toThrow(/Mismatched
|
|
139
|
+
}).toThrow(/Mismatched resources found in actions/)
|
|
177
140
|
})
|
|
178
141
|
|
|
179
142
|
it('throws when actions have mismatched resources', async () => {
|
|
@@ -196,7 +159,7 @@ describe('useApplyDocumentActions', () => {
|
|
|
196
159
|
}).toThrow(/Mismatched resources found in actions/)
|
|
197
160
|
})
|
|
198
161
|
|
|
199
|
-
it('throws when mixing projectId and resource (projectId first)', async () => {
|
|
162
|
+
it('throws when mixing projectId/dataset and resource with a mismatch (projectId first)', async () => {
|
|
200
163
|
const {result} = renderHook(() => useApplyDocumentActions())
|
|
201
164
|
expect(() => {
|
|
202
165
|
result.current([
|
|
@@ -204,19 +167,20 @@ describe('useApplyDocumentActions', () => {
|
|
|
204
167
|
type: 'document.edit',
|
|
205
168
|
documentType: 'post',
|
|
206
169
|
documentId: 'abc',
|
|
207
|
-
projectId: '
|
|
170
|
+
projectId: 'p1',
|
|
171
|
+
dataset: 'd',
|
|
208
172
|
},
|
|
209
173
|
{
|
|
210
174
|
type: 'document.edit',
|
|
211
175
|
documentType: 'post',
|
|
212
176
|
documentId: 'def',
|
|
213
|
-
resource: {projectId: '
|
|
177
|
+
resource: {projectId: 'p2', dataset: 'd'},
|
|
214
178
|
},
|
|
215
179
|
])
|
|
216
|
-
}).toThrow(/
|
|
180
|
+
}).toThrow(/Mismatched resources found in actions/)
|
|
217
181
|
})
|
|
218
182
|
|
|
219
|
-
it('throws when mixing resource and projectId (resource first)', async () => {
|
|
183
|
+
it('throws when mixing resource and projectId/dataset with a mismatch (resource first)', async () => {
|
|
220
184
|
const {result} = renderHook(() => useApplyDocumentActions())
|
|
221
185
|
expect(() => {
|
|
222
186
|
result.current([
|
|
@@ -224,15 +188,16 @@ describe('useApplyDocumentActions', () => {
|
|
|
224
188
|
type: 'document.edit',
|
|
225
189
|
documentType: 'post',
|
|
226
190
|
documentId: 'abc',
|
|
227
|
-
resource: {projectId: '
|
|
191
|
+
resource: {projectId: 'p1', dataset: 'd'},
|
|
228
192
|
},
|
|
229
193
|
{
|
|
230
194
|
type: 'document.edit',
|
|
231
195
|
documentType: 'post',
|
|
232
196
|
documentId: 'def',
|
|
233
|
-
projectId: '
|
|
197
|
+
projectId: 'p2',
|
|
198
|
+
dataset: 'd',
|
|
234
199
|
},
|
|
235
200
|
])
|
|
236
|
-
}).toThrow(/
|
|
201
|
+
}).toThrow(/Mismatched resources found in actions/)
|
|
237
202
|
})
|
|
238
203
|
})
|
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
applyDocumentActions,
|
|
4
|
-
type ApplyDocumentActionsOptions,
|
|
5
|
-
type DocumentAction,
|
|
6
|
-
} from '@sanity/sdk'
|
|
1
|
+
import {type ActionsResult, applyDocumentActions, type DocumentAction} from '@sanity/sdk'
|
|
2
|
+
import {isDeepEqual} from '@sanity/sdk/_internal'
|
|
7
3
|
import {type SanityDocument} from 'groq'
|
|
8
4
|
import {useContext} from 'react'
|
|
9
5
|
|
|
6
|
+
import {type ResourceHandle} from '../../config/handles'
|
|
10
7
|
import {ResourcesContext} from '../../context/ResourcesContext'
|
|
11
8
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
12
9
|
import {
|
|
13
10
|
normalizeResourceOptions,
|
|
14
|
-
|
|
11
|
+
useEffectiveContextResource,
|
|
15
12
|
} from '../helpers/useNormalizedResourceOptions'
|
|
16
13
|
// this import is used in an `{@link useEditDocument}`
|
|
17
|
-
// eslint-disable-next-line
|
|
14
|
+
// eslint-disable-next-line import/consistent-type-specifier-style
|
|
18
15
|
import type {useEditDocument} from './useEditDocument'
|
|
19
16
|
|
|
20
17
|
/**
|
|
@@ -29,7 +26,7 @@ interface UseApplyDocumentActions {
|
|
|
29
26
|
action:
|
|
30
27
|
| DocumentAction<TDocumentType, TDataset, TProjectId>
|
|
31
28
|
| DocumentAction<TDocumentType, TDataset, TProjectId>[],
|
|
32
|
-
options?:
|
|
29
|
+
options?: ResourceHandle,
|
|
33
30
|
) => Promise<ActionsResult<SanityDocument<TDocumentType, `${TProjectId}.${TDataset}`>>>
|
|
34
31
|
}
|
|
35
32
|
|
|
@@ -219,73 +216,42 @@ interface UseApplyDocumentActions {
|
|
|
219
216
|
export const useApplyDocumentActions: UseApplyDocumentActions = () => {
|
|
220
217
|
const instance = useSanityInstance()
|
|
221
218
|
const resources = useContext(ResourcesContext)
|
|
219
|
+
const effectiveContextResource = useEffectiveContextResource()
|
|
222
220
|
|
|
223
221
|
return (actionOrActions, options) => {
|
|
224
222
|
const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions]
|
|
225
|
-
const
|
|
223
|
+
const optionsResource = options
|
|
224
|
+
? normalizeResourceOptions(options, resources, effectiveContextResource).resource
|
|
225
|
+
: undefined
|
|
226
226
|
|
|
227
|
-
|
|
228
|
-
|
|
227
|
+
const normalizedActions = actions.map((action) =>
|
|
228
|
+
normalizeResourceOptions(action, resources, effectiveContextResource),
|
|
229
|
+
)
|
|
229
230
|
let resource
|
|
230
|
-
for (const action of actions) {
|
|
231
|
-
if (action.projectId) {
|
|
232
|
-
if (resource) {
|
|
233
|
-
throw new Error(
|
|
234
|
-
`Mismatches between projectId/dataset options and resource in actions. Found projectId "${action.projectId}" and dataset "${action.dataset}" but expected resource "${resource}".`,
|
|
235
|
-
)
|
|
236
|
-
}
|
|
237
|
-
if (!projectId) projectId = action.projectId
|
|
238
|
-
if (action.projectId !== projectId) {
|
|
239
|
-
throw new Error(
|
|
240
|
-
`Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`,
|
|
241
|
-
)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (action.dataset) {
|
|
245
|
-
if (!dataset) dataset = action.dataset
|
|
246
|
-
if (action.dataset !== dataset) {
|
|
247
|
-
throw new Error(
|
|
248
|
-
`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`,
|
|
249
|
-
)
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
231
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
throw new Error(
|
|
258
|
-
`Mismatched resources found in actions. All actions must belong to the same resource. Found "${action.resource}" but expected "${resource}".`,
|
|
259
|
-
)
|
|
260
|
-
}
|
|
261
|
-
if (projectId || dataset) {
|
|
262
|
-
throw new Error(
|
|
263
|
-
`Mismatches between projectId/dataset options and resource in actions. Found "${action.resource}" but expected project "${projectId}" and dataset "${dataset}".`,
|
|
264
|
-
)
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
if (projectId || dataset) {
|
|
270
|
-
const actualInstance = instance.match({projectId, dataset})
|
|
271
|
-
if (!actualInstance) {
|
|
232
|
+
for (const action of normalizedActions) {
|
|
233
|
+
if (!resource && action.resource) resource = action.resource
|
|
234
|
+
if (!isDeepEqual(action.resource, resource)) {
|
|
272
235
|
throw new Error(
|
|
273
|
-
`
|
|
274
|
-
Please ensure there is a ResourceProvider component with a matching configuration in the component hierarchy.`,
|
|
236
|
+
`Mismatched resources found in actions. All actions must belong to the same resource. Found "${JSON.stringify(action.resource)}" but expected "${JSON.stringify(resource)}".`,
|
|
275
237
|
)
|
|
276
238
|
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (optionsResource && resource && !isDeepEqual(optionsResource, resource)) {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`Mismatched resources found in actions. Found top-level resource "${JSON.stringify(optionsResource)}" but expected resource from action handles "${JSON.stringify(resource)}".`,
|
|
244
|
+
)
|
|
245
|
+
}
|
|
277
246
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
...normalizedOptions,
|
|
282
|
-
})
|
|
247
|
+
const effectiveResource = resource ?? optionsResource ?? effectiveContextResource
|
|
248
|
+
if (!effectiveResource) {
|
|
249
|
+
throw new Error('No resource found. Provide a resource via the action handle or context.')
|
|
283
250
|
}
|
|
284
251
|
|
|
285
252
|
return applyDocumentActions(instance, {
|
|
286
|
-
actions,
|
|
287
|
-
resource,
|
|
288
|
-
...normalizedOptions,
|
|
253
|
+
actions: normalizedActions as DocumentAction[],
|
|
254
|
+
resource: effectiveResource,
|
|
289
255
|
})
|
|
290
256
|
}
|
|
291
257
|
}
|
|
@@ -2,16 +2,14 @@ import {type DocumentOptions, getDocumentState, type JsonMatch, resolveDocument}
|
|
|
2
2
|
import {type SanityDocument} from 'groq'
|
|
3
3
|
import {identity} from 'rxjs'
|
|
4
4
|
|
|
5
|
+
import {type DocumentHandle} from '../../config/handles'
|
|
5
6
|
import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
6
|
-
import {
|
|
7
|
-
useNormalizedResourceOptions,
|
|
8
|
-
type WithResourceNameSupport,
|
|
9
|
-
} from '../helpers/useNormalizedResourceOptions'
|
|
7
|
+
import {useNormalizedResourceOptions} from '../helpers/useNormalizedResourceOptions'
|
|
10
8
|
import {useTrackHookUsage} from '../helpers/useTrackHookUsage'
|
|
11
9
|
// used in an `{@link useDocumentProjection}` and `{@link useQuery}`
|
|
12
|
-
// eslint-disable-next-line import/consistent-type-specifier-style
|
|
10
|
+
// eslint-disable-next-line import/consistent-type-specifier-style
|
|
13
11
|
import type {useDocumentProjection} from '../projection/useDocumentProjection'
|
|
14
|
-
// eslint-disable-next-line import/consistent-type-specifier-style
|
|
12
|
+
// eslint-disable-next-line import/consistent-type-specifier-style
|
|
15
13
|
import type {useQuery} from '../query/useQuery'
|
|
16
14
|
|
|
17
15
|
const useDocumentValue = createStateSourceHook({
|
|
@@ -43,7 +41,7 @@ type UseDocumentOptions<
|
|
|
43
41
|
TDocumentType extends string = string,
|
|
44
42
|
TDataset extends string = string,
|
|
45
43
|
TProjectId extends string = string,
|
|
46
|
-
> =
|
|
44
|
+
> = DocumentHandle<TDocumentType, TDataset, TProjectId> & {path?: TPath}
|
|
47
45
|
|
|
48
46
|
interface UseDocument {
|
|
49
47
|
/** @internal */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {type
|
|
1
|
+
import {type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'
|
|
2
2
|
import {useCallback, useEffect, useInsertionEffect, useRef} from 'react'
|
|
3
3
|
|
|
4
|
+
import {type ResourceHandle} from '../../config/handles'
|
|
4
5
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
5
6
|
import {useNormalizedResourceOptions} from '../helpers/useNormalizedResourceOptions'
|
|
6
7
|
import {useTrackHookUsage} from '../helpers/useTrackHookUsage'
|
|
@@ -11,7 +12,7 @@ import {useTrackHookUsage} from '../helpers/useTrackHookUsage'
|
|
|
11
12
|
export interface UseDocumentEventOptions<
|
|
12
13
|
TDataset extends string = string,
|
|
13
14
|
TProjectId extends string = string,
|
|
14
|
-
> extends
|
|
15
|
+
> extends ResourceHandle<TDataset, TProjectId> {
|
|
15
16
|
onEvent: (documentEvent: DocumentEvent) => void
|
|
16
17
|
}
|
|
17
18
|
|
|
@@ -91,7 +92,7 @@ export function useDocumentEvent<
|
|
|
91
92
|
return ref.current(documentEvent)
|
|
92
93
|
}, [])
|
|
93
94
|
|
|
94
|
-
const instance = useSanityInstance(
|
|
95
|
+
const instance = useSanityInstance()
|
|
95
96
|
useEffect(() => {
|
|
96
97
|
return subscribeDocumentEvents(instance, {
|
|
97
98
|
eventHandler: stableHandler,
|