Skip to content

Commit 553b2f5

Browse files
authored
fix(deps): update message-protocol to 0.7.0 (#379)
1 parent 86ec07e commit 553b2f5

File tree

6 files changed

+73
-31
lines changed

6 files changed

+73
-31
lines changed

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"prettier": "@sanity/prettier-config",
5757
"dependencies": {
5858
"@sanity/logos": "^2.1.13",
59-
"@sanity/message-protocol": "^0.6.0",
59+
"@sanity/message-protocol": "^0.7.0",
6060
"@sanity/sdk": "workspace:*",
6161
"@sanity/types": "^3.78.1",
6262
"@types/lodash-es": "^4.17.12",

packages/react/src/hooks/comlink/useManageFavorite.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ describe('useManageFavorite', () => {
5757
})
5858

5959
expect(node.post).toHaveBeenCalledWith('dashboard/v1/events/favorite/mutate', {
60-
documentId: 'mock-id',
61-
documentType: 'mock-type',
60+
document: {
61+
id: 'mock-id',
62+
type: 'mock-type',
63+
resource: {
64+
id: 'test.test',
65+
type: 'studio',
66+
},
67+
},
6268
eventType: 'added',
63-
resourceType: 'studio',
64-
resourceId: undefined,
6569
})
6670
expect(result.current.isFavorited).toBe(true)
6771
})
@@ -74,11 +78,15 @@ describe('useManageFavorite', () => {
7478
})
7579

7680
expect(node.post).toHaveBeenCalledWith('dashboard/v1/events/favorite/mutate', {
77-
documentId: 'mock-id',
78-
documentType: 'mock-type',
81+
document: {
82+
id: 'mock-id',
83+
type: 'mock-type',
84+
resource: {
85+
id: 'test.test',
86+
type: 'studio',
87+
},
88+
},
7989
eventType: 'removed',
80-
resourceType: 'studio',
81-
resourceId: undefined,
8290
})
8391
expect(result.current.isFavorited).toBe(false)
8492
})

packages/react/src/hooks/comlink/useManageFavorite.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {
88
type StudioResource,
99
} from '@sanity/message-protocol'
1010
import {type DocumentHandle, type FrameMessage} from '@sanity/sdk'
11-
import {useCallback, useState} from 'react'
11+
import {useCallback, useEffect, useState} from 'react'
1212

13+
import {useSanityInstance} from '../context/useSanityInstance'
1314
import {useWindowConnection} from './useWindowConnection'
1415

1516
// should we import this whole type from the message protocol?
@@ -62,21 +63,43 @@ interface UseManageFavoriteProps extends DocumentHandle {
6263
export function useManageFavorite({
6364
documentId,
6465
documentType,
65-
resourceId,
66+
projectId: paramProjectId,
67+
dataset: paramDataset,
68+
resourceId: paramResourceId,
6669
resourceType,
6770
}: UseManageFavoriteProps): ManageFavorite {
6871
const [isFavorited, setIsFavorited] = useState(false) // should load this from a comlink fetch
6972
const [status, setStatus] = useState<Status>('idle')
73+
const [resourceId, setResourceId] = useState<string>(paramResourceId || '')
7074
const {sendMessage} = useWindowConnection<Events.FavoriteMessage, FrameMessage>({
7175
name: SDK_NODE_NAME,
7276
connectTo: SDK_CHANNEL_NAME,
7377
onStatus: setStatus,
7478
})
79+
const instance = useSanityInstance()
80+
const {config} = instance
81+
const instanceProjectId = config?.projectId
82+
const instanceDataset = config?.dataset
83+
const projectId = paramProjectId ?? instanceProjectId
84+
const dataset = paramDataset ?? instanceDataset
7585

76-
if (resourceType !== 'studio' && !resourceId) {
77-
throw new Error('resourceId is required for media-library and canvas resources')
86+
if (resourceType === 'studio' && (!projectId || !dataset)) {
87+
throw new Error('projectId and dataset are required for studio resources')
7888
}
7989

90+
useEffect(() => {
91+
// If resourceType is studio and the resourceId is not provided,
92+
// use the projectId and dataset to generate a resourceId
93+
if (resourceType === 'studio' && !paramResourceId) {
94+
setResourceId(`${projectId}.${dataset}`)
95+
} else if (paramResourceId) {
96+
setResourceId(paramResourceId)
97+
} else {
98+
// For other resource types, resourceId is required
99+
throw new Error('resourceId is required for media-library and canvas resources')
100+
}
101+
}, [resourceType, paramResourceId, projectId, dataset])
102+
80103
const handleFavoriteAction = useCallback(
81104
(action: 'added' | 'removed', setFavoriteState: boolean) => {
82105
if (!documentId || !documentType || !resourceType) return
@@ -86,11 +109,14 @@ export function useManageFavorite({
86109
type: 'dashboard/v1/events/favorite/mutate',
87110
data: {
88111
eventType: action,
89-
documentId,
90-
documentType,
91-
resourceType,
92-
// Resource Id should exist for media-library and canvas resources
93-
resourceId: resourceId!,
112+
document: {
113+
id: documentId,
114+
type: documentType,
115+
resource: {
116+
id: resourceId,
117+
type: resourceType,
118+
},
119+
},
94120
},
95121
response: {
96122
success: true,

packages/react/src/hooks/comlink/useRecordDocumentHistoryEvent.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ describe('useRecordDocumentHistoryEvent', () => {
6262

6363
expect(node.post).toHaveBeenCalledWith('dashboard/v1/events/history', {
6464
eventType: 'viewed',
65-
documentId: 'mock-id',
66-
documentType: 'mock-type',
67-
resourceType: 'studio',
68-
resourceId: 'mock-resource-id',
65+
document: {
66+
id: 'mock-id',
67+
type: 'mock-type',
68+
resource: {
69+
id: 'mock-resource-id',
70+
type: 'studio',
71+
},
72+
},
6973
})
7074
})
7175

packages/react/src/hooks/comlink/useRecordDocumentHistoryEvent.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ export function useRecordDocumentHistoryEvent({
8080
type: 'dashboard/v1/events/history',
8181
data: {
8282
eventType,
83-
documentId,
84-
documentType,
85-
resourceType,
86-
resourceId: resourceId!,
83+
document: {
84+
id: documentId,
85+
type: documentType,
86+
resource: {
87+
id: resourceId!,
88+
type: resourceType,
89+
},
90+
},
8791
},
8892
}
8993

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)