@sanity/sdk-react 0.0.0-alpha.5 → 0.0.0-alpha.6
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
|
@@ -44,9 +44,10 @@ import './App.css'
|
|
|
44
44
|
const sanityConfig: SanityConfig = {
|
|
45
45
|
projectId: '<your-project-id>',
|
|
46
46
|
dataset: '<your-dataset>',
|
|
47
|
-
// optional auth config set projectId and dataset to '' and authScope to '
|
|
47
|
+
// optional auth config set projectId and dataset to '' and authScope to 'global' for a global token
|
|
48
48
|
// auth: {
|
|
49
|
-
// authScope:
|
|
49
|
+
// authScope: 'global',
|
|
50
|
+
// ...
|
|
50
51
|
// },
|
|
51
52
|
}
|
|
52
53
|
|
package/dist/hooks.d.ts
CHANGED
|
@@ -120,13 +120,14 @@ export declare const useCurrentUser: () => CurrentUser | null
|
|
|
120
120
|
* )
|
|
121
121
|
* ```
|
|
122
122
|
*
|
|
123
|
-
* @example Retrieving all movies released since 1980, sorted by
|
|
123
|
+
* @example Retrieving all movies released since 1980, sorted by director’s last name
|
|
124
124
|
* ```
|
|
125
125
|
* const { results } = useDocuments({
|
|
126
126
|
* filter: '_type == "movie" && releaseDate >= "1980-01-01"',
|
|
127
127
|
* sort: [
|
|
128
128
|
* {
|
|
129
|
-
* field
|
|
129
|
+
* // Expand the `director` reference field with the dereferencing operator `->`
|
|
130
|
+
* field: 'director->lastName',
|
|
130
131
|
* sort: 'asc',
|
|
131
132
|
* },
|
|
132
133
|
* ],
|
|
@@ -287,7 +288,9 @@ export declare const useLogOut: () => () => Promise<void>
|
|
|
287
288
|
* <ul>
|
|
288
289
|
* {isPending ? 'Loading…' : results.map(movie => (
|
|
289
290
|
* <li key={movie._id}>
|
|
290
|
-
* <
|
|
291
|
+
* <Suspense fallback='Loading…'>
|
|
292
|
+
* <PreviewComponent document={movie} />
|
|
293
|
+
* </Suspense>
|
|
291
294
|
* </li>
|
|
292
295
|
* ))}
|
|
293
296
|
* </ul>
|
|
@@ -305,7 +308,7 @@ export declare function usePreview({
|
|
|
305
308
|
*/
|
|
306
309
|
export declare interface UsePreviewOptions {
|
|
307
310
|
document: DocumentHandle
|
|
308
|
-
ref
|
|
311
|
+
ref?: React.RefObject<unknown>
|
|
309
312
|
}
|
|
310
313
|
|
|
311
314
|
/**
|
|
@@ -314,7 +317,7 @@ export declare interface UsePreviewOptions {
|
|
|
314
317
|
export declare interface UsePreviewResults {
|
|
315
318
|
/** The results of resolving the document’s preview values */
|
|
316
319
|
results: PreviewValue
|
|
317
|
-
/**
|
|
320
|
+
/** True when preview values are being refreshed */
|
|
318
321
|
isPending: boolean
|
|
319
322
|
}
|
|
320
323
|
|
package/dist/hooks.js
CHANGED
|
@@ -111,12 +111,13 @@ function usePreview({ document: { _id, _type }, ref }) {
|
|
|
111
111
|
), subscribe = useCallback(
|
|
112
112
|
(onStoreChanged) => {
|
|
113
113
|
const subscription = new Observable((observer) => {
|
|
114
|
-
if (typeof IntersectionObserver > "u")
|
|
114
|
+
if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
|
|
115
|
+
return;
|
|
115
116
|
const intersectionObserver = new IntersectionObserver(
|
|
116
117
|
([entry]) => observer.next(entry.isIntersecting),
|
|
117
118
|
{ rootMargin: "0px", threshold: 0 }
|
|
118
119
|
);
|
|
119
|
-
return ref && intersectionObserver.observe(ref), () => intersectionObserver.disconnect();
|
|
120
|
+
return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
|
|
120
121
|
}).pipe(
|
|
121
122
|
startWith(!1),
|
|
122
123
|
distinctUntilChanged(),
|
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/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 * @public\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @public\n * @returns The current user or null if not authenticated\n */\nexport const useCurrentUser = createStateSourceHook(getCurrentUserState)\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 * @public\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @public\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 * @public\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 * @public\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 * @public\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @public\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @public\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 * @public\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 release date\n * ```\n * const { results } = useDocuments({\n * filter: '_type == \"movie\" && releaseDate >= \"1980-01-01\"',\n * sort: [\n * {\n * field: 'releaseDate',\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: HTMLElement | null\n}\n\n/**\n * @alpha\n */\nexport interface UsePreviewResults {\n /** The results of resolving the document’s preview values */\n results: PreviewValue\n /** Whether the resolution of the preview values is pending */\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 isPending ? 'Loading…' : (\n * <article>\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 * <PreviewComponent document={movie} />\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') return\n\n const intersectionObserver = new IntersectionObserver(\n ([entry]) => observer.next(entry.isIntersecting),\n {rootMargin: '0px', threshold: 0},\n )\n if (ref) intersectionObserver.observe(ref)\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,GCAlD,iBAAiB,sBAAsB,mBAAmB;ACkChE,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,O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AAU,EAAC,KAAK,MAAK,GAAE;AAAA,IACxD,CAAC,UAAU,KAAK,KAAK;AAAA,KAIjB,YAAY;AAAA,IAChB,CAAC,mBAA+B;AAC9B,YAAM,eAAe,IAAI,WAAoB,CAAC,aAAa;AAErD,YAAA,OAAO,uBAAyB,IAAa;AAEjD,cAAM,uBAAuB,IAAI;AAAA,UAC/B,CAAC,CAAC,KAAK,MAAM,SAAS,KAAK,MAAM,cAAc;AAAA,UAC/C,EAAC,YAAY,OAAO,WAAW,EAAC;AAAA,QAClC;AACA,eAAI,OAAK,qBAAqB,QAAQ,GAAG,GAClC,MAAM,qBAAqB,WAAW;AAAA,MAC9C,CAAA,EACE;AAAA,QACC,UAAU,EAAK;AAAA,QACf,qBAAqB;AAAA,QACrB;AAAA,UAAU,CAAC,cACT,YACI,IAAI,WAAiB,CAAC,QACb,YAAY,UAAU,MAAM,IAAI,KAAK,CAAC,CAC9C,IACD;AAAA,QAAA;AAAA,MAGP,EAAA,UAAU,EAAC,MAAM,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;"}
|
|
1
|
+
{"version":3,"file":"hooks.js","sources":["../src/hooks/auth/useAuthToken.tsx","../src/hooks/auth/useCurrentUser.tsx","../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 * @public\n * @returns The current user or null if not authenticated\n */\nexport const useAuthToken = createStateSourceHook(getTokenState)\n","import {getCurrentUserState} from '@sanity/sdk'\n\nimport {createStateSourceHook} from '../helpers/createStateSourceHook'\n\n/**\n * Hook to get the currently logged in user\n * @public\n * @returns The current user or null if not authenticated\n */\nexport const useCurrentUser = createStateSourceHook(getCurrentUserState)\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 * @public\n */\nexport type FrameMessageHandler<TWindowMessage extends WindowMessage> = (\n event: TWindowMessage['data'],\n) => TWindowMessage['response'] | Promise<TWindowMessage['response']>\n\n/**\n * @public\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 * @public\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 * @public\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 * @public\n */\nexport type WindowMessageHandler<TFrameMessage extends FrameMessage> = (\n event: TFrameMessage['data'],\n) => TFrameMessage['response']\n\n/**\n * @public\n */\nexport interface UseWindowConnectionOptions<TMessage extends FrameMessage> {\n name: string\n connectTo: string\n onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>\n}\n\n/**\n * @public\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 * @public\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 isPending ? 'Loading…' : (\n * <article>\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,GCAlD,iBAAiB,sBAAsB,mBAAmB;ACkChE,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,cAA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K React toolkit for Content OS",
|
|
6
6
|
"keywords": [
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"@sanity/ui": "^2.8.19",
|
|
72
72
|
"react-error-boundary": "^4.1.2",
|
|
73
73
|
"rxjs": "^7.8.1",
|
|
74
|
-
"@sanity/sdk": "0.0.0-alpha.
|
|
74
|
+
"@sanity/sdk": "0.0.0-alpha.6"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
77
|
"@sanity/client": "^6.24.1",
|
|
@@ -92,10 +92,10 @@
|
|
|
92
92
|
"typescript": "^5.7.2",
|
|
93
93
|
"vite": "^5.4.11",
|
|
94
94
|
"vitest": "^2.1.8",
|
|
95
|
-
"@repo/config-eslint": "0.0.0",
|
|
96
|
-
"@repo/config-test": "0.0.1",
|
|
97
95
|
"@repo/package.config": "0.0.1",
|
|
98
|
-
"@repo/tsconfig": "0.0.1"
|
|
96
|
+
"@repo/tsconfig": "0.0.1",
|
|
97
|
+
"@repo/config-test": "0.0.1",
|
|
98
|
+
"@repo/config-eslint": "0.0.0"
|
|
99
99
|
},
|
|
100
100
|
"peerDependencies": {
|
|
101
101
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -54,13 +54,14 @@ const STABLE_EMPTY = {
|
|
|
54
54
|
* )
|
|
55
55
|
* ```
|
|
56
56
|
*
|
|
57
|
-
* @example Retrieving all movies released since 1980, sorted by
|
|
57
|
+
* @example Retrieving all movies released since 1980, sorted by director’s last name
|
|
58
58
|
* ```
|
|
59
59
|
* const { results } = useDocuments({
|
|
60
60
|
* filter: '_type == "movie" && releaseDate >= "1980-01-01"',
|
|
61
61
|
* sort: [
|
|
62
62
|
* {
|
|
63
|
-
* field
|
|
63
|
+
* // Expand the `director` reference field with the dereferencing operator `->`
|
|
64
|
+
* field: 'director->lastName',
|
|
64
65
|
* sort: 'asc',
|
|
65
66
|
* },
|
|
66
67
|
* ],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {type DocumentHandle, getPreviewState, type PreviewValue, resolvePreview} from '@sanity/sdk'
|
|
2
2
|
import {act, render, screen} from '@testing-library/react'
|
|
3
|
-
import {Suspense,
|
|
3
|
+
import {Suspense, useRef} from 'react'
|
|
4
4
|
import {type Mock} from 'vitest'
|
|
5
5
|
|
|
6
6
|
import {usePreview} from './usePreview'
|
|
@@ -44,11 +44,11 @@ const mockDocument: DocumentHandle = {
|
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
function TestComponent({document}: {document: DocumentHandle}) {
|
|
47
|
-
const
|
|
47
|
+
const ref = useRef(null)
|
|
48
48
|
const {results, isPending} = usePreview({document, ref})
|
|
49
49
|
|
|
50
50
|
return (
|
|
51
|
-
<div ref={
|
|
51
|
+
<div ref={ref}>
|
|
52
52
|
<h1>{results?.title}</h1>
|
|
53
53
|
<p>{results?.subtitle}</p>
|
|
54
54
|
{isPending && <div>Pending...</div>}
|
|
@@ -9,7 +9,7 @@ import {useSanityInstance} from '../context/useSanityInstance'
|
|
|
9
9
|
*/
|
|
10
10
|
export interface UsePreviewOptions {
|
|
11
11
|
document: DocumentHandle
|
|
12
|
-
ref
|
|
12
|
+
ref?: React.RefObject<unknown>
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -18,7 +18,7 @@ export interface UsePreviewOptions {
|
|
|
18
18
|
export interface UsePreviewResults {
|
|
19
19
|
/** The results of resolving the document’s preview values */
|
|
20
20
|
results: PreviewValue
|
|
21
|
-
/**
|
|
21
|
+
/** True when preview values are being refreshed */
|
|
22
22
|
isPending: boolean
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -55,7 +55,9 @@ export interface UsePreviewResults {
|
|
|
55
55
|
* <ul>
|
|
56
56
|
* {isPending ? 'Loading…' : results.map(movie => (
|
|
57
57
|
* <li key={movie._id}>
|
|
58
|
-
* <
|
|
58
|
+
* <Suspense fallback='Loading…'>
|
|
59
|
+
* <PreviewComponent document={movie} />
|
|
60
|
+
* </Suspense>
|
|
59
61
|
* </li>
|
|
60
62
|
* ))}
|
|
61
63
|
* </ul>
|
|
@@ -76,13 +78,17 @@ export function usePreview({document: {_id, _type}, ref}: UsePreviewOptions): Us
|
|
|
76
78
|
(onStoreChanged: () => void) => {
|
|
77
79
|
const subscription = new Observable<boolean>((observer) => {
|
|
78
80
|
// for environments that don't have an intersection observer
|
|
79
|
-
if (typeof IntersectionObserver === 'undefined')
|
|
81
|
+
if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
80
84
|
|
|
81
85
|
const intersectionObserver = new IntersectionObserver(
|
|
82
86
|
([entry]) => observer.next(entry.isIntersecting),
|
|
83
87
|
{rootMargin: '0px', threshold: 0},
|
|
84
88
|
)
|
|
85
|
-
if (ref
|
|
89
|
+
if (ref?.current && ref.current instanceof HTMLElement) {
|
|
90
|
+
intersectionObserver.observe(ref.current)
|
|
91
|
+
}
|
|
86
92
|
return () => intersectionObserver.disconnect()
|
|
87
93
|
})
|
|
88
94
|
.pipe(
|