@sanity/sdk-react 0.0.0-alpha.19 → 0.0.0-alpha.20
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 +1 -1
- package/dist/index.d.ts +130 -29
- package/dist/index.js +193 -99
- package/dist/index.js.map +1 -1
- package/package.json +10 -10
- package/src/_exports/index.ts +9 -1
- package/src/hooks/auth/useDashboardOrganizationId.test.tsx +42 -0
- package/src/hooks/auth/useDashboardOrganizationId.tsx +29 -0
- package/src/hooks/comlink/useFrameConnection.test.tsx +20 -10
- package/src/hooks/comlink/useFrameConnection.ts +33 -56
- package/src/hooks/comlink/useManageFavorite.ts +4 -1
- package/src/hooks/comlink/useRecordDocumentHistoryEvent.ts +6 -2
- package/src/hooks/comlink/useWindowConnection.test.ts +20 -10
- package/src/hooks/comlink/useWindowConnection.ts +69 -41
- package/src/hooks/dashboard/useNavigateToStudioDocument.ts +97 -0
- package/src/hooks/dashboard/useStudioWorkspacesByResourceId.test.tsx +274 -0
- package/src/hooks/dashboard/useStudioWorkspacesByResourceId.ts +91 -0
- package/src/hooks/document/usePermissions.ts +5 -5
- package/src/hooks/preview/usePreview.test.tsx +6 -6
- package/src/hooks/preview/usePreview.tsx +5 -5
- package/src/hooks/projection/useProjection.test.tsx +9 -9
- package/src/hooks/projection/useProjection.ts +25 -13
|
@@ -9,14 +9,22 @@ import {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxj
|
|
|
9
9
|
|
|
10
10
|
import {useSanityInstance} from '../context/useSanityInstance'
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @public
|
|
14
|
+
* @category Types
|
|
15
|
+
*/
|
|
16
|
+
export interface UseProjectionOptions {
|
|
13
17
|
document: DocumentHandle
|
|
14
18
|
projection: ValidProjection
|
|
15
19
|
ref?: React.RefObject<unknown>
|
|
16
20
|
}
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
/**
|
|
23
|
+
* @public
|
|
24
|
+
* @category Types
|
|
25
|
+
*/
|
|
26
|
+
export interface UseProjectionResults<TResult extends object> {
|
|
27
|
+
data: TResult
|
|
20
28
|
isPending: boolean
|
|
21
29
|
}
|
|
22
30
|
|
|
@@ -32,21 +40,25 @@ interface UseProjectionResults<TResult extends object> {
|
|
|
32
40
|
* @param options - The document handle for the document you want to project values from, the projection string, and an optional ref
|
|
33
41
|
* @returns The projection values for the given document and a boolean to indicate whether the resolution is pending
|
|
34
42
|
*
|
|
35
|
-
* @example Using a projection to
|
|
43
|
+
* @example Using a projection to render a preview of document
|
|
36
44
|
* ```
|
|
37
45
|
* // ProjectionComponent.jsx
|
|
38
46
|
* export default function ProjectionComponent({ document }) {
|
|
39
47
|
* const ref = useRef(null)
|
|
40
|
-
* const { results: { title,
|
|
48
|
+
* const { results: { title, coverImage, authors }, isPending } = useProjection({
|
|
41
49
|
* document,
|
|
42
|
-
*
|
|
43
|
-
*
|
|
50
|
+
* ref,
|
|
51
|
+
* projection: `{
|
|
52
|
+
* title,
|
|
53
|
+
* 'coverImage': cover.asset->url,
|
|
54
|
+
* 'authors': array::join(authors[]->{'name': firstName + ' ' + lastName + ' '}.name, ', ')
|
|
55
|
+
* }`,
|
|
44
56
|
* })
|
|
45
57
|
*
|
|
46
58
|
* return (
|
|
47
59
|
* <article ref={ref} style={{ opacity: isPending ? 0.5 : 1}}>
|
|
48
60
|
* <h2>{title}</h2>
|
|
49
|
-
* <
|
|
61
|
+
* <img src={coverImage} alt={title} />
|
|
50
62
|
* <p>{authors}</p>
|
|
51
63
|
* </article>
|
|
52
64
|
* )
|
|
@@ -59,13 +71,13 @@ interface UseProjectionResults<TResult extends object> {
|
|
|
59
71
|
* const { data } = useInfiniteList({ filter: '_type == "article"' })
|
|
60
72
|
* return (
|
|
61
73
|
* <div>
|
|
62
|
-
* <h1>
|
|
74
|
+
* <h1>Books</h1>
|
|
63
75
|
* <ul>
|
|
64
|
-
* {data.map(
|
|
65
|
-
* <li key={
|
|
76
|
+
* {data.map(book => (
|
|
77
|
+
* <li key={book._id}>
|
|
66
78
|
* <Suspense fallback='Loading…'>
|
|
67
79
|
* <ProjectionComponent
|
|
68
|
-
* document={
|
|
80
|
+
* document={book}
|
|
69
81
|
* />
|
|
70
82
|
* </Suspense>
|
|
71
83
|
* </li>
|
|
@@ -126,7 +138,7 @@ export function useProjection<TResult extends object>({
|
|
|
126
138
|
// Create getSnapshot function to return current state
|
|
127
139
|
const getSnapshot = useCallback(() => {
|
|
128
140
|
const currentState = stateSource.getCurrent()
|
|
129
|
-
if (currentState.
|
|
141
|
+
if (currentState.data === null)
|
|
130
142
|
throw resolveProjection(instance, {document: {_id, _type}, projection})
|
|
131
143
|
return currentState as UseProjectionResults<TResult>
|
|
132
144
|
}, [_id, _type, projection, instance, stateSource])
|