@sanity/sdk-react 2.8.0 → 3.0.0-rc.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/README.md +125 -63
- package/dist/index.d.ts +381 -571
- package/dist/index.js +435 -366
- package/dist/index.js.map +1 -1
- package/package.json +7 -9
- package/src/_exports/index.ts +4 -0
- package/src/_exports/sdk-react.ts +16 -0
- package/src/components/SDKProvider.test.tsx +23 -58
- package/src/components/SDKProvider.tsx +38 -30
- package/src/components/SanityApp.test.tsx +12 -68
- package/src/components/SanityApp.tsx +88 -65
- package/src/components/auth/AuthBoundary.test.tsx +8 -26
- package/src/components/auth/LoginError.tsx +5 -5
- package/src/config/handles.ts +53 -0
- package/src/context/ComlinkTokenRefresh.test.tsx +27 -10
- package/src/context/DefaultResourceContext.ts +10 -0
- package/src/context/PerspectiveContext.ts +12 -0
- package/src/context/ResourceProvider.test.tsx +99 -19
- package/src/context/ResourceProvider.tsx +103 -37
- package/src/context/ResourcesContext.tsx +7 -0
- package/src/context/SDKStudioContext.test.tsx +33 -28
- package/src/context/SDKStudioContext.ts +6 -0
- package/src/context/renderSanityApp.test.tsx +49 -151
- package/src/context/renderSanityApp.tsx +8 -12
- package/src/hooks/agent/agentActions.test.tsx +1 -1
- package/src/hooks/agent/agentActions.ts +56 -19
- package/src/hooks/auth/useDashboardOrganizationId.test.tsx +8 -2
- package/src/hooks/auth/useVerifyOrgProjects.test.tsx +32 -8
- package/src/hooks/client/useClient.test.tsx +4 -1
- package/src/hooks/client/useClient.ts +0 -1
- package/src/hooks/context/useDefaultResource.test.tsx +25 -0
- package/src/hooks/context/useDefaultResource.ts +30 -0
- package/src/hooks/context/useSanityInstance.test.tsx +2 -140
- package/src/hooks/context/useSanityInstance.ts +9 -53
- package/src/hooks/dashboard/useDispatchIntent.test.ts +24 -15
- package/src/hooks/dashboard/useDispatchIntent.ts +7 -7
- package/src/hooks/dashboard/useManageFavorite.test.tsx +34 -94
- package/src/hooks/dashboard/useManageFavorite.ts +16 -10
- package/src/hooks/dashboard/useNavigateToStudioDocument.test.ts +7 -5
- package/src/hooks/dashboard/useNavigateToStudioDocument.ts +6 -2
- package/src/hooks/dashboard/useRecordDocumentHistoryEvent.test.ts +2 -0
- package/src/hooks/dashboard/useRecordDocumentHistoryEvent.ts +2 -1
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.test.ts +17 -38
- package/src/hooks/dashboard/utils/useResourceIdFromDocumentHandle.ts +12 -19
- package/src/hooks/datasets/useDatasets.test.ts +8 -22
- package/src/hooks/datasets/useDatasets.ts +8 -16
- package/src/hooks/document/useApplyDocumentActions.test.ts +98 -52
- package/src/hooks/document/useApplyDocumentActions.ts +35 -37
- package/src/hooks/document/useDocument.test.tsx +8 -37
- package/src/hooks/document/useDocument.ts +78 -129
- package/src/hooks/document/useDocumentEvent.test.tsx +7 -19
- package/src/hooks/document/useDocumentEvent.ts +21 -19
- package/src/hooks/document/useDocumentPermissions.test.tsx +75 -84
- package/src/hooks/document/useDocumentPermissions.ts +41 -28
- package/src/hooks/document/useDocumentSyncStatus.test.ts +13 -3
- package/src/hooks/document/useDocumentSyncStatus.ts +19 -14
- package/src/hooks/document/useEditDocument.test.tsx +28 -70
- package/src/hooks/document/useEditDocument.ts +29 -149
- package/src/hooks/documents/useDocuments.test.tsx +44 -64
- package/src/hooks/documents/useDocuments.ts +19 -25
- package/src/hooks/helpers/createCallbackHook.test.tsx +19 -13
- package/src/hooks/helpers/createStateSourceHook.test.tsx +10 -10
- package/src/hooks/helpers/createStateSourceHook.tsx +2 -4
- package/src/hooks/helpers/useNormalizedResourceOptions.test.ts +65 -0
- package/src/hooks/helpers/useNormalizedResourceOptions.ts +127 -0
- package/src/hooks/paginatedDocuments/usePaginatedDocuments.test.tsx +27 -34
- package/src/hooks/paginatedDocuments/usePaginatedDocuments.ts +19 -20
- package/src/hooks/presence/usePresence.test.tsx +71 -9
- package/src/hooks/presence/usePresence.ts +28 -3
- package/src/hooks/preview/useDocumentPreview.test.tsx +85 -193
- package/src/hooks/preview/useDocumentPreview.tsx +42 -62
- package/src/hooks/projection/useDocumentProjection.test.tsx +9 -37
- package/src/hooks/projection/useDocumentProjection.ts +9 -82
- package/src/hooks/projects/useProject.test.ts +1 -2
- package/src/hooks/projects/useProject.ts +7 -8
- package/src/hooks/query/useQuery.test.tsx +5 -6
- package/src/hooks/query/useQuery.ts +12 -91
- package/src/hooks/releases/useActiveReleases.test.tsx +2 -2
- package/src/hooks/releases/useActiveReleases.ts +25 -13
- package/src/hooks/releases/usePerspective.test.tsx +9 -17
- package/src/hooks/releases/usePerspective.ts +29 -18
- package/src/hooks/users/useUser.test.tsx +9 -3
- package/src/hooks/users/useUser.ts +1 -1
- package/src/hooks/users/useUsers.test.tsx +5 -2
- package/src/hooks/users/useUsers.ts +1 -1
- package/src/context/SourcesContext.tsx +0 -7
- package/src/hooks/helpers/useNormalizedSourceOptions.ts +0 -85
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import {type DocumentSource} from '@sanity/sdk'
|
|
2
|
-
import {useContext} from 'react'
|
|
3
|
-
|
|
4
|
-
import {SourcesContext} from '../../context/SourcesContext'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Adds React hook support (sourceName resolution) to core types.
|
|
8
|
-
* This wrapper allows hooks to accept `sourceName` as a convenience,
|
|
9
|
-
* which is then resolved to a `DocumentSource` at the React layer.
|
|
10
|
-
* For now, we are trying to avoid source name resolution in core --
|
|
11
|
-
* functions having sources explicitly passed will reduce complexity.
|
|
12
|
-
*
|
|
13
|
-
* @typeParam T - The core type to extend (must have optional `source` field)
|
|
14
|
-
* @beta
|
|
15
|
-
*/
|
|
16
|
-
export type WithSourceNameSupport<T extends {source?: DocumentSource}> = T & {
|
|
17
|
-
/**
|
|
18
|
-
* Optional name of a source to resolve from context.
|
|
19
|
-
* If provided, will be resolved to a `DocumentSource` via `SourcesContext`.
|
|
20
|
-
* @beta
|
|
21
|
-
*/
|
|
22
|
-
sourceName?: string
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Normalizes hook options by resolving `sourceName` to a `DocumentSource`.
|
|
27
|
-
* This hook ensures that options passed to core layer functions only contain
|
|
28
|
-
* `source` (never `sourceName`), preventing duplicate cache keys and maintaining
|
|
29
|
-
* clean separation between React and core layers.
|
|
30
|
-
*
|
|
31
|
-
* @typeParam T - The options type (must include optional source field)
|
|
32
|
-
* @param options - Hook options that may include `sourceName` and/or `source`
|
|
33
|
-
* @returns Normalized options with `sourceName` removed and `source` resolved
|
|
34
|
-
*
|
|
35
|
-
* @remarks
|
|
36
|
-
* Resolution priority:
|
|
37
|
-
* 1. If `sourceName` is provided, resolves it via `SourcesContext` and uses that
|
|
38
|
-
* 2. Otherwise, uses the inline `source` if provided
|
|
39
|
-
* 3. If neither is provided, returns options without a source field
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* ```tsx
|
|
43
|
-
* function useQuery(options: WithSourceNameSupport<QueryOptions>) {
|
|
44
|
-
* const instance = useSanityInstance(options)
|
|
45
|
-
* const normalized = useNormalizedOptions(options)
|
|
46
|
-
* // normalized now has source but never sourceName
|
|
47
|
-
* const queryKey = getQueryKey(normalized)
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* @beta
|
|
52
|
-
*/
|
|
53
|
-
export function useNormalizedSourceOptions<
|
|
54
|
-
T extends {source?: DocumentSource; sourceName?: string},
|
|
55
|
-
>(options: T): Omit<T, 'sourceName'> {
|
|
56
|
-
const {sourceName, ...rest} = options
|
|
57
|
-
if (sourceName && Object.hasOwn(options, 'source')) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
`Source name ${JSON.stringify(sourceName)} and source ${JSON.stringify(options.source)} cannot be used together.`,
|
|
60
|
-
)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Resolve sourceName to source via context
|
|
64
|
-
const sources = useContext(SourcesContext)
|
|
65
|
-
let resolvedSource: DocumentSource | undefined
|
|
66
|
-
|
|
67
|
-
if (options.source) {
|
|
68
|
-
resolvedSource = options.source
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (sourceName && !Object.hasOwn(sources, sourceName)) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
`There's no source named ${JSON.stringify(sourceName)} in context. Please use <SourceProvider>.`,
|
|
74
|
-
)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (sourceName && sources[sourceName]) {
|
|
78
|
-
resolvedSource = sources[sourceName]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return {
|
|
82
|
-
...rest,
|
|
83
|
-
source: resolvedSource,
|
|
84
|
-
}
|
|
85
|
-
}
|