@sanity/sdk-react 2.9.0 → 2.11.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/dist/index.d.ts +338 -215
- package/dist/index.js +564 -342
- package/dist/index.js.map +1 -1
- package/package.json +9 -14
- package/src/_exports/index.ts +2 -0
- package/src/_exports/sdk-react.ts +8 -0
- package/src/components/SDKProvider.test.tsx +5 -12
- package/src/components/SDKProvider.tsx +58 -28
- package/src/components/SanityApp.tsx +2 -2
- package/src/components/auth/AuthBoundary.tsx +8 -1
- package/src/components/auth/DashboardAccessRequest.tsx +37 -0
- package/src/components/auth/LoginError.test.tsx +191 -5
- package/src/components/auth/LoginError.tsx +100 -56
- package/src/components/errors/ChunkLoadError.test.tsx +59 -0
- package/src/components/errors/ChunkLoadError.tsx +56 -0
- package/src/components/errors/chunkReloadStorage.ts +57 -0
- 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 +56 -51
- package/src/context/ResourcesContext.tsx +7 -0
- package/src/context/SanityInstanceProvider.test.tsx +100 -0
- package/src/context/SanityInstanceProvider.tsx +71 -0
- package/src/hooks/agent/agentActions.ts +55 -38
- package/src/hooks/auth/useVerifyOrgProjects.tsx +13 -6
- 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 +11 -7
- package/src/hooks/dashboard/useDispatchIntent.ts +7 -7
- package/src/hooks/dashboard/useManageFavorite.test.tsx +16 -12
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.test.ts +15 -15
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.ts +13 -17
- package/src/hooks/document/{useApplyDocumentActions.test.ts → useApplyDocumentActions.test.tsx} +46 -81
- package/src/hooks/document/useApplyDocumentActions.ts +33 -67
- package/src/hooks/document/useDocument.ts +4 -6
- package/src/hooks/document/useDocumentEvent.ts +8 -7
- package/src/hooks/document/useDocumentPermissions.test.tsx +60 -152
- package/src/hooks/document/useDocumentPermissions.ts +78 -55
- package/src/hooks/document/useDocumentSyncStatus.ts +2 -2
- package/src/hooks/document/useEditDocument.test.tsx +25 -60
- package/src/hooks/document/useEditDocument.ts +3 -3
- package/src/hooks/documents/useDocuments.ts +19 -11
- package/src/hooks/helpers/createStateSourceHook.tsx +1 -2
- package/src/hooks/helpers/useNormalizedResourceOptions.test.tsx +253 -0
- package/src/hooks/helpers/useNormalizedResourceOptions.ts +169 -0
- package/src/hooks/helpers/useTrackHookUsage.ts +2 -2
- 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 +29 -14
- package/src/hooks/presence/usePresence.test.tsx +56 -9
- package/src/hooks/presence/usePresence.ts +16 -4
- package/src/hooks/preview/useDocumentPreview.tsx +8 -10
- package/src/hooks/projection/useDocumentProjection.ts +7 -9
- 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 +11 -10
- package/src/hooks/releases/useActiveReleases.ts +14 -14
- package/src/hooks/releases/usePerspective.ts +11 -16
- package/src/hooks/users/useUser.ts +1 -1
- package/src/hooks/users/useUsers.ts +1 -1
- package/src/context/SourcesContext.tsx +0 -7
- package/src/hooks/helpers/useNormalizedSourceOptions.ts +0 -107
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,10 +136,10 @@ 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
|
-
it('throws when actions have mismatched
|
|
142
|
+
it('throws when actions have mismatched resources', async () => {
|
|
180
143
|
const {result} = renderHook(() => useApplyDocumentActions())
|
|
181
144
|
expect(() => {
|
|
182
145
|
result.current([
|
|
@@ -184,19 +147,19 @@ describe('useApplyDocumentActions', () => {
|
|
|
184
147
|
type: 'document.edit',
|
|
185
148
|
documentType: 'post',
|
|
186
149
|
documentId: 'abc',
|
|
187
|
-
|
|
150
|
+
resource: {projectId: 'p', dataset: 'd1'},
|
|
188
151
|
},
|
|
189
152
|
{
|
|
190
153
|
type: 'document.edit',
|
|
191
154
|
documentType: 'post',
|
|
192
155
|
documentId: 'def',
|
|
193
|
-
|
|
156
|
+
resource: {projectId: 'p', dataset: 'd2'},
|
|
194
157
|
},
|
|
195
158
|
])
|
|
196
|
-
}).toThrow(/Mismatched
|
|
159
|
+
}).toThrow(/Mismatched resources found in actions/)
|
|
197
160
|
})
|
|
198
161
|
|
|
199
|
-
it('throws when mixing projectId and
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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,18 +1,15 @@
|
|
|
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
|
|
|
10
|
-
import {
|
|
6
|
+
import {type ResourceHandle} from '../../config/handles'
|
|
7
|
+
import {ResourcesContext} from '../../context/ResourcesContext'
|
|
11
8
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
12
9
|
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from '../helpers/
|
|
10
|
+
normalizeResourceOptions,
|
|
11
|
+
useEffectiveContextResource,
|
|
12
|
+
} from '../helpers/useNormalizedResourceOptions'
|
|
16
13
|
// this import is used in an `{@link useEditDocument}`
|
|
17
14
|
// eslint-disable-next-line unused-imports/no-unused-imports, import/consistent-type-specifier-style
|
|
18
15
|
import type {useEditDocument} from './useEditDocument'
|
|
@@ -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
|
|
|
@@ -218,74 +215,43 @@ interface UseApplyDocumentActions {
|
|
|
218
215
|
*/
|
|
219
216
|
export const useApplyDocumentActions: UseApplyDocumentActions = () => {
|
|
220
217
|
const instance = useSanityInstance()
|
|
221
|
-
const
|
|
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
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
let dataset
|
|
229
|
-
let source
|
|
230
|
-
for (const action of actions) {
|
|
231
|
-
if (action.projectId) {
|
|
232
|
-
if (source) {
|
|
233
|
-
throw new Error(
|
|
234
|
-
`Mismatches between projectId/dataset options and source in actions. Found projectId "${action.projectId}" and dataset "${action.dataset}" but expected source "${source}".`,
|
|
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
|
-
}
|
|
223
|
+
const optionsResource = options
|
|
224
|
+
? normalizeResourceOptions(options, resources, effectiveContextResource).resource
|
|
225
|
+
: undefined
|
|
243
226
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`,
|
|
249
|
-
)
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
227
|
+
const normalizedActions = actions.map((action) =>
|
|
228
|
+
normalizeResourceOptions(action, resources, effectiveContextResource),
|
|
229
|
+
)
|
|
230
|
+
let resource
|
|
253
231
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
throw new Error(
|
|
258
|
-
`Mismatched sources found in actions. All actions must belong to the same source. Found "${action.source}" but expected "${source}".`,
|
|
259
|
-
)
|
|
260
|
-
}
|
|
261
|
-
if (projectId || dataset) {
|
|
262
|
-
throw new Error(
|
|
263
|
-
`Mismatches between projectId/dataset options and source in actions. Found "${action.source}" 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
|
-
|
|
288
|
-
...normalizedOptions,
|
|
253
|
+
actions: normalizedActions as DocumentAction[],
|
|
254
|
+
resource: effectiveResource,
|
|
289
255
|
})
|
|
290
256
|
}
|
|
291
257
|
}
|
|
@@ -2,11 +2,9 @@ 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
|
-
useNormalizedSourceOptions,
|
|
8
|
-
type WithSourceNameSupport,
|
|
9
|
-
} from '../helpers/useNormalizedSourceOptions'
|
|
7
|
+
import {useNormalizedResourceOptions} from '../helpers/useNormalizedResourceOptions'
|
|
10
8
|
import {useTrackHookUsage} from '../helpers/useTrackHookUsage'
|
|
11
9
|
// used in an `{@link useDocumentProjection}` and `{@link useQuery}`
|
|
12
10
|
// eslint-disable-next-line import/consistent-type-specifier-style, unused-imports/no-unused-imports
|
|
@@ -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 */
|
|
@@ -240,6 +238,6 @@ interface UseDocument {
|
|
|
240
238
|
*/
|
|
241
239
|
export const useDocument = wrapHookWithData((options: UseDocumentOptions) => {
|
|
242
240
|
useTrackHookUsage('useDocument')
|
|
243
|
-
const normalizedOptions =
|
|
241
|
+
const normalizedOptions = useNormalizedResourceOptions(options)
|
|
244
242
|
return useDocumentValue(normalizedOptions)
|
|
245
243
|
}) as UseDocument
|
|
@@ -1,8 +1,9 @@
|
|
|
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
|
-
import {
|
|
6
|
+
import {useNormalizedResourceOptions} from '../helpers/useNormalizedResourceOptions'
|
|
6
7
|
import {useTrackHookUsage} from '../helpers/useTrackHookUsage'
|
|
7
8
|
|
|
8
9
|
/**
|
|
@@ -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
|
|
|
@@ -79,7 +80,7 @@ export function useDocumentEvent<
|
|
|
79
80
|
): void {
|
|
80
81
|
useTrackHookUsage('useDocumentEvent')
|
|
81
82
|
// Destructure handler and datasetHandle from options
|
|
82
|
-
const normalizedOptions =
|
|
83
|
+
const normalizedOptions = useNormalizedResourceOptions(options)
|
|
83
84
|
const {onEvent, ...datasetHandle} = normalizedOptions
|
|
84
85
|
const ref = useRef(onEvent)
|
|
85
86
|
|
|
@@ -91,11 +92,11 @@ 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,
|
|
98
|
-
|
|
99
|
+
resource: datasetHandle.resource,
|
|
99
100
|
})
|
|
100
|
-
}, [instance, datasetHandle.
|
|
101
|
+
}, [instance, datasetHandle.resource, stableHandler])
|
|
101
102
|
}
|