@sanity/sdk-react 3.0.0-rc.2 → 3.0.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.
Files changed (38) hide show
  1. package/dist/_exports/dashboard.d.ts +209 -0
  2. package/dist/_exports/dashboard.d.ts.map +1 -0
  3. package/dist/_exports/dashboard.js +278 -0
  4. package/dist/_exports/dashboard.js.map +1 -0
  5. package/dist/index.d.ts +86 -261
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +134 -532
  8. package/dist/index.js.map +1 -1
  9. package/dist/useStudioWorkspacesByProjectIdDataset-I4S3CuR5.js +177 -0
  10. package/dist/useStudioWorkspacesByProjectIdDataset-I4S3CuR5.js.map +1 -0
  11. package/package.json +9 -8
  12. package/src/_exports/dashboard.ts +11 -0
  13. package/src/_exports/sdk-react.ts +2 -12
  14. package/src/components/SDKProvider.test.tsx +5 -5
  15. package/src/components/auth/AuthBoundary.tsx +5 -5
  16. package/src/context/{WorkbenchTokenRefresh.test.tsx → DashboardTokenRefresh.test.tsx} +26 -26
  17. package/src/context/{WorkbenchTokenRefresh.tsx → DashboardTokenRefresh.tsx} +14 -14
  18. package/src/context/OrganizationResourcesProvider.test.tsx +9 -9
  19. package/src/context/OrganizationResourcesProvider.tsx +2 -2
  20. package/src/context/{workbenchToken.ts → dashboardToken.ts} +13 -13
  21. package/src/hooks/comlink/useWindowConnection.ts +1 -1
  22. package/src/hooks/{agent → dashboard}/useAgentResourceContext.ts +1 -1
  23. package/src/hooks/dashboard/useFavorite.test.tsx +101 -0
  24. package/src/hooks/dashboard/useFavorite.ts +34 -0
  25. package/src/hooks/dashboard/useFavoriteContext.ts +61 -0
  26. package/src/hooks/dashboard/{useDashboardNavigate.test.ts → useNavigate.test.ts} +3 -3
  27. package/src/hooks/dashboard/{useDashboardNavigate.ts → useNavigate.ts} +5 -5
  28. package/src/hooks/dashboard/useNavigateToStudioDocument.ts +2 -1
  29. package/src/hooks/{auth/useDashboardOrganizationId.test.tsx → dashboard/useOrganizationId.test.tsx} +4 -4
  30. package/src/hooks/{auth/useDashboardOrganizationId.tsx → dashboard/useOrganizationId.tsx} +2 -2
  31. package/src/hooks/dashboard/useUpdateFavorite.test.tsx +146 -0
  32. package/src/hooks/dashboard/useUpdateFavorite.ts +74 -0
  33. package/src/hooks/dashboard/useWindowTitle.ts +1 -1
  34. package/src/hooks/datasets/useDatasets.test.tsx +29 -22
  35. package/src/hooks/datasets/useDatasets.ts +31 -53
  36. package/src/hooks/dashboard/useManageFavorite.test.tsx +0 -379
  37. package/src/hooks/dashboard/useManageFavorite.ts +0 -173
  38. /package/src/hooks/{agent → dashboard}/useAgentResourceContext.test.tsx +0 -0
