@sanity/sdk-react 0.0.0-alpha.3 → 0.0.0-alpha.5
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 +78 -28
- package/dist/_chunks-es/context.js +8 -0
- package/dist/_chunks-es/context.js.map +1 -0
- package/dist/_chunks-es/useLogOut.js +11 -11
- package/dist/_chunks-es/useLogOut.js.map +1 -1
- package/dist/components.d.ts +11 -186
- package/dist/components.js +52 -198
- package/dist/components.js.map +1 -1
- package/dist/context.d.ts +39 -0
- package/dist/context.js +5 -0
- package/dist/context.js.map +1 -0
- package/dist/hooks.d.ts +201 -15
- package/dist/hooks.js +80 -11
- package/dist/hooks.js.map +1 -1
- package/package.json +17 -15
- package/src/_exports/components.ts +2 -13
- package/src/_exports/context.ts +2 -0
- package/src/_exports/hooks.ts +18 -2
- package/src/components/SanityApp.test.tsx +54 -0
- package/src/components/SanityApp.tsx +26 -0
- package/src/components/auth/AuthBoundary.test.tsx +5 -18
- package/src/components/auth/AuthBoundary.tsx +2 -2
- package/src/components/auth/Login.test.tsx +3 -17
- package/src/components/auth/Login.tsx +25 -16
- package/src/components/auth/LoginCallback.test.tsx +2 -17
- package/src/components/auth/LoginCallback.tsx +6 -4
- package/src/components/auth/LoginError.test.tsx +2 -17
- package/src/components/auth/LoginError.tsx +8 -12
- package/src/components/auth/LoginFooter.test.tsx +2 -16
- package/src/components/auth/LoginFooter.tsx +11 -18
- package/src/components/auth/LoginLayout.test.tsx +2 -16
- package/src/components/auth/LoginLayout.tsx +8 -19
- package/src/components/auth/authTestHelpers.tsx +18 -0
- package/src/{components/context → context}/SanityProvider.test.tsx +1 -1
- package/src/hooks/client/useClient.test.tsx +1 -1
- package/src/hooks/comlink/useFrameConnection.test.tsx +122 -0
- package/src/hooks/comlink/useFrameConnection.ts +111 -0
- package/src/hooks/comlink/useWindowConnection.test.ts +94 -0
- package/src/hooks/comlink/useWindowConnection.ts +82 -0
- package/src/hooks/context/useSanityInstance.test.tsx +1 -1
- package/src/hooks/context/useSanityInstance.ts +2 -2
- package/src/hooks/documentCollection/useDocuments.ts +53 -6
- package/src/hooks/helpers/createCallbackHook.tsx +1 -1
- package/src/hooks/helpers/createStateSourceHook.tsx +1 -1
- package/src/hooks/preview/usePreview.test.tsx +17 -8
- package/src/hooks/preview/usePreview.tsx +52 -7
- package/src/vite-env.d.ts +10 -0
- package/dist/assets/bundle-CcAyERuZ.css +0 -11
- package/src/components/DocumentGridLayout/DocumentGridLayout.stories.tsx +0 -113
- package/src/components/DocumentGridLayout/DocumentGridLayout.test.tsx +0 -42
- package/src/components/DocumentGridLayout/DocumentGridLayout.tsx +0 -21
- package/src/components/DocumentListLayout/DocumentListLayout.stories.tsx +0 -105
- package/src/components/DocumentListLayout/DocumentListLayout.test.tsx +0 -42
- package/src/components/DocumentListLayout/DocumentListLayout.tsx +0 -12
- package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.md +0 -49
- package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.stories.tsx +0 -39
- package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.test.tsx +0 -30
- package/src/components/DocumentPreviewLayout/DocumentPreviewLayout.tsx +0 -171
- package/src/components/Login/LoginLinks.test.tsx +0 -100
- package/src/components/Login/LoginLinks.tsx +0 -73
- package/src/css/css.config.js +0 -220
- package/src/css/paramour.css +0 -2347
- package/src/css/styles.css +0 -11
- /package/src/{components/context → context}/SanityProvider.tsx +0 -0
package/dist/hooks.d.ts
CHANGED
|
@@ -3,8 +3,58 @@ import {AuthState} from '@sanity/sdk'
|
|
|
3
3
|
import {CurrentUser} from '@sanity/types'
|
|
4
4
|
import {DocumentHandle} from '@sanity/sdk'
|
|
5
5
|
import {DocumentListOptions} from '@sanity/sdk'
|
|
6
|
+
import {FrameMessage} from '@sanity/sdk'
|
|
6
7
|
import {PreviewValue} from '@sanity/sdk'
|
|
7
|
-
import
|
|
8
|
+
import {SanityInstance} from '@sanity/sdk'
|
|
9
|
+
import {WindowMessage} from '@sanity/sdk'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export declare interface DocumentCollection {
|
|
15
|
+
/** Retrieve more documents matching the provided options */
|
|
16
|
+
loadMore: () => void
|
|
17
|
+
/** The retrieved document handles of the documents matching the provided options */
|
|
18
|
+
results: DocumentHandle[]
|
|
19
|
+
/** Whether a retrieval of documents is in flight */
|
|
20
|
+
isPending: boolean
|
|
21
|
+
/** Whether more documents exist that match the provided options than have been retrieved */
|
|
22
|
+
hasMore: boolean
|
|
23
|
+
/** The total number of documents in the collection */
|
|
24
|
+
count: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export declare interface FrameConnection<TFrameMessage extends FrameMessage> {
|
|
31
|
+
connect: (frameWindow: Window) => () => void
|
|
32
|
+
sendMessage: <T extends TFrameMessage['type']>(
|
|
33
|
+
...params: Extract<
|
|
34
|
+
TFrameMessage,
|
|
35
|
+
{
|
|
36
|
+
type: T
|
|
37
|
+
}
|
|
38
|
+
>['data'] extends undefined
|
|
39
|
+
? [type: T]
|
|
40
|
+
: [
|
|
41
|
+
type: T,
|
|
42
|
+
data: Extract<
|
|
43
|
+
TFrameMessage,
|
|
44
|
+
{
|
|
45
|
+
type: T
|
|
46
|
+
}
|
|
47
|
+
>['data'],
|
|
48
|
+
]
|
|
49
|
+
) => void
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export declare type MessageHandler<TWindowMessage extends WindowMessage> = (
|
|
56
|
+
event: TWindowMessage['data'],
|
|
57
|
+
) => TWindowMessage['response'] | Promise<TWindowMessage['response']>
|
|
8
58
|
|
|
9
59
|
/**
|
|
10
60
|
* A React hook that subscribes to authentication state changes.
|
|
@@ -46,24 +96,73 @@ export declare const useCurrentUser: () => CurrentUser | null
|
|
|
46
96
|
|
|
47
97
|
/**
|
|
48
98
|
* @public
|
|
99
|
+
*
|
|
100
|
+
* The `useDocuments` hook retrieves and provides access to a live collection of documents, optionally filtered, sorted, and matched to a given Content Lake perspective.
|
|
101
|
+
* Because the returned document collection is live, the results will update in real time until the component invoking the hook is unmounted.
|
|
102
|
+
*
|
|
103
|
+
* @param options - Options for narrowing and sorting the document collection
|
|
104
|
+
* @returns The collection of documents matching the provided options (if any), as well as properties describing the collection and a function to load more.
|
|
105
|
+
*
|
|
106
|
+
* @example Retrieving all documents of type 'movie'
|
|
107
|
+
* ```
|
|
108
|
+
* const { results, isPending } = useDocuments({ filter: '_type == "movie"' })
|
|
109
|
+
*
|
|
110
|
+
* return (
|
|
111
|
+
* <div>
|
|
112
|
+
* <h1>Movies</h1>
|
|
113
|
+
* {results && (
|
|
114
|
+
* <ul>
|
|
115
|
+
* {results.map(movie => (<li key={movie._id}>…</li>))}
|
|
116
|
+
* </ul>
|
|
117
|
+
* )}
|
|
118
|
+
* {isPending && <div>Loading movies…</div>}
|
|
119
|
+
* </div>
|
|
120
|
+
* )
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @example Retrieving all movies released since 1980, sorted by release date
|
|
124
|
+
* ```
|
|
125
|
+
* const { results } = useDocuments({
|
|
126
|
+
* filter: '_type == "movie" && releaseDate >= "1980-01-01"',
|
|
127
|
+
* sort: [
|
|
128
|
+
* {
|
|
129
|
+
* field: 'releaseDate',
|
|
130
|
+
* sort: 'asc',
|
|
131
|
+
* },
|
|
132
|
+
* ],
|
|
133
|
+
* })
|
|
134
|
+
*
|
|
135
|
+
* return (
|
|
136
|
+
* <div>
|
|
137
|
+
* <h1>Movies released since 1980</h1>
|
|
138
|
+
* {results && (
|
|
139
|
+
* <ol>
|
|
140
|
+
* {results.map(movie => (<li key={movie._id}>…</li>))}
|
|
141
|
+
* </ol>
|
|
142
|
+
* )}
|
|
143
|
+
* </div>
|
|
144
|
+
* )
|
|
145
|
+
* ```
|
|
49
146
|
*/
|
|
50
|
-
export declare
|
|
51
|
-
loadMore: () => void
|
|
52
|
-
results: DocumentHandle[]
|
|
53
|
-
isPending: boolean
|
|
54
|
-
hasMore: boolean
|
|
55
|
-
count: number
|
|
56
|
-
}
|
|
147
|
+
export declare function useDocuments(options?: DocumentListOptions): DocumentCollection
|
|
57
148
|
|
|
58
149
|
/**
|
|
59
|
-
* Hook to get the list of documents for specified options
|
|
60
|
-
*
|
|
61
150
|
* @public
|
|
62
|
-
*
|
|
63
|
-
* @param options - options for the document list
|
|
64
|
-
* @returns result of the document list and function to load more
|
|
65
151
|
*/
|
|
66
|
-
export declare function
|
|
152
|
+
export declare function useFrameConnection<
|
|
153
|
+
TFrameMessage extends FrameMessage,
|
|
154
|
+
TWindowMessage extends WindowMessage,
|
|
155
|
+
>(options: UseFrameConnectionOptions<TWindowMessage>): FrameConnection<TFrameMessage>
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @public
|
|
159
|
+
*/
|
|
160
|
+
export declare interface UseFrameConnectionOptions<TWindowMessage extends WindowMessage> {
|
|
161
|
+
name: string
|
|
162
|
+
connectTo: string
|
|
163
|
+
targetOrigin: string
|
|
164
|
+
onMessage?: Record<string, MessageHandler<TWindowMessage>>
|
|
165
|
+
}
|
|
67
166
|
|
|
68
167
|
/**
|
|
69
168
|
* A React hook that returns a function for handling authentication callbacks.
|
|
@@ -157,11 +256,49 @@ export declare const useLogOut: () => () => Promise<void>
|
|
|
157
256
|
|
|
158
257
|
/**
|
|
159
258
|
* @alpha
|
|
259
|
+
*
|
|
260
|
+
* The `usePreview` hook takes a document (via a `DocumentHandle`) and returns its resolved preview values,
|
|
261
|
+
* including the document’s `title`, `subtitle`, `media`, and `status`. These values are live and will update in realtime.
|
|
262
|
+
* To reduce unnecessary network requests for resolving the preview values, an optional `ref` can be passed to the hook so that preview
|
|
263
|
+
* resolution will only occur if the `ref` is intersecting the current viewport.
|
|
264
|
+
*
|
|
265
|
+
* @param options - The document handle for the document you want to resolve preview values for, and an optional ref
|
|
266
|
+
* @returns The preview values for the given document and a boolean to indicate whether the resolution is pending
|
|
267
|
+
*
|
|
268
|
+
* @example Combining with useDocuments to render a collection of document previews
|
|
269
|
+
* ```
|
|
270
|
+
* // PreviewComponent.jsx
|
|
271
|
+
* export default function PreviewComponent({ document }) {
|
|
272
|
+
* const { results: { title, subtitle, media }, isPending } = usePreview({ document })
|
|
273
|
+
* return isPending ? 'Loading…' : (
|
|
274
|
+
* <article>
|
|
275
|
+
* {media?.type === 'image-asset' ? <img src={media.url} alt='' /> : ''}
|
|
276
|
+
* <h2>{title}</h2>
|
|
277
|
+
* <p>{subtitle}</p>
|
|
278
|
+
* </article>
|
|
279
|
+
* )
|
|
280
|
+
* }
|
|
281
|
+
*
|
|
282
|
+
* // DocumentList.jsx
|
|
283
|
+
* const { results, isPending } = useDocuments({ filter: '_type == "movie"' })
|
|
284
|
+
* return (
|
|
285
|
+
* <div>
|
|
286
|
+
* <h1>Movies</h1>
|
|
287
|
+
* <ul>
|
|
288
|
+
* {isPending ? 'Loading…' : results.map(movie => (
|
|
289
|
+
* <li key={movie._id}>
|
|
290
|
+
* <PreviewComponent document={movie} />
|
|
291
|
+
* </li>
|
|
292
|
+
* ))}
|
|
293
|
+
* </ul>
|
|
294
|
+
* </div>
|
|
295
|
+
* )
|
|
296
|
+
* ```
|
|
160
297
|
*/
|
|
161
298
|
export declare function usePreview({
|
|
162
299
|
document: {_id, _type},
|
|
163
300
|
ref,
|
|
164
|
-
}: UsePreviewOptions):
|
|
301
|
+
}: UsePreviewOptions): UsePreviewResults
|
|
165
302
|
|
|
166
303
|
/**
|
|
167
304
|
* @alpha
|
|
@@ -171,6 +308,16 @@ export declare interface UsePreviewOptions {
|
|
|
171
308
|
ref: HTMLElement | null
|
|
172
309
|
}
|
|
173
310
|
|
|
311
|
+
/**
|
|
312
|
+
* @alpha
|
|
313
|
+
*/
|
|
314
|
+
export declare interface UsePreviewResults {
|
|
315
|
+
/** The results of resolving the document’s preview values */
|
|
316
|
+
results: PreviewValue
|
|
317
|
+
/** Whether the resolution of the preview values is pending */
|
|
318
|
+
isPending: boolean
|
|
319
|
+
}
|
|
320
|
+
|
|
174
321
|
/**
|
|
175
322
|
* Hook that provides the current Sanity instance from the context.
|
|
176
323
|
* This must be called from within a `SanityProvider` component.
|
|
@@ -183,4 +330,43 @@ export declare interface UsePreviewOptions {
|
|
|
183
330
|
*/
|
|
184
331
|
export declare const useSanityInstance: () => SanityInstance
|
|
185
332
|
|
|
333
|
+
/**
|
|
334
|
+
* @public
|
|
335
|
+
*/
|
|
336
|
+
export declare function useWindowConnection<
|
|
337
|
+
TWindowMessage extends WindowMessage,
|
|
338
|
+
TFrameMessage extends FrameMessage,
|
|
339
|
+
>(options: UseWindowConnectionOptions<TFrameMessage>): WindowConnection<TWindowMessage>
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* @public
|
|
343
|
+
*/
|
|
344
|
+
export declare interface UseWindowConnectionOptions<TMessage extends FrameMessage> {
|
|
345
|
+
name: string
|
|
346
|
+
connectTo: string
|
|
347
|
+
onMessage?: Record<TMessage['type'], WindowMessageHandler<TMessage>>
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* @public
|
|
352
|
+
*/
|
|
353
|
+
export declare interface WindowConnection<TMessage extends WindowMessage> {
|
|
354
|
+
sendMessage: <TType extends TMessage['type']>(
|
|
355
|
+
type: TType,
|
|
356
|
+
data?: Extract<
|
|
357
|
+
TMessage,
|
|
358
|
+
{
|
|
359
|
+
type: TType
|
|
360
|
+
}
|
|
361
|
+
>['data'],
|
|
362
|
+
) => void
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* @public
|
|
367
|
+
*/
|
|
368
|
+
export declare type WindowMessageHandler<TFrameMessage extends FrameMessage> = (
|
|
369
|
+
event: TFrameMessage['data'],
|
|
370
|
+
) => TFrameMessage['response']
|
|
371
|
+
|
|
186
372
|
export {}
|
package/dist/hooks.js
CHANGED
|
@@ -1,9 +1,79 @@
|
|
|
1
1
|
import { createStateSourceHook, useSanityInstance } from "./_chunks-es/useLogOut.js";
|
|
2
2
|
import { useAuthState, useHandleCallback, useLogOut, useLoginUrls } from "./_chunks-es/useLogOut.js";
|
|
3
|
-
import { getTokenState, getCurrentUserState, createDocumentListStore, getPreviewState, resolvePreview } from "@sanity/sdk";
|
|
4
|
-
import {
|
|
3
|
+
import { getTokenState, getCurrentUserState, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, createDocumentListStore, getPreviewState, resolvePreview } from "@sanity/sdk";
|
|
4
|
+
import { useMemo, useEffect, useCallback, useState, useSyncExternalStore } from "react";
|
|
5
5
|
import { Observable, startWith, distinctUntilChanged, switchMap, EMPTY } from "rxjs";
|
|
6
|
-
const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState)
|
|
6
|
+
const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState);
|
|
7
|
+
function useFrameConnection(options) {
|
|
8
|
+
const { onMessage, targetOrigin, name, connectTo } = options, instance = useSanityInstance(), controller = useMemo(
|
|
9
|
+
() => getOrCreateController(instance, targetOrigin),
|
|
10
|
+
[instance, targetOrigin]
|
|
11
|
+
), channel = useMemo(
|
|
12
|
+
() => getOrCreateChannel(instance, {
|
|
13
|
+
name,
|
|
14
|
+
connectTo
|
|
15
|
+
}),
|
|
16
|
+
[instance, name, connectTo]
|
|
17
|
+
);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (!channel || !onMessage) return;
|
|
20
|
+
const unsubscribers = [];
|
|
21
|
+
return Object.entries(onMessage).forEach(([type, handler]) => {
|
|
22
|
+
const unsubscribe = channel.on(type, handler);
|
|
23
|
+
unsubscribers.push(unsubscribe);
|
|
24
|
+
}), () => {
|
|
25
|
+
unsubscribers.forEach((unsub) => unsub());
|
|
26
|
+
};
|
|
27
|
+
}, [channel, onMessage]);
|
|
28
|
+
const connect = useCallback(
|
|
29
|
+
(frameWindow) => {
|
|
30
|
+
const removeTarget = controller?.addTarget(frameWindow);
|
|
31
|
+
return () => {
|
|
32
|
+
removeTarget?.();
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
[controller]
|
|
36
|
+
), sendMessage = useCallback(
|
|
37
|
+
(type, data) => {
|
|
38
|
+
channel?.post(type, data);
|
|
39
|
+
},
|
|
40
|
+
[channel]
|
|
41
|
+
);
|
|
42
|
+
return useEffect(() => () => {
|
|
43
|
+
releaseChannel(instance, name);
|
|
44
|
+
}, [name, instance]), {
|
|
45
|
+
connect,
|
|
46
|
+
sendMessage
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function useWindowConnection(options) {
|
|
50
|
+
const { name, onMessage, connectTo } = options, instance = useSanityInstance(), node = useMemo(
|
|
51
|
+
() => getOrCreateNode(instance, { name, connectTo }),
|
|
52
|
+
[instance, name, connectTo]
|
|
53
|
+
);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
if (!onMessage) return;
|
|
56
|
+
const unsubscribers = [];
|
|
57
|
+
return Object.entries(onMessage).forEach(([type, handler]) => {
|
|
58
|
+
const unsubscribe = node.on(type, handler);
|
|
59
|
+
unsubscribers.push(unsubscribe);
|
|
60
|
+
}), () => {
|
|
61
|
+
unsubscribers.forEach((unsub) => unsub());
|
|
62
|
+
};
|
|
63
|
+
}, [node, onMessage]);
|
|
64
|
+
const sendMessage = useCallback(
|
|
65
|
+
(type, data) => {
|
|
66
|
+
node?.post(type, data);
|
|
67
|
+
},
|
|
68
|
+
[node]
|
|
69
|
+
);
|
|
70
|
+
return useEffect(() => () => {
|
|
71
|
+
releaseNode(instance, name);
|
|
72
|
+
}, [instance, name]), {
|
|
73
|
+
sendMessage
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
const STABLE_EMPTY = {
|
|
7
77
|
results: [],
|
|
8
78
|
isPending: !1,
|
|
9
79
|
hasMore: !1,
|
|
@@ -34,10 +104,7 @@ function useDocuments(options = {}) {
|
|
|
34
104
|
ref.storeInstance?.loadMore();
|
|
35
105
|
}, [ref]), ...state };
|
|
36
106
|
}
|
|
37
|
-
function usePreview({
|
|
38
|
-
document: { _id, _type },
|
|
39
|
-
ref
|
|
40
|
-
}) {
|
|
107
|
+
function usePreview({ document: { _id, _type }, ref }) {
|
|
41
108
|
const instance = useSanityInstance(), stateSource = useMemo(
|
|
42
109
|
() => getPreviewState(instance, { document: { _id, _type } }),
|
|
43
110
|
[instance, _id, _type]
|
|
@@ -61,9 +128,9 @@ function usePreview({
|
|
|
61
128
|
},
|
|
62
129
|
[stateSource, ref]
|
|
63
130
|
), getSnapshot = useCallback(() => {
|
|
64
|
-
const
|
|
65
|
-
if (
|
|
66
|
-
return
|
|
131
|
+
const currentState = stateSource.getCurrent();
|
|
132
|
+
if (currentState.results === null) throw resolvePreview(instance, { document: { _id, _type } });
|
|
133
|
+
return currentState;
|
|
67
134
|
}, [_id, _type, instance, stateSource]);
|
|
68
135
|
return useSyncExternalStore(subscribe, getSnapshot);
|
|
69
136
|
}
|
|
@@ -72,10 +139,12 @@ export {
|
|
|
72
139
|
useAuthToken,
|
|
73
140
|
useCurrentUser,
|
|
74
141
|
useDocuments,
|
|
142
|
+
useFrameConnection,
|
|
75
143
|
useHandleCallback,
|
|
76
144
|
useLogOut,
|
|
77
145
|
useLoginUrls,
|
|
78
146
|
usePreview,
|
|
79
|
-
useSanityInstance
|
|
147
|
+
useSanityInstance,
|
|
148
|
+
useWindowConnection
|
|
80
149
|
};
|
|
81
150
|
//# sourceMappingURL=hooks.js.map
|
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/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 {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 UseDocuments {\n loadMore: () => void\n results: DocumentHandle[]\n isPending: boolean\n hasMore: boolean\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 * Hook to get the list of documents for specified options\n *\n * @public\n *\n * @param options - options for the document list\n * @returns result of the document list and function to load more\n */\nexport function useDocuments(options: DocumentListOptions = {}): UseDocuments {\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 function usePreview({\n document: {_id, _type},\n ref,\n}: UsePreviewOptions): [PreviewValue, boolean] {\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 previewTuple = stateSource.getCurrent()\n if (!previewTuple[0]) throw resolvePreview(instance, {document: {_id, _type}})\n return previewTuple as [PreviewValue, boolean]\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,GCSjE,eAAe;AAAA,EACnB,SAAS,CAAC;AAAA,EACV,WAAW;AAAA,EACX,SAAS;AAAA,EACT,OAAO;AACT;AAUgB,SAAA,aAAa,UAA+B,IAAkB;AAC5E,QAAM,WAAW,kBAAkB,GAI7B,CAAC,GAAG,IAAI,SAIX,OAAO;AAAA,IACR,eAAe;AAAA,IACf,YAAY,MAAM;AAAA,IAClB,gBAAgB;AAAA,EAChB,EAAA,GAII,oBAAoB,KAAK,UAAU,OAAO;AAChD,YAAU,MAAM;AACd,QAAI,eAAe,WAAW,KAAK,MAAM,iBAAiB,CAAC;AAAA,EAAA,GAC1D,CAAC,KAAK,iBAAiB,CAAC;AAE3B,QAAM,YAAY;AAAA,IAChB,CAAC,mBAA+B;AAG1B,UAAA,gBAAgB,wBAAwB,QAAQ,GACpD,IAAI,cAAc,WAAW,IAAI,cAAc;AACzCA,YAAAA,SAAQ,IAAI,cAAc,SAAS;AACzC,UAAI,aAAaA,OAAM;AACjB,YAAA,cAAcA,OAAM,UAAU,cAAc;AAElD,aAAO,MAAM;AAEC,uBAEZ,IAAI,eAAe,QAAQ;AAAA,MAC7B;AAAA,IACF;AAAA,IACA,CAAC,UAAU,GAAG;AAAA,EAGV,GAAA,cAAc,YAAY,MACvB,IAAI,WAAW,GACrB,CAAC,GAAG,CAAC,GAEF,QAAQ,qBAAqB,WAAW,WAAW;AAMlD,SAAA,EAAC,UAJS,YAAY,MAAM;AACjC,QAAI,eAAe,SAAS;AAAA,KAC3B,CAAC,GAAG,CAAC,GAEU,GAAG,MAAK;AAC5B;ACrEO,SAAS,WAAW;AAAA,EACzB,UAAU,EAAC,KAAK,MAAK;AAAA,EACrB;AACF,GAA+C;AACvC,QAAA,WAAW,qBAEX,cAAc;AAAA,IAClB,MAAM,gBAAgB,UAAU,EAAC,UAAU,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,CAAC,aAAa,CAAC,EAAS,OAAA,eAAe,UAAU,EAAC,UAAU,EAAC,KAAK,SAAO;AACtE,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 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":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/sdk-react",
|
|
3
|
-
"version": "0.0.0-alpha.
|
|
3
|
+
"version": "0.0.0-alpha.5",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Sanity SDK React toolkit for Content OS",
|
|
6
6
|
"keywords": [
|
|
@@ -30,16 +30,21 @@
|
|
|
30
30
|
"import": "./dist/index.js",
|
|
31
31
|
"default": "./dist/index.js"
|
|
32
32
|
},
|
|
33
|
-
"./
|
|
34
|
-
"source": "./src/_exports/
|
|
35
|
-
"import": "./dist/
|
|
36
|
-
"default": "./dist/
|
|
33
|
+
"./context": {
|
|
34
|
+
"source": "./src/_exports/context.ts",
|
|
35
|
+
"import": "./dist/context.js",
|
|
36
|
+
"default": "./dist/context.js"
|
|
37
37
|
},
|
|
38
38
|
"./hooks": {
|
|
39
39
|
"source": "./src/_exports/hooks.ts",
|
|
40
40
|
"import": "./dist/hooks.js",
|
|
41
41
|
"default": "./dist/hooks.js"
|
|
42
42
|
},
|
|
43
|
+
"./components": {
|
|
44
|
+
"source": "./src/_exports/components.ts",
|
|
45
|
+
"import": "./dist/components.js",
|
|
46
|
+
"default": "./dist/components.js"
|
|
47
|
+
},
|
|
43
48
|
"./package.json": "./package.json"
|
|
44
49
|
},
|
|
45
50
|
"main": "./dist/index.js",
|
|
@@ -47,8 +52,8 @@
|
|
|
47
52
|
"types": "./dist/index.d.ts",
|
|
48
53
|
"typesVersions": {
|
|
49
54
|
"*": {
|
|
50
|
-
"
|
|
51
|
-
"./dist/
|
|
55
|
+
"context": [
|
|
56
|
+
"./dist/context.d.ts"
|
|
52
57
|
],
|
|
53
58
|
"hooks": [
|
|
54
59
|
"./dist/hooks.d.ts"
|
|
@@ -62,21 +67,17 @@
|
|
|
62
67
|
"browserslist": "extends @sanity/browserslist-config",
|
|
63
68
|
"prettier": "@sanity/prettier-config",
|
|
64
69
|
"dependencies": {
|
|
65
|
-
"@paramour/css": "1.0.0-rc.2",
|
|
66
|
-
"@sanity/icons": "^3.5.2",
|
|
67
70
|
"@sanity/logos": "^2.1.13",
|
|
68
71
|
"@sanity/ui": "^2.8.19",
|
|
69
|
-
"inter-ui": "^4.1.0",
|
|
70
72
|
"react-error-boundary": "^4.1.2",
|
|
71
|
-
"rollup-plugin-import-css": "^3.5.7",
|
|
72
73
|
"rxjs": "^7.8.1",
|
|
73
|
-
"@sanity/sdk": "0.0.0-alpha.
|
|
74
|
+
"@sanity/sdk": "0.0.0-alpha.5"
|
|
74
75
|
},
|
|
75
76
|
"devDependencies": {
|
|
76
77
|
"@sanity/client": "^6.24.1",
|
|
78
|
+
"@sanity/comlink": "^2.0.2",
|
|
77
79
|
"@sanity/pkg-utils": "^6.12.2",
|
|
78
80
|
"@sanity/prettier-config": "^1.0.3",
|
|
79
|
-
"@storybook/react": "^8.4.7",
|
|
80
81
|
"@testing-library/jest-dom": "^6.6.3",
|
|
81
82
|
"@testing-library/react": "^16.1.0",
|
|
82
83
|
"@types/react": "^19.0.3",
|
|
@@ -92,7 +93,9 @@
|
|
|
92
93
|
"vite": "^5.4.11",
|
|
93
94
|
"vitest": "^2.1.8",
|
|
94
95
|
"@repo/config-eslint": "0.0.0",
|
|
95
|
-
"@repo/
|
|
96
|
+
"@repo/config-test": "0.0.1",
|
|
97
|
+
"@repo/package.config": "0.0.1",
|
|
98
|
+
"@repo/tsconfig": "0.0.1"
|
|
96
99
|
},
|
|
97
100
|
"peerDependencies": {
|
|
98
101
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -111,7 +114,6 @@
|
|
|
111
114
|
"docs": "typedoc --out docs --tsconfig ../../tsconfig.tsdoc.json",
|
|
112
115
|
"format": "prettier --write --cache --ignore-unknown .",
|
|
113
116
|
"lint": "eslint .",
|
|
114
|
-
"paramour": "npx paramour --config=./src/css/css.config.js --output=./src/css/paramour.css",
|
|
115
117
|
"test": "vitest run",
|
|
116
118
|
"test:coverage": "vitest run --coverage",
|
|
117
119
|
"test:watch": "vitest",
|
|
@@ -1,13 +1,2 @@
|
|
|
1
|
-
export {AuthBoundary
|
|
2
|
-
export {
|
|
3
|
-
export {Login} from '../components/auth/Login'
|
|
4
|
-
export {LoginCallback} from '../components/auth/LoginCallback'
|
|
5
|
-
export {LoginError, type LoginErrorProps} from '../components/auth/LoginError'
|
|
6
|
-
export {LoginLayout, type LoginLayoutProps} from '../components/auth/LoginLayout'
|
|
7
|
-
export type {SanityProviderProps} from '../components/context/SanityProvider'
|
|
8
|
-
export {SanityProvider} from '../components/context/SanityProvider'
|
|
9
|
-
export {DocumentGridLayout} from '../components/DocumentGridLayout/DocumentGridLayout'
|
|
10
|
-
export {DocumentListLayout} from '../components/DocumentListLayout/DocumentListLayout'
|
|
11
|
-
export {DocumentPreviewLayout} from '../components/DocumentPreviewLayout/DocumentPreviewLayout'
|
|
12
|
-
export {type DocumentPreviewLayoutProps} from '../components/DocumentPreviewLayout/DocumentPreviewLayout'
|
|
13
|
-
export {LoginLinks} from '../components/Login/LoginLinks'
|
|
1
|
+
export {AuthBoundary} from '../components/auth/AuthBoundary'
|
|
2
|
+
export {SanityApp} from '../components/SanityApp'
|
package/src/_exports/hooks.ts
CHANGED
|
@@ -4,6 +4,22 @@ export {useCurrentUser} from '../hooks/auth/useCurrentUser'
|
|
|
4
4
|
export {useHandleCallback} from '../hooks/auth/useHandleCallback'
|
|
5
5
|
export {useLoginUrls} from '../hooks/auth/useLoginUrls'
|
|
6
6
|
export {useLogOut} from '../hooks/auth/useLogOut'
|
|
7
|
+
export {
|
|
8
|
+
type FrameConnection,
|
|
9
|
+
type FrameMessageHandler as MessageHandler,
|
|
10
|
+
useFrameConnection,
|
|
11
|
+
type UseFrameConnectionOptions,
|
|
12
|
+
} from '../hooks/comlink/useFrameConnection'
|
|
13
|
+
export {
|
|
14
|
+
useWindowConnection,
|
|
15
|
+
type UseWindowConnectionOptions,
|
|
16
|
+
type WindowConnection,
|
|
17
|
+
type WindowMessageHandler,
|
|
18
|
+
} from '../hooks/comlink/useWindowConnection'
|
|
7
19
|
export {useSanityInstance} from '../hooks/context/useSanityInstance'
|
|
8
|
-
export {type
|
|
9
|
-
export {
|
|
20
|
+
export {type DocumentCollection, useDocuments} from '../hooks/documentCollection/useDocuments'
|
|
21
|
+
export {
|
|
22
|
+
usePreview,
|
|
23
|
+
type UsePreviewOptions,
|
|
24
|
+
type UsePreviewResults,
|
|
25
|
+
} from '../hooks/preview/usePreview'
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import {AuthStateType, type SanityConfig} from '@sanity/sdk'
|
|
2
|
+
import {render, screen} from '@testing-library/react'
|
|
3
|
+
import {describe, expect, it} from 'vitest'
|
|
4
|
+
|
|
5
|
+
import {SanityApp} from './SanityApp'
|
|
6
|
+
|
|
7
|
+
vi.mock('@sanity/sdk', async () => {
|
|
8
|
+
const actual = await vi.importActual('@sanity/sdk')
|
|
9
|
+
return {
|
|
10
|
+
...actual,
|
|
11
|
+
createSanityInstance: vi.fn(() => ({
|
|
12
|
+
config: {},
|
|
13
|
+
auth: {
|
|
14
|
+
getSession: vi.fn(),
|
|
15
|
+
signIn: vi.fn(),
|
|
16
|
+
signOut: vi.fn(),
|
|
17
|
+
},
|
|
18
|
+
identity: {
|
|
19
|
+
projectId: 'test-project',
|
|
20
|
+
dataset: 'test-dataset',
|
|
21
|
+
},
|
|
22
|
+
dispose: vi.fn(),
|
|
23
|
+
})),
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
vi.mock('../hooks/auth/useAuthState', () => ({
|
|
28
|
+
useAuthState: () => ({
|
|
29
|
+
type: AuthStateType.LOGGED_IN,
|
|
30
|
+
session: {
|
|
31
|
+
user: {
|
|
32
|
+
id: 'test-user',
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
}))
|
|
37
|
+
|
|
38
|
+
describe('SanityApp', () => {
|
|
39
|
+
const mockSanityConfig: SanityConfig = {
|
|
40
|
+
projectId: 'test-project',
|
|
41
|
+
dataset: 'test-dataset',
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
it('renders children correctly', async () => {
|
|
45
|
+
const testMessage = 'Test Child Component'
|
|
46
|
+
render(
|
|
47
|
+
<SanityApp sanityConfig={mockSanityConfig}>
|
|
48
|
+
<div>{testMessage}</div>
|
|
49
|
+
</SanityApp>,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
expect(await screen.findByText(testMessage)).toBeInTheDocument()
|
|
53
|
+
})
|
|
54
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {createSanityInstance, type SanityConfig} from '@sanity/sdk'
|
|
2
|
+
import {type JSX} from 'react'
|
|
3
|
+
|
|
4
|
+
import {SanityProvider} from '../context/SanityProvider'
|
|
5
|
+
import {AuthBoundary} from './auth/AuthBoundary'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
*
|
|
10
|
+
* @returns Rendered child component wrapped in a SanityProvider and AuthBoundary
|
|
11
|
+
*/
|
|
12
|
+
export function SanityApp({
|
|
13
|
+
sanityConfig,
|
|
14
|
+
children,
|
|
15
|
+
}: {
|
|
16
|
+
children: React.ReactNode
|
|
17
|
+
sanityConfig: SanityConfig
|
|
18
|
+
}): JSX.Element {
|
|
19
|
+
const sanityInstance = createSanityInstance(sanityConfig)
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<SanityProvider sanityInstance={sanityInstance}>
|
|
23
|
+
<AuthBoundary>{children}</AuthBoundary>
|
|
24
|
+
</SanityProvider>
|
|
25
|
+
)
|
|
26
|
+
}
|