@sanity/sdk-react 2.19.0 → 2.20.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/dist/index.d.ts +2305 -2726
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4163 -2008
- package/dist/index.js.map +1 -1
- package/package.json +16 -16
- package/src/_exports/sdk-react.ts +3 -0
- package/src/context/ResourceProvider.test.tsx +21 -0
- package/src/context/ResourceProvider.tsx +4 -1
- package/src/hooks/comments/useCommentActions.test.tsx +150 -0
- package/src/hooks/comments/useCommentActions.ts +109 -0
- package/src/hooks/comments/useCommentList.ts +79 -0
- package/src/hooks/comments/useCommentThreads.test.tsx +107 -0
- package/src/hooks/comments/useCommentThreads.ts +73 -0
- package/src/hooks/comments/useComments.test.tsx +242 -0
- package/src/hooks/comments/useComments.ts +59 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Comment,
|
|
3
|
+
type CommentsOptions,
|
|
4
|
+
getCommentsState,
|
|
5
|
+
resolveComments,
|
|
6
|
+
type StateSource,
|
|
7
|
+
} from '@sanity/sdk'
|
|
8
|
+
import {act, render, screen} from '@testing-library/react'
|
|
9
|
+
import {Suspense} from 'react'
|
|
10
|
+
import {type Observable, Subject} from 'rxjs'
|
|
11
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest'
|
|
12
|
+
|
|
13
|
+
import {ResourceProvider} from '../../context/ResourceProvider'
|
|
14
|
+
import {ResourcesContext} from '../../context/ResourcesContext'
|
|
15
|
+
import {useComments} from './useComments'
|
|
16
|
+
|
|
17
|
+
vi.mock('@sanity/sdk', async (importOriginal) => {
|
|
18
|
+
const original = await importOriginal<typeof import('@sanity/sdk')>()
|
|
19
|
+
return {...original, getCommentsState: vi.fn(), resolveComments: vi.fn()}
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const HANDLE = {documentId: 'doc-1', documentType: 'author'}
|
|
23
|
+
|
|
24
|
+
function comment(id: string): Comment {
|
|
25
|
+
return {
|
|
26
|
+
id,
|
|
27
|
+
createdAt: '2026-01-01T00:00:00Z',
|
|
28
|
+
authorId: 'user-1',
|
|
29
|
+
message: null,
|
|
30
|
+
threadId: 'thread-1',
|
|
31
|
+
status: 'open',
|
|
32
|
+
documentId: 'doc-1',
|
|
33
|
+
documentType: 'author',
|
|
34
|
+
fieldPath: '',
|
|
35
|
+
reactions: [],
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Stands in for the store, one source per option set so a test can hold one
|
|
41
|
+
* document loaded and another not.
|
|
42
|
+
*
|
|
43
|
+
* `getCurrent` must hand back the same array every call for a given option set.
|
|
44
|
+
* Returning a fresh one sends `useSyncExternalStore` into a render loop, which
|
|
45
|
+
* is why the store memoises its selectors.
|
|
46
|
+
*/
|
|
47
|
+
function mockSource(
|
|
48
|
+
getCurrent: (options: CommentsOptions) => Comment[] | undefined,
|
|
49
|
+
changed$?: Subject<void>,
|
|
50
|
+
) {
|
|
51
|
+
vi.mocked(getCommentsState).mockImplementation(
|
|
52
|
+
(_instance, options) =>
|
|
53
|
+
({
|
|
54
|
+
getCurrent: () => getCurrent(options),
|
|
55
|
+
subscribe: vi.fn((cb?: () => void) => {
|
|
56
|
+
const subscription = changed$?.subscribe(() => cb?.())
|
|
57
|
+
return () => subscription?.unsubscribe()
|
|
58
|
+
}),
|
|
59
|
+
get observable(): Observable<Comment[] | undefined> {
|
|
60
|
+
throw new Error('Not implemented')
|
|
61
|
+
},
|
|
62
|
+
}) as StateSource<Comment[] | undefined>,
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Hoisted so the context value stays identical across renders. */
|
|
67
|
+
const RELEASE_PERSPECTIVE = {releaseName: 'summer'}
|
|
68
|
+
|
|
69
|
+
function Wrapper({children}: {children: React.ReactNode}) {
|
|
70
|
+
return (
|
|
71
|
+
<ResourceProvider projectId="p" dataset="d" fallback={<p>Loading…</p>}>
|
|
72
|
+
<ResourcesContext.Provider value={{other: {projectId: 'p2', dataset: 'd2'}}}>
|
|
73
|
+
<Suspense fallback={<p data-testid="suspended">Suspended</p>}>{children}</Suspense>
|
|
74
|
+
</ResourcesContext.Provider>
|
|
75
|
+
</ResourceProvider>
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function PerspectiveWrapper({children}: {children: React.ReactNode}) {
|
|
80
|
+
return (
|
|
81
|
+
<ResourceProvider
|
|
82
|
+
projectId="p"
|
|
83
|
+
dataset="d"
|
|
84
|
+
perspective={RELEASE_PERSPECTIVE}
|
|
85
|
+
fallback={<p>Loading…</p>}
|
|
86
|
+
>
|
|
87
|
+
<Suspense fallback={<p data-testid="suspended">Suspended</p>}>{children}</Suspense>
|
|
88
|
+
</ResourceProvider>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
describe('useComments', () => {
|
|
93
|
+
beforeEach(() => {
|
|
94
|
+
vi.resetAllMocks()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('renders the comments once they are loaded', () => {
|
|
98
|
+
const loaded = [comment('a'), comment('b')]
|
|
99
|
+
mockSource(() => loaded)
|
|
100
|
+
|
|
101
|
+
function TestComponent() {
|
|
102
|
+
const {comments, isPending} = useComments(HANDLE)
|
|
103
|
+
return <div data-testid="out">{`${comments.length} ${isPending ? 'pending' : 'idle'}`}</div>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
render(<TestComponent />, {wrapper: Wrapper})
|
|
107
|
+
|
|
108
|
+
expect(screen.getByTestId('out').textContent).toBe('2 idle')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('suspends until the first snapshot arrives', async () => {
|
|
112
|
+
const loaded = [comment('a')]
|
|
113
|
+
const ref: {current: Comment[] | undefined} = {current: undefined}
|
|
114
|
+
const changed$ = new Subject<void>()
|
|
115
|
+
mockSource(() => ref.current, changed$)
|
|
116
|
+
|
|
117
|
+
let settle: () => void = () => {}
|
|
118
|
+
vi.mocked(resolveComments).mockReturnValue(
|
|
119
|
+
new Promise<Comment[]>((resolve) => {
|
|
120
|
+
settle = () => resolve(loaded)
|
|
121
|
+
}),
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
function TestComponent() {
|
|
125
|
+
const {comments} = useComments(HANDLE)
|
|
126
|
+
return <div data-testid="out">{comments.length}</div>
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
render(<TestComponent />, {wrapper: Wrapper})
|
|
130
|
+
expect(screen.getByTestId('suspended')).toBeInTheDocument()
|
|
131
|
+
|
|
132
|
+
await act(async () => {
|
|
133
|
+
ref.current = loaded
|
|
134
|
+
settle()
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
expect(screen.getByTestId('out').textContent).toBe('1')
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('passes the field path and status through to the store', () => {
|
|
141
|
+
const loaded: Comment[] = []
|
|
142
|
+
mockSource(() => loaded)
|
|
143
|
+
|
|
144
|
+
function TestComponent() {
|
|
145
|
+
useComments({...HANDLE, fieldPath: ['body', {_key: 'intro'}], status: 'resolved'})
|
|
146
|
+
return null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
render(<TestComponent />, {wrapper: Wrapper})
|
|
150
|
+
|
|
151
|
+
expect(getCommentsState).toHaveBeenCalledWith(
|
|
152
|
+
expect.anything(),
|
|
153
|
+
expect.objectContaining({
|
|
154
|
+
documentId: 'doc-1',
|
|
155
|
+
fieldPath: 'body[_key=="intro"]',
|
|
156
|
+
status: 'resolved',
|
|
157
|
+
resource: {projectId: 'p', dataset: 'd'},
|
|
158
|
+
}),
|
|
159
|
+
)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('resolves a named resource from context', () => {
|
|
163
|
+
const loaded: Comment[] = []
|
|
164
|
+
mockSource(() => loaded)
|
|
165
|
+
|
|
166
|
+
function TestComponent() {
|
|
167
|
+
useComments({...HANDLE, resourceName: 'other'})
|
|
168
|
+
return null
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
render(<TestComponent />, {wrapper: Wrapper})
|
|
172
|
+
|
|
173
|
+
expect(getCommentsState).toHaveBeenCalledWith(
|
|
174
|
+
expect.anything(),
|
|
175
|
+
expect.objectContaining({resource: {projectId: 'p2', dataset: 'd2'}}),
|
|
176
|
+
)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('fills in the perspective from context', () => {
|
|
180
|
+
// Core turns a release perspective into `target.documentVersionId` and into
|
|
181
|
+
// the key the list is stored under, so dropping it here would read and
|
|
182
|
+
// write the wrong release with nothing to show that anything went wrong.
|
|
183
|
+
const loaded: Comment[] = []
|
|
184
|
+
mockSource(() => loaded)
|
|
185
|
+
|
|
186
|
+
function TestComponent() {
|
|
187
|
+
useComments(HANDLE)
|
|
188
|
+
return null
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
render(<TestComponent />, {wrapper: PerspectiveWrapper})
|
|
192
|
+
|
|
193
|
+
expect(getCommentsState).toHaveBeenCalledWith(
|
|
194
|
+
expect.anything(),
|
|
195
|
+
expect.objectContaining({perspective: RELEASE_PERSPECTIVE}),
|
|
196
|
+
)
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
it('keeps the previous list on screen while a different document loads', async () => {
|
|
200
|
+
const first = [comment('a')]
|
|
201
|
+
const second = [comment('b'), comment('c')]
|
|
202
|
+
// Only `doc-1` is loaded, so switching to `doc-2` has to suspend.
|
|
203
|
+
const byDocument: Record<string, Comment[] | undefined> = {'doc-1': first}
|
|
204
|
+
mockSource((options) => byDocument[options.documentId])
|
|
205
|
+
|
|
206
|
+
let settle: () => void = () => {}
|
|
207
|
+
vi.mocked(resolveComments).mockReturnValue(
|
|
208
|
+
new Promise<Comment[]>((resolve) => {
|
|
209
|
+
settle = () => resolve(second)
|
|
210
|
+
}),
|
|
211
|
+
)
|
|
212
|
+
|
|
213
|
+
function TestComponent({documentId}: {documentId: string}) {
|
|
214
|
+
const {comments, isPending} = useComments({...HANDLE, documentId})
|
|
215
|
+
return <div data-testid="out">{`${comments.length} ${isPending ? 'pending' : 'idle'}`}</div>
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const {rerender} = render(<TestComponent documentId="doc-1" />, {wrapper: Wrapper})
|
|
219
|
+
expect(screen.getByTestId('out').textContent).toBe('1 idle')
|
|
220
|
+
|
|
221
|
+
await act(async () => {
|
|
222
|
+
rerender(<TestComponent documentId="doc-2" />)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
// The swap happens inside a transition, so the render that suspends is
|
|
226
|
+
// thrown away rather than falling back: `doc-1` stays on screen and
|
|
227
|
+
// `isPending` is what reports the switch.
|
|
228
|
+
expect(screen.queryByTestId('suspended')).not.toBeInTheDocument()
|
|
229
|
+
expect(screen.getByTestId('out').textContent).toBe('1 pending')
|
|
230
|
+
expect(getCommentsState).toHaveBeenCalledWith(
|
|
231
|
+
expect.anything(),
|
|
232
|
+
expect.objectContaining({documentId: 'doc-2'}),
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
await act(async () => {
|
|
236
|
+
byDocument['doc-2'] = second
|
|
237
|
+
settle()
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
expect(screen.getByTestId('out').textContent).toBe('2 idle')
|
|
241
|
+
})
|
|
242
|
+
})
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {type Comment, type CommentsOptions, getCommentsState, resolveComments} from '@sanity/sdk'
|
|
2
|
+
import {useMemo} from 'react'
|
|
3
|
+
|
|
4
|
+
import {type WithResourceNameSupport} from '../helpers/useNormalizedResourceOptions'
|
|
5
|
+
import {type CommentListSource, useCommentList} from './useCommentList'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
* @category Types
|
|
10
|
+
*/
|
|
11
|
+
export interface UseCommentsResult {
|
|
12
|
+
/** Every matching comment, newest first, replies included. */
|
|
13
|
+
comments: Comment[]
|
|
14
|
+
/** True while switching to a different document or filter. */
|
|
15
|
+
isPending: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const SOURCE: CommentListSource<Comment[]> = {
|
|
19
|
+
getState: getCommentsState,
|
|
20
|
+
resolve: resolveComments,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Reads a document's comments and keeps them up to date.
|
|
25
|
+
*
|
|
26
|
+
* Comments are shared with the Studio: they live in the project's comments
|
|
27
|
+
* dataset, so a thread started here shows up there and the other way round. The
|
|
28
|
+
* list is flat, replies included; reach for {@link useCommentThreads} to read it
|
|
29
|
+
* grouped.
|
|
30
|
+
*
|
|
31
|
+
* Suspends until the comments have loaded. Switching document or filter is a
|
|
32
|
+
* transition, so the previous list stays on screen and `isPending` goes true
|
|
33
|
+
* rather than the component suspending again.
|
|
34
|
+
*
|
|
35
|
+
* @category Comments
|
|
36
|
+
* @function
|
|
37
|
+
* @param options - The document to read, optionally narrowed by `fieldPath` or `status`
|
|
38
|
+
* @returns The matching comments, and whether a switch is in flight
|
|
39
|
+
*
|
|
40
|
+
* @example Count the open threads on a field
|
|
41
|
+
* ```tsx
|
|
42
|
+
* function TitleCommentCount({documentId}: {documentId: string}) {
|
|
43
|
+
* const {comments} = useComments({
|
|
44
|
+
* documentId,
|
|
45
|
+
* documentType: 'article',
|
|
46
|
+
* fieldPath: 'title',
|
|
47
|
+
* status: 'open',
|
|
48
|
+
* })
|
|
49
|
+
*
|
|
50
|
+
* return <span>{comments.length}</span>
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
export function useComments(options: WithResourceNameSupport<CommentsOptions>): UseCommentsResult {
|
|
57
|
+
const {value, isPending} = useCommentList('useComments', options, SOURCE)
|
|
58
|
+
return useMemo(() => ({comments: value, isPending}), [isPending, value])
|
|
59
|
+
}
|