@@ -1,173 +0,0 @@
1
- import {
2
- type CanvasResource,
3
- type Events,
4
- type MediaResource,
5
- SDK_CHANNEL_NAME,
6
- SDK_NODE_NAME,
7
- type StudioResource,
8
- } from '@sanity/message-protocol'
9
- import {
10
- type DocumentHandle,
11
- type FavoriteStatusResponse,
12
- getFavoritesState,
13
- resolveFavoritesState,
14
- } from '@sanity/sdk'
15
- import {type FrameMessage} from '@sanity/sdk/comlink'
16
- import {useCallback, useMemo, useSyncExternalStore} from 'react'
17
-
18
- import {useWindowConnection} from '../comlink/useWindowConnection'
19
- import {useSanityInstance} from '../context/useSanityInstance'
20
-
21
- interface ManageFavorite extends FavoriteStatusResponse {
22
- favorite: () => Promise<void>
23
- unfavorite: () => Promise<void>
24
- isFavorited: boolean
25
- }
26
-
27
- interface UseManageFavoriteProps extends DocumentHandle {
28
- resourceId?: string
29
- resourceType: StudioResource['type'] | MediaResource['type'] | CanvasResource['type']
30
- /**
31
- * The name of the schema collection this document belongs to.
32
- * Typically is the name of the workspace when used in the context of a studio.
33
- */
34
- schemaName?: string
35
- }
36
-
37
- /**
38
- * @internal
39
- *
40
- * This hook provides functionality to add and remove documents from favorites,
41
- * and tracks the current favorite status of the document.
42
- * @param documentHandle - The document handle containing document ID and type, like `{_id: '123', _type: 'book'}`
43
- * @returns An object containing:
44
- * - `favorite` - Function to add document to favorites
45
- * - `unfavorite` - Function to remove document from favorites
46
- * - `isFavorited` - Boolean indicating if document is currently favorited
47
- *
48
- * @example
49
- * ```tsx
50
- * function FavoriteButton(props: DocumentActionProps) {
51
- * const {documentId, documentType} = props
52
- * const {favorite, unfavorite, isFavorited} = useManageFavorite({
53
- * documentId,
54
- * documentType
55
- * })
56
- *
57
- * return (
58
- * <Button
59
- * onClick={() => isFavorited ? unfavorite() : favorite()}
60
- * text={isFavorited ? 'Remove from favorites' : 'Add to favorites'}
61
- * />
62
- * )
63
- * }
64
- *
65
- * // Wrap the component with Suspense since the hook may suspend
66
- * function MyDocumentAction(props: DocumentActionProps) {
67
- * return (
68
- * <Suspense
69
- * fallback={
70
- * <Button
71
- * text="Loading..."
72
- * disabled
73
- * />
74
- * }
75
- * >
76
- * <FavoriteButton {...props} />
77
- * </Suspense>
78
- * )
79
- * }
80
- * ```
81
- */
82
- export function useManageFavorite({
83
- documentId,
84
- documentType,
85
- projectId: paramProjectId,
86
- dataset: paramDataset,
87
- resourceId: paramResourceId,
88
- resourceType,
89
- schemaName,
90
- }: UseManageFavoriteProps): ManageFavorite {
91
- const {fetch} = useWindowConnection<Events.FavoriteMessage, FrameMessage>({
92
- name: SDK_NODE_NAME,
93
- connectTo: SDK_CHANNEL_NAME,
94
- })
95
- const instance = useSanityInstance()
96
- const {config} = instance
97
- const instanceProjectId = config?.projectId
98
- const instanceDataset = config?.dataset
99
- const projectId = paramProjectId ?? instanceProjectId
100
- const dataset = paramDataset ?? instanceDataset
101
-
102
- if (resourceType === 'studio' && (!projectId || !dataset)) {
103
- throw new Error('projectId and dataset are required for studio resources')
104
- }
105
- // Compute the final resourceId
106
- const resourceId =
107
- resourceType === 'studio' && !paramResourceId ? `${projectId}.${dataset}` : paramResourceId
108
-
109
- if (!resourceId) {
110
- throw new Error('resourceId is required for media-library and canvas resources')
111
- }
112
-
113
- // used for favoriteStore functions like getFavoritesState and resolveFavoritesState
114
- const context = useMemo(
115
- () => ({
116
- documentId,
117
- documentType,
118
- resourceId,
119
- resourceType,
120
- schemaName,
121
- }),
122
- [documentId, documentType, resourceId, resourceType, schemaName],
123
- )
124
-
125
- // Get favorite status using StateSource
126
- const favoriteState = getFavoritesState(instance, context)
127
- const state = useSyncExternalStore(favoriteState.subscribe, favoriteState.getCurrent)
128
-
129
- const isFavorited = state?.isFavorited ?? false
130
-
131
- const handleFavoriteAction = useCallback(
132
- async (action: 'added' | 'removed') => {
133
- if (!fetch || !documentId || !documentType || !resourceType) return
134
-
135
- try {
136
- const payload = {
137
- eventType: action,
138
- document: {
139
- id: documentId,
140
- type: documentType,
141
- resource: {
142
- ...{
143
- id: resourceId,
144
- type: resourceType,
145
- },
146
- ...(schemaName ? {schemaName} : {}),
147
- },
148
- },
149
- }
150
-
151
- const res = await fetch<{success: boolean}>('dashboard/v1/events/favorite/mutate', payload)
152
- if (res.success) {
153
- // Force a re-fetch of the favorite status after successful mutation
154
- await resolveFavoritesState(instance, context)
155
- }
156
- } catch (err) {
157
- // eslint-disable-next-line no-console
158
- console.error(`Failed to ${action === 'added' ? 'favorite' : 'unfavorite'} document:`, err)
159
- throw err
160
- }
161
- },
162
- [fetch, documentId, documentType, resourceId, resourceType, schemaName, instance, context],
163
- )
164
-
165
- const favorite = useCallback(() => handleFavoriteAction('added'), [handleFavoriteAction])
166
- const unfavorite = useCallback(() => handleFavoriteAction('removed'), [handleFavoriteAction])
167
-
168
- return {
169
- favorite,
170
- unfavorite,
171
- isFavorited,
172
- }
173
- }