@sanity/sdk-react 2.7.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 +450 -366
- package/dist/index.js.map +1 -1
- package/package.json +6 -8
- 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 +11 -26
- package/src/components/auth/LoginError.test.tsx +5 -0
- package/src/components/auth/LoginError.tsx +23 -2
- 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
package/dist/index.d.ts
CHANGED
|
@@ -4,28 +4,24 @@ import {AgentPatchOptions} from '@sanity/sdk'
|
|
|
4
4
|
import {AgentPatchResult} from '@sanity/sdk'
|
|
5
5
|
import {AgentPromptOptions} from '@sanity/sdk'
|
|
6
6
|
import {AgentPromptResult} from '@sanity/sdk'
|
|
7
|
+
import {AgentResourceOptions} from '@sanity/sdk'
|
|
7
8
|
import {AgentTransformOptions} from '@sanity/sdk'
|
|
8
9
|
import {AgentTranslateOptions} from '@sanity/sdk'
|
|
9
|
-
import {ApplyDocumentActionsOptions} from '@sanity/sdk'
|
|
10
10
|
import {AuthState} from '@sanity/sdk'
|
|
11
11
|
import {CanvasResource} from '@sanity/message-protocol'
|
|
12
12
|
import {ClientOptions} from '@sanity/sdk'
|
|
13
13
|
import {Context} from 'react'
|
|
14
14
|
import {CurrentUser} from '@sanity/sdk'
|
|
15
|
-
import {DatasetHandle} from '@sanity/sdk'
|
|
16
15
|
import {DatasetsResponse} from '@sanity/client'
|
|
17
16
|
import {DocumentAction} from '@sanity/sdk'
|
|
18
17
|
import {DocumentEvent} from '@sanity/sdk'
|
|
19
|
-
import {DocumentHandle} from '@sanity/sdk'
|
|
20
|
-
import {DocumentOptions} from '@sanity/sdk'
|
|
21
18
|
import {DocumentPermissionsResult} from '@sanity/sdk'
|
|
22
|
-
import {
|
|
19
|
+
import {DocumentResource} from '@sanity/sdk'
|
|
23
20
|
import {FallbackProps} from 'react-error-boundary'
|
|
24
21
|
import {FavoriteStatusResponse} from '@sanity/sdk'
|
|
25
22
|
import {FrameMessage} from '@sanity/sdk'
|
|
26
23
|
import {GetUserOptions} from '@sanity/sdk'
|
|
27
24
|
import {GetUsersOptions} from '@sanity/sdk'
|
|
28
|
-
import {JsonMatch} from '@sanity/sdk'
|
|
29
25
|
import {MediaResource} from '@sanity/message-protocol'
|
|
30
26
|
import {PathChangeMessage} from '@sanity/message-protocol'
|
|
31
27
|
import {PerspectiveHandle} from '@sanity/sdk'
|
|
@@ -40,15 +36,15 @@ import {ReleaseDocument} from '@sanity/sdk'
|
|
|
40
36
|
import {SanityClient} from '@sanity/client'
|
|
41
37
|
import {SanityConfig} from '@sanity/sdk'
|
|
42
38
|
import {SanityDocument} from '@sanity/types'
|
|
43
|
-
import {SanityDocument as SanityDocument_2} from 'groq'
|
|
44
39
|
import {SanityInstance} from '@sanity/sdk'
|
|
45
40
|
import {SanityProject} from '@sanity/sdk'
|
|
46
41
|
import {SanityProject as SanityProject_2} from '@sanity/client'
|
|
47
|
-
import {SanityProjectionResult} from 'groq'
|
|
48
42
|
import {SanityProjectMember} from '@sanity/client'
|
|
49
|
-
import {SanityQueryResult} from 'groq'
|
|
50
43
|
import {SanityUser} from '@sanity/sdk'
|
|
51
44
|
import {SortOrderingItem} from '@sanity/types'
|
|
45
|
+
import {DocumentHandle as StrictDocumentHandle} from '@sanity/sdk'
|
|
46
|
+
import {DocumentTypeHandle as StrictDocumentTypeHandle} from '@sanity/sdk'
|
|
47
|
+
import {ResourceHandle as StrictResourceHandle} from '@sanity/sdk'
|
|
52
48
|
import {StudioResource} from '@sanity/message-protocol'
|
|
53
49
|
import {TokenSource} from '@sanity/sdk'
|
|
54
50
|
import {UserPresence} from '@sanity/sdk'
|
|
@@ -178,6 +174,21 @@ declare interface DispatchIntent {
|
|
|
178
174
|
dispatchIntent: () => void
|
|
179
175
|
}
|
|
180
176
|
|
|
177
|
+
/**
|
|
178
|
+
* SDK React DocumentHandle with optional explicit resource field.
|
|
179
|
+
* Resource is resolved from context when not provided.
|
|
180
|
+
* When a `resourceName` is provided, the resource will be resolved from the context using the `ResourcesContext`,
|
|
181
|
+
* if there is a matching resource by that name.
|
|
182
|
+
* @public
|
|
183
|
+
*/
|
|
184
|
+
export declare interface DocumentHandle<
|
|
185
|
+
TDocumentType extends string = string,
|
|
186
|
+
TDataset extends string = string,
|
|
187
|
+
TProjectId extends string = string,
|
|
188
|
+
> extends DocumentTypeHandle<TDocumentType, TDataset, TProjectId> {
|
|
189
|
+
documentId: string
|
|
190
|
+
}
|
|
191
|
+
|
|
181
192
|
declare interface DocumentInteractionHistory {
|
|
182
193
|
recordEvent: (eventType: 'viewed' | 'edited' | 'created' | 'deleted') => void
|
|
183
194
|
}
|
|
@@ -193,7 +204,9 @@ export declare interface DocumentsOptions<
|
|
|
193
204
|
TDataset extends string = string,
|
|
194
205
|
TProjectId extends string = string,
|
|
195
206
|
>
|
|
196
|
-
extends
|
|
207
|
+
extends
|
|
208
|
+
ResourceHandle<TProjectId, TDataset>,
|
|
209
|
+
Pick<QueryOptions<TDocumentType, TDataset, TProjectId>, 'params'> {
|
|
197
210
|
/**
|
|
198
211
|
* Filter documents by their `_type`. Can be a single type or an array of types.
|
|
199
212
|
*/
|
|
@@ -250,6 +263,28 @@ export declare interface DocumentsResponse<
|
|
|
250
263
|
loadMore: () => void
|
|
251
264
|
}
|
|
252
265
|
|
|
266
|
+
/**
|
|
267
|
+
* SDK React DocumentTypeHandle with optional explicit resource field.
|
|
268
|
+
* Resource is resolved from context when not provided.
|
|
269
|
+
* When a `resourceName` is provided, the resource will be resolved from the context using the `ResourcesContext`,
|
|
270
|
+
* if there is a matching resource by that name.
|
|
271
|
+
* @public
|
|
272
|
+
*/
|
|
273
|
+
export declare interface DocumentTypeHandle<
|
|
274
|
+
TDocumentType extends string = string,
|
|
275
|
+
TDataset extends string = string,
|
|
276
|
+
TProjectId extends string = string,
|
|
277
|
+
> extends ResourceHandle<TProjectId, TDataset> {
|
|
278
|
+
documentType: TDocumentType
|
|
279
|
+
documentId?: string
|
|
280
|
+
liveEdit?: boolean
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** React-layer edit document options: DocumentHandle with optional path */
|
|
284
|
+
declare type EditDocumentOptions<TPath extends string | undefined = undefined> = DocumentHandle & {
|
|
285
|
+
path?: TPath
|
|
286
|
+
}
|
|
287
|
+
|
|
253
288
|
/**
|
|
254
289
|
* @internal
|
|
255
290
|
*/
|
|
@@ -298,11 +333,8 @@ export declare type MessageHandler<TWindowMessage extends WindowMessage> = (
|
|
|
298
333
|
event: TWindowMessage['data'],
|
|
299
334
|
) => TWindowMessage['response'] | Promise<TWindowMessage['response']>
|
|
300
335
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
*/
|
|
304
|
-
declare interface NamedSources {
|
|
305
|
-
[key: string]: SanityConfig
|
|
336
|
+
declare interface NamedResources {
|
|
337
|
+
[key: string]: DocumentResource
|
|
306
338
|
}
|
|
307
339
|
|
|
308
340
|
/**
|
|
@@ -329,7 +361,10 @@ export declare interface PaginatedDocumentsOptions<
|
|
|
329
361
|
TDocumentType extends string = string,
|
|
330
362
|
TDataset extends string = string,
|
|
331
363
|
TProjectId extends string = string,
|
|
332
|
-
>
|
|
364
|
+
>
|
|
365
|
+
extends
|
|
366
|
+
ResourceHandle<TProjectId, TDataset>,
|
|
367
|
+
Pick<QueryOptions<TDocumentType, TDataset, TProjectId>, 'params'> {
|
|
333
368
|
documentType?: TDocumentType | TDocumentType[]
|
|
334
369
|
/**
|
|
335
370
|
* GROQ filter expression to apply to the query
|
|
@@ -445,10 +480,13 @@ export declare type ProjectWithoutMembers = Omit<SanityProject_2, 'members'>
|
|
|
445
480
|
*/
|
|
446
481
|
export declare const REACT_SDK_VERSION: {}
|
|
447
482
|
|
|
483
|
+
/** Options for useQuery: QueryOptions with resource made optional (resolved from context) */
|
|
484
|
+
declare type ReactQueryOptions = Omit<QueryOptions, 'resource' | 'resourceName'> & ResourceHandle
|
|
485
|
+
|
|
448
486
|
/** @internal */
|
|
449
487
|
export declare function renderSanityApp(
|
|
450
488
|
rootElement: HTMLElement | null,
|
|
451
|
-
|
|
489
|
+
namedResources: NamedResources,
|
|
452
490
|
options: RenderSanitySDKAppOptions,
|
|
453
491
|
children: React.ReactNode,
|
|
454
492
|
): () => void
|
|
@@ -458,64 +496,79 @@ declare interface RenderSanitySDKAppOptions {
|
|
|
458
496
|
}
|
|
459
497
|
|
|
460
498
|
/**
|
|
461
|
-
*
|
|
499
|
+
* SDK React ResourceHandle with optional explicit resource field.
|
|
500
|
+
* Resource is resolved from context when not provided.
|
|
501
|
+
* When a `resourceName` is provided, the resource will be resolved from the context using the `ResourcesContext`,
|
|
502
|
+
* if there is a matching resource by that name.
|
|
503
|
+
* @public
|
|
504
|
+
*/
|
|
505
|
+
export declare interface ResourceHandle<
|
|
506
|
+
TProjectId extends string = string,
|
|
507
|
+
TDataset extends string = string,
|
|
508
|
+
> {
|
|
509
|
+
resource?: DocumentResource<TProjectId, TDataset>
|
|
510
|
+
resourceName?: string
|
|
511
|
+
perspective?: PerspectiveHandle['perspective']
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Provides Sanity configuration to child components through React Context.
|
|
462
516
|
*
|
|
463
517
|
* @internal
|
|
464
518
|
*
|
|
465
519
|
* @remarks
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
* -
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
* Features:
|
|
472
|
-
* - Automatically manages the lifecycle of Sanity instances
|
|
473
|
-
* - Disposes instances when the component unmounts
|
|
474
|
-
* - Includes a Suspense boundary for data loading
|
|
475
|
-
* - Enables hierarchical configuration inheritance
|
|
476
|
-
*
|
|
477
|
-
* Use this component to:
|
|
478
|
-
* - Set up project/dataset configuration for an application
|
|
479
|
-
* - Override specific configuration values in a section of your app
|
|
480
|
-
* - Create isolated instance hierarchies for different features
|
|
520
|
+
* - **Root usage** (no parent instance): creates a `SanityInstance` with the
|
|
521
|
+
* given config and provides it via `SanityInstanceContext`.
|
|
522
|
+
* - **Nested usage** (inside an existing provider): sets
|
|
523
|
+
* `ResourceContext` and `PerspectiveContext` so hooks in the subtree
|
|
524
|
+
* resolve the correct resource/perspective without creating a new instance.
|
|
481
525
|
*
|
|
482
|
-
* @example
|
|
526
|
+
* @example Root provider
|
|
483
527
|
* ```tsx
|
|
484
528
|
* <ResourceProvider
|
|
485
|
-
* projectId
|
|
486
|
-
* dataset="production"
|
|
529
|
+
* resource={{ projectId: 'your-project-id', dataset: 'production' }}
|
|
487
530
|
* fallback={<LoadingSpinner />}
|
|
488
531
|
* >
|
|
489
532
|
* <YourApp />
|
|
490
533
|
* </ResourceProvider>
|
|
491
534
|
* ```
|
|
492
535
|
*
|
|
493
|
-
* @example
|
|
536
|
+
* @example Nested override
|
|
494
537
|
* ```tsx
|
|
495
|
-
*
|
|
496
|
-
*
|
|
497
|
-
* <
|
|
498
|
-
*
|
|
499
|
-
* <
|
|
500
|
-
* <PreviewFeatures />
|
|
501
|
-
* </ResourceProvider>
|
|
538
|
+
* <ResourceProvider
|
|
539
|
+
* resource={{ projectId: 'other-project', dataset: 'staging' }}
|
|
540
|
+
* fallback={<LoadingSpinner />}
|
|
541
|
+
* >
|
|
542
|
+
* <SubSection />
|
|
502
543
|
* </ResourceProvider>
|
|
503
544
|
* ```
|
|
504
545
|
*/
|
|
505
546
|
export declare function ResourceProvider({
|
|
506
547
|
children,
|
|
507
548
|
fallback,
|
|
508
|
-
|
|
549
|
+
resource,
|
|
550
|
+
...rest
|
|
509
551
|
}: ResourceProviderProps): React.ReactNode
|
|
510
552
|
|
|
511
553
|
/**
|
|
512
|
-
* Props for the ResourceProvider component
|
|
554
|
+
* Props for the ResourceProvider component.
|
|
555
|
+
*
|
|
556
|
+
* Extends `SanityConfig` (minus `defaultResource`) so new config fields are
|
|
557
|
+
* automatically forwarded. The `resource` prop replaces `defaultResource`
|
|
558
|
+
* with a name that better describes its role at the React layer.
|
|
559
|
+
*
|
|
513
560
|
* @internal
|
|
514
561
|
*/
|
|
515
|
-
export declare interface ResourceProviderProps extends SanityConfig {
|
|
562
|
+
export declare interface ResourceProviderProps extends Omit<SanityConfig, 'defaultResource'> {
|
|
516
563
|
/**
|
|
517
|
-
*
|
|
518
|
-
*
|
|
564
|
+
* The document resource (project/dataset, media library, or canvas)
|
|
565
|
+
* for this subtree. Hooks that don't specify an explicit resource will
|
|
566
|
+
* use this value.
|
|
567
|
+
*/
|
|
568
|
+
resource?: DocumentResource
|
|
569
|
+
/**
|
|
570
|
+
* React node to show while content is loading.
|
|
571
|
+
* Used as the fallback for the internal Suspense boundary.
|
|
519
572
|
*/
|
|
520
573
|
fallback: React.ReactNode
|
|
521
574
|
children: React.ReactNode
|
|
@@ -528,57 +581,49 @@ export declare interface ResourceProviderProps extends SanityConfig {
|
|
|
528
581
|
* as well as application context and state which is used by the Sanity React hooks. Your application
|
|
529
582
|
* must be wrapped with the SanityApp component to function properly.
|
|
530
583
|
*
|
|
531
|
-
* The `config` prop
|
|
532
|
-
*
|
|
584
|
+
* The `config` prop accepts a {@link SanityConfig} object. Use the `resources` prop to declare
|
|
585
|
+
* one or more named data resources for your app.
|
|
533
586
|
*
|
|
534
|
-
* When rendered inside a Sanity Studio that provides `SDKStudioContext`, the `config`
|
|
535
|
-
* optional — `SanityApp` will automatically derive
|
|
536
|
-
* Studio workspace.
|
|
587
|
+
* When rendered inside a Sanity Studio that provides `SDKStudioContext`, the `config` and `resources`
|
|
588
|
+
* props are optional — `SanityApp` will automatically derive them from the Studio workspace.
|
|
537
589
|
*
|
|
538
|
-
*
|
|
539
|
-
* When passing multiple SanityConfig objects to the `config` prop, the first configuration in the array becomes the default
|
|
540
|
-
* configuration used by the App SDK Hooks.
|
|
541
|
-
*
|
|
542
|
-
* When both `config` and `SDKStudioContext` are available, the explicit `config` takes precedence.
|
|
590
|
+
* When both `config` and `SDKStudioContext` are available, the explicit props take precedence.
|
|
543
591
|
*
|
|
544
592
|
* @category Components
|
|
545
593
|
* @param props - Your Sanity configuration and the React children to render
|
|
546
594
|
* @returns Your Sanity application, integrated with your Sanity configuration and application context
|
|
547
595
|
*
|
|
548
|
-
* @example
|
|
596
|
+
* @example Single project
|
|
549
597
|
* ```tsx
|
|
550
|
-
* import { SanityApp
|
|
551
|
-
*
|
|
552
|
-
* import MyAppRoot from './Root'
|
|
598
|
+
* import { SanityApp } from '@sanity/sdk-react'
|
|
553
599
|
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
600
|
+
* export default function MyApp() {
|
|
601
|
+
* return (
|
|
602
|
+
* <SanityApp
|
|
603
|
+
* resources={{
|
|
604
|
+
* default: { projectId: 'my-project-id', dataset: 'production' },
|
|
605
|
+
* }}
|
|
606
|
+
* fallback={<div>Loading…</div>}
|
|
607
|
+
* >
|
|
608
|
+
* <MyAppRoot />
|
|
609
|
+
* </SanityApp>
|
|
610
|
+
* )
|
|
558
611
|
* }
|
|
612
|
+
* ```
|
|
559
613
|
*
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
* {
|
|
564
|
-
* projectId: 'marketing-website-project',
|
|
565
|
-
* dataset: 'production',
|
|
566
|
-
* },
|
|
567
|
-
* // Configuration for a separate blog project
|
|
568
|
-
* {
|
|
569
|
-
* projectId: 'blog-project',
|
|
570
|
-
* dataset: 'production',
|
|
571
|
-
* },
|
|
572
|
-
* // Configuration for a separate ecommerce project
|
|
573
|
-
* {
|
|
574
|
-
* projectId: 'ecommerce-project',
|
|
575
|
-
* dataset: 'production',
|
|
576
|
-
* }
|
|
577
|
-
* ]
|
|
614
|
+
* @example Multiple resources
|
|
615
|
+
* ```tsx
|
|
616
|
+
* import { SanityApp } from '@sanity/sdk-react'
|
|
578
617
|
*
|
|
579
618
|
* export default function MyApp() {
|
|
580
619
|
* return (
|
|
581
|
-
* <SanityApp
|
|
620
|
+
* <SanityApp
|
|
621
|
+
* resources={{
|
|
622
|
+
* default: { projectId: 'abc123', dataset: 'production' },
|
|
623
|
+
* 'blog-project': { projectId: 'def456', dataset: 'production' },
|
|
624
|
+
* }}
|
|
625
|
+
* fallback={<div>Loading…</div>}
|
|
626
|
+
* >
|
|
582
627
|
* <MyAppRoot />
|
|
583
628
|
* </SanityApp>
|
|
584
629
|
* )
|
|
@@ -589,6 +634,7 @@ export declare function SanityApp({
|
|
|
589
634
|
children,
|
|
590
635
|
fallback,
|
|
591
636
|
config: configProp,
|
|
637
|
+
resources: resourcesProp,
|
|
592
638
|
...props
|
|
593
639
|
}: SanityAppProps): ReactElement
|
|
594
640
|
|
|
@@ -598,15 +644,17 @@ export declare function SanityApp({
|
|
|
598
644
|
*/
|
|
599
645
|
export declare interface SanityAppProps {
|
|
600
646
|
/**
|
|
601
|
-
*
|
|
602
|
-
* Optional when `SanityApp` is rendered inside an `SDKStudioContext`
|
|
603
|
-
* (e.g. inside Sanity Studio) — the config is derived from
|
|
604
|
-
* automatically.
|
|
647
|
+
* Core configuration for the SDK instance (auth, studio, perspective).
|
|
648
|
+
* Optional when `SanityApp` is rendered inside an `SDKStudioContext`
|
|
649
|
+
* provider (e.g. inside Sanity Studio) — the config is derived from
|
|
650
|
+
* the workspace automatically.
|
|
605
651
|
*/
|
|
606
|
-
config?: SanityConfig
|
|
607
|
-
/**
|
|
608
|
-
|
|
609
|
-
|
|
652
|
+
config?: SanityConfig
|
|
653
|
+
/**
|
|
654
|
+
* Named document resources for the application. The resource keyed `"default"`
|
|
655
|
+
* is used automatically when no explicit resource is specified in hooks.
|
|
656
|
+
*/
|
|
657
|
+
resources?: Record<string, DocumentResource>
|
|
610
658
|
children: React.ReactNode
|
|
611
659
|
fallback: React.ReactNode
|
|
612
660
|
}
|
|
@@ -619,12 +667,15 @@ export {SanityProjectMember}
|
|
|
619
667
|
* @internal
|
|
620
668
|
*
|
|
621
669
|
* Top-level context provider that provides access to the Sanity SDK.
|
|
622
|
-
*
|
|
623
|
-
*
|
|
670
|
+
*
|
|
671
|
+
* Creates a single `ResourceProvider` (and therefore a single `SanityInstance`)
|
|
672
|
+
* for the given config. Resource resolution is handled by `ResourcesContext`
|
|
673
|
+
* and the `"default"` named resource.
|
|
624
674
|
*/
|
|
625
675
|
export declare function SDKProvider({
|
|
626
676
|
children,
|
|
627
677
|
config,
|
|
678
|
+
resources,
|
|
628
679
|
fallback,
|
|
629
680
|
...props
|
|
630
681
|
}: SDKProviderProps): ReactElement
|
|
@@ -634,9 +685,13 @@ export declare function SDKProvider({
|
|
|
634
685
|
*/
|
|
635
686
|
export declare interface SDKProviderProps extends AuthBoundaryProps {
|
|
636
687
|
children: ReactNode
|
|
637
|
-
config: SanityConfig
|
|
688
|
+
config: SanityConfig
|
|
689
|
+
/**
|
|
690
|
+
* Named document resources map. Provided to `ResourcesContext` for
|
|
691
|
+
* name-based resource resolution in hooks.
|
|
692
|
+
*/
|
|
693
|
+
resources?: Record<string, DocumentResource>
|
|
638
694
|
fallback: ReactNode
|
|
639
|
-
sources?: Record<string, DocumentSource>
|
|
640
695
|
}
|
|
641
696
|
|
|
642
697
|
/**
|
|
@@ -677,6 +732,12 @@ export declare const SDKStudioContext: Context<StudioWorkspaceHandle | null>
|
|
|
677
732
|
|
|
678
733
|
export {SortOrderingItem}
|
|
679
734
|
|
|
735
|
+
export {StrictDocumentHandle}
|
|
736
|
+
|
|
737
|
+
export {StrictDocumentTypeHandle}
|
|
738
|
+
|
|
739
|
+
export {StrictResourceHandle}
|
|
740
|
+
|
|
680
741
|
/**
|
|
681
742
|
* Minimal duck-typed interface representing a Sanity Studio workspace.
|
|
682
743
|
* The Studio's `Workspace` type satisfies this naturally — no import
|
|
@@ -689,6 +750,12 @@ export declare interface StudioWorkspaceHandle {
|
|
|
689
750
|
projectId: string
|
|
690
751
|
/** The dataset name for this workspace. */
|
|
691
752
|
dataset: string
|
|
753
|
+
/**
|
|
754
|
+
* Whether the Studio has determined the user is authenticated.
|
|
755
|
+
* When `true` and the token source emits `null`, the SDK infers
|
|
756
|
+
* cookie-based auth is in use and skips the logged-out state.
|
|
757
|
+
*/
|
|
758
|
+
authenticated?: boolean
|
|
692
759
|
/** Authentication state for this workspace. */
|
|
693
760
|
auth: {
|
|
694
761
|
/**
|
|
@@ -723,13 +790,6 @@ declare interface Subscription {
|
|
|
723
790
|
|
|
724
791
|
declare type Updater<TValue> = TValue | ((currentValue: TValue) => TValue)
|
|
725
792
|
|
|
726
|
-
/**
|
|
727
|
-
* @public
|
|
728
|
-
*/
|
|
729
|
-
declare type UseActiveReleases = {
|
|
730
|
-
(): ReleaseDocument[]
|
|
731
|
-
}
|
|
732
|
-
|
|
733
793
|
/**
|
|
734
794
|
* @public
|
|
735
795
|
|
|
@@ -745,6 +805,14 @@ declare type UseActiveReleases = {
|
|
|
745
805
|
* const activeReleases = useActiveReleases()
|
|
746
806
|
* ```
|
|
747
807
|
*/
|
|
808
|
+
declare type UseActiveReleases = {
|
|
809
|
+
(options?: ResourceHandle | undefined): ReleaseDocument[]
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* @public
|
|
814
|
+
* @function
|
|
815
|
+
*/
|
|
748
816
|
export declare const useActiveReleases: UseActiveReleases
|
|
749
817
|
|
|
750
818
|
/**
|
|
@@ -814,8 +882,8 @@ export declare const useActiveReleases: UseActiveReleases
|
|
|
814
882
|
*
|
|
815
883
|
* @category Agent Actions
|
|
816
884
|
*/
|
|
817
|
-
export declare
|
|
818
|
-
options: AgentGenerateOptions
|
|
885
|
+
export declare function useAgentGenerate(): (
|
|
886
|
+
options: AgentGenerateOptions & WithResourceNameSupport<AgentResourceOptions>,
|
|
819
887
|
) => Subscribable<unknown>
|
|
820
888
|
|
|
821
889
|
/**
|
|
@@ -967,7 +1035,9 @@ export declare const useAgentGenerate: () => (
|
|
|
967
1035
|
*
|
|
968
1036
|
* @category Agent Actions
|
|
969
1037
|
*/
|
|
970
|
-
export declare
|
|
1038
|
+
export declare function useAgentPatch(): (
|
|
1039
|
+
options: AgentPatchOptions & WithResourceNameSupport<AgentResourceOptions>,
|
|
1040
|
+
) => Promise<AgentPatchResult>
|
|
971
1041
|
|
|
972
1042
|
/**
|
|
973
1043
|
* @alpha
|
|
@@ -1063,8 +1133,8 @@ export declare const useAgentPatch: () => (options: AgentPatchOptions) => Promis
|
|
|
1063
1133
|
*
|
|
1064
1134
|
* @category Agent Actions
|
|
1065
1135
|
*/
|
|
1066
|
-
export declare
|
|
1067
|
-
options: AgentPromptOptions
|
|
1136
|
+
export declare function useAgentPrompt(): (
|
|
1137
|
+
options: AgentPromptOptions & WithResourceNameSupport<AgentResourceOptions>,
|
|
1068
1138
|
) => Promise<AgentPromptResult>
|
|
1069
1139
|
|
|
1070
1140
|
/**
|
|
@@ -1170,8 +1240,8 @@ export declare function useAgentResourceContext(options: AgentResourceContextOpt
|
|
|
1170
1240
|
*
|
|
1171
1241
|
* @category Agent Actions
|
|
1172
1242
|
*/
|
|
1173
|
-
export declare
|
|
1174
|
-
options: AgentTransformOptions
|
|
1243
|
+
export declare function useAgentTransform(): (
|
|
1244
|
+
options: AgentTransformOptions & WithResourceNameSupport<AgentResourceOptions>,
|
|
1175
1245
|
) => Subscribable<unknown>
|
|
1176
1246
|
|
|
1177
1247
|
/**
|
|
@@ -1264,8 +1334,8 @@ export declare const useAgentTransform: () => (
|
|
|
1264
1334
|
*
|
|
1265
1335
|
* @category Agent Actions
|
|
1266
1336
|
*/
|
|
1267
|
-
export declare
|
|
1268
|
-
options: AgentTranslateOptions
|
|
1337
|
+
export declare function useAgentTranslate(): (
|
|
1338
|
+
options: AgentTranslateOptions & WithResourceNameSupport<AgentResourceOptions>,
|
|
1269
1339
|
) => Subscribable<unknown>
|
|
1270
1340
|
|
|
1271
1341
|
/**
|
|
@@ -1280,8 +1350,8 @@ declare interface UseApplyDocumentActions {
|
|
|
1280
1350
|
action:
|
|
1281
1351
|
| DocumentAction<TDocumentType, TDataset, TProjectId>
|
|
1282
1352
|
| DocumentAction<TDocumentType, TDataset, TProjectId>[],
|
|
1283
|
-
options?:
|
|
1284
|
-
) => Promise<ActionsResult
|
|
1353
|
+
options?: ResourceHandle,
|
|
1354
|
+
) => Promise<ActionsResult>
|
|
1285
1355
|
}
|
|
1286
1356
|
|
|
1287
1357
|
/**
|
|
@@ -1573,11 +1643,12 @@ declare type UseDatasets = {
|
|
|
1573
1643
|
* Returns metadata for each dataset the current user has access to.
|
|
1574
1644
|
*
|
|
1575
1645
|
* @category Datasets
|
|
1576
|
-
* @
|
|
1646
|
+
* @param options - An object containing the `projectId` to list datasets for.
|
|
1647
|
+
* @returns The metadata for the datasets
|
|
1577
1648
|
*
|
|
1578
1649
|
* @example
|
|
1579
1650
|
* ```tsx
|
|
1580
|
-
* const datasets = useDatasets()
|
|
1651
|
+
* const datasets = useDatasets({projectId: 'my-project-id'})
|
|
1581
1652
|
*
|
|
1582
1653
|
* return (
|
|
1583
1654
|
* <select>
|
|
@@ -1589,7 +1660,7 @@ declare type UseDatasets = {
|
|
|
1589
1660
|
* ```
|
|
1590
1661
|
*
|
|
1591
1662
|
*/
|
|
1592
|
-
(): DatasetsResponse
|
|
1663
|
+
(options: {projectId: string}): DatasetsResponse
|
|
1593
1664
|
}
|
|
1594
1665
|
|
|
1595
1666
|
/**
|
|
@@ -1608,8 +1679,8 @@ export declare const useDatasets: UseDatasets
|
|
|
1608
1679
|
* - `action` - Action to perform (currently only 'edit' is supported). Will prompt a picker if multiple handlers are available.
|
|
1609
1680
|
* - `intentId` - Specific ID of the intent to dispatch. Either `action` or `intentId` is required.
|
|
1610
1681
|
* - `documentHandle` - The document handle containing document ID, type, and either:
|
|
1611
|
-
* - `projectId` and `dataset` for traditional dataset
|
|
1612
|
-
* - `
|
|
1682
|
+
* - `projectId` and `dataset` for traditional dataset resources, like `{documentId: '123', documentType: 'book', projectId: 'abc123', dataset: 'production'}`
|
|
1683
|
+
* - `resource` for media library, canvas, or dataset resources, like `{documentId: '123', documentType: 'sanity.asset', resource: mediaLibraryResource('ml123')}` or `{documentId: '123', documentType: 'sanity.canvas.document', resource: canvasResource('canvas123')}`
|
|
1613
1684
|
* - `paremeters` - Optional parameters to include in the dispatch; will be passed to the resolved intent handler
|
|
1614
1685
|
* @returns An object containing:
|
|
1615
1686
|
* - `dispatchIntent` - Function to dispatch the intent message
|
|
@@ -1658,187 +1729,121 @@ export declare function useDispatchIntent(params: UseDispatchIntentParams): Disp
|
|
|
1658
1729
|
declare interface UseDispatchIntentParams {
|
|
1659
1730
|
action?: 'edit'
|
|
1660
1731
|
intentId?: string
|
|
1661
|
-
documentHandle:
|
|
1732
|
+
documentHandle: DocumentHandle
|
|
1662
1733
|
parameters?: Record<string, unknown>
|
|
1663
1734
|
}
|
|
1664
1735
|
|
|
1665
1736
|
declare interface UseDocument {
|
|
1666
|
-
/** @internal */
|
|
1667
|
-
<TDocumentType extends string, TDataset extends string, TProjectId extends string = string>(
|
|
1668
|
-
options: DocumentOptions<undefined, TDocumentType, TDataset, TProjectId>,
|
|
1669
|
-
): {
|
|
1670
|
-
data: SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`> | null
|
|
1671
|
-
}
|
|
1672
|
-
/** @internal */
|
|
1673
|
-
<
|
|
1674
|
-
TPath extends string,
|
|
1675
|
-
TDocumentType extends string,
|
|
1676
|
-
TDataset extends string = string,
|
|
1677
|
-
TProjectId extends string = string,
|
|
1678
|
-
>(
|
|
1679
|
-
options: DocumentOptions<TPath, TDocumentType, TDataset, TProjectId>,
|
|
1680
|
-
): {
|
|
1681
|
-
data: JsonMatch<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>, TPath> | undefined
|
|
1682
|
-
}
|
|
1683
|
-
/** @internal */
|
|
1684
|
-
<TData>(options: DocumentOptions<undefined>): {
|
|
1685
|
-
data: TData | null
|
|
1686
|
-
}
|
|
1687
|
-
/** @internal */
|
|
1688
|
-
<TData>(options: DocumentOptions<string>): {
|
|
1689
|
-
data: TData | undefined
|
|
1690
|
-
}
|
|
1691
1737
|
/**
|
|
1692
|
-
* ## useDocument via Type Inference (Recommended)
|
|
1693
|
-
*
|
|
1694
1738
|
* @public
|
|
1695
1739
|
*
|
|
1696
|
-
*
|
|
1740
|
+
* ## useDocument with Explicit Types (no path)
|
|
1697
1741
|
*
|
|
1698
|
-
*
|
|
1699
|
-
*
|
|
1700
|
-
* - Provides type-safe access to documents and nested fields
|
|
1701
|
-
* - Supports project/dataset-specific type inference
|
|
1702
|
-
* - Works seamlessly with Typegen-generated types
|
|
1742
|
+
* Provide an explicit type parameter `TData` to type the returned document data.
|
|
1743
|
+
* You can define your own interfaces or use types generated by your Sanity Studio.
|
|
1703
1744
|
*
|
|
1704
|
-
* This hook will suspend while the document data is being fetched
|
|
1705
|
-
*
|
|
1706
|
-
* When fetching a full document:
|
|
1707
|
-
* - Returns the complete document object if it exists
|
|
1708
|
-
* - Returns `null` if the document doesn't exist
|
|
1709
|
-
*
|
|
1710
|
-
* When fetching with a path:
|
|
1711
|
-
* - Returns the value at the specified path if both the document and path exist
|
|
1712
|
-
* - Returns `undefined` if either the document doesn't exist or the path doesn't exist in the document
|
|
1745
|
+
* This hook will suspend while the document data is being fetched.
|
|
1713
1746
|
*
|
|
1714
|
-
* @
|
|
1747
|
+
* @typeParam TData - The explicit type for the document
|
|
1715
1748
|
* @param options - Configuration including `documentId`, `documentType`, and optionally:
|
|
1716
|
-
* - `path`: To select a nested value (returns typed value at path)
|
|
1717
1749
|
* - `projectId`/`dataset`: For multi-project/dataset setups
|
|
1718
|
-
* @returns The document state
|
|
1750
|
+
* @returns The document state
|
|
1719
1751
|
*
|
|
1720
|
-
* @example Basic document fetch
|
|
1752
|
+
* @example Basic document fetch with explicit type
|
|
1721
1753
|
* ```tsx
|
|
1722
|
-
* import {useDocument, type DocumentHandle} from '@sanity/sdk-react'
|
|
1754
|
+
* import {useDocument, type DocumentHandle, type SanityDocument} from '@sanity/sdk-react'
|
|
1755
|
+
*
|
|
1756
|
+
* interface Book extends SanityDocument {
|
|
1757
|
+
* _type: 'book'
|
|
1758
|
+
* title: string
|
|
1759
|
+
* author: string
|
|
1760
|
+
* }
|
|
1723
1761
|
*
|
|
1724
|
-
* interface
|
|
1725
|
-
* doc: DocumentHandle
|
|
1762
|
+
* interface BookViewProps {
|
|
1763
|
+
* doc: DocumentHandle
|
|
1726
1764
|
* }
|
|
1727
1765
|
*
|
|
1728
|
-
* function
|
|
1729
|
-
* const {data:
|
|
1730
|
-
* return <h1>{
|
|
1766
|
+
* function BookView({doc}: BookViewProps) {
|
|
1767
|
+
* const {data: book} = useDocument<Book>({...doc})
|
|
1768
|
+
* return <h1>{book?.title ?? 'Untitled'} by {book?.author ?? 'Unknown'}</h1>
|
|
1731
1769
|
* }
|
|
1732
1770
|
* ```
|
|
1733
1771
|
*
|
|
1734
|
-
* @
|
|
1772
|
+
* @inlineType DocumentOptions
|
|
1773
|
+
*/
|
|
1774
|
+
<TData>(options: UseDocumentOptions<undefined>): {
|
|
1775
|
+
data: TData | null
|
|
1776
|
+
}
|
|
1777
|
+
/**
|
|
1778
|
+
* @public
|
|
1779
|
+
*
|
|
1780
|
+
* ## useDocument with Explicit Types (with path)
|
|
1781
|
+
*
|
|
1782
|
+
* Provide an explicit type parameter `TData` to type the returned field data
|
|
1783
|
+
* when using a `path` to select a nested value.
|
|
1784
|
+
*
|
|
1785
|
+
* @typeParam TData - The explicit type for the field
|
|
1786
|
+
* @param options - Configuration including `documentId`, `documentType`, `path`, and optionally:
|
|
1787
|
+
* - `projectId`/`dataset`: For multi-project/dataset setups
|
|
1788
|
+
* @returns The field value at the specified path
|
|
1789
|
+
*
|
|
1790
|
+
* @example Fetching a specific field with explicit type
|
|
1735
1791
|
* ```tsx
|
|
1736
1792
|
* import {useDocument, type DocumentHandle} from '@sanity/sdk-react'
|
|
1737
1793
|
*
|
|
1738
|
-
*
|
|
1739
|
-
* doc:
|
|
1740
|
-
* }
|
|
1741
|
-
*
|
|
1742
|
-
* function ProductTitle({doc}: ProductTitleProps) {
|
|
1743
|
-
* const {data: title} = useDocument({
|
|
1744
|
-
* ...doc,
|
|
1745
|
-
* path: 'title' // Returns just the title field
|
|
1746
|
-
* })
|
|
1794
|
+
* function BookTitle({doc}: {doc: DocumentHandle}) {
|
|
1795
|
+
* const {data: title} = useDocument<string>({...doc, path: 'title'})
|
|
1747
1796
|
* return <h1>{title ?? 'Untitled'}</h1>
|
|
1748
1797
|
* }
|
|
1749
1798
|
* ```
|
|
1750
1799
|
*
|
|
1751
1800
|
* @inlineType DocumentOptions
|
|
1752
1801
|
*/
|
|
1753
|
-
<
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
TDataset extends string = string,
|
|
1757
|
-
TProjectId extends string = string,
|
|
1758
|
-
>(
|
|
1759
|
-
options: DocumentOptions<TPath, TDocumentType, TDataset, TProjectId>,
|
|
1760
|
-
): TPath extends string
|
|
1761
|
-
? {
|
|
1762
|
-
data:
|
|
1763
|
-
| JsonMatch<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>, TPath>
|
|
1764
|
-
| undefined
|
|
1765
|
-
}
|
|
1766
|
-
: {
|
|
1767
|
-
data: SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`> | null
|
|
1768
|
-
}
|
|
1802
|
+
<TData>(options: UseDocumentOptions<string>): {
|
|
1803
|
+
data: TData | undefined
|
|
1804
|
+
}
|
|
1769
1805
|
/**
|
|
1770
1806
|
* @public
|
|
1771
1807
|
*
|
|
1772
|
-
* ## useDocument
|
|
1808
|
+
* ## useDocument (default)
|
|
1773
1809
|
*
|
|
1774
|
-
*
|
|
1775
|
-
* - You're not using Sanity Typegen
|
|
1776
|
-
* - You need to manually specify document types
|
|
1777
|
-
* - You're working with dynamic document types
|
|
1810
|
+
* When no explicit type parameter is provided, the hook returns `SanityDocument`.
|
|
1778
1811
|
*
|
|
1779
|
-
*
|
|
1780
|
-
* - Requires manual type specification via `TData`
|
|
1781
|
-
* - Returns `TData | null` for full documents
|
|
1782
|
-
* - Returns `TData | undefined` for nested values
|
|
1812
|
+
* This hook will suspend while the document data is being fetched and loaded.
|
|
1783
1813
|
*
|
|
1784
|
-
*
|
|
1814
|
+
* When fetching a full document:
|
|
1815
|
+
* - Returns the complete document object if it exists
|
|
1816
|
+
* - Returns `null` if the document doesn't exist
|
|
1785
1817
|
*
|
|
1786
|
-
*
|
|
1787
|
-
*
|
|
1788
|
-
*
|
|
1818
|
+
* When fetching with a path:
|
|
1819
|
+
* - Returns the value at the specified path if both the document and path exist
|
|
1820
|
+
* - Returns `undefined` if either the document doesn't exist or the path doesn't exist in the document
|
|
1821
|
+
*
|
|
1822
|
+
* @category Documents
|
|
1823
|
+
* @param options - Configuration including `documentId`, `documentType`, and optionally:
|
|
1789
1824
|
* - `path`: To select a nested value
|
|
1790
1825
|
* - `projectId`/`dataset`: For multi-project/dataset setups
|
|
1791
|
-
* @returns The document state (or nested value if path provided)
|
|
1792
|
-
*
|
|
1793
|
-
* @example Basic document fetch with explicit type
|
|
1794
|
-
* ```tsx
|
|
1795
|
-
* import {useDocument, type DocumentHandle, type SanityDocument} from '@sanity/sdk-react'
|
|
1796
|
-
*
|
|
1797
|
-
* interface Book extends SanityDocument {
|
|
1798
|
-
* _type: 'book'
|
|
1799
|
-
* title: string
|
|
1800
|
-
* author: string
|
|
1801
|
-
* }
|
|
1802
|
-
*
|
|
1803
|
-
* interface BookViewProps {
|
|
1804
|
-
* doc: DocumentHandle
|
|
1805
|
-
* }
|
|
1806
|
-
*
|
|
1807
|
-
* function BookView({doc}: BookViewProps) {
|
|
1808
|
-
* const {data: book} = useDocument<Book>({...doc})
|
|
1809
|
-
* return <h1>{book?.title ?? 'Untitled'} by {book?.author ?? 'Unknown'}</h1>
|
|
1810
|
-
* }
|
|
1811
|
-
* ```
|
|
1826
|
+
* @returns The document state (or nested value if path provided).
|
|
1812
1827
|
*
|
|
1813
|
-
* @example
|
|
1828
|
+
* @example Basic document fetch
|
|
1814
1829
|
* ```tsx
|
|
1815
1830
|
* import {useDocument, type DocumentHandle} from '@sanity/sdk-react'
|
|
1816
1831
|
*
|
|
1817
|
-
*
|
|
1818
|
-
*
|
|
1819
|
-
* }
|
|
1820
|
-
*
|
|
1821
|
-
* function BookTitle({doc}: BookTitleProps) {
|
|
1822
|
-
* const {data: title} = useDocument<string>({...doc, path: 'title'})
|
|
1823
|
-
* return <h1>{title ?? 'Untitled'}</h1>
|
|
1832
|
+
* function ProductView({doc}: {doc: DocumentHandle}) {
|
|
1833
|
+
* const {data: product} = useDocument({...doc})
|
|
1834
|
+
* return <h1>{product?.title ?? 'Untitled'}</h1>
|
|
1824
1835
|
* }
|
|
1825
1836
|
* ```
|
|
1826
1837
|
*
|
|
1827
1838
|
* @inlineType DocumentOptions
|
|
1828
1839
|
*/
|
|
1829
|
-
<
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
? {
|
|
1833
|
-
data: TData | undefined
|
|
1834
|
-
}
|
|
1835
|
-
: {
|
|
1836
|
-
data: TData | null
|
|
1837
|
-
}
|
|
1840
|
+
(options: UseDocumentOptions<undefined>): {
|
|
1841
|
+
data: SanityDocument | null
|
|
1842
|
+
}
|
|
1838
1843
|
/**
|
|
1839
1844
|
* @internal
|
|
1840
1845
|
*/
|
|
1841
|
-
(options:
|
|
1846
|
+
(options: UseDocumentOptions): {
|
|
1842
1847
|
data: unknown
|
|
1843
1848
|
}
|
|
1844
1849
|
}
|
|
@@ -1847,10 +1852,11 @@ declare interface UseDocument {
|
|
|
1847
1852
|
* @public
|
|
1848
1853
|
* Reads and subscribes to a document's realtime state, incorporating both local and remote changes.
|
|
1849
1854
|
*
|
|
1850
|
-
*
|
|
1855
|
+
* You can provide an explicit type parameter for full type safety:
|
|
1856
|
+
* - `useDocument<MyType>({...})` returns `{data: MyType | null}`
|
|
1857
|
+
* - `useDocument<string>({..., path: 'title'})` returns `{data: string | undefined}`
|
|
1851
1858
|
*
|
|
1852
|
-
*
|
|
1853
|
-
* 2. **[Explicit Types](#usedocument-via-explicit-types)** - Manually specify types when needed
|
|
1859
|
+
* Without a type parameter, data is typed as `SanityDocument`.
|
|
1854
1860
|
*
|
|
1855
1861
|
* @remarks
|
|
1856
1862
|
* `useDocument` is ideal for realtime editing interfaces where you need immediate feedback on changes.
|
|
@@ -1875,19 +1881,19 @@ export declare const useDocument: UseDocument
|
|
|
1875
1881
|
* Subscribes an event handler to events in your application's document store.
|
|
1876
1882
|
*
|
|
1877
1883
|
* @category Documents
|
|
1878
|
-
* @param options - An object containing the event handler (`onEvent`) and optionally a `
|
|
1884
|
+
* @param options - An object containing the event handler (`onEvent`) and optionally a `resource`. If no resource is provided, the nearest resource from context will be used.
|
|
1879
1885
|
* @example Creating a custom hook for document event toasts
|
|
1880
1886
|
* ```tsx
|
|
1881
|
-
* import {
|
|
1887
|
+
* import {type DocumentResource, type DocumentEvent, useDocumentEvent} from '@sanity/sdk-react'
|
|
1882
1888
|
* import {useToast} from './my-ui-library'
|
|
1883
1889
|
*
|
|
1884
|
-
* // Define options for the custom hook
|
|
1885
|
-
* interface DocumentToastsOptions
|
|
1886
|
-
*
|
|
1890
|
+
* // Define options for the custom hook
|
|
1891
|
+
* interface DocumentToastsOptions {
|
|
1892
|
+
* resource?: DocumentResource
|
|
1887
1893
|
* }
|
|
1888
1894
|
*
|
|
1889
1895
|
* // Define the custom hook
|
|
1890
|
-
* function useDocumentToasts({
|
|
1896
|
+
* function useDocumentToasts({resource}: DocumentToastsOptions = {}) {
|
|
1891
1897
|
* const showToast = useToast() // Get the toast function
|
|
1892
1898
|
*
|
|
1893
1899
|
* // Define the event handler logic to show toasts on specific events
|
|
@@ -1899,24 +1905,20 @@ export declare const useDocument: UseDocument
|
|
|
1899
1905
|
* } else if (event.type === 'deleted') {
|
|
1900
1906
|
* showToast(`Document ${event.documentId} deleted.`)
|
|
1901
1907
|
* } else {
|
|
1902
|
-
* // Optionally log other events for debugging
|
|
1903
1908
|
* console.log('Document Event:', event.type, event.documentId)
|
|
1904
1909
|
* }
|
|
1905
1910
|
* }
|
|
1906
1911
|
*
|
|
1907
|
-
* // Call the original hook, spreading the handle properties
|
|
1908
1912
|
* useDocumentEvent({
|
|
1909
|
-
*
|
|
1913
|
+
* resource,
|
|
1910
1914
|
* onEvent: handleEvent,
|
|
1911
1915
|
* })
|
|
1912
1916
|
* }
|
|
1913
1917
|
*
|
|
1914
1918
|
* function MyComponentWithToasts() {
|
|
1915
|
-
*
|
|
1916
|
-
* const specificHandle = createDatasetHandle({ projectId: 'p1', dataset: 'ds1' })
|
|
1917
|
-
* useDocumentToasts(specificHandle)
|
|
1919
|
+
* useDocumentToasts({resource: {projectId: 'p1', dataset: 'ds1'}})
|
|
1918
1920
|
*
|
|
1919
|
-
* //
|
|
1921
|
+
* // Or rely on context for the resource:
|
|
1920
1922
|
* // useDocumentToasts()
|
|
1921
1923
|
*
|
|
1922
1924
|
* return <div>...</div>
|
|
@@ -1934,10 +1936,19 @@ export declare function useDocumentEvent<
|
|
|
1934
1936
|
declare interface UseDocumentEventOptions<
|
|
1935
1937
|
TDataset extends string = string,
|
|
1936
1938
|
TProjectId extends string = string,
|
|
1937
|
-
> extends
|
|
1939
|
+
> extends ResourceHandle<TProjectId, TDataset> {
|
|
1938
1940
|
onEvent: (documentEvent: DocumentEvent) => void
|
|
1939
1941
|
}
|
|
1940
1942
|
|
|
1943
|
+
declare type UseDocumentOptions<
|
|
1944
|
+
TPath extends string | undefined = undefined,
|
|
1945
|
+
TDocumentType extends string = string,
|
|
1946
|
+
TDataset extends string = string,
|
|
1947
|
+
TProjectId extends string = string,
|
|
1948
|
+
> = DocumentHandle<TDocumentType, TDataset, TProjectId> & {
|
|
1949
|
+
path?: TPath
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1941
1952
|
/**
|
|
1942
1953
|
*
|
|
1943
1954
|
* @public
|
|
@@ -2023,7 +2034,7 @@ export declare function useDocumentPermissions(
|
|
|
2023
2034
|
* @public
|
|
2024
2035
|
*
|
|
2025
2036
|
* Attempts to infer preview values of a document (specified via a `DocumentHandle`),
|
|
2026
|
-
* including the document
|
|
2037
|
+
* including the document's `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.
|
|
2027
2038
|
* To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview
|
|
2028
2039
|
* resolution will only occur if the `ref` is intersecting the current viewport.
|
|
2029
2040
|
*
|
|
@@ -2032,18 +2043,22 @@ export declare function useDocumentPermissions(
|
|
|
2032
2043
|
* @remarks
|
|
2033
2044
|
* Values returned by this hook may not be as expected. It is currently unable to read preview values as defined in your schema;
|
|
2034
2045
|
* 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
|
|
2046
|
+
* We are anticipating being able to significantly improve this hook's functionality and output in a future release.
|
|
2036
2047
|
* For now, we recommend using {@link useDocumentProjection} for rendering individual document fields (or projections of those fields).
|
|
2037
2048
|
*
|
|
2049
|
+
* Internally, this hook is implemented as a specialized projection with post-processing logic.
|
|
2050
|
+
* It uses a fixed GROQ projection to fetch common preview fields (title, subtitle, media) and
|
|
2051
|
+
* transforms the results into the PreviewValue format.
|
|
2052
|
+
*
|
|
2038
2053
|
* @category Documents
|
|
2039
2054
|
* @param options - The document handle for the document you want to infer preview values for, and an optional ref
|
|
2040
2055
|
* @returns The inferred values for the given document and a boolean to indicate whether the resolution is pending
|
|
2041
2056
|
*
|
|
2042
2057
|
* @example Combining with useDocuments to render a collection of document previews
|
|
2043
2058
|
* ```
|
|
2044
|
-
* // PreviewComponent.
|
|
2045
|
-
* export default function PreviewComponent(
|
|
2046
|
-
* const { data: { title, subtitle, media }, isPending } = useDocumentPreview(
|
|
2059
|
+
* // PreviewComponent.tsx
|
|
2060
|
+
* export default function PreviewComponent(docHandle: DocumentHandle) {
|
|
2061
|
+
* const { data: { title, subtitle, media }, isPending } = useDocumentPreview(docHandle)
|
|
2047
2062
|
* return (
|
|
2048
2063
|
* <article style={{ opacity: isPending ? 0.5 : 1}}>
|
|
2049
2064
|
* {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}
|
|
@@ -2053,16 +2068,16 @@ export declare function useDocumentPermissions(
|
|
|
2053
2068
|
* )
|
|
2054
2069
|
* }
|
|
2055
2070
|
*
|
|
2056
|
-
* // DocumentList.
|
|
2057
|
-
* const { data } = useDocuments({
|
|
2071
|
+
* // DocumentList.tsx
|
|
2072
|
+
* const { data } = useDocuments({ documentType: 'movie' })
|
|
2058
2073
|
* return (
|
|
2059
2074
|
* <div>
|
|
2060
2075
|
* <h1>Movies</h1>
|
|
2061
2076
|
* <ul>
|
|
2062
2077
|
* {data.map(movie => (
|
|
2063
|
-
* <li key={movie.
|
|
2078
|
+
* <li key={movie.documentId}>
|
|
2064
2079
|
* <Suspense fallback='Loading…'>
|
|
2065
|
-
* <PreviewComponent
|
|
2080
|
+
* <PreviewComponent {...movie} />
|
|
2066
2081
|
* </Suspense>
|
|
2067
2082
|
* </li>
|
|
2068
2083
|
* ))}
|
|
@@ -2093,7 +2108,7 @@ export declare interface useDocumentPreviewOptions extends DocumentHandle {
|
|
|
2093
2108
|
* @category Types
|
|
2094
2109
|
*/
|
|
2095
2110
|
export declare interface useDocumentPreviewResults {
|
|
2096
|
-
/** The results of inferring the document
|
|
2111
|
+
/** The results of inferring the document's preview values */
|
|
2097
2112
|
data: PreviewValue
|
|
2098
2113
|
/** True when inferred preview values are being refreshed */
|
|
2099
2114
|
isPending: boolean
|
|
@@ -2109,80 +2124,11 @@ export declare interface useDocumentPreviewResults {
|
|
|
2109
2124
|
*
|
|
2110
2125
|
* @category Documents
|
|
2111
2126
|
* @remarks
|
|
2112
|
-
* This hook
|
|
2113
|
-
* - Using Typegen: Infers the return type based on the `documentType`, `dataset`, `projectId`, and `projection`.
|
|
2114
|
-
* - Using explicit type parameter: Allows specifying a custom return type `TData`.
|
|
2127
|
+
* This hook allows specifying an explicit type parameter `TData` for the projected result.
|
|
2115
2128
|
*
|
|
2116
2129
|
* @param options - An object containing the `DocumentHandle` properties (`documentId`, `documentType`, etc.), the `projection` string, optional `params`, and an optional `ref`.
|
|
2117
2130
|
* @returns An object containing the projection results (`data`) and a boolean indicating whether the resolution is pending (`isPending`). Note: Suspense handles initial loading states; `data` being `undefined` after initial loading means the document doesn't exist or the projection yielded no result.
|
|
2118
2131
|
*/
|
|
2119
|
-
/**
|
|
2120
|
-
* @public
|
|
2121
|
-
* Fetch a projection, relying on Typegen for the return type based on the handle and projection.
|
|
2122
|
-
*
|
|
2123
|
-
* @category Documents
|
|
2124
|
-
* @param options - Options including the document handle properties (`documentId`, `documentType`, etc.) and the `projection`.
|
|
2125
|
-
* @returns The projected data, typed based on Typegen.
|
|
2126
|
-
*
|
|
2127
|
-
* @example Using Typegen for a book preview
|
|
2128
|
-
* ```tsx
|
|
2129
|
-
* // ProjectionComponent.tsx
|
|
2130
|
-
* import {useDocumentProjection, type DocumentHandle} from '@sanity/sdk-react'
|
|
2131
|
-
* import {useRef} from 'react'
|
|
2132
|
-
* import {defineProjection} from 'groq'
|
|
2133
|
-
*
|
|
2134
|
-
* // Define props using DocumentHandle with the specific document type
|
|
2135
|
-
* type ProjectionComponentProps = {
|
|
2136
|
-
* doc: DocumentHandle<'book'> // Typegen knows 'book'
|
|
2137
|
-
* }
|
|
2138
|
-
*
|
|
2139
|
-
* // This is required for typegen to generate the correct return type
|
|
2140
|
-
* const myProjection = defineProjection(`{
|
|
2141
|
-
* title,
|
|
2142
|
-
* 'coverImage': cover.asset->url,
|
|
2143
|
-
* 'authors': array::join(authors[]->{'name': firstName + ' ' + lastName}.name, ', ')
|
|
2144
|
-
* }`)
|
|
2145
|
-
*
|
|
2146
|
-
* export default function ProjectionComponent({ doc }: ProjectionComponentProps) {
|
|
2147
|
-
* const ref = useRef(null) // Optional ref to track viewport intersection for lazy loading
|
|
2148
|
-
*
|
|
2149
|
-
* // Spread the doc handle into the options
|
|
2150
|
-
* // Typegen infers the return type based on 'book' and the projection
|
|
2151
|
-
* const { data } = useDocumentProjection({
|
|
2152
|
-
* ...doc, // Pass the handle properties
|
|
2153
|
-
* ref,
|
|
2154
|
-
* projection: myProjection,
|
|
2155
|
-
* })
|
|
2156
|
-
*
|
|
2157
|
-
* // Suspense handles initial load, check for data existence after
|
|
2158
|
-
* return (
|
|
2159
|
-
* <article ref={ref}>
|
|
2160
|
-
* <h2>{data.title ?? 'Untitled'}</h2>
|
|
2161
|
-
* {data.coverImage && <img src={data.coverImage} alt={data.title} />}
|
|
2162
|
-
* <p>{data.authors ?? 'Unknown authors'}</p>
|
|
2163
|
-
* </article>
|
|
2164
|
-
* )
|
|
2165
|
-
* }
|
|
2166
|
-
*
|
|
2167
|
-
* // Usage:
|
|
2168
|
-
* // import {createDocumentHandle} from '@sanity/sdk-react'
|
|
2169
|
-
* // const myDocHandle = createDocumentHandle({ documentId: 'book123', documentType: 'book' })
|
|
2170
|
-
* // <Suspense fallback='Loading preview...'>
|
|
2171
|
-
* // <ProjectionComponent doc={myDocHandle} />
|
|
2172
|
-
* // </Suspense>
|
|
2173
|
-
* ```
|
|
2174
|
-
*/
|
|
2175
|
-
export declare function useDocumentProjection<
|
|
2176
|
-
TProjection extends string = string,
|
|
2177
|
-
TDocumentType extends string = string,
|
|
2178
|
-
TDataset extends string = string,
|
|
2179
|
-
TProjectId extends string = string,
|
|
2180
|
-
>(
|
|
2181
|
-
options: useDocumentProjectionOptions<TProjection, TDocumentType, TDataset, TProjectId>,
|
|
2182
|
-
): useDocumentProjectionResults<
|
|
2183
|
-
SanityProjectionResult<TProjection, TDocumentType, `${TProjectId}.${TDataset}`>
|
|
2184
|
-
>
|
|
2185
|
-
|
|
2186
2132
|
/**
|
|
2187
2133
|
* @public
|
|
2188
2134
|
* Fetch a projection with an explicitly defined return type `TData`.
|
|
@@ -2241,7 +2187,7 @@ export declare interface useDocumentProjectionOptions<
|
|
|
2241
2187
|
TDocumentType extends string = string,
|
|
2242
2188
|
TDataset extends string = string,
|
|
2243
2189
|
TProjectId extends string = string,
|
|
2244
|
-
> extends
|
|
2190
|
+
> extends DocumentHandle<TDocumentType, TDataset, TProjectId> {
|
|
2245
2191
|
/** The GROQ projection string */
|
|
2246
2192
|
projection: TProjection
|
|
2247
2193
|
/** Optional parameters for the projection query */
|
|
@@ -2272,18 +2218,16 @@ export declare interface useDocumentProjectionResults<TData> {
|
|
|
2272
2218
|
* @returns An object containing the list of document handles, the loading state, the total count of retrieved document handles, and a function to load more
|
|
2273
2219
|
*
|
|
2274
2220
|
* @remarks
|
|
2275
|
-
* - The returned document handles include
|
|
2221
|
+
* - The returned document handles include resource information from the current Sanity instance
|
|
2276
2222
|
* - This makes them ready to use with document operations and other document hooks
|
|
2277
|
-
* - The hook automatically uses the correct Sanity instance based on the
|
|
2223
|
+
* - The hook automatically uses the correct Sanity instance based on the resource in the options
|
|
2278
2224
|
*
|
|
2279
2225
|
* @example Basic infinite list with loading more
|
|
2280
2226
|
* ```tsx
|
|
2281
2227
|
* import {
|
|
2282
2228
|
* useDocuments,
|
|
2283
|
-
* createDatasetHandle,
|
|
2284
|
-
* type DatasetHandle,
|
|
2285
2229
|
* type DocumentHandle,
|
|
2286
|
-
* type
|
|
2230
|
+
* type DocumentResource,
|
|
2287
2231
|
* } from '@sanity/sdk-react'
|
|
2288
2232
|
* import {Suspense} from 'react'
|
|
2289
2233
|
*
|
|
@@ -2299,14 +2243,14 @@ export declare interface useDocumentProjectionResults<TData> {
|
|
|
2299
2243
|
*
|
|
2300
2244
|
* // Define props for the list component
|
|
2301
2245
|
* interface DocumentListProps {
|
|
2302
|
-
*
|
|
2246
|
+
* resource: DocumentResource
|
|
2303
2247
|
* documentType: string
|
|
2304
2248
|
* search?: string
|
|
2305
2249
|
* }
|
|
2306
2250
|
*
|
|
2307
|
-
* function DocumentList({
|
|
2251
|
+
* function DocumentList({resource, documentType, search}: DocumentListProps) {
|
|
2308
2252
|
* const { data, hasMore, isPending, loadMore, count } = useDocuments({
|
|
2309
|
-
*
|
|
2253
|
+
* resource,
|
|
2310
2254
|
* documentType,
|
|
2311
2255
|
* search,
|
|
2312
2256
|
* batchSize: 10,
|
|
@@ -2320,7 +2264,7 @@ export declare interface useDocumentProjectionResults<TData> {
|
|
|
2320
2264
|
* {data.map((docHandle) => (
|
|
2321
2265
|
* <li key={docHandle.documentId}>
|
|
2322
2266
|
* <Suspense fallback="Loading…">
|
|
2323
|
-
* <MyDocumentComponent
|
|
2267
|
+
* <MyDocumentComponent doc={docHandle} />
|
|
2324
2268
|
* </Suspense>
|
|
2325
2269
|
* </li>
|
|
2326
2270
|
* ))}
|
|
@@ -2335,8 +2279,7 @@ export declare interface useDocumentProjectionResults<TData> {
|
|
|
2335
2279
|
* }
|
|
2336
2280
|
*
|
|
2337
2281
|
* // Usage:
|
|
2338
|
-
* //
|
|
2339
|
-
* // <DocumentList dataset={myDatasetHandle} documentType="post" search="Sanity" />
|
|
2282
|
+
* // <DocumentList resource={{projectId: 'p1', dataset: 'production'}} documentType="post" search="Sanity" />
|
|
2340
2283
|
* ```
|
|
2341
2284
|
*
|
|
2342
2285
|
* @example Using `filter` and `params` options for narrowing a collection
|
|
@@ -2381,7 +2324,7 @@ export declare function useDocuments<
|
|
|
2381
2324
|
filter,
|
|
2382
2325
|
orderings,
|
|
2383
2326
|
documentType,
|
|
2384
|
-
...
|
|
2327
|
+
...rawOptions
|
|
2385
2328
|
}: DocumentsOptions<TDocumentType, TDataset, TProjectId>): DocumentsResponse<
|
|
2386
2329
|
TDocumentType,
|
|
2387
2330
|
TDataset,
|
|
@@ -2393,9 +2336,9 @@ declare type UseDocumentSyncStatus = {
|
|
|
2393
2336
|
* Exposes the document's sync status between local and remote document states.
|
|
2394
2337
|
*
|
|
2395
2338
|
* @category Documents
|
|
2396
|
-
* @param doc - The document handle to get sync status for. If you pass a `
|
|
2397
|
-
* the document will be read from the specified
|
|
2398
|
-
* the
|
|
2339
|
+
* @param doc - The document handle to get sync status for. If you pass a `resource` in the handle,
|
|
2340
|
+
* the document will be read from the specified resource. If no `resource` is provided,
|
|
2341
|
+
* the resource will be resolved from context.
|
|
2399
2342
|
* @returns `true` if local changes are synced with remote, `false` if changes are pending. Note: Suspense handles loading states.
|
|
2400
2343
|
* @example Show sync status indicator
|
|
2401
2344
|
* ```tsx
|
|
@@ -2430,67 +2373,28 @@ declare type UseDocumentSyncStatus = {
|
|
|
2430
2373
|
*/
|
|
2431
2374
|
export declare const useDocumentSyncStatus: UseDocumentSyncStatus
|
|
2432
2375
|
|
|
2433
|
-
/**
|
|
2434
|
-
* @public
|
|
2435
|
-
* Edit an entire document, relying on Typegen for the type.
|
|
2436
|
-
*
|
|
2437
|
-
* @param options - Document options including `documentId`, `documentType`, and optionally `projectId`/`dataset`.
|
|
2438
|
-
* @returns A stable function to update the document state. Accepts either the new document state or an updater function `(currentValue) => nextValue`.
|
|
2439
|
-
* Returns a promise resolving to the {@link ActionsResult}.
|
|
2440
|
-
*/
|
|
2441
|
-
export declare function useEditDocument<
|
|
2442
|
-
TDocumentType extends string = string,
|
|
2443
|
-
TDataset extends string = string,
|
|
2444
|
-
TProjectId extends string = string,
|
|
2445
|
-
>(
|
|
2446
|
-
options: DocumentOptions<undefined, TDocumentType, TDataset, TProjectId>,
|
|
2447
|
-
): (
|
|
2448
|
-
nextValue: Updater<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>>,
|
|
2449
|
-
) => Promise<ActionsResult<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>>>
|
|
2450
|
-
|
|
2451
|
-
/**
|
|
2452
|
-
* @public
|
|
2453
|
-
* Edit a specific path within a document, relying on Typegen for the type.
|
|
2454
|
-
*
|
|
2455
|
-
* @param options - Document options including `documentId`, `documentType`, `path`, and optionally `projectId`/`dataset`.
|
|
2456
|
-
* @returns A stable function to update the value at the specified path. Accepts either the new value or an updater function `(currentValue) => nextValue`.
|
|
2457
|
-
* Returns a promise resolving to the {@link ActionsResult}.
|
|
2458
|
-
*/
|
|
2459
|
-
export declare function useEditDocument<
|
|
2460
|
-
TPath extends string = string,
|
|
2461
|
-
TDocumentType extends string = string,
|
|
2462
|
-
TDataset extends string = string,
|
|
2463
|
-
TProjectId extends string = string,
|
|
2464
|
-
>(
|
|
2465
|
-
options: DocumentOptions<TPath, TDocumentType, TDataset, TProjectId>,
|
|
2466
|
-
): (
|
|
2467
|
-
nextValue: Updater<
|
|
2468
|
-
JsonMatch<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>, TPath>
|
|
2469
|
-
>,
|
|
2470
|
-
) => Promise<ActionsResult<SanityDocument_2<TDocumentType, `${TProjectId}.${TDataset}`>>>
|
|
2471
|
-
|
|
2472
2376
|
/**
|
|
2473
2377
|
* @public
|
|
2474
2378
|
* Edit an entire document with an explicit type `TData`.
|
|
2475
2379
|
*
|
|
2476
|
-
* @param options - Document options including `documentId` and optionally `
|
|
2380
|
+
* @param options - Document options including `documentId` and optionally `resource` or `resourceName`.
|
|
2477
2381
|
* @returns A stable function to update the document state. Accepts either the new document state (`TData`) or an updater function `(currentValue: TData) => nextValue: TData`.
|
|
2478
2382
|
* Returns a promise resolving to the {@link ActionsResult}.
|
|
2479
2383
|
*/
|
|
2480
2384
|
export declare function useEditDocument<TData>(
|
|
2481
|
-
options:
|
|
2385
|
+
options: EditDocumentOptions<undefined>,
|
|
2482
2386
|
): (nextValue: Updater<TData>) => Promise<ActionsResult>
|
|
2483
2387
|
|
|
2484
2388
|
/**
|
|
2485
2389
|
* @public
|
|
2486
2390
|
* Edit a specific path within a document with an explicit type `TData`.
|
|
2487
2391
|
*
|
|
2488
|
-
* @param options - Document options including `documentId`, `path`, and optionally `
|
|
2392
|
+
* @param options - Document options including `documentId`, `path`, and optionally `resource` or `resourceName`.
|
|
2489
2393
|
* @returns A stable function to update the value at the specified path. Accepts either the new value (`TData`) or an updater function `(currentValue: TData) => nextValue: TData`.
|
|
2490
2394
|
* Returns a promise resolving to the {@link ActionsResult}.
|
|
2491
2395
|
*/
|
|
2492
2396
|
export declare function useEditDocument<TData>(
|
|
2493
|
-
options:
|
|
2397
|
+
options: EditDocumentOptions<string>,
|
|
2494
2398
|
): (nextValue: Updater<TData>) => Promise<ActionsResult>
|
|
2495
2399
|
|
|
2496
2400
|
/**
|
|
@@ -2626,8 +2530,7 @@ export declare const useLogOut: () => () => Promise<void>
|
|
|
2626
2530
|
export declare function useManageFavorite({
|
|
2627
2531
|
documentId,
|
|
2628
2532
|
documentType,
|
|
2629
|
-
|
|
2630
|
-
dataset: paramDataset,
|
|
2533
|
+
resource: paramResource,
|
|
2631
2534
|
resourceId: paramResourceId,
|
|
2632
2535
|
resourceType,
|
|
2633
2536
|
schemaName,
|
|
@@ -2703,18 +2606,16 @@ export declare function useNavigateToStudioDocument(
|
|
|
2703
2606
|
* @returns An object containing the list of document handles, pagination details, and functions to navigate between pages
|
|
2704
2607
|
*
|
|
2705
2608
|
* @remarks
|
|
2706
|
-
* - The returned document handles include
|
|
2609
|
+
* - The returned document handles include resource information from the current Sanity instance
|
|
2707
2610
|
* - This makes them ready to use with document operations and other document hooks
|
|
2708
|
-
* - The hook automatically uses the correct Sanity instance based on the
|
|
2611
|
+
* - The hook automatically uses the correct Sanity instance based on the resource in the options
|
|
2709
2612
|
*
|
|
2710
2613
|
* @example Paginated list of documents with navigation
|
|
2711
2614
|
* ```tsx
|
|
2712
2615
|
* import {
|
|
2713
2616
|
* usePaginatedDocuments,
|
|
2714
|
-
* createDatasetHandle,
|
|
2715
|
-
* type DatasetHandle,
|
|
2716
2617
|
* type DocumentHandle,
|
|
2717
|
-
* type
|
|
2618
|
+
* type DocumentResource,
|
|
2718
2619
|
* useDocumentProjection
|
|
2719
2620
|
* } from '@sanity/sdk-react'
|
|
2720
2621
|
* import {Suspense} from 'react'
|
|
@@ -2737,10 +2638,10 @@ export declare function useNavigateToStudioDocument(
|
|
|
2737
2638
|
* // Define props for the list component
|
|
2738
2639
|
* interface PaginatedDocumentListProps {
|
|
2739
2640
|
* documentType: string
|
|
2740
|
-
*
|
|
2641
|
+
* resource?: DocumentResource
|
|
2741
2642
|
* }
|
|
2742
2643
|
*
|
|
2743
|
-
* function PaginatedDocumentList({documentType,
|
|
2644
|
+
* function PaginatedDocumentList({documentType, resource}: PaginatedDocumentListProps) {
|
|
2744
2645
|
* const {
|
|
2745
2646
|
* data,
|
|
2746
2647
|
* isPending,
|
|
@@ -2751,7 +2652,7 @@ export declare function useNavigateToStudioDocument(
|
|
|
2751
2652
|
* hasNextPage,
|
|
2752
2653
|
* hasPreviousPage
|
|
2753
2654
|
* } = usePaginatedDocuments({
|
|
2754
|
-
*
|
|
2655
|
+
* resource,
|
|
2755
2656
|
* documentType,
|
|
2756
2657
|
* pageSize: 10,
|
|
2757
2658
|
* orderings: [{field: '_createdAt', direction: 'desc'}],
|
|
@@ -2783,8 +2684,7 @@ export declare function useNavigateToStudioDocument(
|
|
|
2783
2684
|
* }
|
|
2784
2685
|
*
|
|
2785
2686
|
* // Usage:
|
|
2786
|
-
* //
|
|
2787
|
-
* // <PaginatedDocumentList dataset={myDatasetHandle} documentType="post" />
|
|
2687
|
+
* // <PaginatedDocumentList resource={{projectId: 'p1', dataset: 'production'}} documentType="post" />
|
|
2788
2688
|
* ```
|
|
2789
2689
|
*/
|
|
2790
2690
|
export declare function usePaginatedDocuments<
|
|
@@ -2798,20 +2698,13 @@ export declare function usePaginatedDocuments<
|
|
|
2798
2698
|
params,
|
|
2799
2699
|
orderings,
|
|
2800
2700
|
search,
|
|
2801
|
-
...
|
|
2701
|
+
...rawOptions
|
|
2802
2702
|
}: PaginatedDocumentsOptions<TDocumentType, TDataset, TProjectId>): PaginatedDocumentsResponse<
|
|
2803
2703
|
TDocumentType,
|
|
2804
2704
|
TDataset,
|
|
2805
2705
|
TProjectId
|
|
2806
2706
|
>
|
|
2807
2707
|
|
|
2808
|
-
/**
|
|
2809
|
-
* @public
|
|
2810
|
-
*/
|
|
2811
|
-
declare type UsePerspective = {
|
|
2812
|
-
(perspectiveHandle: PerspectiveHandle): string | string[]
|
|
2813
|
-
}
|
|
2814
|
-
|
|
2815
2708
|
/**
|
|
2816
2709
|
* @public
|
|
2817
2710
|
* @function
|
|
@@ -2825,22 +2718,34 @@ declare type UsePerspective = {
|
|
|
2825
2718
|
* @example
|
|
2826
2719
|
* ```tsx
|
|
2827
2720
|
* import {usePerspective, useQuery} from '@sanity/sdk-react'
|
|
2828
|
-
|
|
2829
|
-
* const perspective = usePerspective({
|
|
2830
|
-
*
|
|
2831
|
-
*
|
|
2721
|
+
*
|
|
2722
|
+
* const perspective = usePerspective({
|
|
2723
|
+
* perspective: 'rxg1346',
|
|
2724
|
+
* resource: {projectId: 'abc123', dataset: 'production'},
|
|
2725
|
+
* })
|
|
2726
|
+
* const {data} = useQuery<Movie[]>({
|
|
2727
|
+
* query: '*[_type == "movie"]',
|
|
2728
|
+
* perspective,
|
|
2832
2729
|
* })
|
|
2833
2730
|
* ```
|
|
2834
2731
|
*
|
|
2835
2732
|
* @returns The perspective for the given perspective handle.
|
|
2836
2733
|
*/
|
|
2734
|
+
declare type UsePerspective = {
|
|
2735
|
+
(perspectiveHandle?: ResourceHandle): string | string[]
|
|
2736
|
+
}
|
|
2737
|
+
|
|
2738
|
+
/**
|
|
2739
|
+
* @public
|
|
2740
|
+
* @function
|
|
2741
|
+
*/
|
|
2837
2742
|
export declare const usePerspective: UsePerspective
|
|
2838
2743
|
|
|
2839
2744
|
/**
|
|
2840
2745
|
* A hook for subscribing to presence information for the current project.
|
|
2841
2746
|
* @public
|
|
2842
2747
|
*/
|
|
2843
|
-
export declare function usePresence(): {
|
|
2748
|
+
export declare function usePresence(options?: ResourceHandle): {
|
|
2844
2749
|
locations: UserPresence[]
|
|
2845
2750
|
}
|
|
2846
2751
|
|
|
@@ -2850,12 +2755,12 @@ declare type UseProject = {
|
|
|
2850
2755
|
* Returns metadata for a given project
|
|
2851
2756
|
*
|
|
2852
2757
|
* @category Projects
|
|
2853
|
-
* @param
|
|
2758
|
+
* @param projectHandle - An optional project handle identifying which project to retrieve metadata for
|
|
2854
2759
|
* @returns The metadata for the project
|
|
2855
2760
|
* @example
|
|
2856
2761
|
* ```tsx
|
|
2857
2762
|
* function ProjectMetadata({ projectId }: { projectId: string }) {
|
|
2858
|
-
* const project = useProject(projectId)
|
|
2763
|
+
* const project = useProject({ projectId })
|
|
2859
2764
|
*
|
|
2860
2765
|
* return (
|
|
2861
2766
|
* <figure style={{ backgroundColor: project.metadata.color || 'lavender'}}>
|
|
@@ -2911,79 +2816,11 @@ declare type UseProjects = <TIncludeMembers extends boolean = false>(options?: {
|
|
|
2911
2816
|
*/
|
|
2912
2817
|
export declare const useProjects: UseProjects
|
|
2913
2818
|
|
|
2914
|
-
/**
|
|
2915
|
-
* @public
|
|
2916
|
-
* Executes a GROQ query, inferring the result type from the query string and options.
|
|
2917
|
-
* Leverages Sanity Typegen if configured for enhanced type safety.
|
|
2918
|
-
*
|
|
2919
|
-
* @param options - Configuration for the query, including `query`, optional `params`, `projectId`, `dataset`, etc.
|
|
2920
|
-
* @returns An object containing `data` (typed based on the query) and `isPending` (for transitions).
|
|
2921
|
-
*
|
|
2922
|
-
* @example Basic usage (Inferred Type)
|
|
2923
|
-
* ```tsx
|
|
2924
|
-
* import {useQuery} from '@sanity/sdk-react'
|
|
2925
|
-
* import {defineQuery} from 'groq'
|
|
2926
|
-
*
|
|
2927
|
-
* const myQuery = defineQuery(`*[_type == "movie"]{_id, title}`)
|
|
2928
|
-
*
|
|
2929
|
-
* function MovieList() {
|
|
2930
|
-
* // Typegen infers the return type for data
|
|
2931
|
-
* const {data} = useQuery({ query: myQuery })
|
|
2932
|
-
*
|
|
2933
|
-
* return (
|
|
2934
|
-
* <div>
|
|
2935
|
-
* <h2>Movies</h2>
|
|
2936
|
-
* <ul>
|
|
2937
|
-
* {data.map(movie => <li key={movie._id}>{movie.title}</li>)}
|
|
2938
|
-
* </ul>
|
|
2939
|
-
* </div>
|
|
2940
|
-
* )
|
|
2941
|
-
* }
|
|
2942
|
-
* // Suspense boundary should wrap <MovieList /> for initial load
|
|
2943
|
-
* ```
|
|
2944
|
-
*
|
|
2945
|
-
* @example Using parameters (Inferred Type)
|
|
2946
|
-
* ```tsx
|
|
2947
|
-
* import {useQuery} from '@sanity/sdk-react'
|
|
2948
|
-
* import {defineQuery} from 'groq'
|
|
2949
|
-
*
|
|
2950
|
-
* const myQuery = defineQuery(`*[_type == "movie" && _id == $id][0]`)
|
|
2951
|
-
*
|
|
2952
|
-
* function MovieDetails({movieId}: {movieId: string}) {
|
|
2953
|
-
* // Typegen infers the return type based on query and params
|
|
2954
|
-
* const {data, isPending} = useQuery({
|
|
2955
|
-
* query: myQuery,
|
|
2956
|
-
* params: { id: movieId }
|
|
2957
|
-
* })
|
|
2958
|
-
*
|
|
2959
|
-
* return (
|
|
2960
|
-
* // utilize `isPending` to signal to users that new data is coming in
|
|
2961
|
-
* // (e.g. the `movieId` changed and we're loading in the new one)
|
|
2962
|
-
* <div style={{ opacity: isPending ? 0.5 : 1 }}>
|
|
2963
|
-
* {data ? <h1>{data.title}</h1> : <p>Movie not found</p>}
|
|
2964
|
-
* </div>
|
|
2965
|
-
* )
|
|
2966
|
-
* }
|
|
2967
|
-
* ```
|
|
2968
|
-
*/
|
|
2969
|
-
export declare function useQuery<
|
|
2970
|
-
TQuery extends string = string,
|
|
2971
|
-
TDataset extends string = string,
|
|
2972
|
-
TProjectId extends string = string,
|
|
2973
|
-
>(
|
|
2974
|
-
options: UseQueryOptions<TQuery, TDataset, TProjectId>,
|
|
2975
|
-
): {
|
|
2976
|
-
/** The query result, typed based on the GROQ query string */
|
|
2977
|
-
data: SanityQueryResult<TQuery, `${TProjectId}.${TDataset}`>
|
|
2978
|
-
/** True if a query transition is in progress */
|
|
2979
|
-
isPending: boolean
|
|
2980
|
-
}
|
|
2981
|
-
|
|
2982
2819
|
/**
|
|
2983
2820
|
* @public
|
|
2984
2821
|
* Executes a GROQ query with an explicitly provided result type `TData`.
|
|
2985
2822
|
*
|
|
2986
|
-
* @param options - Configuration for the query, including `query`, optional `params`, `
|
|
2823
|
+
* @param options - Configuration for the query, including `query`, optional `params`, `resource`, etc.
|
|
2987
2824
|
* @returns An object containing `data` (cast to `TData`) and `isPending` (indicates whether a query resolution is pending; note that Suspense handles initial loading states). *
|
|
2988
2825
|
* @example Manually typed query result
|
|
2989
2826
|
* ```tsx
|
|
@@ -3007,23 +2844,13 @@ export declare function useQuery<
|
|
|
3007
2844
|
* }
|
|
3008
2845
|
* ```
|
|
3009
2846
|
*/
|
|
3010
|
-
export declare function useQuery<TData>(options:
|
|
2847
|
+
export declare function useQuery<TData>(options: ReactQueryOptions): {
|
|
3011
2848
|
/** The query result, cast to the provided type TData */
|
|
3012
2849
|
data: TData
|
|
3013
2850
|
/** True if another query is resolving in the background (suspense handles the initial loading state) */
|
|
3014
2851
|
isPending: boolean
|
|
3015
2852
|
}
|
|
3016
2853
|
|
|
3017
|
-
/**
|
|
3018
|
-
* Hook options for useQuery, supporting both direct source and sourceName.
|
|
3019
|
-
* @beta
|
|
3020
|
-
*/
|
|
3021
|
-
declare type UseQueryOptions<
|
|
3022
|
-
TQuery extends string = string,
|
|
3023
|
-
TDataset extends string = string,
|
|
3024
|
-
TProjectId extends string = string,
|
|
3025
|
-
> = WithSourceNameSupport<QueryOptions<TQuery, TDataset, TProjectId>>
|
|
3026
|
-
|
|
3027
2854
|
/**
|
|
3028
2855
|
* @internal
|
|
3029
2856
|
* Hook for managing document interaction history in Sanity Studio.
|
|
@@ -3086,6 +2913,30 @@ declare interface UseRecordDocumentHistoryEventProps extends DocumentHandle {
|
|
|
3086
2913
|
schemaName?: string
|
|
3087
2914
|
}
|
|
3088
2915
|
|
|
2916
|
+
/**
|
|
2917
|
+
* Returns the current {@link DocumentResource} from context.
|
|
2918
|
+
*
|
|
2919
|
+
* @public
|
|
2920
|
+
*
|
|
2921
|
+
* @category Platform
|
|
2922
|
+
* @returns The resource set by the nearest `ResourceProvider`, or `undefined`
|
|
2923
|
+
*
|
|
2924
|
+
* @remarks
|
|
2925
|
+
* With the flat instance model, nested `ResourceProvider`s override the
|
|
2926
|
+
* active resource via React context rather than creating child instances.
|
|
2927
|
+
* Use this hook when you need the current project/dataset/resource for a
|
|
2928
|
+
* subtree instead of reading from `useSanityInstance().config`.
|
|
2929
|
+
*
|
|
2930
|
+
* @example Get the current resource
|
|
2931
|
+
* ```tsx
|
|
2932
|
+
* const resource = useResource()
|
|
2933
|
+
* if (resource && 'projectId' in resource) {
|
|
2934
|
+
* console.log(resource.projectId, resource.dataset)
|
|
2935
|
+
* }
|
|
2936
|
+
* ```
|
|
2937
|
+
*/
|
|
2938
|
+
export declare function useResource(): DocumentResource | undefined
|
|
2939
|
+
|
|
3089
2940
|
/**
|
|
3090
2941
|
* @public
|
|
3091
2942
|
* @category Types
|
|
@@ -3125,60 +2976,26 @@ export declare interface UsersResult {
|
|
|
3125
2976
|
}
|
|
3126
2977
|
|
|
3127
2978
|
/**
|
|
3128
|
-
* Retrieves the current Sanity instance
|
|
2979
|
+
* Retrieves the current Sanity instance from React context.
|
|
3129
2980
|
*
|
|
3130
2981
|
* @public
|
|
3131
2982
|
*
|
|
3132
2983
|
* @category Platform
|
|
3133
|
-
* @
|
|
3134
|
-
* @returns The current or matching Sanity instance
|
|
2984
|
+
* @returns The current instance
|
|
3135
2985
|
*
|
|
3136
2986
|
* @remarks
|
|
3137
|
-
* This hook accesses the nearest Sanity instance from
|
|
3138
|
-
*
|
|
3139
|
-
* that matches the specified configuration using shallow comparison of properties.
|
|
3140
|
-
*
|
|
3141
|
-
* The hook must be used within a component wrapped by a `ResourceProvider` or `SanityApp`.
|
|
3142
|
-
*
|
|
3143
|
-
* Use this hook when you need to:
|
|
3144
|
-
* - Access the current SanityInstance from context
|
|
3145
|
-
* - Find a specific instance with matching project/dataset configuration
|
|
3146
|
-
* - Access a parent instance with specific configuration values
|
|
2987
|
+
* This hook accesses the nearest Sanity instance from React context,
|
|
2988
|
+
* provided by a `ResourceProvider` or `SanityApp`.
|
|
3147
2989
|
*
|
|
3148
2990
|
* @example Get the current instance
|
|
3149
2991
|
* ```tsx
|
|
3150
|
-
* // Get the current instance from context
|
|
3151
2992
|
* const instance = useSanityInstance()
|
|
3152
|
-
* console.log(instance.config
|
|
3153
|
-
* ```
|
|
3154
|
-
*
|
|
3155
|
-
* @example Find an instance with specific configuration
|
|
3156
|
-
* ```tsx
|
|
3157
|
-
* // Find an instance matching the given project and dataset
|
|
3158
|
-
* const instance = useSanityInstance({
|
|
3159
|
-
* projectId: 'abc123',
|
|
3160
|
-
* dataset: 'production'
|
|
3161
|
-
* })
|
|
3162
|
-
*
|
|
3163
|
-
* // Use instance for API calls
|
|
3164
|
-
* const fetchDocument = (docId) => {
|
|
3165
|
-
* // Instance is guaranteed to have the matching config
|
|
3166
|
-
* return client.fetch(`*[_id == $id][0]`, { id: docId })
|
|
3167
|
-
* }
|
|
3168
|
-
* ```
|
|
3169
|
-
*
|
|
3170
|
-
* @example Match partial configuration
|
|
3171
|
-
* ```tsx
|
|
3172
|
-
* // Find an instance with specific auth configuration
|
|
3173
|
-
* const instance = useSanityInstance({
|
|
3174
|
-
* auth: { requireLogin: true }
|
|
3175
|
-
* })
|
|
2993
|
+
* console.log(instance.config)
|
|
3176
2994
|
* ```
|
|
3177
2995
|
*
|
|
3178
2996
|
* @throws Error if no SanityInstance is found in context
|
|
3179
|
-
* @throws Error if no matching instance is found for the provided config
|
|
3180
2997
|
*/
|
|
3181
|
-
export declare const useSanityInstance: (
|
|
2998
|
+
export declare const useSanityInstance: () => SanityInstance
|
|
3182
2999
|
|
|
3183
3000
|
/**
|
|
3184
3001
|
* Hook that fetches studio workspaces and organizes them by projectId:dataset
|
|
@@ -3368,26 +3185,19 @@ export declare type WindowMessageHandler<TFrameMessage extends FrameMessage> = (
|
|
|
3368
3185
|
) => TFrameMessage['response']
|
|
3369
3186
|
|
|
3370
3187
|
/**
|
|
3371
|
-
*
|
|
3372
|
-
* This
|
|
3373
|
-
*
|
|
3374
|
-
*
|
|
3375
|
-
* functions having sources explicitly passed will reduce complexity.
|
|
3376
|
-
*
|
|
3377
|
-
* @typeParam T - The core type to extend (must have optional `source` field)
|
|
3378
|
-
* @beta
|
|
3188
|
+
* You should generally prefer to use the React-layer handle types (ResourceHandle, DocumentHandle) from '\@sanity/sdk-react' instead.
|
|
3189
|
+
* This type is useful for non-handles (like document actions) that we still want to resolve resources for.
|
|
3190
|
+
* Adds React hook support (resourceName resolution) to core types.
|
|
3191
|
+
* @internal
|
|
3379
3192
|
*/
|
|
3380
|
-
declare type
|
|
3381
|
-
|
|
3382
|
-
source?: DocumentSource
|
|
3383
|
-
},
|
|
3384
|
-
> = T & {
|
|
3193
|
+
declare type WithResourceNameSupport<T> = Omit<T, 'resource'> & {
|
|
3194
|
+
resource?: DocumentResource
|
|
3385
3195
|
/**
|
|
3386
|
-
* Optional name of a
|
|
3387
|
-
* If provided, will be resolved to a `
|
|
3196
|
+
* Optional name of a resource to resolve from context.
|
|
3197
|
+
* If provided, will be resolved to a `DocumentResource` via `ResourcesContext`.
|
|
3388
3198
|
* @beta
|
|
3389
3199
|
*/
|
|
3390
|
-
|
|
3200
|
+
resourceName?: string
|
|
3391
3201
|
}
|
|
3392
3202
|
|
|
3393
3203
|
declare interface WorkspacesByProjectIdDataset {
|