|
| 1 | +import {type Status} from '@sanity/comlink' |
| 2 | +import {type DocumentHandle} from '@sanity/sdk' |
| 3 | +import {useCallback, useState} from 'react' |
| 4 | + |
| 5 | +import {useWindowConnection} from '../comlink/useWindowConnection' |
| 6 | +import {useStudioWorkspacesByResourceId} from './useStudioWorkspacesByResourceId' |
| 7 | + |
| 8 | +interface NavigateToResourceMessage { |
| 9 | + type: 'core/v1/bridge/navigate-to-resource' |
| 10 | + data: { |
| 11 | + /** |
| 12 | + * Resource ID |
| 13 | + */ |
| 14 | + resourceId: string |
| 15 | + /** |
| 16 | + * Resource type |
| 17 | + * @example 'application' | 'studio' |
| 18 | + */ |
| 19 | + resourceType: string |
| 20 | + /** |
| 21 | + * Path within the resource to navigate to. |
| 22 | + */ |
| 23 | + path?: string |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +interface NavigateToStudioResult { |
| 28 | + navigateToStudioDocument: () => void |
| 29 | + isConnected: boolean |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * @public |
| 34 | + * Hook that provides a function to navigate to a studio document. |
| 35 | + * @param documentHandle - The document handle containing document ID, type, and resource ID |
| 36 | + * @returns An object containing: |
| 37 | + * - navigateToStudioDocument - Function that when called will navigate to the studio document |
| 38 | + * - isConnected - Boolean indicating if connection to Core UI is established |
| 39 | + */ |
| 40 | +export function useNavigateToStudioDocument( |
| 41 | + documentHandle: DocumentHandle, |
| 42 | +): NavigateToStudioResult { |
| 43 | + const {workspacesByResourceId, isConnected: workspacesConnected} = |
| 44 | + useStudioWorkspacesByResourceId() |
| 45 | + const [status, setStatus] = useState<Status>('idle') |
| 46 | + const {sendMessage} = useWindowConnection<NavigateToResourceMessage, never>({ |
| 47 | + name: 'core/nodes/sdk', |
| 48 | + connectTo: 'core/channels/sdk', |
| 49 | + onStatus: setStatus, |
| 50 | + }) |
| 51 | + |
| 52 | + const navigateToStudioDocument = useCallback(() => { |
| 53 | + if (!workspacesConnected || status !== 'connected' || !documentHandle.resourceId) { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + // Extract projectId and dataset from the resourceId (current format: document:projectId.dataset:documentId) |
| 58 | + const [, projectAndDataset] = documentHandle.resourceId.split(':') |
| 59 | + const [projectId, dataset] = projectAndDataset.split('.') |
| 60 | + if (!projectId || !dataset) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + // Find the workspace for this document |
| 65 | + const workspaces = workspacesByResourceId[`${projectId}:${dataset}`] |
| 66 | + if (!workspaces?.length) { |
| 67 | + // eslint-disable-next-line no-console |
| 68 | + console.warn('No workspace found for document', documentHandle.resourceId) |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + if (workspaces.length > 1) { |
| 73 | + // eslint-disable-next-line no-console |
| 74 | + console.warn('Multiple workspaces found for document', documentHandle.resourceId) |
| 75 | + // eslint-disable-next-line no-console |
| 76 | + console.warn('Using the first one', workspaces[0]) |
| 77 | + } |
| 78 | + |
| 79 | + const workspace = workspaces[0] |
| 80 | + |
| 81 | + const message: NavigateToResourceMessage = { |
| 82 | + type: 'core/v1/bridge/navigate-to-resource', |
| 83 | + data: { |
| 84 | + resourceId: workspace._ref, |
| 85 | + resourceType: 'studio', |
| 86 | + path: `/intent/edit/id=${documentHandle._id};type=${documentHandle._type}`, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + sendMessage(message.type, message.data) |
| 91 | + }, [documentHandle, workspacesConnected, status, sendMessage, workspacesByResourceId]) |
| 92 | + |
| 93 | + return { |
| 94 | + navigateToStudioDocument, |
| 95 | + isConnected: workspacesConnected && status === 'connected', |
| 96 | + } |
| 97 | +} |
0 commit comments