@sanity/sdk-react 2.4.0 → 2.6.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 +652 -4
- package/dist/index.d.ts +85 -14
- package/dist/index.js +184 -57
- package/dist/index.js.map +1 -1
- package/package.json +9 -8
- package/src/_exports/sdk-react.ts +5 -0
- package/src/components/SDKProvider.tsx +8 -3
- package/src/components/SanityApp.tsx +2 -1
- package/src/context/SourcesContext.tsx +7 -0
- package/src/context/renderSanityApp.test.tsx +355 -0
- package/src/context/renderSanityApp.tsx +48 -0
- package/src/hooks/agent/useAgentResourceContext.test.tsx +245 -0
- package/src/hooks/agent/useAgentResourceContext.ts +106 -0
- package/src/hooks/context/useSource.tsx +34 -0
- package/src/hooks/dashboard/useDispatchIntent.test.ts +25 -22
- package/src/hooks/dashboard/useDispatchIntent.ts +9 -10
- package/src/hooks/dashboard/utils/{getResourceIdFromDocumentHandle.test.ts → useResourceIdFromDocumentHandle.test.ts} +33 -59
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.ts +46 -0
- package/src/hooks/document/useApplyDocumentActions.test.ts +124 -9
- package/src/hooks/document/useApplyDocumentActions.ts +44 -4
- package/src/hooks/document/useDocumentPermissions.test.tsx +3 -3
- package/src/hooks/document/useDocumentPermissions.ts +9 -6
- package/src/hooks/document/useEditDocument.ts +3 -0
- package/src/hooks/documents/useDocuments.ts +3 -2
- package/src/hooks/paginatedDocuments/usePaginatedDocuments.ts +1 -0
- package/src/hooks/query/useQuery.ts +21 -8
- package/src/hooks/releases/usePerspective.test.tsx +1 -0
- package/src/hooks/releases/usePerspective.ts +1 -1
- package/src/hooks/dashboard/types.ts +0 -12
- package/src/hooks/dashboard/utils/getResourceIdFromDocumentHandle.ts +0 -53
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
canvasSource,
|
|
3
|
-
datasetSource,
|
|
4
|
-
type DocumentHandle,
|
|
5
|
-
type DocumentSource,
|
|
6
|
-
mediaLibrarySource,
|
|
7
|
-
} from '@sanity/sdk'
|
|
1
|
+
import {type DocumentHandle} from '@sanity/sdk'
|
|
8
2
|
import {describe, expect, it} from 'vitest'
|
|
9
3
|
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
4
|
+
import {renderHook} from '../../../../test/test-utils'
|
|
5
|
+
import {useResourceIdFromDocumentHandle} from './useResourceIdFromDocumentHandle'
|
|
12
6
|
|
|
13
7
|
describe('getResourceIdFromDocumentHandle', () => {
|
|
14
8
|
describe('with traditional DocumentHandle (projectId/dataset)', () => {
|
|
15
9
|
it('should return resource ID from projectId and dataset', () => {
|
|
16
|
-
const documentHandle
|
|
10
|
+
const documentHandle = {
|
|
17
11
|
documentId: 'test-document-id',
|
|
18
12
|
documentType: 'test-document-type',
|
|
19
13
|
projectId: 'test-project-id',
|
|
20
14
|
dataset: 'test-dataset',
|
|
21
15
|
}
|
|
22
16
|
|
|
23
|
-
const result =
|
|
17
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
24
18
|
|
|
25
|
-
expect(result).toEqual({
|
|
19
|
+
expect(result.current).toEqual({
|
|
26
20
|
id: 'test-project-id.test-dataset',
|
|
27
21
|
type: undefined,
|
|
28
22
|
})
|
|
@@ -31,33 +25,33 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
31
25
|
|
|
32
26
|
describe('with DocumentHandleWithSource - media library', () => {
|
|
33
27
|
it('should return media library ID and resourceType when media library source is provided', () => {
|
|
34
|
-
const documentHandle:
|
|
28
|
+
const documentHandle: DocumentHandle = {
|
|
35
29
|
documentId: 'test-asset-id',
|
|
36
30
|
documentType: 'sanity.asset',
|
|
37
|
-
|
|
31
|
+
sourceName: 'media-library',
|
|
38
32
|
}
|
|
39
33
|
|
|
40
|
-
const result =
|
|
34
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
41
35
|
|
|
42
|
-
expect(result).toEqual({
|
|
43
|
-
id: '
|
|
36
|
+
expect(result.current).toEqual({
|
|
37
|
+
id: 'media-library-id',
|
|
44
38
|
type: 'media-library',
|
|
45
39
|
})
|
|
46
40
|
})
|
|
47
41
|
|
|
48
42
|
it('should prioritize source over projectId/dataset when both are provided', () => {
|
|
49
|
-
const documentHandle
|
|
43
|
+
const documentHandle = {
|
|
50
44
|
documentId: 'test-asset-id',
|
|
51
45
|
documentType: 'sanity.asset',
|
|
52
46
|
projectId: 'test-project-id',
|
|
53
47
|
dataset: 'test-dataset',
|
|
54
|
-
|
|
48
|
+
sourceName: 'media-library',
|
|
55
49
|
}
|
|
56
50
|
|
|
57
|
-
const result =
|
|
51
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
58
52
|
|
|
59
|
-
expect(result).toEqual({
|
|
60
|
-
id: '
|
|
53
|
+
expect(result.current).toEqual({
|
|
54
|
+
id: 'media-library-id',
|
|
61
55
|
type: 'media-library',
|
|
62
56
|
})
|
|
63
57
|
})
|
|
@@ -65,16 +59,16 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
65
59
|
|
|
66
60
|
describe('with DocumentHandleWithSource - canvas', () => {
|
|
67
61
|
it('should return canvas ID and resourceType when canvas source is provided', () => {
|
|
68
|
-
const documentHandle
|
|
62
|
+
const documentHandle = {
|
|
69
63
|
documentId: 'test-canvas-document-id',
|
|
70
64
|
documentType: 'sanity.canvas.document',
|
|
71
|
-
|
|
65
|
+
sourceName: 'canvas',
|
|
72
66
|
}
|
|
73
67
|
|
|
74
|
-
const result =
|
|
68
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
75
69
|
|
|
76
|
-
expect(result).toEqual({
|
|
77
|
-
id: '
|
|
70
|
+
expect(result.current).toEqual({
|
|
71
|
+
id: 'canvas-id',
|
|
78
72
|
type: 'canvas',
|
|
79
73
|
})
|
|
80
74
|
})
|
|
@@ -82,32 +76,32 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
82
76
|
|
|
83
77
|
describe('with DocumentHandleWithSource - dataset source', () => {
|
|
84
78
|
it('should return dataset resource ID when dataset source is provided', () => {
|
|
85
|
-
const documentHandle
|
|
79
|
+
const documentHandle = {
|
|
86
80
|
documentId: 'test-document-id',
|
|
87
81
|
documentType: 'test-document-type',
|
|
88
|
-
|
|
82
|
+
sourceName: 'dataset',
|
|
89
83
|
}
|
|
90
84
|
|
|
91
|
-
const result =
|
|
85
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
92
86
|
|
|
93
|
-
expect(result).toEqual({
|
|
87
|
+
expect(result.current).toEqual({
|
|
94
88
|
id: 'source-project-id.source-dataset',
|
|
95
89
|
type: undefined,
|
|
96
90
|
})
|
|
97
91
|
})
|
|
98
92
|
|
|
99
93
|
it('should use dataset source over projectId/dataset when both are provided', () => {
|
|
100
|
-
const documentHandle
|
|
94
|
+
const documentHandle = {
|
|
101
95
|
documentId: 'test-document-id',
|
|
102
96
|
documentType: 'test-document-type',
|
|
103
97
|
projectId: 'test-project-id',
|
|
104
98
|
dataset: 'test-dataset',
|
|
105
|
-
|
|
99
|
+
sourceName: 'dataset',
|
|
106
100
|
}
|
|
107
101
|
|
|
108
|
-
const result =
|
|
102
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
109
103
|
|
|
110
|
-
expect(result).toEqual({
|
|
104
|
+
expect(result.current).toEqual({
|
|
111
105
|
id: 'source-project-id.source-dataset',
|
|
112
106
|
type: undefined,
|
|
113
107
|
})
|
|
@@ -116,37 +110,17 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
116
110
|
|
|
117
111
|
describe('edge cases', () => {
|
|
118
112
|
it('should handle DocumentHandleWithSource with undefined source', () => {
|
|
119
|
-
const documentHandle
|
|
120
|
-
documentId: 'test-document-id',
|
|
121
|
-
documentType: 'test-document-type',
|
|
122
|
-
projectId: 'test-project-id',
|
|
123
|
-
dataset: 'test-dataset',
|
|
124
|
-
source: undefined,
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const result = getResourceIdFromDocumentHandle(documentHandle)
|
|
128
|
-
|
|
129
|
-
expect(result).toEqual({
|
|
130
|
-
id: 'test-project-id.test-dataset',
|
|
131
|
-
type: undefined,
|
|
132
|
-
})
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
it('should fall back to projectId/dataset when source is not recognized', () => {
|
|
136
|
-
const documentHandle: DocumentHandleWithSource = {
|
|
113
|
+
const documentHandle = {
|
|
137
114
|
documentId: 'test-document-id',
|
|
138
115
|
documentType: 'test-document-type',
|
|
139
116
|
projectId: 'test-project-id',
|
|
140
117
|
dataset: 'test-dataset',
|
|
141
|
-
|
|
142
|
-
__sanity_internal_sourceId: 'unknown-format',
|
|
143
|
-
} as unknown as DocumentSource,
|
|
118
|
+
sourceName: undefined,
|
|
144
119
|
}
|
|
145
120
|
|
|
146
|
-
const result =
|
|
121
|
+
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
147
122
|
|
|
148
|
-
|
|
149
|
-
expect(result).toEqual({
|
|
123
|
+
expect(result.current).toEqual({
|
|
150
124
|
id: 'test-project-id.test-dataset',
|
|
151
125
|
type: undefined,
|
|
152
126
|
})
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type DocumentHandle,
|
|
3
|
+
isCanvasSource,
|
|
4
|
+
isDatasetSource,
|
|
5
|
+
isMediaLibrarySource,
|
|
6
|
+
} from '@sanity/sdk'
|
|
7
|
+
|
|
8
|
+
import {useSource} from '../../context/useSource'
|
|
9
|
+
|
|
10
|
+
interface DashboardMessageResource {
|
|
11
|
+
id: string
|
|
12
|
+
type?: 'media-library' | 'canvas'
|
|
13
|
+
}
|
|
14
|
+
/** Currently only used for dispatching intents to the dashboard,
|
|
15
|
+
* but could easily be extended to other dashboard hooks
|
|
16
|
+
* @beta
|
|
17
|
+
*/
|
|
18
|
+
export function useResourceIdFromDocumentHandle(
|
|
19
|
+
documentHandle: DocumentHandle,
|
|
20
|
+
): DashboardMessageResource {
|
|
21
|
+
const source = useSource(documentHandle)
|
|
22
|
+
const {projectId, dataset} = documentHandle
|
|
23
|
+
let resourceId: string = ''
|
|
24
|
+
let resourceType: 'media-library' | 'canvas' | undefined
|
|
25
|
+
if (projectId && dataset) {
|
|
26
|
+
resourceId = `${projectId}.${dataset}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (source) {
|
|
30
|
+
if (isDatasetSource(source)) {
|
|
31
|
+
resourceId = `${source.projectId}.${source.dataset}`
|
|
32
|
+
resourceType = undefined
|
|
33
|
+
} else if (isMediaLibrarySource(source)) {
|
|
34
|
+
resourceId = source.mediaLibraryId
|
|
35
|
+
resourceType = 'media-library'
|
|
36
|
+
} else if (isCanvasSource(source)) {
|
|
37
|
+
resourceId = source.canvasId
|
|
38
|
+
resourceType = 'canvas'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
id: resourceId,
|
|
44
|
+
type: resourceType,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -1,20 +1,135 @@
|
|
|
1
|
-
import {applyDocumentActions} from '@sanity/sdk'
|
|
1
|
+
import {applyDocumentActions, type SanityInstance} from '@sanity/sdk'
|
|
2
2
|
import {describe, it} from 'vitest'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {useSanityInstance} from '../context/useSanityInstance'
|
|
5
|
+
import {useApplyDocumentActions} from './useApplyDocumentActions'
|
|
5
6
|
|
|
6
|
-
vi.mock('../helpers/createCallbackHook', () => ({
|
|
7
|
-
createCallbackHook: vi.fn((cb) => () => cb),
|
|
8
|
-
}))
|
|
9
7
|
vi.mock('@sanity/sdk', async (importOriginal) => {
|
|
10
8
|
const original = await importOriginal<typeof import('@sanity/sdk')>()
|
|
11
9
|
return {...original, applyDocumentActions: vi.fn()}
|
|
12
10
|
})
|
|
13
11
|
|
|
12
|
+
vi.mock('../context/useSanityInstance')
|
|
13
|
+
|
|
14
|
+
// These are quite fragile mocks, but they are useful enough for now.
|
|
15
|
+
const instances: Record<string, SanityInstance | undefined> = {
|
|
16
|
+
'p123.d': {__id: 'p123.d'} as unknown as SanityInstance,
|
|
17
|
+
'p.d123': {__id: 'p.d123'} as unknown as SanityInstance,
|
|
18
|
+
'p123.d123': {__id: 'p123.d123'} as unknown as SanityInstance,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const instance = {
|
|
22
|
+
match({projectId = 'p', dataset = 'd'}): SanityInstance | undefined {
|
|
23
|
+
return instances[`${projectId}.${dataset}`]
|
|
24
|
+
},
|
|
25
|
+
} as unknown as SanityInstance
|
|
26
|
+
|
|
14
27
|
describe('useApplyDocumentActions', () => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
vi.resetAllMocks()
|
|
30
|
+
vi.mocked(useSanityInstance).mockReturnValueOnce(instance)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('uses the SanityInstance', async () => {
|
|
34
|
+
const apply = useApplyDocumentActions()
|
|
35
|
+
apply({
|
|
36
|
+
type: 'document.edit',
|
|
37
|
+
documentType: 'post',
|
|
38
|
+
documentId: 'abc',
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instance, {
|
|
42
|
+
actions: [
|
|
43
|
+
{
|
|
44
|
+
type: 'document.edit',
|
|
45
|
+
documentType: 'post',
|
|
46
|
+
documentId: 'abc',
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('uses SanityInstance.match when projectId is overrideen', async () => {
|
|
53
|
+
const apply = useApplyDocumentActions()
|
|
54
|
+
apply({
|
|
55
|
+
type: 'document.edit',
|
|
56
|
+
documentType: 'post',
|
|
57
|
+
documentId: 'abc',
|
|
58
|
+
|
|
59
|
+
projectId: 'p123',
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instances['p123.d'], {
|
|
63
|
+
actions: [
|
|
64
|
+
{
|
|
65
|
+
type: 'document.edit',
|
|
66
|
+
documentType: 'post',
|
|
67
|
+
documentId: 'abc',
|
|
68
|
+
|
|
69
|
+
projectId: 'p123',
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('uses SanityInstance when dataset is overrideen', async () => {
|
|
76
|
+
const apply = useApplyDocumentActions()
|
|
77
|
+
apply({
|
|
78
|
+
type: 'document.edit',
|
|
79
|
+
documentType: 'post',
|
|
80
|
+
documentId: 'abc',
|
|
81
|
+
|
|
82
|
+
dataset: 'd123',
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instance, {
|
|
86
|
+
actions: [
|
|
87
|
+
{
|
|
88
|
+
type: 'document.edit',
|
|
89
|
+
documentType: 'post',
|
|
90
|
+
documentId: 'abc',
|
|
91
|
+
|
|
92
|
+
dataset: 'd123',
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('uses SanityInstance.amcth when projectId and dataset is overrideen', async () => {
|
|
99
|
+
const apply = useApplyDocumentActions()
|
|
100
|
+
apply({
|
|
101
|
+
type: 'document.edit',
|
|
102
|
+
documentType: 'post',
|
|
103
|
+
documentId: 'abc',
|
|
104
|
+
|
|
105
|
+
projectId: 'p123',
|
|
106
|
+
dataset: 'd123',
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
expect(applyDocumentActions).toHaveBeenCalledExactlyOnceWith(instances['p123.d123'], {
|
|
110
|
+
actions: [
|
|
111
|
+
{
|
|
112
|
+
type: 'document.edit',
|
|
113
|
+
documentType: 'post',
|
|
114
|
+
documentId: 'abc',
|
|
115
|
+
|
|
116
|
+
projectId: 'p123',
|
|
117
|
+
dataset: 'd123',
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it("throws if SanityInstance.match doesn't find anything", async () => {
|
|
124
|
+
const apply = useApplyDocumentActions()
|
|
125
|
+
expect(() => {
|
|
126
|
+
apply({
|
|
127
|
+
type: 'document.edit',
|
|
128
|
+
documentType: 'post',
|
|
129
|
+
documentId: 'abc',
|
|
130
|
+
|
|
131
|
+
projectId: 'other',
|
|
132
|
+
})
|
|
133
|
+
}).toThrow()
|
|
19
134
|
})
|
|
20
135
|
})
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from '@sanity/sdk'
|
|
7
7
|
import {type SanityDocument} from 'groq'
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import {useSanityInstance} from '../context/useSanityInstance'
|
|
10
10
|
// this import is used in an `{@link useEditDocument}`
|
|
11
11
|
// eslint-disable-next-line unused-imports/no-unused-imports, import/consistent-type-specifier-style
|
|
12
12
|
import type {useEditDocument} from './useEditDocument'
|
|
@@ -150,6 +150,46 @@ interface UseApplyDocumentActions {
|
|
|
150
150
|
* }
|
|
151
151
|
* ```
|
|
152
152
|
*/
|
|
153
|
-
export const useApplyDocumentActions =
|
|
154
|
-
|
|
155
|
-
|
|
153
|
+
export const useApplyDocumentActions: UseApplyDocumentActions = () => {
|
|
154
|
+
const instance = useSanityInstance()
|
|
155
|
+
|
|
156
|
+
return (actionOrActions, options) => {
|
|
157
|
+
const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions]
|
|
158
|
+
|
|
159
|
+
let projectId
|
|
160
|
+
let dataset
|
|
161
|
+
for (const action of actions) {
|
|
162
|
+
if (action.projectId) {
|
|
163
|
+
if (!projectId) projectId = action.projectId
|
|
164
|
+
if (action.projectId !== projectId) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
`Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`,
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (action.dataset) {
|
|
171
|
+
if (!dataset) dataset = action.dataset
|
|
172
|
+
if (action.dataset !== dataset) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`,
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (projectId || dataset) {
|
|
182
|
+
const actualInstance = instance.match({projectId, dataset})
|
|
183
|
+
if (!actualInstance) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`Could not find a matching Sanity instance for the requested action: ${JSON.stringify({projectId, dataset}, null, 2)}.
|
|
186
|
+
Please ensure there is a ResourceProvider component with a matching configuration in the component hierarchy.`,
|
|
187
|
+
)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return applyDocumentActions(actualInstance, {actions, ...options})
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return applyDocumentActions(instance, {actions, ...options})
|
|
194
|
+
}
|
|
195
|
+
}
|
|
@@ -97,7 +97,7 @@ describe('usePermissions', () => {
|
|
|
97
97
|
})
|
|
98
98
|
|
|
99
99
|
// ResourceProvider handles the instance configuration
|
|
100
|
-
expect(getPermissionsState).toHaveBeenCalledWith(expect.any(Object), mockAction)
|
|
100
|
+
expect(getPermissionsState).toHaveBeenCalledWith(expect.any(Object), {actions: [mockAction]})
|
|
101
101
|
expect(result.current).toEqual(mockPermissionAllowed)
|
|
102
102
|
})
|
|
103
103
|
|
|
@@ -140,7 +140,7 @@ describe('usePermissions', () => {
|
|
|
140
140
|
),
|
|
141
141
|
})
|
|
142
142
|
|
|
143
|
-
expect(getPermissionsState).toHaveBeenCalledWith(expect.any(Object), actions)
|
|
143
|
+
expect(getPermissionsState).toHaveBeenCalledWith(expect.any(Object), {actions})
|
|
144
144
|
})
|
|
145
145
|
|
|
146
146
|
it('should throw an error if actions have mismatched project IDs', () => {
|
|
@@ -226,7 +226,7 @@ describe('usePermissions', () => {
|
|
|
226
226
|
|
|
227
227
|
// Now it should render properly
|
|
228
228
|
await waitFor(() => {
|
|
229
|
-
expect(getPermissionsState).toHaveBeenCalledWith(expect.any(Object), mockAction)
|
|
229
|
+
expect(getPermissionsState).toHaveBeenCalledWith(expect.any(Object), {actions: [mockAction]})
|
|
230
230
|
})
|
|
231
231
|
})
|
|
232
232
|
|
|
@@ -84,7 +84,10 @@ import {useSanityInstance} from '../context/useSanityInstance'
|
|
|
84
84
|
export function useDocumentPermissions(
|
|
85
85
|
actionOrActions: DocumentAction | DocumentAction[],
|
|
86
86
|
): DocumentPermissionsResult {
|
|
87
|
-
const actions =
|
|
87
|
+
const actions = useMemo(
|
|
88
|
+
() => (Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions]),
|
|
89
|
+
[actionOrActions],
|
|
90
|
+
)
|
|
88
91
|
// if actions is an array, we need to check that all actions belong to the same project and dataset
|
|
89
92
|
let projectId
|
|
90
93
|
let dataset
|
|
@@ -111,20 +114,20 @@ export function useDocumentPermissions(
|
|
|
111
114
|
|
|
112
115
|
const instance = useSanityInstance({projectId, dataset})
|
|
113
116
|
const isDocumentReady = useCallback(
|
|
114
|
-
() => getPermissionsState(instance,
|
|
115
|
-
[
|
|
117
|
+
() => getPermissionsState(instance, {actions}).getCurrent() !== undefined,
|
|
118
|
+
[actions, instance],
|
|
116
119
|
)
|
|
117
120
|
if (!isDocumentReady()) {
|
|
118
121
|
throw firstValueFrom(
|
|
119
|
-
getPermissionsState(instance,
|
|
122
|
+
getPermissionsState(instance, {actions}).observable.pipe(
|
|
120
123
|
filter((result) => result !== undefined),
|
|
121
124
|
),
|
|
122
125
|
)
|
|
123
126
|
}
|
|
124
127
|
|
|
125
128
|
const {subscribe, getCurrent} = useMemo(
|
|
126
|
-
() => getPermissionsState(instance,
|
|
127
|
-
[
|
|
129
|
+
() => getPermissionsState(instance, {actions}),
|
|
130
|
+
[actions, instance],
|
|
128
131
|
)
|
|
129
132
|
|
|
130
133
|
return useSyncExternalStore(subscribe, getCurrent) as DocumentPermissionsResult
|
|
@@ -100,6 +100,9 @@ export function useEditDocument<TData>(
|
|
|
100
100
|
* 3. **Explicit Type (Full Document):** Edit the entire document with a manually specified type.
|
|
101
101
|
* 4. **Explicit Type (Specific Path):** Edit a specific field with a manually specified type.
|
|
102
102
|
*
|
|
103
|
+
* **LiveEdit Documents:**
|
|
104
|
+
* For documents using {@link DocumentHandle.liveEdit | liveEdit mode} (set via `liveEdit: true` in the document handle), edits are applied directly to the published document without creating a draft.
|
|
105
|
+
*
|
|
103
106
|
* This hook relies on the document state being loaded. If the document is not yet available
|
|
104
107
|
* (e.g., during initial load), the component using this hook will suspend.
|
|
105
108
|
*
|
|
@@ -23,8 +23,8 @@ export interface DocumentsOptions<
|
|
|
23
23
|
TDocumentType extends string = string,
|
|
24
24
|
TDataset extends string = string,
|
|
25
25
|
TProjectId extends string = string,
|
|
26
|
-
>
|
|
27
|
-
|
|
26
|
+
>
|
|
27
|
+
extends DatasetHandle<TDataset, TProjectId>, Pick<QueryOptions, 'perspective' | 'params'> {
|
|
28
28
|
/**
|
|
29
29
|
* Filter documents by their `_type`. Can be a single type or an array of types.
|
|
30
30
|
*/
|
|
@@ -39,6 +39,7 @@ export interface DocumentsOptions<
|
|
|
39
39
|
batchSize?: number
|
|
40
40
|
/**
|
|
41
41
|
* Sorting configuration for the results
|
|
42
|
+
* @beta
|
|
42
43
|
*/
|
|
43
44
|
orderings?: SortOrderingItem[]
|
|
44
45
|
/**
|
|
@@ -9,6 +9,16 @@ import {type SanityQueryResult} from 'groq'
|
|
|
9
9
|
import {useEffect, useMemo, useRef, useState, useSyncExternalStore, useTransition} from 'react'
|
|
10
10
|
|
|
11
11
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
12
|
+
import {useSource} from '../context/useSource'
|
|
13
|
+
|
|
14
|
+
interface UseQueryOptions<
|
|
15
|
+
TQuery extends string = string,
|
|
16
|
+
TDataset extends string = string,
|
|
17
|
+
TProjectId extends string = string,
|
|
18
|
+
TSourceName extends string = string,
|
|
19
|
+
> extends QueryOptions<TQuery, TDataset, TProjectId> {
|
|
20
|
+
sourceName?: TSourceName
|
|
21
|
+
}
|
|
12
22
|
|
|
13
23
|
// Overload 1: Inferred Type (using Typegen)
|
|
14
24
|
/**
|
|
@@ -70,8 +80,9 @@ export function useQuery<
|
|
|
70
80
|
TQuery extends string = string,
|
|
71
81
|
TDataset extends string = string,
|
|
72
82
|
TProjectId extends string = string,
|
|
83
|
+
TSourceName extends string = string,
|
|
73
84
|
>(
|
|
74
|
-
options:
|
|
85
|
+
options: UseQueryOptions<TQuery, TDataset, TProjectId, TSourceName>,
|
|
75
86
|
): {
|
|
76
87
|
/** The query result, typed based on the GROQ query string */
|
|
77
88
|
data: SanityQueryResult<TQuery, `${TProjectId}.${TDataset}`>
|
|
@@ -137,6 +148,8 @@ export function useQuery(options: QueryOptions): {data: unknown; isPending: bool
|
|
|
137
148
|
// Implementation returns unknown, overloads define specifics
|
|
138
149
|
const instance = useSanityInstance(options)
|
|
139
150
|
|
|
151
|
+
const source = useSource(options)
|
|
152
|
+
|
|
140
153
|
// Use React's useTransition to avoid UI jank when queries change
|
|
141
154
|
const [isPending, startTransition] = useTransition()
|
|
142
155
|
|
|
@@ -144,8 +157,6 @@ export function useQuery(options: QueryOptions): {data: unknown; isPending: bool
|
|
|
144
157
|
const queryKey = getQueryKey(options)
|
|
145
158
|
// Use a deferred state to avoid immediate re-renders when the query changes
|
|
146
159
|
const [deferredQueryKey, setDeferredQueryKey] = useState(queryKey)
|
|
147
|
-
// Parse the deferred query key back into a query and options
|
|
148
|
-
const deferred = useMemo(() => parseQueryKey(deferredQueryKey), [deferredQueryKey])
|
|
149
160
|
|
|
150
161
|
// Create an AbortController to cancel in-flight requests when needed
|
|
151
162
|
const ref = useRef<AbortController>(new AbortController())
|
|
@@ -166,10 +177,11 @@ export function useQuery(options: QueryOptions): {data: unknown; isPending: bool
|
|
|
166
177
|
}, [deferredQueryKey, queryKey])
|
|
167
178
|
|
|
168
179
|
// Get the state source for this query from the query store
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
180
|
+
// Memoize the options object by depending on the stable string key instead of the parsed object
|
|
181
|
+
const {getCurrent, subscribe} = useMemo(() => {
|
|
182
|
+
const deferred = parseQueryKey(deferredQueryKey)
|
|
183
|
+
return getQueryState(instance, {...deferred, source})
|
|
184
|
+
}, [instance, deferredQueryKey, source])
|
|
173
185
|
|
|
174
186
|
// If data isn't available yet, suspend rendering
|
|
175
187
|
if (getCurrent() === undefined) {
|
|
@@ -182,8 +194,9 @@ export function useQuery(options: QueryOptions): {data: unknown; isPending: bool
|
|
|
182
194
|
// the captured signal remains unchanged for this suspended render.
|
|
183
195
|
// Thus, the promise thrown here uses a stable abort signal, ensuring correct behavior.
|
|
184
196
|
const currentSignal = ref.current.signal
|
|
197
|
+
const deferred = parseQueryKey(deferredQueryKey)
|
|
185
198
|
|
|
186
|
-
throw resolveQuery(instance, {...deferred, signal: currentSignal})
|
|
199
|
+
throw resolveQuery(instance, {...deferred, source, signal: currentSignal})
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
// Subscribe to updates and get the current data
|
|
@@ -43,7 +43,7 @@ export const usePerspective: UsePerspective = createStateSourceHook({
|
|
|
43
43
|
instance: SanityInstance,
|
|
44
44
|
perspectiveHandle?: PerspectiveHandle,
|
|
45
45
|
) => StateSource<string | string[]>,
|
|
46
|
-
shouldSuspend: (instance: SanityInstance, options
|
|
46
|
+
shouldSuspend: (instance: SanityInstance, options: PerspectiveHandle): boolean =>
|
|
47
47
|
getPerspectiveState(instance, options).getCurrent() === undefined,
|
|
48
48
|
suspender: (instance: SanityInstance, _options?: PerspectiveHandle) =>
|
|
49
49
|
firstValueFrom(getActiveReleasesState(instance).observable.pipe(filter(Boolean))),
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import {type DocumentHandle, type DocumentSource} from '@sanity/sdk'
|
|
2
|
-
/**
|
|
3
|
-
* Document handle that optionally includes a source (e.g., media library source)
|
|
4
|
-
* or projectId and dataset for traditional dataset sources
|
|
5
|
-
* (but now marked optional since it's valid to just use a source)
|
|
6
|
-
* @beta
|
|
7
|
-
*/
|
|
8
|
-
export interface DocumentHandleWithSource extends Omit<DocumentHandle, 'projectId' | 'dataset'> {
|
|
9
|
-
source?: DocumentSource
|
|
10
|
-
projectId?: string
|
|
11
|
-
dataset?: string
|
|
12
|
-
}
|