@sanity/sdk-react 2.7.0 → 2.9.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/dist/index.d.ts +144 -25
- package/dist/index.js +248 -139
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/_exports/sdk-react.ts +1 -0
- package/src/components/SanityApp.tsx +1 -0
- package/src/components/auth/AuthBoundary.test.tsx +3 -0
- package/src/components/auth/LoginError.test.tsx +5 -0
- package/src/components/auth/LoginError.tsx +22 -1
- package/src/context/ResourceProvider.test.tsx +7 -1
- package/src/context/ResourceProvider.tsx +6 -0
- package/src/context/SDKStudioContext.ts +6 -0
- package/src/hooks/dashboard/useDispatchIntent.test.ts +2 -0
- package/src/hooks/dashboard/useWindowTitle.test.ts +213 -0
- package/src/hooks/dashboard/useWindowTitle.ts +112 -0
- package/src/hooks/document/useApplyDocumentActions.test.ts +113 -10
- package/src/hooks/document/useApplyDocumentActions.ts +99 -3
- package/src/hooks/document/useDocument.ts +22 -6
- package/src/hooks/document/useDocumentEvent.test.tsx +3 -3
- package/src/hooks/document/useDocumentEvent.ts +10 -3
- package/src/hooks/document/useDocumentPermissions.test.tsx +86 -2
- package/src/hooks/document/useDocumentPermissions.ts +22 -0
- package/src/hooks/document/useDocumentSyncStatus.test.ts +13 -2
- package/src/hooks/document/useDocumentSyncStatus.ts +14 -5
- package/src/hooks/document/useEditDocument.ts +34 -8
- package/src/hooks/documents/useDocuments.ts +2 -0
- package/src/hooks/helpers/useNormalizedSourceOptions.ts +50 -28
- package/src/hooks/helpers/useTrackHookUsage.ts +37 -0
- package/src/hooks/paginatedDocuments/usePaginatedDocuments.ts +2 -0
- package/src/hooks/presence/usePresence.ts +2 -0
- package/src/hooks/preview/useDocumentPreview.test.tsx +84 -193
- package/src/hooks/preview/useDocumentPreview.tsx +39 -55
- package/src/hooks/projection/useDocumentProjection.ts +2 -0
- package/src/hooks/query/useQuery.ts +2 -0
- package/src/hooks/releases/useActiveReleases.ts +32 -13
- package/src/hooks/releases/usePerspective.ts +26 -14
- package/src/hooks/users/useUser.ts +2 -0
- package/src/hooks/users/useUsers.ts +2 -0
package/dist/index.d.ts
CHANGED
|
@@ -28,7 +28,6 @@ import {GetUsersOptions} from '@sanity/sdk'
|
|
|
28
28
|
import {JsonMatch} from '@sanity/sdk'
|
|
29
29
|
import {MediaResource} from '@sanity/message-protocol'
|
|
30
30
|
import {PathChangeMessage} from '@sanity/message-protocol'
|
|
31
|
-
import {PerspectiveHandle} from '@sanity/sdk'
|
|
32
31
|
import {PreviewValue} from '@sanity/sdk'
|
|
33
32
|
import {ProjectHandle} from '@sanity/sdk'
|
|
34
33
|
import {PropsWithChildren} from 'react'
|
|
@@ -689,6 +688,12 @@ export declare interface StudioWorkspaceHandle {
|
|
|
689
688
|
projectId: string
|
|
690
689
|
/** The dataset name for this workspace. */
|
|
691
690
|
dataset: string
|
|
691
|
+
/**
|
|
692
|
+
* Whether the Studio has determined the user is authenticated.
|
|
693
|
+
* When `true` and the token source emits `null`, the SDK infers
|
|
694
|
+
* cookie-based auth is in use and skips the logged-out state.
|
|
695
|
+
*/
|
|
696
|
+
authenticated?: boolean
|
|
692
697
|
/** Authentication state for this workspace. */
|
|
693
698
|
auth: {
|
|
694
699
|
/**
|
|
@@ -723,13 +728,6 @@ declare interface Subscription {
|
|
|
723
728
|
|
|
724
729
|
declare type Updater<TValue> = TValue | ((currentValue: TValue) => TValue)
|
|
725
730
|
|
|
726
|
-
/**
|
|
727
|
-
* @public
|
|
728
|
-
*/
|
|
729
|
-
declare type UseActiveReleases = {
|
|
730
|
-
(): ReleaseDocument[]
|
|
731
|
-
}
|
|
732
|
-
|
|
733
731
|
/**
|
|
734
732
|
* @public
|
|
735
733
|
|
|
@@ -745,6 +743,14 @@ declare type UseActiveReleases = {
|
|
|
745
743
|
* const activeReleases = useActiveReleases()
|
|
746
744
|
* ```
|
|
747
745
|
*/
|
|
746
|
+
declare type UseActiveReleases = {
|
|
747
|
+
(options?: WithSourceNameSupport<SanityConfig> | undefined): ReleaseDocument[]
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* @public
|
|
752
|
+
* @function
|
|
753
|
+
*/
|
|
748
754
|
export declare const useActiveReleases: UseActiveReleases
|
|
749
755
|
|
|
750
756
|
/**
|
|
@@ -1280,7 +1286,7 @@ declare interface UseApplyDocumentActions {
|
|
|
1280
1286
|
action:
|
|
1281
1287
|
| DocumentAction<TDocumentType, TDataset, TProjectId>
|
|
1282
1288
|
| DocumentAction<TDocumentType, TDataset, TProjectId>[],
|
|
1283
|
-
options?: ApplyDocumentActionsOptions
|
|
1289
|
+
options?: WithSourceNameSupport<ApplyDocumentActionsOptions>,
|
|
1284
1290
|
) => Promise<ActionsResult<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>>>
|
|
1285
1291
|
}
|
|
1286
1292
|
|
|
@@ -1406,6 +1412,66 @@ declare interface UseApplyDocumentActions {
|
|
|
1406
1412
|
* return <button onClick={handleCreateArticle}>Create Article</button>
|
|
1407
1413
|
* }
|
|
1408
1414
|
* ```
|
|
1415
|
+
*
|
|
1416
|
+
* @example Create a new document in a release
|
|
1417
|
+
* ```tsx
|
|
1418
|
+
* import {
|
|
1419
|
+
* createDocument,
|
|
1420
|
+
* createDocumentHandle,
|
|
1421
|
+
* useApplyDocumentActions
|
|
1422
|
+
* } from '@sanity/sdk-react'
|
|
1423
|
+
*
|
|
1424
|
+
* function CreateArticleButton() {
|
|
1425
|
+
* const apply = useApplyDocumentActions()
|
|
1426
|
+
*
|
|
1427
|
+
* const handleCreateArticle = () => {
|
|
1428
|
+
* // Use any valid document ID — not the internal `versions.<releaseName>.<id>` format or "drafts.<id>" format.
|
|
1429
|
+
* // New documents must be explicitly created with `createDocument` to become part of a release.
|
|
1430
|
+
* const newDocHandle = createDocumentHandle({
|
|
1431
|
+
* documentId: crypto.randomUUID(), // or the existing document ID you want to make a release version of
|
|
1432
|
+
* documentType: 'article',
|
|
1433
|
+
* perspective: {releaseName: 'summer-drop'},
|
|
1434
|
+
* })
|
|
1435
|
+
*
|
|
1436
|
+
* apply(
|
|
1437
|
+
* createDocument(newDocHandle, {
|
|
1438
|
+
* title: 'New Article',
|
|
1439
|
+
* author: 'John Doe',
|
|
1440
|
+
* publishedAt: new Date().toISOString(),
|
|
1441
|
+
* })
|
|
1442
|
+
* )
|
|
1443
|
+
* }
|
|
1444
|
+
*
|
|
1445
|
+
* return <button onClick={handleCreateArticle}>Create Article</button>
|
|
1446
|
+
* }
|
|
1447
|
+
* ```
|
|
1448
|
+
*
|
|
1449
|
+
* @example Edit an existing document in a release
|
|
1450
|
+
* ```tsx
|
|
1451
|
+
* import {
|
|
1452
|
+
* editDocument,
|
|
1453
|
+
* createDocumentHandle,
|
|
1454
|
+
* useApplyDocumentActions
|
|
1455
|
+
* } from '@sanity/sdk-react'
|
|
1456
|
+
*
|
|
1457
|
+
* function EditArticleInReleaseButton({documentId}: {documentId: string}) {
|
|
1458
|
+
* const apply = useApplyDocumentActions()
|
|
1459
|
+
*
|
|
1460
|
+
* const handleEdit = () => {
|
|
1461
|
+
* // Pass the document's regular ID — not `versions.<releaseName>.<id>`.
|
|
1462
|
+
* // Documents that already have a version in the release can be edited directly with `editDocument`.
|
|
1463
|
+
* const docHandle = createDocumentHandle({
|
|
1464
|
+
* documentId,
|
|
1465
|
+
* documentType: 'article',
|
|
1466
|
+
* perspective: {releaseName: 'summer-drop'},
|
|
1467
|
+
* })
|
|
1468
|
+
*
|
|
1469
|
+
* apply(editDocument(docHandle, {title: 'Updated for release'}))
|
|
1470
|
+
* }
|
|
1471
|
+
*
|
|
1472
|
+
* return <button onClick={handleEdit}>Edit in Release</button>
|
|
1473
|
+
* }
|
|
1474
|
+
* ```
|
|
1409
1475
|
*/
|
|
1410
1476
|
export declare const useApplyDocumentActions: UseApplyDocumentActions
|
|
1411
1477
|
|
|
@@ -1665,7 +1731,7 @@ declare interface UseDispatchIntentParams {
|
|
|
1665
1731
|
declare interface UseDocument {
|
|
1666
1732
|
/** @internal */
|
|
1667
1733
|
<TDocumentType extends string, TDataset extends string, TProjectId extends string = string>(
|
|
1668
|
-
options:
|
|
1734
|
+
options: UseDocumentOptions<undefined, TDocumentType, TDataset, TProjectId>,
|
|
1669
1735
|
): {
|
|
1670
1736
|
data: SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`> | null
|
|
1671
1737
|
}
|
|
@@ -1676,7 +1742,7 @@ declare interface UseDocument {
|
|
|
1676
1742
|
TDataset extends string = string,
|
|
1677
1743
|
TProjectId extends string = string,
|
|
1678
1744
|
>(
|
|
1679
|
-
options:
|
|
1745
|
+
options: UseDocumentOptions<TPath, TDocumentType>,
|
|
1680
1746
|
): {
|
|
1681
1747
|
data: JsonMatch<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>, TPath> | undefined
|
|
1682
1748
|
}
|
|
@@ -1756,7 +1822,7 @@ declare interface UseDocument {
|
|
|
1756
1822
|
TDataset extends string = string,
|
|
1757
1823
|
TProjectId extends string = string,
|
|
1758
1824
|
>(
|
|
1759
|
-
options:
|
|
1825
|
+
options: UseDocumentOptions<TPath, TDocumentType>,
|
|
1760
1826
|
): TPath extends string
|
|
1761
1827
|
? {
|
|
1762
1828
|
data:
|
|
@@ -1827,7 +1893,7 @@ declare interface UseDocument {
|
|
|
1827
1893
|
* @inlineType DocumentOptions
|
|
1828
1894
|
*/
|
|
1829
1895
|
<TData, TPath extends string>(
|
|
1830
|
-
options:
|
|
1896
|
+
options: UseDocumentOptions<TPath>,
|
|
1831
1897
|
): TPath extends string
|
|
1832
1898
|
? {
|
|
1833
1899
|
data: TData | undefined
|
|
@@ -1838,7 +1904,7 @@ declare interface UseDocument {
|
|
|
1838
1904
|
/**
|
|
1839
1905
|
* @internal
|
|
1840
1906
|
*/
|
|
1841
|
-
(options:
|
|
1907
|
+
(options: UseDocumentOptions): {
|
|
1842
1908
|
data: unknown
|
|
1843
1909
|
}
|
|
1844
1910
|
}
|
|
@@ -1938,6 +2004,13 @@ declare interface UseDocumentEventOptions<
|
|
|
1938
2004
|
onEvent: (documentEvent: DocumentEvent) => void
|
|
1939
2005
|
}
|
|
1940
2006
|
|
|
2007
|
+
declare type UseDocumentOptions<
|
|
2008
|
+
TPath extends string | undefined = undefined,
|
|
2009
|
+
TDocumentType extends string = string,
|
|
2010
|
+
TDataset extends string = string,
|
|
2011
|
+
TProjectId extends string = string,
|
|
2012
|
+
> = WithSourceNameSupport<DocumentOptions<TPath, TDocumentType, TDataset, TProjectId>>
|
|
2013
|
+
|
|
1941
2014
|
/**
|
|
1942
2015
|
*
|
|
1943
2016
|
* @public
|
|
@@ -2023,7 +2096,7 @@ export declare function useDocumentPermissions(
|
|
|
2023
2096
|
* @public
|
|
2024
2097
|
*
|
|
2025
2098
|
* Attempts to infer preview values of a document (specified via a `DocumentHandle`),
|
|
2026
|
-
* including the document
|
|
2099
|
+
* including the document's `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.
|
|
2027
2100
|
* To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview
|
|
2028
2101
|
* resolution will only occur if the `ref` is intersecting the current viewport.
|
|
2029
2102
|
*
|
|
@@ -2032,9 +2105,13 @@ export declare function useDocumentPermissions(
|
|
|
2032
2105
|
* @remarks
|
|
2033
2106
|
* Values returned by this hook may not be as expected. It is currently unable to read preview values as defined in your schema;
|
|
2034
2107
|
* instead, it attempts to infer these preview values by checking against a basic set of potential fields on your document.
|
|
2035
|
-
* We are anticipating being able to significantly improve this hook
|
|
2108
|
+
* We are anticipating being able to significantly improve this hook's functionality and output in a future release.
|
|
2036
2109
|
* For now, we recommend using {@link useDocumentProjection} for rendering individual document fields (or projections of those fields).
|
|
2037
2110
|
*
|
|
2111
|
+
* Internally, this hook is implemented as a specialized projection with post-processing logic.
|
|
2112
|
+
* It uses a fixed GROQ projection to fetch common preview fields (title, subtitle, media) and
|
|
2113
|
+
* transforms the results into the PreviewValue format.
|
|
2114
|
+
*
|
|
2038
2115
|
* @category Documents
|
|
2039
2116
|
* @param options - The document handle for the document you want to infer preview values for, and an optional ref
|
|
2040
2117
|
* @returns The inferred values for the given document and a boolean to indicate whether the resolution is pending
|
|
@@ -2080,7 +2157,7 @@ export declare function useDocumentPreview({
|
|
|
2080
2157
|
* @public
|
|
2081
2158
|
* @category Types
|
|
2082
2159
|
*/
|
|
2083
|
-
export declare interface useDocumentPreviewOptions extends DocumentHandle {
|
|
2160
|
+
export declare interface useDocumentPreviewOptions extends WithSourceNameSupport<DocumentHandle> {
|
|
2084
2161
|
/**
|
|
2085
2162
|
* Optional ref object to track visibility. When provided, preview resolution
|
|
2086
2163
|
* only occurs when the referenced element is visible in the viewport.
|
|
@@ -2093,7 +2170,7 @@ export declare interface useDocumentPreviewOptions extends DocumentHandle {
|
|
|
2093
2170
|
* @category Types
|
|
2094
2171
|
*/
|
|
2095
2172
|
export declare interface useDocumentPreviewResults {
|
|
2096
|
-
/** The results of inferring the document
|
|
2173
|
+
/** The results of inferring the document's preview values */
|
|
2097
2174
|
data: PreviewValue
|
|
2098
2175
|
/** True when inferred preview values are being refreshed */
|
|
2099
2176
|
isPending: boolean
|
|
@@ -2805,13 +2882,6 @@ export declare function usePaginatedDocuments<
|
|
|
2805
2882
|
TProjectId
|
|
2806
2883
|
>
|
|
2807
2884
|
|
|
2808
|
-
/**
|
|
2809
|
-
* @public
|
|
2810
|
-
*/
|
|
2811
|
-
declare type UsePerspective = {
|
|
2812
|
-
(perspectiveHandle: PerspectiveHandle): string | string[]
|
|
2813
|
-
}
|
|
2814
|
-
|
|
2815
2885
|
/**
|
|
2816
2886
|
* @public
|
|
2817
2887
|
* @function
|
|
@@ -2834,6 +2904,14 @@ declare type UsePerspective = {
|
|
|
2834
2904
|
*
|
|
2835
2905
|
* @returns The perspective for the given perspective handle.
|
|
2836
2906
|
*/
|
|
2907
|
+
declare type UsePerspective = {
|
|
2908
|
+
(perspectiveHandle: DatasetHandle): string | string[]
|
|
2909
|
+
}
|
|
2910
|
+
|
|
2911
|
+
/**
|
|
2912
|
+
* @public
|
|
2913
|
+
* @function
|
|
2914
|
+
*/
|
|
2837
2915
|
export declare const usePerspective: UsePerspective
|
|
2838
2916
|
|
|
2839
2917
|
/**
|
|
@@ -3336,6 +3414,47 @@ export declare interface UseWindowConnectionOptions<TMessage extends FrameMessag
|
|
|
3336
3414
|
onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>
|
|
3337
3415
|
}
|
|
3338
3416
|
|
|
3417
|
+
/**
|
|
3418
|
+
* Sets the browser's document title, automatically including the app's name
|
|
3419
|
+
* from the manifest.
|
|
3420
|
+
*
|
|
3421
|
+
* This follows the same convention as Sanity Studio workspaces, where the
|
|
3422
|
+
* workspace name is always present in the title:
|
|
3423
|
+
*
|
|
3424
|
+
* - With a view title: `<viewTitle> | <appTitle>`
|
|
3425
|
+
* - Without a view title: `<appTitle>`
|
|
3426
|
+
*
|
|
3427
|
+
* The Sanity dashboard appends `| Sanity` to produce the final browser tab title.
|
|
3428
|
+
*
|
|
3429
|
+
* @param viewTitle - An optional view-specific title to prepend to the app title.
|
|
3430
|
+
*
|
|
3431
|
+
* @example
|
|
3432
|
+
* ```tsx
|
|
3433
|
+
* import {useWindowTitle} from '@sanity/sdk-react'
|
|
3434
|
+
*
|
|
3435
|
+
* function MoviesList() {
|
|
3436
|
+
* useWindowTitle('Movies')
|
|
3437
|
+
* return <div>...</div>
|
|
3438
|
+
* }
|
|
3439
|
+
*
|
|
3440
|
+
* // Browser tab: "Movies | My App | Sanity"
|
|
3441
|
+
* ```
|
|
3442
|
+
*
|
|
3443
|
+
* @example
|
|
3444
|
+
* ```tsx
|
|
3445
|
+
* // Call without arguments to show just the app title
|
|
3446
|
+
* function AppRoot() {
|
|
3447
|
+
* useWindowTitle()
|
|
3448
|
+
* return <Outlet />
|
|
3449
|
+
* }
|
|
3450
|
+
*
|
|
3451
|
+
* // Browser tab: "My App | Sanity"
|
|
3452
|
+
* ```
|
|
3453
|
+
*
|
|
3454
|
+
* @public
|
|
3455
|
+
*/
|
|
3456
|
+
export declare function useWindowTitle(viewTitle?: string): void
|
|
3457
|
+
|
|
3339
3458
|
/**
|
|
3340
3459
|
* @internal
|
|
3341
3460
|
*/
|