Skip to content

Commit f1617da

Browse files
juice49binoy14
andauthored
feat!: update useManageFavorite signature (#360)
* feat!: update `useManageFavorite` signature - chore(deps): update `@sanity/message-protocol` to 0.0.6 * fix: upgrade message protocal package and signature - chore: adds a migration guide --------- Co-authored-by: Binoy Patel <[email protected]>
1 parent 1d52a4f commit f1617da

13 files changed

+184
-52
lines changed

apps/kitchensink-react/src/AppRoutes.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Route, Routes} from 'react-router'
55

66
import Framed from './Comlink/Framed'
77
import ParentApp from './Comlink/ParentApp'
8-
import {DocumentCoreInteractionsRoute} from './DocumentCollection/DocumentCoreInteractionsRoute'
8+
import {DocumentDashboardInteractionsRoute} from './DocumentCollection/DocumentDashboardInteractionsRoute'
99
import {DocumentEditorRoute} from './DocumentCollection/DocumentEditorRoute'
1010
import {DocumentGridRoute} from './DocumentCollection/DocumentGridRoute'
1111
import {DocumentListRoute} from './DocumentCollection/DocumentListRoute'
@@ -52,8 +52,8 @@ const documentCollectionRoutes = [
5252
element: <DocumentProjectionRoute />,
5353
},
5454
{
55-
path: 'document-core-interactions',
56-
element: <DocumentCoreInteractionsRoute />,
55+
path: 'document-dashboard-interactions',
56+
element: <DocumentDashboardInteractionsRoute />,
5757
},
5858
{
5959
path: 'dashboard-context',

apps/kitchensink-react/src/DocumentCollection/DocumentCoreInteractionsRoute.tsx renamed to apps/kitchensink-react/src/DocumentCollection/DocumentDashboardInteractionsRoute.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,18 @@ function ActionButtons({document}: {document: DocumentHandle}) {
1818
unfavorite,
1919
isFavorited,
2020
isConnected: isFavoriteConnected,
21-
} = useManageFavorite(document)
22-
const {recordEvent, isConnected: isHistoryConnected} = useRecordDocumentHistoryEvent(document)
21+
} = useManageFavorite({
22+
documentId: document._id,
23+
documentType: document._type,
24+
resourceType: 'studio',
25+
resourceId: document.resourceId,
26+
})
27+
const {recordEvent, isConnected: isHistoryConnected} = useRecordDocumentHistoryEvent({
28+
documentId: document._id,
29+
documentType: document._type,
30+
resourceType: 'studio',
31+
resourceId: document.resourceId,
32+
})
2333
const {navigateToStudioDocument, isConnected: isNavigateConnected} =
2434
useNavigateToStudioDocument(document)
2535

@@ -53,7 +63,7 @@ function ActionButtons({document}: {document: DocumentHandle}) {
5363
)
5464
}
5565

