@sanity/sdk-react 0.0.0-alpha.8 → 0.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -126
- package/dist/index.d.ts +4641 -2
- package/dist/index.js +960 -2
- package/dist/index.js.map +1 -1
- package/package.json +17 -40
- package/src/_exports/index.ts +58 -10
- package/src/components/Login/LoginLinks.test.tsx +90 -0
- package/src/components/Login/LoginLinks.tsx +58 -0
- package/src/components/SDKProvider.test.tsx +79 -0
- package/src/components/SDKProvider.tsx +42 -0
- package/src/components/SanityApp.test.tsx +104 -2
- package/src/components/SanityApp.tsx +54 -17
- package/src/components/auth/AuthBoundary.test.tsx +2 -2
- package/src/components/auth/AuthBoundary.tsx +13 -3
- package/src/components/auth/Login.test.tsx +1 -1
- package/src/components/auth/Login.tsx +11 -26
- package/src/components/auth/LoginCallback.tsx +4 -7
- package/src/components/auth/LoginError.tsx +12 -8
- package/src/components/auth/LoginFooter.tsx +13 -20
- package/src/components/auth/LoginLayout.tsx +8 -9
- package/src/components/auth/authTestHelpers.tsx +1 -8
- package/src/components/utils.ts +22 -0
- package/src/context/SanityInstanceContext.ts +4 -0
- package/src/context/SanityProvider.test.tsx +1 -1
- package/src/context/SanityProvider.tsx +10 -8
- package/src/hooks/_synchronous-groq-js.mjs +4 -0
- package/src/hooks/auth/useAuthState.tsx +0 -2
- package/src/hooks/auth/useCurrentUser.tsx +26 -20
- package/src/hooks/client/useClient.ts +8 -30
- package/src/hooks/comlink/useFrameConnection.test.tsx +45 -10
- package/src/hooks/comlink/useFrameConnection.ts +24 -5
- package/src/hooks/comlink/useManageFavorite.test.ts +106 -0
- package/src/hooks/comlink/useManageFavorite.ts +98 -0
- package/src/hooks/comlink/useRecordDocumentHistoryEvent.test.ts +77 -0
- package/src/hooks/comlink/useRecordDocumentHistoryEvent.ts +75 -0
- package/src/hooks/comlink/useWindowConnection.test.ts +43 -12
- package/src/hooks/comlink/useWindowConnection.ts +13 -1
- package/src/hooks/context/useSanityInstance.test.tsx +1 -1
- package/src/hooks/context/useSanityInstance.ts +21 -5
- package/src/hooks/datasets/useDatasets.ts +37 -0
- package/src/hooks/document/useApplyActions.test.ts +25 -0
- package/src/hooks/document/useApplyActions.ts +74 -0
- package/src/hooks/document/useDocument.test.ts +81 -0
- package/src/hooks/document/useDocument.ts +107 -0
- package/src/hooks/document/useDocumentEvent.test.ts +63 -0
- package/src/hooks/document/useDocumentEvent.ts +54 -0
- package/src/hooks/document/useDocumentSyncStatus.test.ts +16 -0
- package/src/hooks/document/useDocumentSyncStatus.ts +30 -0
- package/src/hooks/document/useEditDocument.test.ts +179 -0
- package/src/hooks/document/useEditDocument.ts +195 -0
- package/src/hooks/document/usePermissions.ts +82 -0
- package/src/hooks/helpers/createCallbackHook.tsx +3 -2
- package/src/hooks/helpers/createStateSourceHook.test.tsx +66 -0
- package/src/hooks/helpers/createStateSourceHook.tsx +29 -10
- package/src/hooks/infiniteList/useInfiniteList.test.tsx +152 -0
- package/src/hooks/infiniteList/useInfiniteList.ts +174 -0
- package/src/hooks/paginatedList/usePaginatedList.test.tsx +259 -0
- package/src/hooks/paginatedList/usePaginatedList.ts +290 -0
- package/src/hooks/preview/usePreview.tsx +7 -4
- package/src/hooks/projection/useProjection.test.tsx +218 -0
- package/src/hooks/projection/useProjection.ts +135 -0
- package/src/hooks/projects/useProject.ts +45 -0
- package/src/hooks/projects/useProjects.ts +41 -0
- package/src/hooks/query/useQuery.test.tsx +188 -0
- package/src/hooks/query/useQuery.ts +103 -0
- package/src/hooks/users/useUsers.test.ts +163 -0
- package/src/hooks/users/useUsers.ts +107 -0
- package/src/utils/getEnv.ts +21 -0
- package/src/version.ts +8 -0
- package/dist/_chunks-es/context.js +0 -8
- package/dist/_chunks-es/context.js.map +0 -1
- package/dist/_chunks-es/useLogOut.js +0 -44
- package/dist/_chunks-es/useLogOut.js.map +0 -1
- package/dist/components.d.ts +0 -111
- package/dist/components.js +0 -153
- package/dist/components.js.map +0 -1
- package/dist/context.d.ts +0 -45
- package/dist/context.js +0 -5
- package/dist/context.js.map +0 -1
- package/dist/hooks.d.ts +0 -3485
- package/dist/hooks.js +0 -167
- package/dist/hooks.js.map +0 -1
- package/src/_exports/components.ts +0 -2
- package/src/_exports/context.ts +0 -2
- package/src/_exports/hooks.ts +0 -27
- package/src/hooks/client/useClient.test.tsx +0 -130
- package/src/hooks/documentCollection/useDocuments.test.ts +0 -130
- package/src/hooks/documentCollection/useDocuments.ts +0 -135
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type DocumentHandle,
|
|
3
|
+
getProjectionState,
|
|
4
|
+
resolveProjection,
|
|
5
|
+
type ValidProjection,
|
|
6
|
+
} from '@sanity/sdk'
|
|
7
|
+
import {act, render, screen} from '@testing-library/react'
|
|
8
|
+
import {Suspense, useRef} from 'react'
|
|
9
|
+
import {type Mock} from 'vitest'
|
|
10
|
+
|
|
11
|
+
import {useProjection} from './useProjection'
|
|
12
|
+
|
|
13
|
+
// Mock IntersectionObserver
|
|
14
|
+
const mockIntersectionObserver = vi.fn()
|
|
15
|
+
let intersectionObserverCallback: (entries: IntersectionObserverEntry[]) => void
|
|
16
|
+
|
|
17
|
+
beforeAll(() => {
|
|
18
|
+
vi.stubGlobal(
|
|
19
|
+
'IntersectionObserver',
|
|
20
|
+
class {
|
|
21
|
+
constructor(callback: (entries: IntersectionObserverEntry[]) => void) {
|
|
22
|
+
intersectionObserverCallback = callback
|
|
23
|
+
mockIntersectionObserver(callback)
|
|
24
|
+
}
|
|
25
|
+
observe = vi.fn()
|
|
26
|
+
disconnect = vi.fn()
|
|
27
|
+
},
|
|
28
|
+
)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
// Mock the projection store
|
|
32
|
+
vi.mock('@sanity/sdk', () => {
|
|
33
|
+
const getCurrent = vi.fn()
|
|
34
|
+
const subscribe = vi.fn()
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
resolveProjection: vi.fn(),
|
|
38
|
+
getProjectionState: vi.fn().mockReturnValue({getCurrent, subscribe}),
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
vi.mock('../context/useSanityInstance', () => ({
|
|
43
|
+
useSanityInstance: () => ({}),
|
|
44
|
+
}))
|
|
45
|
+
|
|
46
|
+
const mockDocument: DocumentHandle = {
|
|
47
|
+
_id: 'doc1',
|
|
48
|
+
_type: 'exampleType',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface ProjectionResult {
|
|
52
|
+
title: string
|
|
53
|
+
description: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function TestComponent({
|
|
57
|
+
document,
|
|
58
|
+
projection,
|
|
59
|
+
}: {
|
|
60
|
+
document: DocumentHandle
|
|
61
|
+
projection: ValidProjection
|
|
62
|
+
}) {
|
|
63
|
+
const ref = useRef(null)
|
|
64
|
+
const {results, isPending} = useProjection<ProjectionResult>({document, projection, ref})
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div ref={ref}>
|
|
68
|
+
<h1>{results.title}</h1>
|
|
69
|
+
<p>{results.description}</p>
|
|
70
|
+
{isPending && <div>Pending...</div>}
|
|
71
|
+
</div>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe('useProjection', () => {
|
|
76
|
+
let getCurrent: Mock
|
|
77
|
+
let subscribe: Mock
|
|
78
|
+
|
|
79
|
+
beforeEach(() => {
|
|
80
|
+
// @ts-expect-error mock does not need param
|
|
81
|
+
getCurrent = getProjectionState().getCurrent as Mock
|
|
82
|
+
// @ts-expect-error mock does not need param
|
|
83
|
+
subscribe = getProjectionState().subscribe as Mock
|
|
84
|
+
|
|
85
|
+
// Reset all mocks between tests
|
|
86
|
+
getCurrent.mockReset()
|
|
87
|
+
subscribe.mockReset()
|
|
88
|
+
mockIntersectionObserver.mockReset()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('it only subscribes when element is visible', async () => {
|
|
92
|
+
// Setup initial state
|
|
93
|
+
getCurrent.mockReturnValue({
|
|
94
|
+
results: {title: 'Initial Title', description: 'Initial Description'},
|
|
95
|
+
isPending: false,
|
|
96
|
+
})
|
|
97
|
+
const eventsUnsubscribe = vi.fn()
|
|
98
|
+
subscribe.mockImplementation(() => eventsUnsubscribe)
|
|
99
|
+
|
|
100
|
+
render(
|
|
101
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
102
|
+
<TestComponent document={mockDocument} projection="{name, description}" />
|
|
103
|
+
</Suspense>,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
// Initially, element is not intersecting
|
|
107
|
+
expect(screen.getByText('Initial Title')).toBeInTheDocument()
|
|
108
|
+
expect(subscribe).not.toHaveBeenCalled()
|
|
109
|
+
|
|
110
|
+
// Simulate element becoming visible
|
|
111
|
+
await act(async () => {
|
|
112
|
+
intersectionObserverCallback([{isIntersecting: true} as IntersectionObserverEntry])
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// After element becomes visible, events subscription should be active
|
|
116
|
+
expect(subscribe).toHaveBeenCalled()
|
|
117
|
+
expect(eventsUnsubscribe).not.toHaveBeenCalled()
|
|
118
|
+
|
|
119
|
+
// Simulate element becoming hidden
|
|
120
|
+
await act(async () => {
|
|
121
|
+
intersectionObserverCallback([{isIntersecting: false} as IntersectionObserverEntry])
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
// When hidden, should maintain last known state
|
|
125
|
+
expect(screen.getByText('Initial Title')).toBeInTheDocument()
|
|
126
|
+
expect(eventsUnsubscribe).toHaveBeenCalled()
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('it suspends and resolves data when element becomes visible', async () => {
|
|
130
|
+
// Mock the initial state to trigger suspense
|
|
131
|
+
getCurrent.mockReturnValueOnce({
|
|
132
|
+
results: null,
|
|
133
|
+
isPending: true,
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
const resolvedData = {
|
|
137
|
+
results: {title: 'Resolved Title', description: 'Resolved Description'},
|
|
138
|
+
isPending: false,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Mock resolveProjection to return a promise that resolves immediately
|
|
142
|
+
;(resolveProjection as Mock).mockReturnValueOnce(Promise.resolve(resolvedData))
|
|
143
|
+
|
|
144
|
+
// After suspense resolves, return the resolved data
|
|
145
|
+
getCurrent.mockReturnValue(resolvedData)
|
|
146
|
+
|
|
147
|
+
// Setup subscription that does nothing (we'll manually trigger updates)
|
|
148
|
+
subscribe.mockReturnValue(() => {})
|
|
149
|
+
|
|
150
|
+
render(
|
|
151
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
152
|
+
<TestComponent document={mockDocument} projection="{title, description}" />
|
|
153
|
+
</Suspense>,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
await act(async () => {
|
|
157
|
+
intersectionObserverCallback([{isIntersecting: true} as IntersectionObserverEntry])
|
|
158
|
+
await Promise.resolve()
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
expect(screen.getByText('Resolved Title')).toBeInTheDocument()
|
|
162
|
+
expect(screen.getByText('Resolved Description')).toBeInTheDocument()
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('it handles environments without IntersectionObserver', async () => {
|
|
166
|
+
// Temporarily remove IntersectionObserver
|
|
167
|
+
const originalIntersectionObserver = window.IntersectionObserver
|
|
168
|
+
// @ts-expect-error - Intentionally removing IntersectionObserver
|
|
169
|
+
delete window.IntersectionObserver
|
|
170
|
+
|
|
171
|
+
getCurrent.mockReturnValue({
|
|
172
|
+
results: {title: 'Fallback Title', description: 'Fallback Description'},
|
|
173
|
+
isPending: false,
|
|
174
|
+
})
|
|
175
|
+
subscribe.mockImplementation(() => vi.fn())
|
|
176
|
+
|
|
177
|
+
render(
|
|
178
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
179
|
+
<TestComponent document={mockDocument} projection="{title, description}" />
|
|
180
|
+
</Suspense>,
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
expect(screen.getByText('Fallback Title')).toBeInTheDocument()
|
|
184
|
+
|
|
185
|
+
// Restore IntersectionObserver
|
|
186
|
+
window.IntersectionObserver = originalIntersectionObserver
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('it updates when projection changes', async () => {
|
|
190
|
+
getCurrent.mockReturnValue({
|
|
191
|
+
results: {title: 'Initial Title'},
|
|
192
|
+
isPending: false,
|
|
193
|
+
})
|
|
194
|
+
const eventsUnsubscribe = vi.fn()
|
|
195
|
+
subscribe.mockImplementation(() => eventsUnsubscribe)
|
|
196
|
+
|
|
197
|
+
const {rerender} = render(
|
|
198
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
199
|
+
<TestComponent document={mockDocument} projection="{title}" />
|
|
200
|
+
</Suspense>,
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
// Change projection
|
|
204
|
+
getCurrent.mockReturnValue({
|
|
205
|
+
results: {title: 'Updated Title', description: 'Added Description'},
|
|
206
|
+
isPending: false,
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
rerender(
|
|
210
|
+
<Suspense fallback={<div>Loading...</div>}>
|
|
211
|
+
<TestComponent document={mockDocument} projection="{title, description}" />
|
|
212
|
+
</Suspense>,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
expect(screen.getByText('Updated Title')).toBeInTheDocument()
|
|
216
|
+
expect(screen.getByText('Added Description')).toBeInTheDocument()
|
|
217
|
+
})
|
|
218
|
+
})
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type DocumentHandle,
|
|
3
|
+
getProjectionState,
|
|
4
|
+
resolveProjection,
|
|
5
|
+
type ValidProjection,
|
|
6
|
+
} from '@sanity/sdk'
|
|
7
|
+
import {useCallback, useMemo, useSyncExternalStore} from 'react'
|
|
8
|
+
import {distinctUntilChanged, EMPTY, Observable, startWith, switchMap} from 'rxjs'
|
|
9
|
+
|
|
10
|
+
import {useSanityInstance} from '../context/useSanityInstance'
|
|
11
|
+
|
|
12
|
+
interface UseProjectionOptions {
|
|
13
|
+
document: DocumentHandle
|
|
14
|
+
projection: ValidProjection
|
|
15
|
+
ref?: React.RefObject<unknown>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface UseProjectionResults<TResult extends object> {
|
|
19
|
+
results: TResult
|
|
20
|
+
isPending: boolean
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @beta
|
|
25
|
+
*
|
|
26
|
+
* Returns the projection values of a document (specified via a `DocumentHandle`),
|
|
27
|
+
* based on the provided projection string. These values are live and will update in realtime.
|
|
28
|
+
* To reduce unnecessary network requests for resolving the projection values, an optional `ref` can be passed to the hook so that projection
|
|
29
|
+
* resolution will only occur if the `ref` is intersecting the current viewport.
|
|
30
|
+
*
|
|
31
|
+
* @category Documents
|
|
32
|
+
* @param options - The document handle for the document you want to project values from, the projection string, and an optional ref
|
|
33
|
+
* @returns The projection values for the given document and a boolean to indicate whether the resolution is pending
|
|
34
|
+
*
|
|
35
|
+
* @example Using a projection to display specific document fields
|
|
36
|
+
* ```
|
|
37
|
+
* // ProjectionComponent.jsx
|
|
38
|
+
* export default function ProjectionComponent({ document }) {
|
|
39
|
+
* const ref = useRef(null)
|
|
40
|
+
* const { results: { title, description, authors }, isPending } = useProjection({
|
|
41
|
+
* document,
|
|
42
|
+
* projection: '{title, "description": pt::text("description"), "authors": array::join(authors[]->name, ", ")}',
|
|
43
|
+
* ref
|
|
44
|
+
* })
|
|
45
|
+
*
|
|
46
|
+
* return (
|
|
47
|
+
* <article ref={ref} style={{ opacity: isPending ? 0.5 : 1}}>
|
|
48
|
+
* <h2>{title}</h2>
|
|
49
|
+
* <p>{description}</p>
|
|
50
|
+
* <p>{authors}</p>
|
|
51
|
+
* </article>
|
|
52
|
+
* )
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example Combining with useInfiniteList to render a collection with specific fields
|
|
57
|
+
* ```
|
|
58
|
+
* // DocumentList.jsx
|
|
59
|
+
* const { data } = useInfiniteList({ filter: '_type == "article"' })
|
|
60
|
+
* return (
|
|
61
|
+
* <div>
|
|
62
|
+
* <h1>Articles</h1>
|
|
63
|
+
* <ul>
|
|
64
|
+
* {data.map(article => (
|
|
65
|
+
* <li key={article._id}>
|
|
66
|
+
* <Suspense fallback='Loading…'>
|
|
67
|
+
* <ProjectionComponent
|
|
68
|
+
* document={article}
|
|
69
|
+
* />
|
|
70
|
+
* </Suspense>
|
|
71
|
+
* </li>
|
|
72
|
+
* ))}
|
|
73
|
+
* </ul>
|
|
74
|
+
* </div>
|
|
75
|
+
* )
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export function useProjection<TResult extends object>({
|
|
79
|
+
document: {_id, _type},
|
|
80
|
+
projection,
|
|
81
|
+
ref,
|
|
82
|
+
}: UseProjectionOptions): UseProjectionResults<TResult> {
|
|
83
|
+
const instance = useSanityInstance()
|
|
84
|
+
|
|
85
|
+
const stateSource = useMemo(
|
|
86
|
+
() => getProjectionState<TResult>(instance, {document: {_id, _type}, projection}),
|
|
87
|
+
[instance, _id, _type, projection],
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
// Create subscribe function for useSyncExternalStore
|
|
91
|
+
const subscribe = useCallback(
|
|
92
|
+
(onStoreChanged: () => void) => {
|
|
93
|
+
const subscription = new Observable<boolean>((observer) => {
|
|
94
|
+
// for environments that don't have an intersection observer
|
|
95
|
+
if (typeof IntersectionObserver === 'undefined' || typeof HTMLElement === 'undefined') {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const intersectionObserver = new IntersectionObserver(
|
|
100
|
+
([entry]) => observer.next(entry.isIntersecting),
|
|
101
|
+
{rootMargin: '0px', threshold: 0},
|
|
102
|
+
)
|
|
103
|
+
if (ref?.current && ref.current instanceof HTMLElement) {
|
|
104
|
+
intersectionObserver.observe(ref.current)
|
|
105
|
+
}
|
|
106
|
+
return () => intersectionObserver.disconnect()
|
|
107
|
+
})
|
|
108
|
+
.pipe(
|
|
109
|
+
startWith(false),
|
|
110
|
+
distinctUntilChanged(),
|
|
111
|
+
switchMap((isVisible) =>
|
|
112
|
+
isVisible
|
|
113
|
+
? new Observable<void>((obs) => {
|
|
114
|
+
return stateSource.subscribe(() => obs.next())
|
|
115
|
+
})
|
|
116
|
+
: EMPTY,
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
.subscribe({next: onStoreChanged})
|
|
120
|
+
|
|
121
|
+
return () => subscription.unsubscribe()
|
|
122
|
+
},
|
|
123
|
+
[stateSource, ref],
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
// Create getSnapshot function to return current state
|
|
127
|
+
const getSnapshot = useCallback(() => {
|
|
128
|
+
const currentState = stateSource.getCurrent()
|
|
129
|
+
if (currentState.results === null)
|
|
130
|
+
throw resolveProjection(instance, {document: {_id, _type}, projection})
|
|
131
|
+
return currentState as UseProjectionResults<TResult>
|
|
132
|
+
}, [_id, _type, projection, instance, stateSource])
|
|
133
|
+
|
|
134
|
+
return useSyncExternalStore(subscribe, getSnapshot)
|
|
135
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getProjectState,
|
|
3
|
+
resolveProject,
|
|
4
|
+
type SanityInstance,
|
|
5
|
+
type SanityProject,
|
|
6
|
+
type StateSource,
|
|
7
|
+
} from '@sanity/sdk'
|
|
8
|
+
|
|
9
|
+
import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
10
|
+
|
|
11
|
+
type UseProject = {
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* Returns metadata for a given project
|
|
15
|
+
*
|
|
16
|
+
* @category Projects
|
|
17
|
+
* @param projectId - The ID of the project to retrieve metadata for
|
|
18
|
+
* @returns The metadata for the project
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* function ProjectMetadata({ projectId }: { projectId: string }) {
|
|
22
|
+
* const project = useProject(projectId)
|
|
23
|
+
*
|
|
24
|
+
* return (
|
|
25
|
+
* <figure style={{ backgroundColor: project.metadata.color || 'lavender'}}>
|
|
26
|
+
* <h1>{project.displayName}</h1>
|
|
27
|
+
* </figure>
|
|
28
|
+
* )
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
(projectId: string): SanityProject
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @public */
|
|
36
|
+
export const useProject: UseProject = createStateSourceHook({
|
|
37
|
+
// remove `undefined` since we're suspending when that is the case
|
|
38
|
+
getState: getProjectState as (
|
|
39
|
+
instance: SanityInstance,
|
|
40
|
+
projectId: string,
|
|
41
|
+
) => StateSource<SanityProject>,
|
|
42
|
+
shouldSuspend: (instance, projectId) =>
|
|
43
|
+
getProjectState(instance, projectId).getCurrent() === undefined,
|
|
44
|
+
suspender: resolveProject,
|
|
45
|
+
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {type SanityProject} from '@sanity/client'
|
|
2
|
+
import {getProjectsState, resolveProjects, type SanityInstance, type StateSource} from '@sanity/sdk'
|
|
3
|
+
|
|
4
|
+
import {createStateSourceHook} from '../helpers/createStateSourceHook'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
* @category Types
|
|
9
|
+
*/
|
|
10
|
+
export type ProjectWithoutMembers = Omit<SanityProject, 'members'>
|
|
11
|
+
|
|
12
|
+
type UseProjects = {
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* Returns metadata for each project in your organization.
|
|
16
|
+
*
|
|
17
|
+
* @category Projects
|
|
18
|
+
* @returns An array of metadata (minus the projects’ members) for each project in your organization
|
|
19
|
+
* @example
|
|
20
|
+
* ```tsx
|
|
21
|
+
* const projects = useProjects()
|
|
22
|
+
*
|
|
23
|
+
* return (
|
|
24
|
+
* <select>
|
|
25
|
+
* {projects.map((project) => (
|
|
26
|
+
* <option key={project.id}>{project.displayName}</option>
|
|
27
|
+
* ))}
|
|
28
|
+
* </select>
|
|
29
|
+
* )
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
(): ProjectWithoutMembers[]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** @public */
|
|
36
|
+
export const useProjects: UseProjects = createStateSourceHook({
|
|
37
|
+
// remove `undefined` since we're suspending when that is the case
|
|
38
|
+
getState: getProjectsState as (instance: SanityInstance) => StateSource<ProjectWithoutMembers[]>,
|
|
39
|
+
shouldSuspend: (instance) => getProjectsState(instance).getCurrent() === undefined,
|
|
40
|
+
suspender: resolveProjects,
|
|
41
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {getQueryState, resolveQuery, type StateSource} from '@sanity/sdk'
|
|
2
|
+
import {act, render, screen} from '@testing-library/react'
|
|
3
|
+
import {Suspense, useState} from 'react'
|
|
4
|
+
import {type Observable, Subject} from 'rxjs'
|
|
5
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
6
|
+
|
|
7
|
+
import {useQuery} from './useQuery'
|
|
8
|
+
|
|
9
|
+
// Mock the functions from '@sanity/sdk'
|
|
10
|
+
vi.mock('@sanity/sdk', async (importOriginal) => {
|
|
11
|
+
const original = await importOriginal<typeof import('@sanity/sdk')>()
|
|
12
|
+
return {
|
|
13
|
+
...original,
|
|
14
|
+
getQueryState: vi.fn(),
|
|
15
|
+
resolveQuery: vi.fn(),
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
// Mock the Sanity instance hook to return a dummy instance
|
|
20
|
+
vi.mock('../context/useSanityInstance', () => ({
|
|
21
|
+
useSanityInstance: vi.fn().mockReturnValue({}),
|
|
22
|
+
}))
|
|
23
|
+
|
|
24
|
+
describe('useQuery', () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
vi.resetAllMocks()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should render data immediately when available', () => {
|
|
30
|
+
const getCurrent = vi.fn().mockReturnValue('test data')
|
|
31
|
+
vi.mocked(getQueryState).mockReturnValue({
|
|
32
|
+
getCurrent,
|
|
33
|
+
subscribe: vi.fn(),
|
|
34
|
+
get observable(): Observable<unknown> {
|
|
35
|
+
throw new Error('Not implemented')
|
|
36
|
+
},
|
|
37
|
+
} as StateSource<unknown>)
|
|
38
|
+
|
|
39
|
+
function TestComponent() {
|
|
40
|
+
const {data, isPending} = useQuery<string>('test query')
|
|
41
|
+
return (
|
|
42
|
+
<div data-testid="output">
|
|
43
|
+
{data} - {isPending ? 'pending' : 'not pending'}
|
|
44
|
+
</div>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
render(<TestComponent />)
|
|
49
|
+
|
|
50
|
+
// Verify that the output contains the data and that isPending is false
|
|
51
|
+
expect(screen.getByTestId('output').textContent).toContain('test data')
|
|
52
|
+
expect(screen.getByTestId('output').textContent).toContain('not pending')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('should suspend rendering until data is resolved via Suspense', async () => {
|
|
56
|
+
const ref = {current: undefined as string | undefined}
|
|
57
|
+
const getCurrent = vi.fn(() => ref.current)
|
|
58
|
+
const storeChanged$ = new Subject<void>()
|
|
59
|
+
|
|
60
|
+
vi.mocked(getQueryState).mockReturnValue({
|
|
61
|
+
getCurrent,
|
|
62
|
+
subscribe: vi.fn((cb) => {
|
|
63
|
+
const subscription = storeChanged$.subscribe(cb)
|
|
64
|
+
return () => subscription.unsubscribe()
|
|
65
|
+
}),
|
|
66
|
+
get observable(): Observable<unknown> {
|
|
67
|
+
throw new Error('Not implemented')
|
|
68
|
+
},
|
|
69
|
+
} as StateSource<unknown>)
|
|
70
|
+
|
|
71
|
+
// Create a controllable promise to simulate the query resolution
|
|
72
|
+
let resolvePromise: () => void
|
|
73
|
+
// Mock resolveQuery to return our fake promise
|
|
74
|
+
vi.mocked(resolveQuery).mockReturnValue(
|
|
75
|
+
new Promise<void>((resolve) => {
|
|
76
|
+
resolvePromise = () => {
|
|
77
|
+
ref.current = 'resolved data'
|
|
78
|
+
storeChanged$.next()
|
|
79
|
+
resolve()
|
|
80
|
+
}
|
|
81
|
+
}),
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
function TestComponent() {
|
|
85
|
+
const {data} = useQuery<string>('test query')
|
|
86
|
+
return <div data-testid="output">{data}</div>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
render(
|
|
90
|
+
<Suspense fallback={<div data-testid="fallback">Loading...</div>}>
|
|
91
|
+
<TestComponent />
|
|
92
|
+
</Suspense>,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
// Initially, since storeValue is undefined, the component should suspend and fallback is shown
|
|
96
|
+
expect(screen.getByTestId('fallback')).toBeInTheDocument()
|
|
97
|
+
|
|
98
|
+
// Now simulate that data becomes available
|
|
99
|
+
await act(async () => {
|
|
100
|
+
resolvePromise()
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
expect(screen.getByTestId('output').textContent).toContain('resolved data')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('should display transition pending state during query change', async () => {
|
|
107
|
+
const ref = {current: undefined as string | undefined}
|
|
108
|
+
const getCurrent = vi.fn(() => ref.current)
|
|
109
|
+
const storeChanged$ = new Subject<void>()
|
|
110
|
+
|
|
111
|
+
vi.mocked(getQueryState).mockImplementation((_instance, query) => {
|
|
112
|
+
if (query === 'query1') {
|
|
113
|
+
return {
|
|
114
|
+
getCurrent: vi.fn().mockReturnValue('data1'),
|
|
115
|
+
subscribe: vi.fn(),
|
|
116
|
+
get observable(): Observable<unknown> {
|
|
117
|
+
throw new Error('Not implemented')
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
getCurrent,
|
|
124
|
+
subscribe: vi.fn((cb) => {
|
|
125
|
+
const subscription = storeChanged$.subscribe(cb)
|
|
126
|
+
return () => subscription.unsubscribe()
|
|
127
|
+
}),
|
|
128
|
+
get observable(): Observable<unknown> {
|
|
129
|
+
throw new Error('Not implemented')
|
|
130
|
+
},
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
// Create a controllable promise to simulate the query resolution
|
|
135
|
+
let resolvePromise: () => void
|
|
136
|
+
// Mock resolveQuery to return our fake promise
|
|
137
|
+
vi.mocked(resolveQuery).mockReturnValue(
|
|
138
|
+
new Promise<void>((resolve) => {
|
|
139
|
+
resolvePromise = () => {
|
|
140
|
+
ref.current = 'data2'
|
|
141
|
+
storeChanged$.next()
|
|
142
|
+
resolve()
|
|
143
|
+
}
|
|
144
|
+
}),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
function WrapperComponent() {
|
|
148
|
+
const [query, setQuery] = useState('query1')
|
|
149
|
+
const {data, isPending} = useQuery<string>(query)
|
|
150
|
+
return (
|
|
151
|
+
<div>
|
|
152
|
+
<div data-testid="output">
|
|
153
|
+
{data} - {isPending ? 'pending' : 'not pending'}
|
|
154
|
+
</div>
|
|
155
|
+
<button data-testid="button" onClick={() => setQuery('query2')}>
|
|
156
|
+
Change Query
|
|
157
|
+
</button>
|
|
158
|
+
</div>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
render(<WrapperComponent />)
|
|
163
|
+
|
|
164
|
+
// Initially, should show data1 and not pending
|
|
165
|
+
expect(screen.getByTestId('output').textContent).toContain('data1')
|
|
166
|
+
expect(screen.getByTestId('output').textContent).toContain('not pending')
|
|
167
|
+
|
|
168
|
+
// Trigger query change to "query2"
|
|
169
|
+
act(() => {
|
|
170
|
+
screen.getByTestId('button').click()
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
// Immediately after clicking, deferredQueryKey is still for query1,
|
|
174
|
+
// so the hook returns data from the previous query ('data1') but isPending should now be true.
|
|
175
|
+
expect(screen.getByTestId('output').textContent).toContain('data1')
|
|
176
|
+
expect(screen.getByTestId('output').textContent).toContain('pending')
|
|
177
|
+
|
|
178
|
+
// Simulate the completion of the transition.
|
|
179
|
+
await act(async () => {
|
|
180
|
+
// Update the global variable so that getCurrent now returns data2 for the new query.
|
|
181
|
+
resolvePromise()
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
// Now, the component should render with the new deferred query and display new data.
|
|
185
|
+
expect(screen.getByTestId('output').textContent).toContain('data2')
|
|
186
|
+
expect(screen.getByTestId('output').textContent).toContain('not pending')
|
|
187
|
+
})
|
|
188
|
+
})
|