@sanity/sdk-react 0.0.0-alpha.8 → 0.0.0-alpha.9

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.
@@ -34,6 +34,7 @@ function useLoginUrls() {
34
34
  }
35
35
  const useLogOut = createCallbackHook(logout);
36
36
  export {
37
+ createCallbackHook,
37
38
  createStateSourceHook,
38
39
  useAuthState,
39
40
  useHandleCallback,
package/dist/hooks.d.ts CHANGED
@@ -1,13 +1,21 @@
1
+ import {ActionsResult} from '@sanity/sdk'
2
+ import {ApplyActionsOptions} from '@sanity/sdk'
1
3
  import {AuthProvider} from '@sanity/sdk'
2
4
  import {AuthState} from '@sanity/sdk'
3
5
  import {ClientOptions} from '@sanity/sdk'
4
6
  import {CurrentUser} from '@sanity/sdk'
7
+ import {DocumentAction} from '@sanity/sdk'
8
+ import {DocumentEvent} from '@sanity/sdk'
5
9
  import {DocumentHandle} from '@sanity/sdk'
6
10
  import {DocumentListOptions} from '@sanity/sdk'
7
11
  import {FrameMessage} from '@sanity/sdk'
12
+ import {JsonMatch} from '@sanity/sdk'
13
+ import {JsonMatchPath} from '@sanity/sdk'
8
14
  import {Observable} from 'rxjs'
9
15
  import {PreviewValue} from '@sanity/sdk'
10
16
  import {Requester} from 'get-it'
17
+ import {SanityDocument as SanityDocument_2} from '@sanity/types'
18
+ import {SanityDocumentLike} from '@sanity/types'
11
19
  import {SanityInstance} from '@sanity/sdk'
12
20
  import {WindowMessage} from '@sanity/sdk'
13
21
 
@@ -3096,6 +3104,12 @@ declare interface UploadClientConfig {
3096
3104
  }
3097
3105
  }
3098
3106
 
3107
+ /** @beta */
3108
+ export declare function useApplyActions(): <TDocument extends SanityDocument_2>(
3109
+ action: DocumentAction<TDocument> | DocumentAction<TDocument>[],
3110
+ options?: ApplyActionsOptions,
3111
+ ) => Promise<ActionsResult<TDocument>>
3112
+
3099
3113
  /**
3100
3114
  * @internal
3101
3115
  * A React hook that subscribes to authentication state changes.
@@ -3179,6 +3193,20 @@ export declare function useClient(options: ClientOptions): SanityClient
3179
3193
  */
3180
3194
  export declare const useCurrentUser: () => CurrentUser | null
3181
3195
 
3196
+ /** @beta */
3197
+ export declare function useDocument<
3198
+ TDocument extends SanityDocument_2,
3199
+ TPath extends JsonMatchPath<TDocument>,
3200
+ >(doc: string | DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined
3201
+
3202
+ /** @beta */
3203
+ export declare function useDocument<TDocument extends SanityDocument_2>(
3204
+ doc: string | DocumentHandle<TDocument>,
3205
+ ): TDocument | null
3206
+
3207
+ /** @beta */
3208
+ export declare function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void): void
3209
+
3182
3210
  /**
3183
3211
  * @public
3184
3212
  *
@@ -3232,6 +3260,25 @@ export declare const useCurrentUser: () => CurrentUser | null
3232
3260
  */
3233
3261
  export declare function useDocuments(options?: DocumentListOptions): DocumentCollection
3234
3262
 
3263
+ /** @beta */
3264
+ export declare const useDocumentSyncStatus: (
3265
+ doc: string | DocumentHandle<SanityDocumentLike>,
3266
+ ) => boolean | undefined
3267
+
3268
+ /** @beta */
3269
+ export declare function useEditDocument<
3270
+ TDocument extends SanityDocument_2,
3271
+ TPath extends JsonMatchPath<TDocument>,
3272
+ >(
3273
+ doc: string | DocumentHandle<TDocument>,
3274
+ path: TPath,
3275
+ ): (nextValue: JsonMatch<TDocument, TPath>) => Promise<ActionsResult<TDocument>>
3276
+
3277
+ /** @beta */
3278
+ export declare function useEditDocument<TDocument extends SanityDocument_2>(
3279
+ doc: string | DocumentHandle<TDocument>,
3280
+ ): (nextValue: Partial<TDocument>) => Promise<ActionsResult<TDocument>>
3281
+
3235
3282
  /**
3236
3283
  * @internal
3237
3284
  */
package/dist/hooks.js CHANGED
@@ -1,7 +1,7 @@
1
- import { createStateSourceHook, useSanityInstance } from "./_chunks-es/useLogOut.js";
1
+ import { createStateSourceHook, useSanityInstance, createCallbackHook } from "./_chunks-es/useLogOut.js";
2
2
  import { useAuthState, useHandleCallback, useLogOut, useLoginUrls } from "./_chunks-es/useLogOut.js";
3
- import { getTokenState, getCurrentUserState, getSubscribableClient, getClient, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, createDocumentListStore, getPreviewState, resolvePreview } from "@sanity/sdk";
4
- import { useCallback, useSyncExternalStore, useMemo, useEffect, useState } from "react";
3
+ import { getTokenState, getCurrentUserState, getSubscribableClient, getClient, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, applyActions, getDocumentState, resolveDocument, subscribeDocumentEvents, getDocumentSyncStatus, editDocument, createDocumentListStore, getPreviewState, resolvePreview } from "@sanity/sdk";
4
+ import { useCallback, useSyncExternalStore, useMemo, useEffect, useRef, useInsertionEffect, useState } from "react";
5
5
  import { Observable, startWith, distinctUntilChanged, switchMap, EMPTY } from "rxjs";
6
6
  const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState);
7
7
  function useClient(options) {
@@ -88,6 +88,52 @@ function useWindowConnection(options) {
88
88
  sendMessage
89
89
  };
90
90
  }
