@sanity/sdk-react 2.15.0 → 2.17.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 CHANGED
@@ -146,7 +146,7 @@ await apply([publishDocument(handle1), publishDocument(handle2), deleteDocument(
146
146
  useDocumentEvent({
147
147
  ...handle,
148
148
  onEvent: (event) => {
149
- // event.type: 'documentEdited' | 'documentPublished' | 'documentDeleted' | ...
149
+ // event.type: 'edited' | 'published' | 'deleted' | 'created' | 'unpublished' | 'discarded' | ...
150
150
  console.log(event.type, event.documentId)
151
151
  },
152
152
  })
@@ -297,6 +297,27 @@ function ArticleList() {
297
297
 
298
298
  ---
299
299
 
300
+ ### Viewport-Based Lazy Loading
301
+
302
+ Pass a `ref` to `useDocumentProjection` to fetch only when the element enters the viewport. Wrap the component in Suspense like any other data hook:
303
+
304
+ ```tsx
305
+ import {useRef} from 'react'
306
+
307
+ function LazyArticleCard({handle}: {handle: DocumentHandle}) {
308
+ const ref = useRef<HTMLDivElement>(null)
309
+ const {data} = useDocumentProjection({
310
+ ...handle,
311
+ ref, // Only fetches when element is visible
312
+ projection: '{ title }',
313
+ })
314
+
315
+ return <div ref={ref}>{data?.title}</div>
316
+ }
317
+ ```
318
+
319
+ ---
320
+
300
321
  ### Draft/Published Model
301
322
 
302
323
  Sanity has two document states:
package/dist/index.d.ts CHANGED
@@ -34,6 +34,7 @@ import { OrganizationsOptions } from "@sanity/sdk";
34
34
  import { PathChangeMessage } from "@sanity/message-protocol";
35
35
  import { PreviewValue } from "@sanity/sdk";
36
36
  import { Project } from "@sanity/sdk";
37
+ import { ProjectHandle } from "@sanity/sdk";
37
38
  import { ProjectOptions } from "@sanity/sdk";
38
39
  import { ProjectsOptions } from "@sanity/sdk";
39
40
  import { PropsWithChildren } from "react";
@@ -668,6 +669,35 @@ export declare interface SanityAppProps {
668
669
  resources?: Record<string, DocumentResource>;
669
670
  children: React.ReactNode;
670
671
  fallback: React.ReactNode;
672
+ /**
673
+ * Set this to automatically fetch and register the organization's media library and canvas as named resources.
674
+ * These resources will be available to hooks as `media-library` and `canvas`.
675
+ *
676
+ * The SDK App must be running in the organization's Dashboard to use this feature.
677
+ *
678
+ * @example
679
+ * ```tsx
680
+ *
681
+ * const MyApp = () => {
682
+ * // should "just work" because of inferMediaLibraryAndCanvas.
683
+ * const {data: assets} = useDocuments({
684
+ * documentType: 'sanity.asset',
685
+ * resourceName: 'media-library',
686
+ * })
687
+ * return (
688
+ * <div>
689
+ * {assets.map((asset) => (
690
+ * <div key={asset._id}>{asset.originalFilename}</div>
691
+ * ))}
692
+ * </div>
693
+ * )
694
+ * }
695
+ * <SanityApp inferMediaLibraryAndCanvas>
696
+ * <MyApp />
697
+ * </SanityApp>
698
+ * ```
699
+ */
700
+ inferMediaLibraryAndCanvas?: boolean;
671
701
  }
672
702
 
673
703
  export { SanityDocument };
@@ -744,6 +774,7 @@ export declare function SDKProvider({
744
774
  children,
745
775
  config,
746
776
  fallback,
777
+ inferMediaLibraryAndCanvas,
747
778
  ...props
748
779
  }: SDKProviderProps): ReactElement;
749
780
 
@@ -755,6 +786,8 @@ export declare interface SDKProviderProps extends AuthBoundaryProps {
755
786
  config: SanityConfig | SanityConfig[];
756
787
  fallback: ReactNode;
757
788
  resources?: Record<string, DocumentResource>;
789
+ /** When set, automatically fetches and registers the organization's media library and canvas as named resources. */
790
+ inferMediaLibraryAndCanvas?: boolean;
758
791
  }
759
792
 
760
793
  /**
@@ -1908,6 +1941,8 @@ declare type UseDatasets = {
1908
1941
  * Returns metadata for each dataset the current user has access to.
1909
1942
  *
1910
1943
  * @category Datasets
1944
+ * @param options - Optional project/resource to read datasets for. Defaults to
1945
+ * the resource named in `ResourceProvider`/`SDKProvider`.
1911
1946
  * @returns The metadata for your the datasets
1912
1947
  *
1913
1948
  * @example
@@ -1923,8 +1958,14 @@ declare type UseDatasets = {
1923
1958
  * )
1924
1959
  * ```
1925
1960
  *
1926
- */
1927
- (): DatasetsResponse;
1961
+ * @remarks
1962
+ * The `projectId` is resolved in order from:
1963
+ * 1. an explicit `projectId` option
1964
+ * 2. A legacy ProjectContext (e.g. a `<ResourceProvider projectId="…">` with no dataset), then
1965
+ * 3. The active resource (`ResourceProvider`/`SDKProvider`)
1966
+ * 4. `instance.config`.
1967
+ */
1968
+ (options?: ProjectHandle): DatasetsResponse;
1928
1969
  };
1929
1970
 
1930
1971
  /**
@@ -3304,6 +3345,12 @@ export declare function usePresence(options?: ResourceHandle): {
3304
3345
  * const projectWithoutMembers = useProject({projectId, includeMembers: false})
3305
3346
  * const projectWithoutFeatures = useProject({projectId, includeFeatures: false})
3306
3347
  * ```
3348
+ * @remarks
3349
+ * The `projectId` is resolved in order from:
3350
+ * 1. an explicit `projectId` option
3351
+ * 2. A legacy ProjectContext (e.g. a `<ResourceProvider projectId="…">` with no dataset), then
3352
+ * 3. The active resource (`ResourceProvider`/`SDKProvider`)
3353
+ * 4. `instance.config`.
3307
3354
  * @public
3308
3355
  * @function
3309
3356
  */
@@ -3718,6 +3765,12 @@ export declare function useUser(options: GetUserOptions): UserResult;
3718
3765
  * </div>
3719
3766
  * )
3720
3767
  * ```
3768
+ * @remarks
3769
+ * For project-scoped queries the `projectId` is resolved in order from:
3770
+ * 1. an explicit `projectId` option
3771
+ * 2. A legacy ProjectContext (e.g. a `<ResourceProvider projectId="…">` with no dataset), then
3772
+ * 3. The active resource (`ResourceProvider`/`SDKProvider`)
3773
+ * 4. `instance.config`.
3721
3774
  */
3722
3775
  export declare function useUsers(options?: GetUsersOptions): UsersResult;
3723
3776