@sanity/sdk-react 2.18.0 → 3.0.0-rc.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 +267 -112
- package/dist/index.js +131 -89
- package/dist/index.js.map +1 -1
- package/package.json +11 -9
- package/src/_exports/sdk-react.ts +14 -1
- package/src/components/auth/AuthBoundary.tsx +10 -5
- package/src/context/WorkbenchTokenRefresh.test.tsx +106 -0
- package/src/context/WorkbenchTokenRefresh.tsx +61 -0
- package/src/context/workbenchToken.ts +63 -0
- package/src/hooks/access/useCheckPermissions.test-d.ts +15 -0
- package/src/hooks/access/useCheckPermissions.test.tsx +53 -0
- package/src/hooks/access/useCheckPermissions.ts +24 -0
- package/src/hooks/applications/useApplication.test-d.ts +30 -0
- package/src/hooks/applications/useApplication.ts +22 -0
- package/src/hooks/applications/useApplications.test-d.ts +31 -0
- package/src/hooks/applications/useApplications.ts +25 -0
- package/src/hooks/applications/useCreateUserApplication.test-d.ts +14 -0
- package/src/hooks/applications/useCreateUserApplication.ts +11 -0
- package/src/hooks/applications/useDeleteApplication.test-d.ts +12 -0
- package/src/hooks/applications/useDeleteApplication.ts +11 -0
- package/src/hooks/applications/useDeleteUserApplication.test-d.ts +14 -0
- package/src/hooks/applications/useDeleteUserApplication.ts +11 -0
- package/src/hooks/applications/useUpdateApplication.test-d.ts +12 -0
- package/src/hooks/applications/useUpdateApplication.ts +11 -0
- package/src/hooks/applications/useUpdateUserApplication.test-d.ts +14 -0
- package/src/hooks/applications/useUpdateUserApplication.ts +11 -0
- package/src/hooks/applications/useUserApplication.test-d.ts +11 -0
- package/src/hooks/applications/useUserApplication.ts +14 -0
- package/src/hooks/applications/useUserApplications.test-d.ts +11 -0
- package/src/hooks/applications/useUserApplications.ts +14 -0
- package/src/hooks/helpers/createFetcherHook.test.tsx +180 -0
- package/src/hooks/helpers/createFetcherHook.ts +69 -0
- package/src/hooks/helpers/createMutationHook.test.tsx +125 -0
- package/src/hooks/helpers/createMutationHook.tsx +93 -0
- package/src/hooks/installations/useInstallation.test-d.ts +19 -0
- package/src/hooks/installations/useInstallation.ts +22 -0
- package/src/hooks/installations/useInstallations.test-d.ts +26 -0
- package/src/hooks/installations/useInstallations.ts +25 -0
- package/src/hooks/organizations/useOrganization.test-d.ts +19 -12
- package/src/hooks/organizations/useOrganization.test.ts +50 -52
- package/src/hooks/organizations/useOrganization.ts +13 -19
- package/src/hooks/organizations/useOrganizations.test-d.ts +26 -13
- package/src/hooks/organizations/useOrganizations.test.ts +49 -71
- package/src/hooks/organizations/useOrganizations.ts +14 -20
- package/src/hooks/projects/useProject.test-d.ts +18 -11
- package/src/hooks/projects/useProject.test.tsx +29 -23
- package/src/hooks/projects/useProject.ts +12 -16
- package/src/hooks/projects/useProjects.test-d.ts +22 -11
- package/src/hooks/projects/useProjects.test.ts +45 -98
- package/src/hooks/projects/useProjects.ts +16 -17
- package/src/hooks/dashboard/useDispatchIntent.test.ts +0 -251
- package/src/hooks/dashboard/useDispatchIntent.ts +0 -157
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.test.ts +0 -128
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.ts +0 -42
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
import {type DocumentHandle} from '@sanity/sdk'
|
|
2
|
-
import {renderHook as reactRenderHook} from '@testing-library/react'
|
|
3
|
-
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
4
|
-
|
|
5
|
-
import {renderHook} from '../../../test/test-utils'
|
|
6
|
-
import {useDispatchIntent} from './useDispatchIntent'
|
|
7
|
-
|
|
8
|
-
// Mock the useWindowConnection hook
|
|
9
|
-
const mockSendMessage = vi.fn()
|
|
10
|
-
vi.mock('../comlink/useWindowConnection', () => ({
|
|
11
|
-
useWindowConnection: vi.fn(() => ({
|
|
12
|
-
sendMessage: mockSendMessage,
|
|
13
|
-
})),
|
|
14
|
-
}))
|
|
15
|
-
|
|
16
|
-
describe('useDispatchIntent', () => {
|
|
17
|
-
const mockDocumentHandle: DocumentHandle = {
|
|
18
|
-
documentId: 'test-document-id',
|
|
19
|
-
documentType: 'test-document-type',
|
|
20
|
-
projectId: 'test-project-id',
|
|
21
|
-
dataset: 'test-dataset',
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
vi.clearAllMocks()
|
|
26
|
-
// Reset mock implementation to default behavior
|
|
27
|
-
mockSendMessage.mockImplementation(() => {})
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
it('should return dispatchIntent function', () => {
|
|
31
|
-
const {result} = renderHook(() =>
|
|
32
|
-
useDispatchIntent({action: 'edit', documentHandle: mockDocumentHandle}),
|
|
33
|
-
)
|
|
34
|
-
|
|
35
|
-
expect(result.current).toEqual({
|
|
36
|
-
dispatchIntent: expect.any(Function),
|
|
37
|
-
})
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should throw error when neither action nor intentId is provided', () => {
|
|
41
|
-
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
42
|
-
const {result} = renderHook(() => useDispatchIntent({documentHandle: mockDocumentHandle}))
|
|
43
|
-
|
|
44
|
-
expect(() => result.current.dispatchIntent()).toThrow(
|
|
45
|
-
'useDispatchIntent: Either `action` or `intentId` must be provided.',
|
|
46
|
-
)
|
|
47
|
-
consoleErrorSpy.mockRestore()
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
it('should handle errors gracefully', () => {
|
|
51
|
-
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
52
|
-
mockSendMessage.mockImplementation(() => {
|
|
53
|
-
throw new Error('Test error')
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
const {result} = renderHook(() =>
|
|
57
|
-
useDispatchIntent({action: 'edit', documentHandle: mockDocumentHandle}),
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
expect(() => result.current.dispatchIntent()).toThrow('Test error')
|
|
61
|
-
expect(consoleErrorSpy).toHaveBeenCalledWith('Failed to dispatch intent:', expect.any(Error))
|
|
62
|
-
|
|
63
|
-
consoleErrorSpy.mockRestore()
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it('should use memoized dispatchIntent function', () => {
|
|
67
|
-
const params = {action: 'edit' as const, documentHandle: mockDocumentHandle}
|
|
68
|
-
const {result, rerender} = renderHook(
|
|
69
|
-
({params: hookParams}: {params: typeof params}) => useDispatchIntent(hookParams),
|
|
70
|
-
{
|
|
71
|
-
initialProps: {params},
|
|
72
|
-
},
|
|
73
|
-
)
|
|
74
|
-
|
|
75
|
-
const firstDispatchIntent = result.current.dispatchIntent
|
|
76
|
-
|
|
77
|
-
// Rerender with the same params
|
|
78
|
-
rerender({params: {action: 'edit' as const, documentHandle: mockDocumentHandle}})
|
|
79
|
-
|
|
80
|
-
expect(result.current.dispatchIntent).toBe(firstDispatchIntent)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('should create new dispatchIntent function when documentHandle changes', () => {
|
|
84
|
-
const {result, rerender} = renderHook(
|
|
85
|
-
(params: {action: 'edit'; documentHandle: DocumentHandle}) => useDispatchIntent(params),
|
|
86
|
-
{
|
|
87
|
-
initialProps: {action: 'edit' as const, documentHandle: mockDocumentHandle},
|
|
88
|
-
},
|
|
89
|
-
)
|
|
90
|
-
|
|
91
|
-
const firstDispatchIntent = result.current.dispatchIntent
|
|
92
|
-
|
|
93
|
-
const newDocumentHandle: DocumentHandle = {
|
|
94
|
-
documentId: 'new-document-id',
|
|
95
|
-
documentType: 'new-document-type',
|
|
96
|
-
projectId: 'new-project-id',
|
|
97
|
-
dataset: 'new-dataset',
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
rerender({action: 'edit' as const, documentHandle: newDocumentHandle})
|
|
101
|
-
|
|
102
|
-
expect(result.current.dispatchIntent).not.toBe(firstDispatchIntent)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('should warn if both action and intentId are provided', () => {
|
|
106
|
-
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
107
|
-
const {result} = renderHook(() =>
|
|
108
|
-
useDispatchIntent({
|
|
109
|
-
action: 'edit' as const,
|
|
110
|
-
intentId: 'custom-intent' as never, // test runtime error when both are provided
|
|
111
|
-
documentHandle: mockDocumentHandle,
|
|
112
|
-
}),
|
|
113
|
-
)
|
|
114
|
-
result.current.dispatchIntent()
|
|
115
|
-
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
116
|
-
'useDispatchIntent: Both `action` and `intentId` were provided. Using `intentId` and ignoring `action`.',
|
|
117
|
-
)
|
|
118
|
-
consoleWarnSpy.mockRestore()
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
it('should send intent message with action and params when both are provided', () => {
|
|
122
|
-
const intentParams = {view: 'editor', tab: 'content'}
|
|
123
|
-
const {result} = renderHook(() =>
|
|
124
|
-
useDispatchIntent({
|
|
125
|
-
action: 'edit',
|
|
126
|
-
documentHandle: mockDocumentHandle,
|
|
127
|
-
parameters: intentParams,
|
|
128
|
-
}),
|
|
129
|
-
)
|
|
130
|
-
|
|
131
|
-
result.current.dispatchIntent()
|
|
132
|
-
|
|
133
|
-
expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', {
|
|
134
|
-
action: 'edit',
|
|
135
|
-
document: {
|
|
136
|
-
id: 'test-document-id',
|
|
137
|
-
type: 'test-document-type',
|
|
138
|
-
},
|
|
139
|
-
resource: {
|
|
140
|
-
id: 'test-project-id.test-dataset',
|
|
141
|
-
},
|
|
142
|
-
parameters: intentParams,
|
|
143
|
-
})
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
it('should send intent message with intentId and params when both are provided', () => {
|
|
147
|
-
const intentParams = {view: 'editor', tab: 'content'}
|
|
148
|
-
const {result} = renderHook(() =>
|
|
149
|
-
useDispatchIntent({
|
|
150
|
-
intentId: 'custom-intent',
|
|
151
|
-
documentHandle: mockDocumentHandle,
|
|
152
|
-
parameters: intentParams,
|
|
153
|
-
}),
|
|
154
|
-
)
|
|
155
|
-
|
|
156
|
-
result.current.dispatchIntent()
|
|
157
|
-
|
|
158
|
-
expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', {
|
|
159
|
-
intentId: 'custom-intent',
|
|
160
|
-
document: {
|
|
161
|
-
id: 'test-document-id',
|
|
162
|
-
type: 'test-document-type',
|
|
163
|
-
},
|
|
164
|
-
resource: {
|
|
165
|
-
id: 'test-project-id.test-dataset',
|
|
166
|
-
},
|
|
167
|
-
parameters: intentParams,
|
|
168
|
-
})
|
|
169
|
-
})
|
|
170
|
-
|
|
171
|
-
it('should send intent message with media library resource', () => {
|
|
172
|
-
const mockMediaLibraryHandle = {
|
|
173
|
-
documentId: 'test-asset-id',
|
|
174
|
-
documentType: 'sanity.asset',
|
|
175
|
-
resourceName: 'media-library',
|
|
176
|
-
} as const
|
|
177
|
-
|
|
178
|
-
const {result} = renderHook(() =>
|
|
179
|
-
useDispatchIntent({
|
|
180
|
-
action: 'edit',
|
|
181
|
-
documentHandle: mockMediaLibraryHandle,
|
|
182
|
-
}),
|
|
183
|
-
)
|
|
184
|
-
|
|
185
|
-
result.current.dispatchIntent()
|
|
186
|
-
|
|
187
|
-
expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', {
|
|
188
|
-
action: 'edit',
|
|
189
|
-
document: {
|
|
190
|
-
id: 'test-asset-id',
|
|
191
|
-
type: 'sanity.asset',
|
|
192
|
-
},
|
|
193
|
-
resource: {
|
|
194
|
-
id: 'media-library-id',
|
|
195
|
-
type: 'media-library',
|
|
196
|
-
},
|
|
197
|
-
})
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
it('should send intent message with canvas resource', () => {
|
|
201
|
-
const mockCanvasHandle = {
|
|
202
|
-
documentId: 'test-canvas-document-id',
|
|
203
|
-
documentType: 'sanity.canvas.document',
|
|
204
|
-
resourceName: 'canvas',
|
|
205
|
-
} as const
|
|
206
|
-
|
|
207
|
-
const {result} = renderHook(() =>
|
|
208
|
-
useDispatchIntent({
|
|
209
|
-
action: 'edit',
|
|
210
|
-
documentHandle: mockCanvasHandle,
|
|
211
|
-
}),
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
result.current.dispatchIntent()
|
|
215
|
-
|
|
216
|
-
expect(mockSendMessage).toHaveBeenCalledWith('dashboard/v1/events/intents/dispatch-intent', {
|
|
217
|
-
action: 'edit',
|
|
218
|
-
document: {
|
|
219
|
-
id: 'test-canvas-document-id',
|
|
220
|
-
type: 'sanity.canvas.document',
|
|
221
|
-
},
|
|
222
|
-
resource: {
|
|
223
|
-
id: 'canvas-id',
|
|
224
|
-
type: 'canvas',
|
|
225
|
-
},
|
|
226
|
-
})
|
|
227
|
-
})
|
|
228
|
-
|
|
229
|
-
describe('error handling', () => {
|
|
230
|
-
it('should throw error when neither resource nor projectId/dataset is provided', () => {
|
|
231
|
-
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
232
|
-
const invalidHandle = {
|
|
233
|
-
documentId: 'test-document-id',
|
|
234
|
-
documentType: 'test-document-type',
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Use a minimal wrapper with no resource context so the normalization has nothing to fall back to
|
|
238
|
-
const {result} = reactRenderHook(() =>
|
|
239
|
-
useDispatchIntent({
|
|
240
|
-
action: 'edit',
|
|
241
|
-
documentHandle: invalidHandle as unknown as DocumentHandle,
|
|
242
|
-
}),
|
|
243
|
-
)
|
|
244
|
-
|
|
245
|
-
expect(() => result.current.dispatchIntent()).toThrow(
|
|
246
|
-
'useDispatchIntent: Unable to determine resource. Either `resource`, `resourceName`, or both `projectId` and `dataset` must be provided in documentHandle.',
|
|
247
|
-
)
|
|
248
|
-
consoleErrorSpy.mockRestore()
|
|
249
|
-
})
|
|
250
|
-
})
|
|
251
|
-
})
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import {SDK_CHANNEL_NAME, SDK_NODE_NAME} from '@sanity/message-protocol'
|
|
2
|
-
import {type FrameMessage} from '@sanity/sdk'
|
|
3
|
-
import {useCallback} from 'react'
|
|
4
|
-
|
|
5
|
-
import {type DocumentHandle} from '../../config/handles'
|
|
6
|
-
import {useWindowConnection} from '../comlink/useWindowConnection'
|
|
7
|
-
import {useResourceIdFromDocumentHandle} from './utils/useResourceIdFromDocumentHandle'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Message type for sending intents to the dashboard
|
|
11
|
-
* @beta
|
|
12
|
-
*/
|
|
13
|
-
interface IntentMessage {
|
|
14
|
-
type: 'dashboard/v1/events/intents/dispatch-intent'
|
|
15
|
-
data: {
|
|
16
|
-
action?: 'edit'
|
|
17
|
-
intentId?: string
|
|
18
|
-
document: {
|
|
19
|
-
id: string
|
|
20
|
-
type: string
|
|
21
|
-
}
|
|
22
|
-
resource?: {
|
|
23
|
-
id: string
|
|
24
|
-
type?: 'media-library' | 'canvas'
|
|
25
|
-
}
|
|
26
|
-
parameters?: Record<string, unknown>
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Return type for the useDispatchIntent hook
|
|
32
|
-
* @beta
|
|
33
|
-
*/
|
|
34
|
-
interface DispatchIntent {
|
|
35
|
-
dispatchIntent: () => void
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Parameters for the useDispatchIntent hook
|
|
40
|
-
* @beta
|
|
41
|
-
*/
|
|
42
|
-
interface UseDispatchIntentParams {
|
|
43
|
-
action?: 'edit'
|
|
44
|
-
intentId?: string
|
|
45
|
-
documentHandle: DocumentHandle
|
|
46
|
-
parameters?: Record<string, unknown>
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @beta
|
|
51
|
-
*
|
|
52
|
-
* A hook for dispatching intent messages to the Dashboard with a document handle.
|
|
53
|
-
* This allows applications to signal their intent to pass the referenced document to other applications that have registered the ability to perform specific actions on that document.
|
|
54
|
-
*
|
|
55
|
-
* @param params - Object containing:
|
|
56
|
-
* - `action` - Action to perform (currently only 'edit' is supported). Will prompt a picker if multiple handlers are available.
|
|
57
|
-
* - `intentId` - Specific ID of the intent to dispatch. Either `action` or `intentId` is required.
|
|
58
|
-
* - `documentHandle` - The document handle containing document ID, type, and either:
|
|
59
|
-
* - `projectId` and `dataset` for traditional dataset resources, like `{documentId: '123', documentType: 'book', projectId: 'abc123', dataset: 'production'}`
|
|
60
|
-
* - `resource` for media library, canvas, or dataset resources, like `{documentId: '123', documentType: 'sanity.asset', resource: mediaLibrarySource('ml123')}` or `{documentId: '123', documentType: 'sanity.canvas.document', resource: canvasSource('canvas123')}`
|
|
61
|
-
* - `paremeters` - Optional parameters to include in the dispatch; will be passed to the resolved intent handler
|
|
62
|
-
* @returns An object containing:
|
|
63
|
-
* - `dispatchIntent` - Function to dispatch the intent message
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```tsx
|
|
67
|
-
* import {useDispatchIntent} from '@sanity/sdk-react'
|
|
68
|
-
* import {Button} from '@sanity/ui'
|
|
69
|
-
* import {Suspense} from 'react'
|
|
70
|
-
*
|
|
71
|
-
* function DispatchIntentButton({documentId, documentType, projectId, dataset}) {
|
|
72
|
-
* const {dispatchIntent} = useDispatchIntent({
|
|
73
|
-
* action: 'edit',
|
|
74
|
-
* documentHandle: {documentId, documentType, projectId, dataset},
|
|
75
|
-
* })
|
|
76
|
-
*
|
|
77
|
-
* return (
|
|
78
|
-
* <Button
|
|
79
|
-
* onClick={() => dispatchIntent()}
|
|
80
|
-
* text="Dispatch Intent"
|
|
81
|
-
* />
|
|
82
|
-
* )
|
|
83
|
-
* }
|
|
84
|
-
*
|
|
85
|
-
* // Wrap the component with Suspense since the hook may suspend
|
|
86
|
-
* function MyDocumentAction({documentId, documentType, projectId, dataset}) {
|
|
87
|
-
* return (
|
|
88
|
-
* <Suspense fallback={<Button text="Loading..." disabled />}>
|
|
89
|
-
* <DispatchIntentButton
|
|
90
|
-
* documentId={documentId}
|
|
91
|
-
* documentType={documentType}
|
|
92
|
-
* projectId={projectId}
|
|
93
|
-
* dataset={dataset}
|
|
94
|
-
* />
|
|
95
|
-
* </Suspense>
|
|
96
|
-
* )
|
|
97
|
-
* }
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
export function useDispatchIntent(params: UseDispatchIntentParams): DispatchIntent {
|
|
101
|
-
const {action, intentId, documentHandle, parameters} = params
|
|
102
|
-
const {sendMessage} = useWindowConnection<IntentMessage, FrameMessage>({
|
|
103
|
-
name: SDK_NODE_NAME,
|
|
104
|
-
connectTo: SDK_CHANNEL_NAME,
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
const resource = useResourceIdFromDocumentHandle(documentHandle)
|
|
108
|
-
|
|
109
|
-
const dispatchIntent = useCallback(() => {
|
|
110
|
-
try {
|
|
111
|
-
if (!action && !intentId) {
|
|
112
|
-
throw new Error('useDispatchIntent: Either `action` or `intentId` must be provided.')
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (action && intentId) {
|
|
116
|
-
// eslint-disable-next-line no-console -- warn if both action and intentId are provided
|
|
117
|
-
console.warn(
|
|
118
|
-
'useDispatchIntent: Both `action` and `intentId` were provided. Using `intentId` and ignoring `action`.',
|
|
119
|
-
)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Validate that we have a resource ID (which is computed from resource/resourceName or projectId+dataset)
|
|
123
|
-
if (!resource.id) {
|
|
124
|
-
throw new Error(
|
|
125
|
-
'useDispatchIntent: Unable to determine resource. Either `resource`, `resourceName`, or both `projectId` and `dataset` must be provided in documentHandle.',
|
|
126
|
-
)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const message: IntentMessage = {
|
|
130
|
-
type: 'dashboard/v1/events/intents/dispatch-intent',
|
|
131
|
-
data: {
|
|
132
|
-
...(action && !intentId ? {action} : {}),
|
|
133
|
-
...(intentId ? {intentId} : {}),
|
|
134
|
-
document: {
|
|
135
|
-
id: documentHandle.documentId,
|
|
136
|
-
type: documentHandle.documentType,
|
|
137
|
-
},
|
|
138
|
-
resource: {
|
|
139
|
-
id: resource.id,
|
|
140
|
-
...(resource.type ? {type: resource.type} : {}),
|
|
141
|
-
},
|
|
142
|
-
...(parameters && Object.keys(parameters).length > 0 ? {parameters} : {}),
|
|
143
|
-
},
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
sendMessage(message.type, message.data)
|
|
147
|
-
} catch (error) {
|
|
148
|
-
// eslint-disable-next-line no-console
|
|
149
|
-
console.error('Failed to dispatch intent:', error)
|
|
150
|
-
throw error
|
|
151
|
-
}
|
|
152
|
-
}, [action, intentId, documentHandle, parameters, sendMessage, resource.id, resource.type])
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
dispatchIntent,
|
|
156
|
-
}
|
|
157
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {describe, expect, it} from 'vitest'
|
|
2
|
-
|
|
3
|
-
import {renderHook} from '../../../../test/test-utils'
|
|
4
|
-
import {useResourceIdFromDocumentHandle} from './useResourceIdFromDocumentHandle'
|
|
5
|
-
|
|
6
|
-
describe('getResourceIdFromDocumentHandle', () => {
|
|
7
|
-
describe('with traditional DocumentHandle (projectId/dataset)', () => {
|
|
8
|
-
it('should return resource ID from projectId and dataset', () => {
|
|
9
|
-
const documentHandle = {
|
|
10
|
-
documentId: 'test-document-id',
|
|
11
|
-
documentType: 'test-document-type',
|
|
12
|
-
projectId: 'test-project-id',
|
|
13
|
-
dataset: 'test-dataset',
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
17
|
-
|
|
18
|
-
expect(result.current).toEqual({
|
|
19
|
-
id: 'test-project-id.test-dataset',
|
|
20
|
-
type: undefined,
|
|
21
|
-
})
|
|
22
|
-
})
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
describe('with DocumentHandleWithSource - media library', () => {
|
|
26
|
-
it('should return media library ID and resourceType when media library resource is provided', () => {
|
|
27
|
-
const documentHandle = {
|
|
28
|
-
documentId: 'test-asset-id',
|
|
29
|
-
documentType: 'sanity.asset',
|
|
30
|
-
resourceName: 'media-library',
|
|
31
|
-
} as const
|
|
32
|
-
|
|
33
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
34
|
-
|
|
35
|
-
expect(result.current).toEqual({
|
|
36
|
-
id: 'media-library-id',
|
|
37
|
-
type: 'media-library',
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should prioritize resource over projectId/dataset when both are provided', () => {
|
|
42
|
-
const documentHandle = {
|
|
43
|
-
documentId: 'test-asset-id',
|
|
44
|
-
documentType: 'sanity.asset',
|
|
45
|
-
projectId: 'test-project-id',
|
|
46
|
-
dataset: 'test-dataset',
|
|
47
|
-
resourceName: 'media-library',
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
51
|
-
|
|
52
|
-
expect(result.current).toEqual({
|
|
53
|
-
id: 'media-library-id',
|
|
54
|
-
type: 'media-library',
|
|
55
|
-
})
|
|
56
|
-
})
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
describe('with DocumentHandleWithSource - canvas', () => {
|
|
60
|
-
it('should return canvas ID and resourceType when canvas resource is provided', () => {
|
|
61
|
-
const documentHandle = {
|
|
62
|
-
documentId: 'test-canvas-document-id',
|
|
63
|
-
documentType: 'sanity.canvas.document',
|
|
64
|
-
resourceName: 'canvas',
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
68
|
-
|
|
69
|
-
expect(result.current).toEqual({
|
|
70
|
-
id: 'canvas-id',
|
|
71
|
-
type: 'canvas',
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
describe('with DocumentHandleWithSource - dataset resource', () => {
|
|
77
|
-
it('should return dataset resource ID when dataset resource is provided', () => {
|
|
78
|
-
const documentHandle = {
|
|
79
|
-
documentId: 'test-document-id',
|
|
80
|
-
documentType: 'test-document-type',
|
|
81
|
-
resourceName: 'dataset',
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
85
|
-
|
|
86
|
-
expect(result.current).toEqual({
|
|
87
|
-
id: 'resource-project-id.resource-dataset',
|
|
88
|
-
type: undefined,
|
|
89
|
-
})
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
it('should use dataset resource over projectId/dataset when both are provided', () => {
|
|
93
|
-
const documentHandle = {
|
|
94
|
-
documentId: 'test-document-id',
|
|
95
|
-
documentType: 'test-document-type',
|
|
96
|
-
projectId: 'test-project-id',
|
|
97
|
-
dataset: 'test-dataset',
|
|
98
|
-
resourceName: 'dataset',
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
102
|
-
|
|
103
|
-
expect(result.current).toEqual({
|
|
104
|
-
id: 'resource-project-id.resource-dataset',
|
|
105
|
-
type: undefined,
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
describe('edge cases', () => {
|
|
111
|
-
it('should handle DocumentHandleWithSource with undefined resource', () => {
|
|
112
|
-
const documentHandle = {
|
|
113
|
-
documentId: 'test-document-id',
|
|
114
|
-
documentType: 'test-document-type',
|
|
115
|
-
projectId: 'test-project-id',
|
|
116
|
-
dataset: 'test-dataset',
|
|
117
|
-
resourceName: undefined,
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
121
|
-
|
|
122
|
-
expect(result.current).toEqual({
|
|
123
|
-
id: 'test-project-id.test-dataset',
|
|
124
|
-
type: undefined,
|
|
125
|
-
})
|
|
126
|
-
})
|
|
127
|
-
})
|
|
128
|
-
})
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
type DocumentHandle,
|
|
3
|
-
isCanvasResource,
|
|
4
|
-
isDatasetResource,
|
|
5
|
-
isMediaLibraryResource,
|
|
6
|
-
} from '@sanity/sdk'
|
|
7
|
-
|
|
8
|
-
import {useNormalizedResourceOptions} from '../../helpers/useNormalizedResourceOptions'
|
|
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 options = useNormalizedResourceOptions(documentHandle)
|
|
22
|
-
const {resource} = options
|
|
23
|
-
let resourceId: string = ''
|
|
24
|
-
let resourceType: 'media-library' | 'canvas' | undefined
|
|
25
|
-
if (resource) {
|
|
26
|
-
if (isDatasetResource(resource)) {
|
|
27
|
-
resourceId = `${resource.projectId}.${resource.dataset}`
|
|
28
|
-
resourceType = undefined
|
|
29
|
-
} else if (isMediaLibraryResource(resource)) {
|
|
30
|
-
resourceId = resource.mediaLibraryId
|
|
31
|
-
resourceType = 'media-library'
|
|
32
|
-
} else if (isCanvasResource(resource)) {
|
|
33
|
-
resourceId = resource.canvasId
|
|
34
|
-
resourceType = 'canvas'
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
id: resourceId,
|
|
40
|
-
type: resourceType,
|
|
41
|
-
}
|
|
42
|
-
}
|