91
+ function useApplyActions() {
92
+ return _useApplyActions();
93
+ }
94
+ const _useApplyActions = createCallbackHook(applyActions);
95
+ function useDocument(doc, path) {
96
+ const documentId = typeof doc == "string" ? doc : doc._id, instance = useSanityInstance();
97
+ if (!useCallback(
98
+ () => getDocumentState(instance, documentId).getCurrent() !== void 0,
99
+ [instance, documentId]
100
+ )()) throw resolveDocument(instance, documentId);
101
+ const { subscribe, getCurrent } = useMemo(
102
+ () => getDocumentState(instance, documentId, path),
103
+ [documentId, instance, path]
104
+ );
105
+ return useSyncExternalStore(subscribe, getCurrent);
106
+ }
107
+ function useDocumentEvent(handler) {
108
+ const ref = useRef(handler);
109
+ useInsertionEffect(() => {
110
+ ref.current = handler;
111
+ });
112
+ const stableHandler = useCallback((documentEvent) => ref.current(documentEvent), []), instance = useSanityInstance();
113
+ useEffect(() => subscribeDocumentEvents(instance, stableHandler), [instance, stableHandler]);
114
+ }
115
+ const useDocumentSyncStatus = createStateSourceHook(getDocumentSyncStatus), ignoredKeys = ["_id", "_type", "_createdAt", "_updatedAt", "_rev"];
116
+ function useEditDocument(doc, path) {
117
+ const documentId = typeof doc == "string" ? doc : doc._id, instance = useSanityInstance(), apply = useApplyActions();
118
+ if (!useCallback(
119
+ () => getDocumentState(instance, documentId).getCurrent() !== void 0,
120
+ [instance, documentId]
121
+ )()) throw resolveDocument(instance, documentId);
122
+ return useCallback(
123
+ (next) => {
124
+ if (path)
125
+ return apply(editDocument(documentId, { set: { [path]: next } }));
126
+ const current = getDocumentState(instance, documentId).getCurrent();
127
+ if (typeof next != "object" || !next)
128
+ throw new Error(
129
+ "No path was provided to `useEditDocument` and the value provided was not a document object."
130
+ );
131
+ const editActions = Object.entries(next).filter(([key]) => !ignoredKeys.includes(key)).filter(([key, value]) => current?.[key] !== value).map(([key, value]) => editDocument(documentId, { set: { [key]: value } }));
132
+ return apply(editActions);
133
+ },
134
+ [apply, documentId, instance, path]
135
+ );
136
+ }
91
137
  const STABLE_EMPTY = {
92
138
  results: [],
93
139
  isPending: !1,
@@ -151,11 +197,16 @@ function usePreview({ document: { _id, _type }, ref }) {
151
197
  return useSyncExternalStore(subscribe, getSnapshot);
152
198
  }
153
199
  export {
200
+ useApplyActions,
154
201
  useAuthState,
155
202
  useAuthToken,
156
203
  useClient,
157
204
  useCurrentUser,
205
+ useDocument,
206
+ useDocumentEvent,
207
+ useDocumentSyncStatus,
158
208
  useDocuments,
209
+ useEditDocument,
159
210
  useFrameConnection,
160
211
  useHandleCallback,
161
212
  useLogOut,
package/dist/hooks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../src/hooks/client/useClient.ts","../src/hooks/comlink/useFrameConnection.ts","../src/hooks/comlink/useWindowConnection.ts","../src/hooks/documentCollection/useDocuments.ts","../src/hooks/preview/usePreview.tsx"],"sourcesContent":["import {getTokenState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @internal\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {type CurrentUser, getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @TODO This should suspend! And possibly not return `null`?\n *\n * @public\n *\n * The `useCurrentUser` hook returns the currently authenticated user’s profile information (their name, email, roles, etc).\n * If no users are currently logged in, the hook returns null.\n *\n * @returns The current user data, or `null` if not authenticated\n *\n * @example Rendering a basic user profile\n * ```\n * const user = useCurrentUser()\n *\n * return (\n * <figure>\n * <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />\n * <h2>{user?.name}</h2>\n * </figure>\n * )\n * ```\n */\nexport const useCurrentUser: () => CurrentUser | null = createStateSourceHook(getCurrentUserState)\n","import {type SanityClient} from '@sanity/client'\nimport {type ClientOptions, getClient, getSubscribableClient} from '@sanity/sdk'\nimport {useCallback, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * A React hook that provides a client that subscribes to changes in your application,\n * such as user authentication changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to changes\n * and ensure consistency between server and client rendering.\n *\n * @returns A Sanity client\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * const client = useClient()\n * const [document, setDocument] = useState(null)\n * useEffect(async () => {\n * const doc = client.fetch('*[_id == \"myDocumentId\"]')\n * setDocument(doc)\n * }, [])\n * return <div>{JSON.stringify(document) ?? 'Loading...'}</div>\n * }\n * ```\n *\n * @public\n */\nexport function useClient(options: ClientOptions): SanityClient {\n const instance = useSanityInstance()\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const client$ = getSubscribableClient(instance, options)\n const subscription = client$.subscribe({\n next: onStoreChange,\n error: (error) => {\n // @TODO: We should tackle error handling / error boundaries soon\n // eslint-disable-next-line no-console\n console.error('Error in useClient subscription:', error)\n },\n })\n return () => subscription.unsubscribe()\n },\n [instance, options],\n )\n\n const getSnapshot = useCallback(() => {\n return getClient(instance, options)\n }, [instance, options])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n","import {\n type FrameMessage,\n getOrCreateChannel,\n getOrCreateController,\n releaseChannel,\n type WindowMessage,\n} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @internal\n */\nexport interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {\n name: string\n connectTo: string\n targetOrigin: string\n onMessage?: Record<string, FrameMessageHandler<TWindowMessage>>\n}\n\n/**\n * @internal\n */\nexport interface FrameConnection<TFrameMessage extends FrameMessage> {\n connect: (frameWindow: Window) => () => void // Return cleanup function\n sendMessage: <T extends TFrameMessage['type']>(\n ...params: Extract<TFrameMessage, {type: T}>['data'] extends undefined\n ? [type: T]\n : [type: T, data: Extract<TFrameMessage, {type: T}>['data']]\n ) => void\n}\n\n/**\n * @internal\n */\nexport function useFrameConnection<\n TFrameMessage extends FrameMessage,\n TWindowMessage extends WindowMessage,\n>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage> {\n const {onMessage, targetOrigin, name, connectTo} = options\n const instance = useSanityInstance()\n\n const controller = useMemo(\n () => getOrCreateController(instance, targetOrigin),\n [instance, targetOrigin],\n )\n\n const channel = useMemo(\n () =>\n getOrCreateChannel(instance, {\n name,\n connectTo,\n }),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!channel || !onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = channel.on(type, handler)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [channel, onMessage])\n\n const connect = useCallback(\n (frameWindow: Window) => {\n const removeTarget = controller?.addTarget(frameWindow)\n return () => {\n removeTarget?.()\n }\n },\n [controller],\n )\n\n const sendMessage = useCallback(\n <T extends TFrameMessage['type']>(\n type: T,\n data?: Extract<TFrameMessage, {type: T}>['data'],\n ) => {\n channel?.post(type, data)\n },\n [channel],\n )\n\n // cleanup channel on unmount\n useEffect(() => {\n return () => {\n releaseChannel(instance, name)\n }\n }, [name, instance])\n\n return {\n connect,\n sendMessage,\n }\n}\n","import {type FrameMessage, getOrCreateNode, releaseNode, type WindowMessage} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @internal\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @internal\n */\nexport interface WindowConnection<TMessage extends WindowMessage> {\n sendMessage: <TType extends TMessage['type']>(\n type: TType,\n data?: Extract<TMessage, {type: TType}>['data'],\n ) => void\n}\n\n/**\n * @internal\n */\nexport function useWindowConnection<\n TWindowMessage extends WindowMessage,\n TFrameMessage extends FrameMessage,\n>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage> {\n const {name, onMessage, connectTo} = options\n const instance = useSanityInstance()\n\n const node = useMemo(\n () => getOrCreateNode(instance, {name, connectTo}),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = node.on(type, handler as WindowMessageHandler<TFrameMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [node, onMessage])\n\n const sendMessage = useCallback(\n <TType extends WindowMessage['type']>(\n type: TType,\n data?: Extract<WindowMessage, {type: TType}>['data'],\n ) => {\n node?.post(type, data)\n },\n [node],\n )\n\n // cleanup node on unmount\n useEffect(() => {\n return () => {\n releaseNode(instance, name)\n }\n }, [instance, name])\n\n return {\n sendMessage,\n }\n}\n","import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\nexport interface DocumentCollection {\n /** Retrieve more documents matching the provided options */\n loadMore: () => void\n /** The retrieved document handles of the documents matching the provided options */\n results: DocumentHandle[]\n /** Whether a retrieval of documents is in flight */\n isPending: boolean\n /** Whether more documents exist that match the provided options than have been retrieved */\n hasMore: boolean\n /** The total number of documents in the collection */\n count: number\n}\n\ntype DocumentListStore = ReturnType<typeof createDocumentListStore>\ntype DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']\nconst STABLE_EMPTY = {\n results: [],\n isPending: false,\n hasMore: false,\n count: 0,\n}\n\n/**\n * @public\n *\n * The `useDocuments` hook retrieves and provides access to a live collection of documents, optionally filtered, sorted, and matched to a given Content Lake perspective.\n * Because the returned document collection is live, the results will update in real time until the component invoking the hook is unmounted.\n *\n * @param options - Options for narrowing and sorting the document collection\n * @returns The collection of documents matching the provided options (if any), as well as properties describing the collection and a function to load more.\n *\n * @example Retrieving all documents of type 'movie'\n * ```\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n *\n * return (\n * <div>\n * <h1>Movies</h1>\n * {results && (\n * <ul>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ul>\n * )}\n * {isPending && <div>Loading movies…</div>}\n * </div>\n * )\n * ```\n *\n * @example Retrieving all movies released since 1980, sorted by director’s last name\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * // Expand the `director` reference field with the dereferencing operator `->`\n * field: 'director->lastName',\n * sort: 'asc',\n * },\n * ],\n * })\n *\n * return (\n * <div>\n * <h1>Movies released since 1980</h1>\n * {results && (\n * <ol>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ol>\n * )}\n * </div>\n * )\n * ```\n */\nexport function useDocuments(options: DocumentListOptions = {}): DocumentCollection {\n const instance = useSanityInstance()\n\n // NOTE: useState is used because it guaranteed to return a stable reference\n // across renders\n const [ref] = useState<{\n storeInstance: DocumentListStore | null\n getCurrent: DocumentListState\n initialOptions: DocumentListOptions\n }>(() => ({\n storeInstance: null,\n getCurrent: () => STABLE_EMPTY,\n initialOptions: options,\n }))\n\n // serialize options to ensure it only calls `setOptions` when the values\n // themselves changes (in cases where devs put config inline)\n const serializedOptions = JSON.stringify(options)\n useEffect(() => {\n ref.storeInstance?.setOptions(JSON.parse(serializedOptions))\n }, [ref, serializedOptions])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n // to match the lifecycle of `useSyncExternalState`, we create the store\n // instance after subscribe and mutate the ref to connect everything\n ref.storeInstance = createDocumentListStore(instance)\n ref.storeInstance.setOptions(ref.initialOptions)\n const state = ref.storeInstance.getState()\n ref.getCurrent = state.getCurrent\n const unsubscribe = state.subscribe(onStoreChanged)\n\n return () => {\n // unsubscribe to clean up the state subscriptions\n unsubscribe()\n // dispose of the instance\n ref.storeInstance?.dispose()\n }\n },\n [instance, ref],\n )\n\n const getSnapshot = useCallback(() => {\n return ref.getCurrent()\n }, [ref])\n\n const state = useSyncExternalStore(subscribe, getSnapshot)\n\n const loadMore = useCallback(() => {\n ref.storeInstance?.loadMore()\n }, [ref])\n\n return {loadMore, ...state}\n}\n","import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @alpha\n */\nexport interface UsePreviewOptions {\n document: DocumentHandle\n ref?: React.RefObject<unknown>\n}\n\n/**\n * @alpha\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** True when preview values are being refreshed */\n isPending: boolean\n}\n\n/**\n * @alpha\n *\n * The `usePreview` hook takes a document (via a `DocumentHandle`) and returns its resolved preview values,\n * including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.\n * To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview\n * resolution will only occur if the `ref` is intersecting the current viewport.\n *\n * @param options - The document handle for the document you want to resolve preview values for, and an optional ref\n * @returns The preview values for the given document and a boolean to indicate whether the resolution is pending\n *\n * @example Combining with useDocuments to render a collection of document previews\n * ```\n * // PreviewComponent.jsx\n * export default function PreviewComponent({ document }) {\n * const { results: { title, subtitle, media }, isPending } = usePreview({ document })\n * return (\n * <article style={{ opacity: isPending ? 0.5 : 1}}>\n * {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}\n * <h2>{title}</h2>\n * <p>{subtitle}</p>\n * </article>\n * )\n * }\n *\n * // DocumentList.jsx\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n * return (\n * <div>\n * <h1>Movies</h1>\n * <ul>\n * {isPending ? 'Loading…' : results.map(movie => (\n * <li key={movie._id}>\n * <Suspense fallback='Loading…'>\n * <PreviewComponent document={movie} />\n * </Suspense>\n * </li>\n * ))}\n * </ul>\n * </div>\n * )\n * ```\n */\nexport function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): UsePreviewResults {\n const instance = useSanityInstance()\n\n const stateSource = useMemo(\n () => getPreviewState(instance, {document: {_id, _type}}),\n [instance, _id, _type],\n )\n\n // Create subscribe function for useSyncExternalStore\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n const subscription = new Observable<boolean>((observer) => {\n // for environments that don't have an intersection observer\n if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {\n return\n }\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref?.current && ref.current instanceof HTMLElement) {\n intersectionObserver.observe(ref.current)\n }\n return () => intersectionObserver.disconnect()\n })\n .pipe(\n startWith(false),\n distinctUntilChanged(),\n switchMap((isVisible) =>\n isVisible\n ? new Observable<void>((obs) => {\n return stateSource.subscribe(() => obs.next())\n })\n : EMPTY,\n ),\n )\n .subscribe({next: onStoreChanged})\n\n return () => subscription.unsubscribe()\n },\n [stateSource, ref],\n )\n\n // Create getSnapshot function to return current state\n const getSnapshot = useCallback(() => {\n const currentState = stateSource.getCurrent()\n if (currentState.results === null) throw resolvePreview(instance, {document: {_id, _type}})\n return currentState as UsePreviewResults\n }, [_id, _type, instance, stateSource])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n"],"names":["state"],"mappings":";;;;;AASa,MAAA,eAAe,sBAAsB,aAAa,GCiBlD,iBAA2C,sBAAsB,mBAAmB;ACK1F,SAAS,UAAU,SAAsC;AACxD,QAAA,WAAW,qBAEX,YAAY;AAAA,IAChB,CAAC,kBAA8B;AAE7B,YAAM,eADU,sBAAsB,UAAU,OAAO,EAC1B,UAAU;AAAA,QACrC,MAAM;AAAA,QACN,OAAO,CAAC,UAAU;AAGR,kBAAA,MAAM,oCAAoC,KAAK;AAAA,QAAA;AAAA,MACzD,CACD;AACM,aAAA,MAAM,aAAa,YAAY;AAAA,IACxC;AAAA,IACA,CAAC,UAAU,OAAO;AAAA,EAAA,GAGd,cAAc,YAAY,MACvB,UAAU,UAAU,OAAO,GACjC,CAAC,UAAU,OAAO,CAAC;AAEf,SAAA,qBAAqB,WAAW,WAAW;AACpD;ACZO,SAAS,mBAGd,SAAoF;AAC9E,QAAA,EAAC,WAAW,cAAc,MAAM,UAAA,IAAa,SA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AAe,YAAY,WAAW;AAC5C,QAAI,aAAa,YAAY,KAAM,OAAM,eAAe,UAAU,EAAC,UAAU,EAAC,KAAK,MAAK,EAAA,CAAE;AACnF,WAAA;AAAA,KACN,CAAC,KAAK,OAAO,UAAU,WAAW,CAAC;AAE/B,SAAA,qBAAqB,WAAW,WAAW;AACpD;"}
1
+ {"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../src/hooks/client/useClient.ts","../src/hooks/comlink/useFrameConnection.ts","../src/hooks/comlink/useWindowConnection.ts","../src/hooks/document/useApplyActions.ts","../src/hooks/document/useDocument.ts","../src/hooks/document/useDocumentEvent.ts","../src/hooks/document/useDocumentSyncStatus.ts","../src/hooks/document/useEditDocument.ts","../src/hooks/documentCollection/useDocuments.ts","../src/hooks/preview/usePreview.tsx"],"sourcesContent":["import {getTokenState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @internal\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {type CurrentUser, getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * @TODO This should suspend! And possibly not return `null`?\n *\n * @public\n *\n * The `useCurrentUser` hook returns the currently authenticated user’s profile information (their name, email, roles, etc).\n * If no users are currently logged in, the hook returns null.\n *\n * @returns The current user data, or `null` if not authenticated\n *\n * @example Rendering a basic user profile\n * ```\n * const user = useCurrentUser()\n *\n * return (\n * <figure>\n * <img src={user?.profileImage} alt=`Profile image for ${user?.name}` />\n * <h2>{user?.name}</h2>\n * </figure>\n * )\n * ```\n */\nexport const useCurrentUser: () => CurrentUser | null = createStateSourceHook(getCurrentUserState)\n","import {type SanityClient} from '@sanity/client'\nimport {type ClientOptions, getClient, getSubscribableClient} from '@sanity/sdk'\nimport {useCallback, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * A React hook that provides a client that subscribes to changes in your application,\n * such as user authentication changes.\n *\n * @remarks\n * The hook uses `useSyncExternalStore` to safely subscribe to changes\n * and ensure consistency between server and client rendering.\n *\n * @returns A Sanity client\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * const client = useClient()\n * const [document, setDocument] = useState(null)\n * useEffect(async () => {\n * const doc = client.fetch('*[_id == \"myDocumentId\"]')\n * setDocument(doc)\n * }, [])\n * return <div>{JSON.stringify(document) ?? 'Loading...'}</div>\n * }\n * ```\n *\n * @public\n */\nexport function useClient(options: ClientOptions): SanityClient {\n const instance = useSanityInstance()\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const client$ = getSubscribableClient(instance, options)\n const subscription = client$.subscribe({\n next: onStoreChange,\n error: (error) => {\n // @TODO: We should tackle error handling / error boundaries soon\n // eslint-disable-next-line no-console\n console.error('Error in useClient subscription:', error)\n },\n })\n return () => subscription.unsubscribe()\n },\n [instance, options],\n )\n\n const getSnapshot = useCallback(() => {\n return getClient(instance, options)\n }, [instance, options])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n","import {\n type FrameMessage,\n getOrCreateChannel,\n getOrCreateController,\n releaseChannel,\n type WindowMessage,\n} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @internal\n */\nexport interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {\n name: string\n connectTo: string\n targetOrigin: string\n onMessage?: Record<string, FrameMessageHandler<TWindowMessage>>\n}\n\n/**\n * @internal\n */\nexport interface FrameConnection<TFrameMessage extends FrameMessage> {\n connect: (frameWindow: Window) => () => void // Return cleanup function\n sendMessage: <T extends TFrameMessage['type']>(\n ...params: Extract<TFrameMessage, {type: T}>['data'] extends undefined\n ? [type: T]\n : [type: T, data: Extract<TFrameMessage, {type: T}>['data']]\n ) => void\n}\n\n/**\n * @internal\n */\nexport function useFrameConnection<\n TFrameMessage extends FrameMessage,\n TWindowMessage extends WindowMessage,\n>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage> {\n const {onMessage, targetOrigin, name, connectTo} = options\n const instance = useSanityInstance()\n\n const controller = useMemo(\n () => getOrCreateController(instance, targetOrigin),\n [instance, targetOrigin],\n )\n\n const channel = useMemo(\n () =>\n getOrCreateChannel(instance, {\n name,\n connectTo,\n }),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!channel || !onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = channel.on(type, handler)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [channel, onMessage])\n\n const connect = useCallback(\n (frameWindow: Window) => {\n const removeTarget = controller?.addTarget(frameWindow)\n return () => {\n removeTarget?.()\n }\n },\n [controller],\n )\n\n const sendMessage = useCallback(\n <T extends TFrameMessage['type']>(\n type: T,\n data?: Extract<TFrameMessage, {type: T}>['data'],\n ) => {\n channel?.post(type, data)\n },\n [channel],\n )\n\n // cleanup channel on unmount\n useEffect(() => {\n return () => {\n releaseChannel(instance, name)\n }\n }, [name, instance])\n\n return {\n connect,\n sendMessage,\n }\n}\n","import {type FrameMessage, getOrCreateNode, releaseNode, type WindowMessage} from '@sanity/sdk'\nimport {useCallback, useEffect, useMemo} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @internal\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @internal\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @internal\n */\nexport interface WindowConnection<TMessage extends WindowMessage> {\n sendMessage: <TType extends TMessage['type']>(\n type: TType,\n data?: Extract<TMessage, {type: TType}>['data'],\n ) => void\n}\n\n/**\n * @internal\n */\nexport function useWindowConnection<\n TWindowMessage extends WindowMessage,\n TFrameMessage extends FrameMessage,\n>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage> {\n const {name, onMessage, connectTo} = options\n const instance = useSanityInstance()\n\n const node = useMemo(\n () => getOrCreateNode(instance, {name, connectTo}),\n [instance, name, connectTo],\n )\n\n useEffect(() => {\n if (!onMessage) return\n\n const unsubscribers: Array<() => void> = []\n\n Object.entries(onMessage).forEach(([type, handler]) => {\n const unsubscribe = node.on(type, handler as WindowMessageHandler<TFrameMessage>)\n unsubscribers.push(unsubscribe)\n })\n\n return () => {\n unsubscribers.forEach((unsub) => unsub())\n }\n }, [node, onMessage])\n\n const sendMessage = useCallback(\n <TType extends WindowMessage['type']>(\n type: TType,\n data?: Extract<WindowMessage, {type: TType}>['data'],\n ) => {\n node?.post(type, data)\n },\n [node],\n )\n\n // cleanup node on unmount\n useEffect(() => {\n return () => {\n releaseNode(instance, name)\n }\n }, [instance, name])\n\n return {\n sendMessage,\n }\n}\n","import {\n type ActionsResult,\n applyActions,\n type ApplyActionsOptions,\n type DocumentAction,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\n\nimport {createCallbackHook} from '../helpers/createCallbackHook'\n\n/** @beta */\nexport function useApplyActions(): <TDocument extends SanityDocument>(\n action: DocumentAction<TDocument> | DocumentAction<TDocument>[],\n options?: ApplyActionsOptions,\n) => Promise<ActionsResult<TDocument>>\n/** @beta */\nexport function useApplyActions(): (\n action: DocumentAction | DocumentAction[],\n options?: ApplyActionsOptions,\n) => Promise<ActionsResult> {\n return _useApplyActions()\n}\n\nconst _useApplyActions = createCallbackHook(applyActions)\n","import {\n type DocumentHandle,\n getDocumentState,\n type JsonMatch,\n type JsonMatchPath,\n resolveDocument,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/** @beta */\nexport function useDocument<\n TDocument extends SanityDocument,\n TPath extends JsonMatchPath<TDocument>,\n>(doc: string | DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined\n/** @beta */\nexport function useDocument<TDocument extends SanityDocument>(\n doc: string | DocumentHandle<TDocument>,\n): TDocument | null\n/** @beta */\nexport function useDocument(doc: string | DocumentHandle, path?: string): unknown {\n const documentId = typeof doc === 'string' ? doc : doc._id\n const instance = useSanityInstance()\n const isDocumentReady = useCallback(\n () => getDocumentState(instance, documentId).getCurrent() !== undefined,\n [instance, documentId],\n )\n if (!isDocumentReady()) throw resolveDocument(instance, documentId)\n\n const {subscribe, getCurrent} = useMemo(\n () => getDocumentState(instance, documentId, path),\n [documentId, instance, path],\n )\n\n return useSyncExternalStore(subscribe, getCurrent)\n}\n","import {type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'\nimport {useCallback, useEffect, useInsertionEffect, useRef} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/** @beta */\nexport function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void): void {\n const ref = useRef(handler)\n\n useInsertionEffect(() => {\n ref.current = handler\n })\n\n const stableHandler = useCallback((documentEvent: DocumentEvent) => {\n return ref.current(documentEvent)\n }, [])\n\n const instance = useSanityInstance()\n useEffect(() => {\n return subscribeDocumentEvents(instance, stableHandler)\n }, [instance, stableHandler])\n}\n","import {getDocumentSyncStatus} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/** @beta */\nexport const useDocumentSyncStatus = createStateSourceHook(getDocumentSyncStatus)\n","import {\n type ActionsResult,\n type DocumentHandle,\n editDocument,\n getDocumentState,\n type JsonMatch,\n type JsonMatchPath,\n resolveDocument,\n} from '@sanity/sdk'\nimport {type SanityDocument} from '@sanity/types'\nimport {useCallback} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\nimport {useApplyActions} from './useApplyActions'\n\nconst ignoredKeys = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']\n\n/** @beta */\nexport function useEditDocument<\n TDocument extends SanityDocument,\n TPath extends JsonMatchPath<TDocument>,\n>(\n doc: string | DocumentHandle<TDocument>,\n path: TPath,\n): (nextValue: JsonMatch<TDocument, TPath>) => Promise<ActionsResult<TDocument>>\n/** @beta */\nexport function useEditDocument<TDocument extends SanityDocument>(\n doc: string | DocumentHandle<TDocument>,\n): (nextValue: Partial<TDocument>) => Promise<ActionsResult<TDocument>>\n/** @beta */\nexport function useEditDocument(\n doc: string | DocumentHandle,\n path?: string,\n): (nextValue: unknown) => Promise<ActionsResult> {\n const documentId = typeof doc === 'string' ? doc : doc._id\n const instance = useSanityInstance()\n const apply = useApplyActions()\n const isDocumentReady = useCallback(\n () => getDocumentState(instance, documentId).getCurrent() !== undefined,\n [instance, documentId],\n )\n if (!isDocumentReady()) throw resolveDocument(instance, documentId)\n\n return useCallback(\n (next: unknown) => {\n if (path) {\n return apply(editDocument(documentId, {set: {[path]: next}}))\n }\n\n const current = getDocumentState(instance, documentId).getCurrent()\n\n if (typeof next !== 'object' || !next) {\n throw new Error(\n `No path was provided to \\`useEditDocument\\` and the value provided was not a document object.`,\n )\n }\n\n const editActions = Object.entries(next)\n .filter(([key]) => !ignoredKeys.includes(key))\n .filter(([key, value]) => current?.[key] !== value)\n .map(([key, value]) => editDocument(documentId, {set: {[key]: value}}))\n\n return apply(editActions)\n },\n [apply, documentId, instance, path],\n )\n}\n","import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'\nimport {useCallback, useEffect, useState, useSyncExternalStore} from 'react'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @public\n */\nexport interface DocumentCollection {\n /** Retrieve more documents matching the provided options */\n loadMore: () => void\n /** The retrieved document handles of the documents matching the provided options */\n results: DocumentHandle[]\n /** Whether a retrieval of documents is in flight */\n isPending: boolean\n /** Whether more documents exist that match the provided options than have been retrieved */\n hasMore: boolean\n /** The total number of documents in the collection */\n count: number\n}\n\ntype DocumentListStore = ReturnType<typeof createDocumentListStore>\ntype DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']\nconst STABLE_EMPTY = {\n results: [],\n isPending: false,\n hasMore: false,\n count: 0,\n}\n\n/**\n * @public\n *\n * The `useDocuments` hook retrieves and provides access to a live collection of documents, optionally filtered, sorted, and matched to a given Content Lake perspective.\n * Because the returned document collection is live, the results will update in real time until the component invoking the hook is unmounted.\n *\n * @param options - Options for narrowing and sorting the document collection\n * @returns The collection of documents matching the provided options (if any), as well as properties describing the collection and a function to load more.\n *\n * @example Retrieving all documents of type 'movie'\n * ```\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n *\n * return (\n * <div>\n * <h1>Movies</h1>\n * {results && (\n * <ul>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ul>\n * )}\n * {isPending && <div>Loading movies…</div>}\n * </div>\n * )\n * ```\n *\n * @example Retrieving all movies released since 1980, sorted by director’s last name\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * // Expand the `director` reference field with the dereferencing operator `->`\n * field: 'director->lastName',\n * sort: 'asc',\n * },\n * ],\n * })\n *\n * return (\n * <div>\n * <h1>Movies released since 1980</h1>\n * {results && (\n * <ol>\n * {results.map(movie => (<li key={movie._id}>…</li>))}\n * </ol>\n * )}\n * </div>\n * )\n * ```\n */\nexport function useDocuments(options: DocumentListOptions = {}): DocumentCollection {\n const instance = useSanityInstance()\n\n // NOTE: useState is used because it guaranteed to return a stable reference\n // across renders\n const [ref] = useState<{\n storeInstance: DocumentListStore | null\n getCurrent: DocumentListState\n initialOptions: DocumentListOptions\n }>(() => ({\n storeInstance: null,\n getCurrent: () => STABLE_EMPTY,\n initialOptions: options,\n }))\n\n // serialize options to ensure it only calls `setOptions` when the values\n // themselves changes (in cases where devs put config inline)\n const serializedOptions = JSON.stringify(options)\n useEffect(() => {\n ref.storeInstance?.setOptions(JSON.parse(serializedOptions))\n }, [ref, serializedOptions])\n\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n // to match the lifecycle of `useSyncExternalState`, we create the store\n // instance after subscribe and mutate the ref to connect everything\n ref.storeInstance = createDocumentListStore(instance)\n ref.storeInstance.setOptions(ref.initialOptions)\n const state = ref.storeInstance.getState()\n ref.getCurrent = state.getCurrent\n const unsubscribe = state.subscribe(onStoreChanged)\n\n return () => {\n // unsubscribe to clean up the state subscriptions\n unsubscribe()\n // dispose of the instance\n ref.storeInstance?.dispose()\n }\n },\n [instance, ref],\n )\n\n const getSnapshot = useCallback(() => {\n return ref.getCurrent()\n }, [ref])\n\n const state = useSyncExternalStore(subscribe, getSnapshot)\n\n const loadMore = useCallback(() => {\n ref.storeInstance?.loadMore()\n }, [ref])\n\n return {loadMore, ...state}\n}\n","import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'\nimport {useCallback, useMemo, useSyncExternalStore} from 'react'\nimport {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'\n\nimport {useSanityInstance} from '../context/useSanityInstance'\n\n/**\n * @alpha\n */\nexport interface UsePreviewOptions {\n document: DocumentHandle\n ref?: React.RefObject<unknown>\n}\n\n/**\n * @alpha\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** True when preview values are being refreshed */\n isPending: boolean\n}\n\n/**\n * @alpha\n *\n * The `usePreview` hook takes a document (via a `DocumentHandle`) and returns its resolved preview values,\n * including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.\n * To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview\n * resolution will only occur if the `ref` is intersecting the current viewport.\n *\n * @param options - The document handle for the document you want to resolve preview values for, and an optional ref\n * @returns The preview values for the given document and a boolean to indicate whether the resolution is pending\n *\n * @example Combining with useDocuments to render a collection of document previews\n * ```\n * // PreviewComponent.jsx\n * export default function PreviewComponent({ document }) {\n * const { results: { title, subtitle, media }, isPending } = usePreview({ document })\n * return (\n * <article style={{ opacity: isPending ? 0.5 : 1}}>\n * {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}\n * <h2>{title}</h2>\n * <p>{subtitle}</p>\n * </article>\n * )\n * }\n *\n * // DocumentList.jsx\n * const { results, isPending } = useDocuments({ filter: '_type == \"movie\"' })\n * return (\n * <div>\n * <h1>Movies</h1>\n * <ul>\n * {isPending ? 'Loading…' : results.map(movie => (\n * <li key={movie._id}>\n * <Suspense fallback='Loading…'>\n * <PreviewComponent document={movie} />\n * </Suspense>\n * </li>\n * ))}\n * </ul>\n * </div>\n * )\n * ```\n */\nexport function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): UsePreviewResults {\n const instance = useSanityInstance()\n\n const stateSource = useMemo(\n () => getPreviewState(instance, {document: {_id, _type}}),\n [instance, _id, _type],\n )\n\n // Create subscribe function for useSyncExternalStore\n const subscribe = useCallback(\n (onStoreChanged: () => void) => {\n const subscription = new Observable<boolean>((observer) => {\n // for environments that don't have an intersection observer\n if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {\n return\n }\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref?.current && ref.current instanceof HTMLElement) {\n intersectionObserver.observe(ref.current)\n }\n return () => intersectionObserver.disconnect()\n })\n .pipe(\n startWith(false),\n distinctUntilChanged(),\n switchMap((isVisible) =>\n isVisible\n ? new Observable<void>((obs) => {\n return stateSource.subscribe(() => obs.next())\n })\n : EMPTY,\n ),\n )\n .subscribe({next: onStoreChanged})\n\n return () => subscription.unsubscribe()\n },\n [stateSource, ref],\n )\n\n // Create getSnapshot function to return current state\n const getSnapshot = useCallback(() => {\n const currentState = stateSource.getCurrent()\n if (currentState.results === null) throw resolvePreview(instance, {document: {_id, _type}})\n return currentState as UsePreviewResults\n }, [_id, _type, instance, stateSource])\n\n return useSyncExternalStore(subscribe, getSnapshot)\n}\n"],"names":["state"],"mappings":";;;;;AASa,MAAA,eAAe,sBAAsB,aAAa,GCiBlD,iBAA2C,sBAAsB,mBAAmB;ACK1F,SAAS,UAAU,SAAsC;AACxD,QAAA,WAAW,qBAEX,YAAY;AAAA,IAChB,CAAC,kBAA8B;AAE7B,YAAM,eADU,sBAAsB,UAAU,OAAO,EAC1B,UAAU;AAAA,QACrC,MAAM;AAAA,QACN,OAAO,CAAC,UAAU;AAGR,kBAAA,MAAM,oCAAoC,KAAK;AAAA,QAAA;AAAA,MACzD,CACD;AACM,aAAA,MAAM,aAAa,YAAY;AAAA,IACxC;AAAA,IACA,CAAC,UAAU,OAAO;AAAA,EAAA,GAGd,cAAc,YAAY,MACvB,UAAU,UAAU,OAAO,GACjC,CAAC,UAAU,OAAO,CAAC;AAEf,SAAA,qBAAqB,WAAW,WAAW;AACpD;ACZO,SAAS,mBAGd,SAAoF;AAC9E,QAAA,EAAC,WAAW,cAAc,MAAM,UAAA,IAAa,SAC7C,WAAW,qBAEX,aAAa;AAAA,IACjB,MAAM,sBAAsB,UAAU,YAAY;AAAA,IAClD,CAAC,UAAU,YAAY;AAAA,KAGnB,UAAU;AAAA,IACd,MACE,mBAAmB,UAAU;AAAA,MAC3B;AAAA,MACA;AAAA,IAAA,CACD;AAAA,IACH,CAAC,UAAU,MAAM,SAAS;AAAA,EAC5B;AAEA,YAAU,MAAM;AACV,QAAA,CAAC,WAAW,CAAC,UAAW;AAE5B,UAAM,gBAAmC,CAAC;AAEnC,WAAA,OAAA,QAAQ,SAAS,EAAE,QAAQ,CAAC,CAAC,MAAM,OAAO,MAAM;AACrD,YAAM,cAAc,QAAQ,GAAG,MAAM,OAAO;AAC5C,oBAAc,KAAK,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M,gBAAe;AAE5B,aAAA,MAAM,aAAa,YAAY;AAAA,IACxC;AAAA,IACA,CAAC,aAAa,GAAG;AAAA,EAAA,GAIb,cAAc,YAAY,MAAM;AAC9B,UAAA,eAAe,YAAY,WAAW;AAC5C,QAAI,aAAa,YAAY,KAAM,OAAM,eAAe,UAAU,EAAC,UAAU,EAAC,KAAK,MAAK,EAAA,CAAE;AACnF,WAAA;AAAA,KACN,CAAC,KAAK,OAAO,UAAU,WAAW,CAAC;AAE/B,SAAA,qBAAqB,WAAW,WAAW;AACpD;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/sdk-react",
3
- "version": "0.0.0-alpha.8",
3
+ "version": "0.0.0-alpha.9",
4
4
  "private": false,
5
5
  "description": "Sanity SDK React toolkit for Content OS",
6
6
  "keywords": [
@@ -69,10 +69,11 @@
69
69
  "dependencies": {
70
70
  "@sanity/logos": "^2.1.13",
71
71
  "@sanity/os": "^0.2.0",
72
+ "@sanity/types": "^3.67.1",
72
73
  "@sanity/ui": "^2.8.19",
73
74
  "react-error-boundary": "^4.1.2",
74
75
  "rxjs": "^7.8.1",
75
- "@sanity/sdk": "0.0.0-alpha.8"
76
+ "@sanity/sdk": "0.0.0-alpha.9"
76
77
  },
77
78
  "devDependencies": {
78
79
  "@sanity/client": "^6.27.2",
@@ -18,6 +18,11 @@ export {
18
18
  type WindowMessageHandler,
19
19
  } from '../hooks/comlink/useWindowConnection'
20
20
  export {useSanityInstance} from '../hooks/context/useSanityInstance'
21
+ export {useApplyActions} from '../hooks/document/useApplyActions'
22
+ export {useDocument} from '../hooks/document/useDocument'
23
+ export {useDocumentEvent} from '../hooks/document/useDocumentEvent'
24
+ export {useDocumentSyncStatus} from '../hooks/document/useDocumentSyncStatus'
25
+ export {useEditDocument} from '../hooks/document/useEditDocument'
21
26
  export {type DocumentCollection, useDocuments} from '../hooks/documentCollection/useDocuments'
22
27
  export {
23
28
  usePreview,
@@ -0,0 +1,24 @@
1
+ import {applyActions, createDocument} from '@sanity/sdk'
2
+ import {describe, it} from 'vitest'
3
+
4
+ import {createCallbackHook} from '../helpers/createCallbackHook'
5
+
6
+ vi.mock('../helpers/createCallbackHook', () => ({
7
+ createCallbackHook: vi.fn((cb) => () => cb),
8
+ }))
9
+ vi.mock('@sanity/sdk', async (importOriginal) => {
10
+ const original = await importOriginal<typeof import('@sanity/sdk')>()
11
+ return {...original, applyActions: vi.fn()}
12
+ })
13
+
14
+ describe('useApplyActions', () => {
15
+ it('calls `createCallbackHook` with `applyActions`', async () => {
16
+ const {useApplyActions} = await import('./useApplyActions')
17
+ expect(createCallbackHook).toHaveBeenCalledWith(applyActions)
18
+
19
+ expect(applyActions).not.toHaveBeenCalled()
20
+ const apply = useApplyActions()
21
+ apply(createDocument({_type: 'author'}))
22
+ expect(applyActions).toHaveBeenCalled()
23
+ })
24
+ })
@@ -0,0 +1,24 @@
1
+ import {
2
+ type ActionsResult,
3
+ applyActions,
4
+ type ApplyActionsOptions,
5
+ type DocumentAction,
6
+ } from '@sanity/sdk'
7
+ import {type SanityDocument} from '@sanity/types'
8
+
9
+ import {createCallbackHook} from '../helpers/createCallbackHook'
10
+
11
+ /** @beta */
12
+ export function useApplyActions(): <TDocument extends SanityDocument>(
13
+ action: DocumentAction<TDocument> | DocumentAction<TDocument>[],
14
+ options?: ApplyActionsOptions,
15
+ ) => Promise<ActionsResult<TDocument>>
16
+ /** @beta */
17
+ export function useApplyActions(): (
18
+ action: DocumentAction | DocumentAction[],
19
+ options?: ApplyActionsOptions,
20
+ ) => Promise<ActionsResult> {
21
+ return _useApplyActions()
22
+ }
23
+
24
+ const _useApplyActions = createCallbackHook(applyActions)
@@ -0,0 +1,81 @@
1
+ // tests/useDocument.test.ts
2
+ import {
3
+ createSanityInstance,
4
+ getDocumentState,
5
+ resolveDocument,
6
+ type StateSource,
7
+ } from '@sanity/sdk'
8
+ import {type SanityDocument} from '@sanity/types'
9
+ import {renderHook} from '@testing-library/react'
10
+ import {beforeEach, describe, expect, it, vi} from 'vitest'
11
+
12
+ import {useSanityInstance} from '../context/useSanityInstance'
13
+ import {useDocument} from './useDocument'
14
+
15
+ vi.mock('@sanity/sdk', async (importOriginal) => {
16
+ const original = await importOriginal<typeof import('@sanity/sdk')>()
17
+ return {...original, getDocumentState: vi.fn(), resolveDocument: vi.fn()}
18
+ })
19
+
20
+ vi.mock('../context/useSanityInstance', () => ({
21
+ useSanityInstance: vi.fn(),
22
+ }))
23
+
24
+ // Create a fake instance to be returned by useSanityInstance.
25
+ const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
26
+ const doc: SanityDocument = {
27
+ _id: 'doc1',
28
+ foo: 'bar',
29
+ _type: 'book',
30
+ _rev: 'tx0',
31
+ _createdAt: '2025-02-06T00:11:00.000Z',
32
+ _updatedAt: '2025-02-06T00:11:00.000Z',
33
+ }
34
+
35
+ describe('useDocument hook', () => {
36
+ beforeEach(() => {
37
+ vi.resetAllMocks()
38
+ vi.mocked(useSanityInstance).mockReturnValue(instance)
39
+ })
40
+
41
+ it('returns the current document when ready (without a path)', () => {
42
+ const getCurrent = vi.fn().mockReturnValue(doc)
43
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
44
+ vi.mocked(getDocumentState).mockReturnValue({
45
+ getCurrent,
46
+ subscribe,
47
+ } as unknown as StateSource<unknown>)
48
+
49
+ const {result} = renderHook(() => useDocument('doc1'))
50
+
51
+ expect(result.current).toEqual(doc)
52
+ expect(getCurrent).toHaveBeenCalled()
53
+ expect(subscribe).toHaveBeenCalled()
54
+ })
55
+
56
+ it('throws a promise (suspends) when the document is not ready', () => {
57
+ const getCurrent = vi.fn().mockReturnValue(undefined)
58
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
59
+ vi.mocked(getDocumentState).mockReturnValue({
60
+ getCurrent,
61
+ subscribe,
62
+ } as unknown as StateSource<unknown>)
63
+
64
+ const resolveDocPromise = Promise.resolve(doc)
65
+
66
+ // Also, simulate resolveDocument to return a known promise.
67
+ vi.mocked(resolveDocument).mockReturnValue(resolveDocPromise)
68
+
69
+ // Render the hook and capture the thrown promise.
70
+ const {result} = renderHook(() => {
71
+ try {
72
+ return useDocument('doc1')
73
+ } catch (e) {
74
+ return e
75
+ }
76
+ })
77
+
78
+ // When the document is not ready, the hook throws the promise from resolveDocument.
79
+ expect(result.current).toBe(resolveDocPromise)
80
+ })
81
+ })
@@ -0,0 +1,38 @@
1
+ import {
2
+ type DocumentHandle,
3
+ getDocumentState,
4
+ type JsonMatch,
5
+ type JsonMatchPath,
6
+ resolveDocument,
7
+ } from '@sanity/sdk'
8
+ import {type SanityDocument} from '@sanity/types'
9
+ import {useCallback, useMemo, useSyncExternalStore} from 'react'
10
+
11
+ import {useSanityInstance} from '../context/useSanityInstance'
12
+
13
+ /** @beta */
14
+ export function useDocument<
15
+ TDocument extends SanityDocument,
16
+ TPath extends JsonMatchPath<TDocument>,
17
+ >(doc: string | DocumentHandle<TDocument>, path: TPath): JsonMatch<TDocument, TPath> | undefined
18
+ /** @beta */
19
+ export function useDocument<TDocument extends SanityDocument>(
20
+ doc: string | DocumentHandle<TDocument>,
21
+ ): TDocument | null
22
+ /** @beta */
23
+ export function useDocument(doc: string | DocumentHandle, path?: string): unknown {
24
+ const documentId = typeof doc === 'string' ? doc : doc._id
25
+ const instance = useSanityInstance()
26
+ const isDocumentReady = useCallback(
27
+ () => getDocumentState(instance, documentId).getCurrent() !== undefined,
28
+ [instance, documentId],
29
+ )
30
+ if (!isDocumentReady()) throw resolveDocument(instance, documentId)
31
+
32
+ const {subscribe, getCurrent} = useMemo(
33
+ () => getDocumentState(instance, documentId, path),
34
+ [documentId, instance, path],
35
+ )
36
+
37
+ return useSyncExternalStore(subscribe, getCurrent)
38
+ }
@@ -0,0 +1,53 @@
1
+ // tests/useDocumentEvent.test.ts
2
+ import {createSanityInstance, type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'
3
+ import {renderHook} from '@testing-library/react'
4
+ import {beforeEach, describe, expect, it, vi} from 'vitest'
5
+
6
+ import {useSanityInstance} from '../context/useSanityInstance'
7
+ import {useDocumentEvent} from './useDocumentEvent'
8
+
9
+ vi.mock('@sanity/sdk', async (importOriginal) => {
10
+ const original = await importOriginal<typeof import('@sanity/sdk')>()
11
+ return {...original, subscribeDocumentEvents: vi.fn()}
12
+ })
13
+
14
+ vi.mock('../context/useSanityInstance', () => ({
15
+ useSanityInstance: vi.fn(),
16
+ }))
17
+
18
+ const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
19
+
20
+ describe('useDocumentEvent hook', () => {
21
+ beforeEach(() => {
22
+ vi.resetAllMocks()
23
+ vi.mocked(useSanityInstance).mockReturnValue(instance)
24
+ })
25
+
26
+ it('calls subscribeDocumentEvents with instance and a stable handler', () => {
27
+ const handler = vi.fn()
28
+ const unsubscribe = vi.fn()
29
+ vi.mocked(subscribeDocumentEvents).mockReturnValue(unsubscribe)
30
+
31
+ renderHook(() => useDocumentEvent(handler))
32
+
33
+ expect(vi.mocked(subscribeDocumentEvents)).toHaveBeenCalledTimes(1)
34
+ expect(vi.mocked(subscribeDocumentEvents).mock.calls[0][0]).toBe(instance)
35
+
36
+ const stableHandler = vi.mocked(subscribeDocumentEvents).mock.calls[0][1]
37
+ expect(typeof stableHandler).toBe('function')
38
+
39
+ const event = {type: 'edited', documentId: 'doc1', outgoing: {}} as DocumentEvent
40
+ stableHandler(event)
41
+ expect(handler).toHaveBeenCalledWith(event)
42
+ })
43
+
44
+ it('calls the unsubscribe function on unmount', () => {
45
+ const handler = vi.fn()
46
+ const unsubscribe = vi.fn()
47
+ vi.mocked(subscribeDocumentEvents).mockReturnValue(unsubscribe)
48
+
49
+ const {unmount} = renderHook(() => useDocumentEvent(handler))
50
+ unmount()
51
+ expect(unsubscribe).toHaveBeenCalledTimes(1)
52
+ })
53
+ })
@@ -0,0 +1,22 @@
1
+ import {type DocumentEvent, subscribeDocumentEvents} from '@sanity/sdk'
2
+ import {useCallback, useEffect, useInsertionEffect, useRef} from 'react'
3
+
4
+ import {useSanityInstance} from '../context/useSanityInstance'
5
+
6
+ /** @beta */
7
+ export function useDocumentEvent(handler: (documentEvent: DocumentEvent) => void): void {
8
+ const ref = useRef(handler)
9
+
10
+ useInsertionEffect(() => {
11
+ ref.current = handler
12
+ })
13
+
14
+ const stableHandler = useCallback((documentEvent: DocumentEvent) => {
15
+ return ref.current(documentEvent)
16
+ }, [])
17
+
18
+ const instance = useSanityInstance()
19
+ useEffect(() => {
20
+ return subscribeDocumentEvents(instance, stableHandler)
21
+ }, [instance, stableHandler])
22
+ }
@@ -0,0 +1,16 @@
1
+ import {getDocumentSyncStatus} from '@sanity/sdk'
2
+ import {identity} from 'rxjs'
3
+ import {describe, it} from 'vitest'
4
+
5
+ import {createStateSourceHook} from '../helpers/createStateSourceHook'
6
+
7
+ vi.mock('../helpers/createStateSourceHook', () => ({createStateSourceHook: vi.fn(identity)}))
8
+ vi.mock('@sanity/sdk', () => ({getDocumentSyncStatus: vi.fn()}))
9
+
10
+ describe('useAuthToken', () => {
11
+ it('calls `createStateSourceHook` with `getTokenState`', async () => {
12
+ const {useDocumentSyncStatus} = await import('./useDocumentSyncStatus')
13
+ expect(createStateSourceHook).toHaveBeenCalledWith(getDocumentSyncStatus)
14
+ expect(useDocumentSyncStatus).toBe(getDocumentSyncStatus)
15
+ })
16
+ })
@@ -0,0 +1,6 @@
1
+ import {getDocumentSyncStatus} from '@sanity/sdk'
2
+
3
+ import {createStateSourceHook} from '../helpers/createStateSourceHook'
4
+
5
+ /** @beta */
6
+ export const useDocumentSyncStatus = createStateSourceHook(getDocumentSyncStatus)
@@ -0,0 +1,134 @@
1
+ // tests/useEditDocument.test.ts
2
+ import {
3
+ createSanityInstance,
4
+ editDocument,
5
+ getDocumentState,
6
+ resolveDocument,
7
+ type StateSource,
8
+ } from '@sanity/sdk'
9
+ import {type SanityDocument} from '@sanity/types'
10
+ import {renderHook} from '@testing-library/react'
11
+ import {beforeEach, describe, expect, it, vi} from 'vitest'
12
+
13
+ import {useSanityInstance} from '../context/useSanityInstance'
14
+ import {useApplyActions} from './useApplyActions'
15
+ import {useEditDocument} from './useEditDocument'
16
+
17
+ vi.mock('@sanity/sdk', async (importOriginal) => {
18
+ const original = await importOriginal<typeof import('@sanity/sdk')>()
19
+ return {
20
+ ...original,
21
+ getDocumentState: vi.fn(),
22
+ resolveDocument: vi.fn(),
23
+ editDocument: vi.fn(original.editDocument),
24
+ }
25
+ })
26
+
27
+ vi.mock('../context/useSanityInstance', () => ({
28
+ useSanityInstance: vi.fn(),
29
+ }))
30
+
31
+ vi.mock('./useApplyActions', () => ({
32
+ useApplyActions: vi.fn(),
33
+ }))
34
+
35
+ // Create a fake instance to be returned by useSanityInstance.
36
+ const instance = createSanityInstance({projectId: 'p', dataset: 'd'})
37
+
38
+ const doc: SanityDocument = {
39
+ _id: 'doc1',
40
+ foo: 'bar',
41
+ _type: 'book',
42
+ _rev: 'tx0',
43
+ _createdAt: '2025-02-06T00:11:00.000Z',
44
+ _updatedAt: '2025-02-06T00:11:00.000Z',
45
+ }
46
+
47
+ describe('useEditDocument hook', () => {
48
+ beforeEach(() => {
49
+ vi.clearAllMocks()
50
+ vi.mocked(useSanityInstance).mockReturnValue(instance)
51
+ })
52
+
53
+ it('applies a single edit action for the given path', async () => {
54
+ const getCurrent = vi.fn().mockReturnValue(doc)
55
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
56
+ vi.mocked(getDocumentState).mockReturnValue({
57
+ getCurrent,
58
+ subscribe,
59
+ } as unknown as StateSource<SanityDocument>)
60
+
61
+ const apply = vi.fn().mockResolvedValue({transactionId: 'tx1'})
62
+ vi.mocked(useApplyActions).mockReturnValue(apply)
63
+
64
+ const {result} = renderHook(() => useEditDocument('doc1', 'foo'))
65
+ const promise = result.current('newValue')
66
+ expect(editDocument).toHaveBeenCalledWith('doc1', {set: {foo: 'newValue'}})
67
+ expect(apply).toHaveBeenCalledWith(editDocument('doc1', {set: {foo: 'newValue'}}))
68
+ const actionsResult = await promise
69
+ expect(actionsResult).toEqual({transactionId: 'tx1'})
70
+ })
71
+
72
+ it('applies edit actions for changed fields', async () => {
73
+ // Set up current document state.
74
+ const currentDoc = {...doc, foo: 'bar', extra: 'old'}
75
+ const getCurrent = vi.fn().mockReturnValue(currentDoc)
76
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
77
+ vi.mocked(getDocumentState).mockReturnValue({
78
+ getCurrent,
79
+ subscribe,
80
+ } as unknown as StateSource<SanityDocument>)
81
+
82
+ const apply = vi.fn().mockResolvedValue({transactionId: 'tx2'})
83
+ vi.mocked(useApplyActions).mockReturnValue(apply)
84
+
85
+ const {result} = renderHook(() => useEditDocument('doc1'))
86
+ const promise = result.current({foo: 'baz', extra: 'old', _id: 'doc1'})
87
+ expect(apply).toHaveBeenCalledWith([editDocument('doc1', {set: {foo: 'baz'}})])
88
+ const actionsResult = await promise
89
+ expect(actionsResult).toEqual({transactionId: 'tx2'})
90
+ })
91
+
92
+ it('throws an error if next value is not an object', () => {
93
+ const getCurrent = vi.fn().mockReturnValue(doc)
94
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
95
+ vi.mocked(getDocumentState).mockReturnValue({
96
+ getCurrent,
97
+ subscribe,
98
+ } as unknown as StateSource<SanityDocument>)
99
+
100
+ const fakeApply = vi.fn()
101
+ vi.mocked(useApplyActions).mockReturnValue(fakeApply)
102
+
103
+ const {result} = renderHook(() => useEditDocument('doc1'))
104
+ expect(() => result.current('notAnObject' as unknown as Partial<SanityDocument>)).toThrowError(
105
+ 'No path was provided to `useEditDocument` and the value provided was not a document object.',
106
+ )
107
+ })
108
+
109
+ it('throws a promise (suspends) when the document is not ready', () => {
110
+ const getCurrent = vi.fn().mockReturnValue(undefined)
111
+ const subscribe = vi.fn().mockReturnValue(vi.fn())
112
+ vi.mocked(getDocumentState).mockReturnValue({
113
+ getCurrent,
114
+ subscribe,
115
+ } as unknown as StateSource<unknown>)
116
+
117
+ const resolveDocPromise = Promise.resolve(doc)
118
+
119
+ // Also, simulate resolveDocument to return a known promise.
120
+ vi.mocked(resolveDocument).mockReturnValue(resolveDocPromise)
121
+
122
+ // Render the hook and capture the thrown promise.
123
+ const {result} = renderHook(() => {
124
+ try {
125
+ return useEditDocument('doc1')
126
+ } catch (e) {
127
+ return e
128
+ }
129
+ })
130
+
131
+ // When the document is not ready, the hook throws the promise from resolveDocument.
132
+ expect(result.current).toBe(resolveDocPromise)
133
+ })
134
+ })
@@ -0,0 +1,67 @@
1
+ import {
2
+ type ActionsResult,
3
+ type DocumentHandle,
4
+ editDocument,
5
+ getDocumentState,
6
+ type JsonMatch,
7
+ type JsonMatchPath,
8
+ resolveDocument,
9
+ } from '@sanity/sdk'
10
+ import {type SanityDocument} from '@sanity/types'
11
+ import {useCallback} from 'react'
12
+
13
+ import {useSanityInstance} from '../context/useSanityInstance'
14
+ import {useApplyActions} from './useApplyActions'
15
+
16
+ const ignoredKeys = ['_id', '_type', '_createdAt', '_updatedAt', '_rev']
17
+
18
+ /** @beta */
19
+ export function useEditDocument<
20
+ TDocument extends SanityDocument,
21
+ TPath extends JsonMatchPath<TDocument>,
22
+ >(
23
+ doc: string | DocumentHandle<TDocument>,
24
+ path: TPath,
25
+ ): (nextValue: JsonMatch<TDocument, TPath>) => Promise<ActionsResult<TDocument>>
26
+ /** @beta */
27
+ export function useEditDocument<TDocument extends SanityDocument>(
28
+ doc: string | DocumentHandle<TDocument>,
29
+ ): (nextValue: Partial<TDocument>) => Promise<ActionsResult<TDocument>>
30
+ /** @beta */
31
+ export function useEditDocument(
32
+ doc: string | DocumentHandle,
33
+ path?: string,
34
+ ): (nextValue: unknown) => Promise<ActionsResult> {
35
+ const documentId = typeof doc === 'string' ? doc : doc._id
36
+ const instance = useSanityInstance()
37
+ const apply = useApplyActions()
38
+ const isDocumentReady = useCallback(
39
+ () => getDocumentState(instance, documentId).getCurrent() !== undefined,
40
+ [instance, documentId],
41
+ )
42
+ if (!isDocumentReady()) throw resolveDocument(instance, documentId)
43
+
44
+ return useCallback(
45
+ (next: unknown) => {
46
+ if (path) {
47
+ return apply(editDocument(documentId, {set: {[path]: next}}))
48
+ }
49
+
50
+ const current = getDocumentState(instance, documentId).getCurrent()
51
+
52
+ if (typeof next !== 'object' || !next) {
53
+ throw new Error(
54
+ `No path was provided to \`useEditDocument\` and the value provided was not a document object.`,
55
+ )
56
+ }
57
+
58
+ const editActions = Object.entries(next)
59
+ .filter(([key]) => !ignoredKeys.includes(key))
60
+ .filter(([key, value]) => current?.[key] !== value)
61
+ .map(([key, value]) => editDocument(documentId, {set: {[key]: value}}))
62
+
63
+ return apply(editActions)
64
+ },
65
+ [apply, documentId, instance, path],
66
+ )
67
+ }