56-
export function DocumentCoreInteractionsRoute(): JSX.Element {
66+
export function DocumentDashboardInteractionsRoute(): JSX.Element {
5767
const {isPending, data, hasMore, loadMore} = useDocuments({
5868
filter: '_type == "book"',
5969
orderings: [{field: '_updatedAt', direction: 'desc'}],

packages/react/guides/MIGRATION.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
# Migration guide
22

3+
## Migrating to @sanity/sdk-react@0.0.0-rc.2
4+
5+
⚠️ Breaking Changes
6+
7+
### Core to Dashboard Namespace Changes (Internal)
8+
9+
- All Core-related endpoints have been renamed to use the Dashboard namespace
10+
- `core/v1/events/favorite` -> `dashboard/v1/events/favorite/mutate`
11+
- `core/v1/events/history` -> `dashboard/v1/events/history`
12+
- `core/v1/bridge/navigate-to-resource` -> `dashboard/v1/bridge/navigate-to-resource`
13+
- `core/v1/bridge/context` -> `dashboard/v1/bridge/context`
14+
15+
### Hook Parameter Changes
16+
17+
#### `useManageFavorite`
18+
19+
```typescript
20+
// Before
21+
const {favorite, unfavorite, isFavorited} = useManageFavorite({
22+
_id: 'doc123',
23+
_type: 'book',
24+
})
25+
26+
// After
27+
const {favorite, unfavorite, isFavorited} = useManageFavorite({
28+
documentId: 'doc123',
29+
documentType: 'book',
30+
resourceType: 'studio', // Required
31+
resourceId: 'resource123', // Required for non-studio resources
32+
})
33+
```
34+
35+
#### `useRecordDocumentHistoryEvent`
36+
37+
```typescript
38+
// Before
39+
const {recordEvent} = useRecordDocumentHistoryEvent({
40+
_id: 'doc123',
41+
_type: 'book',
42+
})
43+
44+
// After
45+
const {recordEvent} = useRecordDocumentHistoryEvent({
46+
documentId: 'doc123',
47+
documentType: 'book',
48+
resourceType: 'studio', // Required
49+
resourceId: 'resource123', // Required for non-studio resources
50+
})
51+
```
52+
53+
---
54+
355
## Migrating to @sanity/sdk-react@0.0.0-rc.1
456

557
### `results` -> `data`

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.1.0",
59+
"@sanity/message-protocol": "^0.6.0",
6060
"@sanity/sdk": "workspace:*",
6161
"@sanity/types": "^3.78.1",
6262
"react-error-boundary": "^5.0.0",

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ describe('useManageFavorite', () => {
1919
let statusCallback: ((status: Status) => void) | null = null
2020

2121
const mockDocumentHandle = {
22-
_id: 'mock-id',
23-
_type: 'mock-type',
22+
documentId: 'mock-id',
23+
documentType: 'mock-type',
24+
resourceType: 'studio' as const,
2425
}
2526

2627
function createMockNode() {
@@ -55,10 +56,12 @@ describe('useManageFavorite', () => {
5556
result.current.favorite()
5657
})
5758

58-
expect(node.post).toHaveBeenCalledWith('core/v1/events/favorite', {
59+
expect(node.post).toHaveBeenCalledWith('dashboard/v1/events/favorite/mutate', {
5960
documentId: 'mock-id',
6061
documentType: 'mock-type',
6162
eventType: 'added',
63+
resourceType: 'studio',
64+
resourceId: undefined,
6265
})
6366
expect(result.current.isFavorited).toBe(true)
6467
})
@@ -70,10 +73,12 @@ describe('useManageFavorite', () => {
7073
result.current.unfavorite()
7174
})
7275

73-
expect(node.post).toHaveBeenCalledWith('core/v1/events/favorite', {
76+
expect(node.post).toHaveBeenCalledWith('dashboard/v1/events/favorite/mutate', {
7477
documentId: 'mock-id',
7578
documentType: 'mock-type',
7679
eventType: 'removed',
80+
resourceType: 'studio',
81+
resourceId: undefined,
7782
})
7883
expect(result.current.isFavorited).toBe(false)
7984
})

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import {type Status} from '@sanity/comlink'
2-
import {type Events, SDK_CHANNEL_NAME, SDK_NODE_NAME} from '@sanity/message-protocol'
3-
import {type DocumentHandle, type FrameMessage} from '@sanity/sdk'
2+
import {
3+
type CanvasResource,
4+
type Events,
5+
type MediaResource,
6+
SDK_CHANNEL_NAME,
7+
SDK_NODE_NAME,
8+
type StudioResource,
9+
} from '@sanity/message-protocol'
10+
import {type FrameMessage} from '@sanity/sdk'
411
import {useCallback, useState} from 'react'
512

613
import {useWindowConnection} from './useWindowConnection'
@@ -14,27 +21,34 @@ interface ManageFavorite {
1421
isConnected: boolean
1522
}
1623

24+
interface UseManageFavoriteProps {
25+
documentId: string
26+
documentType: string
27+
resourceId?: string
28+
resourceType: StudioResource['type'] | MediaResource['type'] | CanvasResource['type']
29+
}
30+
1731
/**
1832
* @beta
1933
*
2034
* ## useManageFavorite
2135
* This hook provides functionality to add and remove documents from favorites,
2236
* and tracks the current favorite status of the document.
23-
* @category Core UI Communication
37+
* @category Dashboard Communication
2438
* @param documentHandle - The document handle containing document ID and type, like `{_id: '123', _type: 'book'}`
2539
* @returns An object containing:
2640
* - `favorite` - Function to add document to favorites
2741
* - `unfavorite` - Function to remove document from favorites
2842
* - `isFavorited` - Boolean indicating if document is currently favorited
29-
* - `isConnected` - Boolean indicating if connection to Core UI is established
43+
* - `isConnected` - Boolean indicating if connection to Dashboard UI is established
3044
*
3145
* @example
3246
* ```tsx
3347
* function MyDocumentAction(props: DocumentActionProps) {
34-
* const {_id, _type} = props
48+
* const {documentId, documentType} = props
3549
* const {favorite, unfavorite, isFavorited, isConnected} = useManageFavorite({
36-
* _id,
37-
* _type
50+
* documentId,
51+
* documentType
3852
* })
3953
*
4054
* return (
@@ -47,7 +61,12 @@ interface ManageFavorite {
4761
* }
4862
* ```
4963
*/
50-
export function useManageFavorite({_id, _type}: DocumentHandle): ManageFavorite {
64+
export function useManageFavorite({
65+
documentId,
66+
documentType,
67+
resourceId,
68+
resourceType,
69+
}: UseManageFavoriteProps): ManageFavorite {
5170
const [isFavorited, setIsFavorited] = useState(false) // should load this from a comlink fetch
5271
const [status, setStatus] = useState<Status>('idle')
5372
const {sendMessage} = useWindowConnection<Events.FavoriteMessage, FrameMessage>({
@@ -56,17 +75,27 @@ export function useManageFavorite({_id, _type}: DocumentHandle): ManageFavorite
5675
onStatus: setStatus,
5776
})
5877

78+
if (resourceType !== 'studio' && !resourceId) {
79+
throw new Error('resourceId is required for media-library and canvas resources')
80+
}
81+
5982
const handleFavoriteAction = useCallback(
6083
(action: 'added' | 'removed', setFavoriteState: boolean) => {
61-
if (!_id || !_type) return
84+
if (!documentId || !documentType || !resourceType) return
6285

6386
try {
6487
const message: Events.FavoriteMessage = {
65-
type: 'core/v1/events/favorite',
88+
type: 'dashboard/v1/events/favorite/mutate',
6689
data: {
6790
eventType: action,
68-
documentId: _id,
69-
documentType: _type,
91+
documentId,
92+
documentType,
93+
resourceType,
94+
// Resource Id should exist for media-library and canvas resources
95+
resourceId: resourceId!,
96+
},
97+
response: {
98+
success: true,
7099
},
71100
}
72101

@@ -82,7 +111,7 @@ export function useManageFavorite({_id, _type}: DocumentHandle): ManageFavorite
82111
throw error
83112
}
84113
},
85-
[_id, _type, sendMessage],
114+
[documentId, documentType, resourceId, resourceType, sendMessage],
86115
)
87116

88117
const favorite = useCallback(() => handleFavoriteAction('added', true), [handleFavoriteAction])

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ describe('useRecordDocumentHistoryEvent', () => {
1919
let statusCallback: ((status: Status) => void) | null = null
2020

2121
const mockDocumentHandle = {
22-
_id: 'mock-id',
23-
_type: 'mock-type',
22+
documentId: 'mock-id',
23+
documentType: 'mock-type',
24+
resourceType: 'studio' as const,
25+
resourceId: 'mock-resource-id',
2426
}
2527

2628
function createMockNode() {
@@ -58,10 +60,12 @@ describe('useRecordDocumentHistoryEvent', () => {
5860

5961
result.current.recordEvent('viewed')
6062

61-
expect(node.post).toHaveBeenCalledWith('core/v1/events/history', {
63+
expect(node.post).toHaveBeenCalledWith('dashboard/v1/events/history', {
6264
eventType: 'viewed',
6365
documentId: 'mock-id',
6466
documentType: 'mock-type',
67+
resourceType: 'studio',
68+
resourceId: 'mock-resource-id',
6569
})
6670
})
6771

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import {type Status} from '@sanity/comlink'
2-
import {type Events, SDK_CHANNEL_NAME, SDK_NODE_NAME} from '@sanity/message-protocol'
3-
import {type DocumentHandle, type FrameMessage} from '@sanity/sdk'
2+
import {
3+
type CanvasResource,
4+
type Events,
5+
type MediaResource,
6+
SDK_CHANNEL_NAME,
7+
SDK_NODE_NAME,
8+
type StudioResource,
9+
} from '@sanity/message-protocol'
10+
import {type FrameMessage} from '@sanity/sdk'
411
import {useCallback, useState} from 'react'
512

613
import {useWindowConnection} from './useWindowConnection'
@@ -10,6 +17,16 @@ interface DocumentInteractionHistory {
1017
isConnected: boolean
1118
}
1219

20+
/**
21+
* @public
22+
*/
23+
interface UseRecordDocumentHistoryEventProps {
24+
documentId: string
25+
documentType: string
26+
resourceType: StudioResource['type'] | MediaResource['type'] | CanvasResource['type']
27+
resourceId?: string
28+
}
29+
1330
/**
1431
* @public
1532
* Hook for managing document interaction history in Sanity Studio.
@@ -23,10 +40,12 @@ interface DocumentInteractionHistory {
2340
* @example
2441
* ```tsx
2542
* function MyDocumentAction(props: DocumentActionProps) {
26-
* const {_id, _type} = props
43+
* const {documentId, documentType, resourceType, resourceId} = props
2744
* const {recordEvent, isConnected} = useRecordDocumentHistoryEvent({
28-
* _id,
29-
* _type
45+
* documentId,
46+
* documentType,
47+
* resourceType,
48+
* resourceId,
3049
* })
3150
*
3251
* return (
@@ -40,25 +59,33 @@ interface DocumentInteractionHistory {
4059
* ```
4160
*/
4261
export function useRecordDocumentHistoryEvent({
43-
_id,
44-
_type,
45-
}: DocumentHandle): DocumentInteractionHistory {
62+
documentId,
63+
documentType,
64+
resourceType,
65+
resourceId,
66+
}: UseRecordDocumentHistoryEventProps): DocumentInteractionHistory {
4667
const [status, setStatus] = useState<Status>('idle')
4768
const {sendMessage} = useWindowConnection<Events.HistoryMessage, FrameMessage>({
4869
name: SDK_NODE_NAME,
4970
connectTo: SDK_CHANNEL_NAME,
5071
onStatus: setStatus,
5172
})
5273

74+
if (resourceType !== 'studio' && !resourceId) {
75+
throw new Error('resourceId is required for media-library and canvas resources')
76+
}
77+
5378
const recordEvent = useCallback(
5479
(eventType: 'viewed' | 'edited' | 'created' | 'deleted') => {
5580
try {
5681
const message: Events.HistoryMessage = {
57-
type: 'core/v1/events/history',
82+
type: 'dashboard/v1/events/history',
5883
data: {
5984
eventType,
60-
documentId: _id,
61-
documentType: _type,
85+
documentId,
86+
documentType,
87+
resourceType,
88+
resourceId: resourceId!,
6289
},
6390
}
6491

@@ -69,7 +96,7 @@ export function useRecordDocumentHistoryEvent({
6996
throw error
7097
}
7198
},
72-
[_id, _type, sendMessage],
99+
[documentId, documentType, resourceId, resourceType, sendMessage],
73100
)
74101

75102
return {

0 commit comments

Comments
 (0)