@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
|
@@ -3,78 +3,56 @@ import {useContext} from 'react'
|
|
|
3
3
|
|
|
4
4
|
import {SanityInstanceContext} from '../../context/SanityInstanceContext'
|
|
5
5
|
|
|
6
|
+
const warnedCallers = new Set<string>()
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
|
-
* Retrieves the current Sanity instance
|
|
9
|
+
* Retrieves the current Sanity instance from context
|
|
8
10
|
*
|
|
9
11
|
* @public
|
|
10
12
|
*
|
|
11
13
|
* @category Platform
|
|
12
|
-
* @param config -
|
|
13
|
-
* @returns The current
|
|
14
|
+
* @param config - Deprecated. Formerly used to match against the instance hierarchy.
|
|
15
|
+
* @returns The current Sanity instance
|
|
14
16
|
*
|
|
15
17
|
* @remarks
|
|
16
|
-
* This hook accesses the nearest Sanity instance from the React context.
|
|
17
|
-
* a configuration object, it traverses up the instance hierarchy to find the closest instance
|
|
18
|
-
* that matches the specified configuration using shallow comparison of properties.
|
|
19
|
-
*
|
|
18
|
+
* This hook accesses the nearest Sanity instance from the React context.
|
|
20
19
|
* The hook must be used within a component wrapped by a `ResourceProvider` or `SanityApp`.
|
|
21
20
|
*
|
|
22
|
-
* Use this hook when you need to:
|
|
23
|
-
* - Access the current SanityInstance from context
|
|
24
|
-
* - Find a specific instance with matching project/dataset configuration
|
|
25
|
-
* - Access a parent instance with specific configuration values
|
|
26
|
-
*
|
|
27
21
|
* @example Get the current instance
|
|
28
22
|
* ```tsx
|
|
29
|
-
* // Get the current instance from context
|
|
30
23
|
* const instance = useSanityInstance()
|
|
31
24
|
* console.log(instance.config.projectId)
|
|
32
25
|
* ```
|
|
33
26
|
*
|
|
34
|
-
* @example Find an instance with specific configuration
|
|
35
|
-
* ```tsx
|
|
36
|
-
* // Find an instance matching the given project and dataset
|
|
37
|
-
* const instance = useSanityInstance({
|
|
38
|
-
* projectId: 'abc123',
|
|
39
|
-
* dataset: 'production'
|
|
40
|
-
* })
|
|
41
|
-
*
|
|
42
|
-
* // Use instance for API calls
|
|
43
|
-
* const fetchDocument = (docId) => {
|
|
44
|
-
* // Instance is guaranteed to have the matching config
|
|
45
|
-
* return client.fetch(`*[_id == $id][0]`, { id: docId })
|
|
46
|
-
* }
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* @example Match partial configuration
|
|
50
|
-
* ```tsx
|
|
51
|
-
* // Find an instance with specific auth configuration
|
|
52
|
-
* const instance = useSanityInstance({
|
|
53
|
-
* auth: { requireLogin: true }
|
|
54
|
-
* })
|
|
55
|
-
* ```
|
|
56
|
-
*
|
|
57
27
|
* @throws Error if no SanityInstance is found in context
|
|
58
|
-
* @throws Error if no matching instance is found for the provided config
|
|
59
28
|
*/
|
|
60
|
-
export const useSanityInstance = (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
29
|
+
export const useSanityInstance = (
|
|
30
|
+
/**
|
|
31
|
+
* @deprecated Passing a config to match against the instance hierarchy is deprecated.
|
|
32
|
+
* Use `useSanityInstance()` without arguments instead.
|
|
33
|
+
*/
|
|
34
|
+
config?: SanityConfig,
|
|
35
|
+
): SanityInstance => {
|
|
36
|
+
if (config !== undefined) {
|
|
37
|
+
const caller = new Error().stack?.split('\n')[2]?.trim() ?? 'unknown'
|
|
38
|
+
if (!warnedCallers.has(caller)) {
|
|
39
|
+
warnedCallers.add(caller)
|
|
40
|
+
// eslint-disable-next-line no-console
|
|
41
|
+
console.warn(
|
|
42
|
+
'[useSanityInstance] Passing a config argument is deprecated and has no effect. ' +
|
|
43
|
+
'SDK apps use a single instance for all resources, so the config argument is no longer needed. ' +
|
|
44
|
+
'Call useSanityInstance() without arguments instead, or useResource() to get your currently active resource.',
|
|
45
|
+
)
|
|
46
|
+
}
|
|
67
47
|
}
|
|
68
48
|
|
|
69
|
-
|
|
49
|
+
const instance = useContext(SanityInstanceContext)
|
|
70
50
|
|
|
71
|
-
|
|
72
|
-
if (!match) {
|
|
51
|
+
if (!instance) {
|
|
73
52
|
throw new Error(
|
|
74
|
-
`
|
|
75
|
-
Please ensure there is a ResourceProvider component with a matching configuration in the component hierarchy.`,
|
|
53
|
+
`SanityInstance context not found. Please ensure that your component is wrapped in a ResourceProvider or a SanityApp component.`,
|
|
76
54
|
)
|
|
77
55
|
}
|
|
78
56
|
|
|
79
|
-
return
|
|
57
|
+
return instance
|
|
80
58
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {type DocumentHandle} from '@sanity/sdk'
|
|
2
|
+
import {renderHook as reactRenderHook} from '@testing-library/react'
|
|
2
3
|
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
3
4
|
|
|
4
5
|
import {renderHook} from '../../../test/test-utils'
|
|
@@ -167,11 +168,11 @@ describe('useDispatchIntent', () => {
|
|
|
167
168
|
})
|
|
168
169
|
})
|
|
169
170
|
|
|
170
|
-
it('should send intent message with media library
|
|
171
|
+
it('should send intent message with media library resource', () => {
|
|
171
172
|
const mockMediaLibraryHandle = {
|
|
172
173
|
documentId: 'test-asset-id',
|
|
173
174
|
documentType: 'sanity.asset',
|
|
174
|
-
|
|
175
|
+
resourceName: 'media-library',
|
|
175
176
|
} as const
|
|
176
177
|
|
|
177
178
|
const {result} = renderHook(() =>
|
|
@@ -196,11 +197,11 @@ describe('useDispatchIntent', () => {
|
|
|
196
197
|
})
|
|
197
198
|
})
|
|
198
199
|
|
|
199
|
-
it('should send intent message with canvas
|
|
200
|
+
it('should send intent message with canvas resource', () => {
|
|
200
201
|
const mockCanvasHandle = {
|
|
201
202
|
documentId: 'test-canvas-document-id',
|
|
202
203
|
documentType: 'sanity.canvas.document',
|
|
203
|
-
|
|
204
|
+
resourceName: 'canvas',
|
|
204
205
|
} as const
|
|
205
206
|
|
|
206
207
|
const {result} = renderHook(() =>
|
|
@@ -226,13 +227,15 @@ describe('useDispatchIntent', () => {
|
|
|
226
227
|
})
|
|
227
228
|
|
|
228
229
|
describe('error handling', () => {
|
|
229
|
-
it('should throw error when neither
|
|
230
|
+
it('should throw error when neither resource nor projectId/dataset is provided', () => {
|
|
231
|
+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
|
230
232
|
const invalidHandle = {
|
|
231
233
|
documentId: 'test-document-id',
|
|
232
234
|
documentType: 'test-document-type',
|
|
233
235
|
}
|
|
234
236
|
|
|
235
|
-
|
|
237
|
+
// Use a minimal wrapper with no resource context so the normalization has nothing to fall back to
|
|
238
|
+
const {result} = reactRenderHook(() =>
|
|
236
239
|
useDispatchIntent({
|
|
237
240
|
action: 'edit',
|
|
238
241
|
documentHandle: invalidHandle as unknown as DocumentHandle,
|
|
@@ -240,8 +243,9 @@ describe('useDispatchIntent', () => {
|
|
|
240
243
|
)
|
|
241
244
|
|
|
242
245
|
expect(() => result.current.dispatchIntent()).toThrow(
|
|
243
|
-
'useDispatchIntent: Unable to determine resource. Either `
|
|
246
|
+
'useDispatchIntent: Unable to determine resource. Either `resource`, `resourceName`, or both `projectId` and `dataset` must be provided in documentHandle.',
|
|
244
247
|
)
|
|
248
|
+
consoleErrorSpy.mockRestore()
|
|
245
249
|
})
|
|
246
250
|
})
|
|
247
251
|
})
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {SDK_CHANNEL_NAME, SDK_NODE_NAME} from '@sanity/message-protocol'
|
|
2
|
-
import {type
|
|
2
|
+
import {type FrameMessage} from '@sanity/sdk'
|
|
3
3
|
import {useCallback} from 'react'
|
|
4
4
|
|
|
5
|
+
import {type DocumentHandle} from '../../config/handles'
|
|
5
6
|
import {useWindowConnection} from '../comlink/useWindowConnection'
|
|
6
|
-
import {type WithSourceNameSupport} from '../helpers/useNormalizedSourceOptions'
|
|
7
7
|
import {useResourceIdFromDocumentHandle} from './utils/useResourceIdFromDocumentHandle'
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -42,7 +42,7 @@ interface DispatchIntent {
|
|
|
42
42
|
interface UseDispatchIntentParams {
|
|
43
43
|
action?: 'edit'
|
|
44
44
|
intentId?: string
|
|
45
|
-
documentHandle:
|
|
45
|
+
documentHandle: DocumentHandle
|
|
46
46
|
parameters?: Record<string, unknown>
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -56,8 +56,8 @@ interface UseDispatchIntentParams {
|
|
|
56
56
|
* - `action` - Action to perform (currently only 'edit' is supported). Will prompt a picker if multiple handlers are available.
|
|
57
57
|
* - `intentId` - Specific ID of the intent to dispatch. Either `action` or `intentId` is required.
|
|
58
58
|
* - `documentHandle` - The document handle containing document ID, type, and either:
|
|
59
|
-
* - `projectId` and `dataset` for traditional dataset
|
|
60
|
-
* - `
|
|
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
61
|
* - `paremeters` - Optional parameters to include in the dispatch; will be passed to the resolved intent handler
|
|
62
62
|
* @returns An object containing:
|
|
63
63
|
* - `dispatchIntent` - Function to dispatch the intent message
|
|
@@ -119,10 +119,10 @@ export function useDispatchIntent(params: UseDispatchIntentParams): DispatchInte
|
|
|
119
119
|
)
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
// Validate that we have a resource ID (which is computed from
|
|
122
|
+
// Validate that we have a resource ID (which is computed from resource/resourceName or projectId+dataset)
|
|
123
123
|
if (!resource.id) {
|
|
124
124
|
throw new Error(
|
|
125
|
-
'useDispatchIntent: Unable to determine resource. Either `
|
|
125
|
+
'useDispatchIntent: Unable to determine resource. Either `resource`, `resourceName`, or both `projectId` and `dataset` must be provided in documentHandle.',
|
|
126
126
|
)
|
|
127
127
|
}
|
|
128
128
|
|
|
@@ -2,12 +2,15 @@ import {type Message} from '@sanity/comlink'
|
|
|
2
2
|
import {type FavoriteStatusResponse, getFavoritesState, resolveFavoritesState} from '@sanity/sdk'
|
|
3
3
|
import {act, renderHook} from '@testing-library/react'
|
|
4
4
|
import {BehaviorSubject} from 'rxjs'
|
|
5
|
-
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
5
|
+
import {beforeEach, describe, expect, it, type Mock, vi} from 'vitest'
|
|
6
6
|
|
|
7
7
|
import {ResourceProvider} from '../../context/ResourceProvider'
|
|
8
8
|
import {useWindowConnection, type WindowConnection} from '../comlink/useWindowConnection'
|
|
9
9
|
import {useManageFavorite} from './useManageFavorite'
|
|
10
10
|
|
|
11
|
+
type MockFetch = (type: string, data?: unknown, options?: unknown) => Promise<{success: boolean}>
|
|
12
|
+
type MockSendMessage = (type: string, data?: unknown) => void
|
|
13
|
+
|
|
11
14
|
vi.mock(import('@sanity/sdk'), async (importOriginal) => {
|
|
12
15
|
const actual = await importOriginal()
|
|
13
16
|
return {
|
|
@@ -23,8 +26,8 @@ vi.mock('../comlink/useWindowConnection', () => ({
|
|
|
23
26
|
|
|
24
27
|
describe('useManageFavorite', () => {
|
|
25
28
|
let favoriteStatusSubject: BehaviorSubject<FavoriteStatusResponse>
|
|
26
|
-
let mockFetch:
|
|
27
|
-
let mockSendMessage:
|
|
29
|
+
let mockFetch: Mock<MockFetch>
|
|
30
|
+
let mockSendMessage: Mock<MockSendMessage>
|
|
28
31
|
|
|
29
32
|
const mockDocumentHandle = {
|
|
30
33
|
documentId: 'mock-id',
|
|
@@ -56,15 +59,16 @@ describe('useManageFavorite', () => {
|
|
|
56
59
|
})
|
|
57
60
|
|
|
58
61
|
// Mock useWindowConnection
|
|
59
|
-
mockFetch = vi.fn().mockResolvedValue({success: true})
|
|
60
|
-
mockSendMessage = vi.fn()
|
|
61
|
-
vi.mocked(useWindowConnection).mockImplementation(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
62
|
+
mockFetch = vi.fn<MockFetch>().mockResolvedValue({success: true})
|
|
63
|
+
mockSendMessage = vi.fn<MockSendMessage>()
|
|
64
|
+
vi.mocked(useWindowConnection).mockImplementation(
|
|
65
|
+
() =>
|
|
66
|
+
({
|
|
67
|
+
fetch: (type: string, data?: unknown, options: unknown = {}) =>
|
|
68
|
+
mockFetch(type, data, options),
|
|
69
|
+
sendMessage: mockSendMessage,
|
|
70
|
+
}) as unknown as WindowConnection<Message>,
|
|
71
|
+
)
|
|
68
72
|
})
|
|
69
73
|
|
|
70
74
|
afterEach(() => {
|
|
@@ -23,11 +23,11 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
describe('with DocumentHandleWithSource - media library', () => {
|
|
26
|
-
it('should return media library ID and resourceType when media library
|
|
26
|
+
it('should return media library ID and resourceType when media library resource is provided', () => {
|
|
27
27
|
const documentHandle = {
|
|
28
28
|
documentId: 'test-asset-id',
|
|
29
29
|
documentType: 'sanity.asset',
|
|
30
|
-
|
|
30
|
+
resourceName: 'media-library',
|
|
31
31
|
} as const
|
|
32
32
|
|
|
33
33
|
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
@@ -38,13 +38,13 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
38
38
|
})
|
|
39
39
|
})
|
|
40
40
|
|
|
41
|
-
it('should prioritize
|
|
41
|
+
it('should prioritize resource over projectId/dataset when both are provided', () => {
|
|
42
42
|
const documentHandle = {
|
|
43
43
|
documentId: 'test-asset-id',
|
|
44
44
|
documentType: 'sanity.asset',
|
|
45
45
|
projectId: 'test-project-id',
|
|
46
46
|
dataset: 'test-dataset',
|
|
47
|
-
|
|
47
|
+
resourceName: 'media-library',
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
@@ -57,11 +57,11 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
57
57
|
})
|
|
58
58
|
|
|
59
59
|
describe('with DocumentHandleWithSource - canvas', () => {
|
|
60
|
-
it('should return canvas ID and resourceType when canvas
|
|
60
|
+
it('should return canvas ID and resourceType when canvas resource is provided', () => {
|
|
61
61
|
const documentHandle = {
|
|
62
62
|
documentId: 'test-canvas-document-id',
|
|
63
63
|
documentType: 'sanity.canvas.document',
|
|
64
|
-
|
|
64
|
+
resourceName: 'canvas',
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
@@ -73,48 +73,48 @@ describe('getResourceIdFromDocumentHandle', () => {
|
|
|
73
73
|
})
|
|
74
74
|
})
|
|
75
75
|
|
|
76
|
-
describe('with DocumentHandleWithSource - dataset
|
|
77
|
-
it('should return dataset resource ID when dataset
|
|
76
|
+
describe('with DocumentHandleWithSource - dataset resource', () => {
|
|
77
|
+
it('should return dataset resource ID when dataset resource is provided', () => {
|
|
78
78
|
const documentHandle = {
|
|
79
79
|
documentId: 'test-document-id',
|
|
80
80
|
documentType: 'test-document-type',
|
|
81
|
-
|
|
81
|
+
resourceName: 'dataset',
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
85
85
|
|
|
86
86
|
expect(result.current).toEqual({
|
|
87
|
-
id: '
|
|
87
|
+
id: 'resource-project-id.resource-dataset',
|
|
88
88
|
type: undefined,
|
|
89
89
|
})
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
-
it('should use dataset
|
|
92
|
+
it('should use dataset resource over projectId/dataset when both are provided', () => {
|
|
93
93
|
const documentHandle = {
|
|
94
94
|
documentId: 'test-document-id',
|
|
95
95
|
documentType: 'test-document-type',
|
|
96
96
|
projectId: 'test-project-id',
|
|
97
97
|
dataset: 'test-dataset',
|
|
98
|
-
|
|
98
|
+
resourceName: 'dataset',
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
102
102
|
|
|
103
103
|
expect(result.current).toEqual({
|
|
104
|
-
id: '
|
|
104
|
+
id: 'resource-project-id.resource-dataset',
|
|
105
105
|
type: undefined,
|
|
106
106
|
})
|
|
107
107
|
})
|
|
108
108
|
})
|
|
109
109
|
|
|
110
110
|
describe('edge cases', () => {
|
|
111
|
-
it('should handle DocumentHandleWithSource with undefined
|
|
111
|
+
it('should handle DocumentHandleWithSource with undefined resource', () => {
|
|
112
112
|
const documentHandle = {
|
|
113
113
|
documentId: 'test-document-id',
|
|
114
114
|
documentType: 'test-document-type',
|
|
115
115
|
projectId: 'test-project-id',
|
|
116
116
|
dataset: 'test-dataset',
|
|
117
|
-
|
|
117
|
+
resourceName: undefined,
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
const {result} = renderHook(() => useResourceIdFromDocumentHandle(documentHandle))
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
type DocumentHandle,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
isCanvasResource,
|
|
4
|
+
isDatasetResource,
|
|
5
|
+
isMediaLibraryResource,
|
|
6
6
|
} from '@sanity/sdk'
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import {useNormalizedResourceOptions} from '../../helpers/useNormalizedResourceOptions'
|
|
9
9
|
|
|
10
10
|
interface DashboardMessageResource {
|
|
11
11
|
id: string
|
|
@@ -18,23 +18,19 @@ interface DashboardMessageResource {
|
|
|
18
18
|
export function useResourceIdFromDocumentHandle(
|
|
19
19
|
documentHandle: DocumentHandle,
|
|
20
20
|
): DashboardMessageResource {
|
|
21
|
-
const options =
|
|
22
|
-
const {
|
|
21
|
+
const options = useNormalizedResourceOptions(documentHandle)
|
|
22
|
+
const {resource} = options
|
|
23
23
|
let resourceId: string = ''
|
|
24
24
|
let resourceType: 'media-library' | 'canvas' | undefined
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (source) {
|
|
30
|
-
if (isDatasetSource(source)) {
|
|
31
|
-
resourceId = `${source.projectId}.${source.dataset}`
|
|
25
|
+
if (resource) {
|
|
26
|
+
if (isDatasetResource(resource)) {
|
|
27
|
+
resourceId = `${resource.projectId}.${resource.dataset}`
|
|
32
28
|
resourceType = undefined
|
|
33
|
-
} else if (
|
|
34
|
-
resourceId =
|
|
29
|
+
} else if (isMediaLibraryResource(resource)) {
|
|
30
|
+
resourceId = resource.mediaLibraryId
|
|
35
31
|
resourceType = 'media-library'
|
|
36
|
-
} else if (
|
|
37
|
-
resourceId =
|
|
32
|
+
} else if (isCanvasResource(resource)) {
|
|
33
|
+
resourceId = resource.canvasId
|
|
38
34
|
resourceType = 'canvas'
|
|
39
35
|
}
|
|
40
36
|
}
|