@sanity/sdk-react 2.14.1 → 2.16.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 +40 -30
- package/dist/index.d.ts +512 -335
- package/dist/index.js +540 -332
- package/dist/index.js.map +1 -1
- package/package.json +37 -39
- package/src/_exports/sdk-react.ts +1 -0
- package/src/components/SDKProvider.test.tsx +135 -22
- package/src/components/SDKProvider.tsx +54 -17
- package/src/components/SanityApp.tsx +29 -0
- package/src/components/auth/AuthBoundary.recovery.test.tsx +86 -0
- package/src/components/auth/AuthBoundary.tsx +11 -1
- package/src/context/OrganizationResourcesProvider.test.tsx +189 -0
- package/src/context/OrganizationResourcesProvider.tsx +111 -0
- package/src/context/ProjectContext.ts +15 -0
- package/src/context/ResourceProvider.test.tsx +57 -1
- package/src/context/ResourceProvider.tsx +30 -9
- package/src/hooks/datasets/useDatasets.test.tsx +116 -0
- package/src/hooks/datasets/useDatasets.ts +20 -8
- package/src/hooks/document/useApplyDocumentActions.ts +1 -1
- package/src/hooks/document/useCreateDocument.test.tsx +83 -0
- package/src/hooks/document/useCreateDocument.ts +117 -0
- package/src/hooks/document/useDocument.ts +2 -2
- package/src/hooks/helpers/useResolvedProjectId.test.tsx +59 -0
- package/src/hooks/helpers/useResolvedProjectId.ts +35 -0
- package/src/hooks/projects/useProject.test.tsx +114 -0
- package/src/hooks/projects/useProject.ts +17 -7
- package/src/hooks/users/useUsers.test.tsx +101 -2
- package/src/hooks/users/useUsers.ts +35 -3
- package/src/utils/resolveOrgResources.test.ts +111 -0
- package/src/utils/resolveOrgResources.ts +69 -0
- package/src/hooks/datasets/useDatasets.test.ts +0 -80
- package/src/hooks/projects/useProject.test.ts +0 -80
package/README.md
CHANGED
|
@@ -108,6 +108,10 @@ const {data} = useQuery({
|
|
|
108
108
|
#### Document Manipulation
|
|
109
109
|
|
|
110
110
|
```tsx
|
|
111
|
+
// Create a new document and get back its handle
|
|
112
|
+
const createArticle = useCreateDocument({documentType: 'article'})
|
|
113
|
+
const newHandle = await createArticle({title: 'Untitled', status: 'draft'})
|
|
114
|
+
|
|
111
115
|
// Edit field (emits optimistic updates to useEditDocument listeners, creates a draft automatically)
|
|
112
116
|
const editTitle = useEditDocument({...handle, path: 'title'})
|
|
113
117
|
editTitle('New Title') // fires on every keystroke, debounced internally
|
|
@@ -133,13 +137,6 @@ await apply(publishDocument(handle))
|
|
|
133
137
|
|
|
134
138
|
// Batch actions
|
|
135
139
|
await apply([publishDocument(handle1), publishDocument(handle2), deleteDocument(handle3)])
|
|
136
|
-
|
|
137
|
-
// Create new document with an optional initial content
|
|
138
|
-
const newHandle = createDocumentHandle({
|
|
139
|
-
documentId: crypto.randomUUID(),
|
|
140
|
-
documentType: 'article',
|
|
141
|
-
})
|
|
142
|
-
await apply(createDocument(newHandle, {title: 'Untitled', status: 'draft'}))
|
|
143
140
|
```
|
|
144
141
|
|
|
145
142
|
#### Events & Permissions
|
|
@@ -149,7 +146,7 @@ await apply(createDocument(newHandle, {title: 'Untitled', status: 'draft'}))
|
|
|
149
146
|
useDocumentEvent({
|
|
150
147
|
...handle,
|
|
151
148
|
onEvent: (event) => {
|
|
152
|
-
// event.type: '
|
|
149
|
+
// event.type: 'edited' | 'published' | 'deleted' | 'created' | 'unpublished' | 'discarded' | ...
|
|
153
150
|
console.log(event.type, event.documentId)
|
|
154
151
|
},
|
|
155
152
|
})
|
|
@@ -183,41 +180,33 @@ The `useApplyDocumentActions` hook is used to perform document lifecycle operati
|
|
|
183
180
|
|
|
184
181
|
#### Creating Documents
|
|
185
182
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
1. Generate your own document ID (using `crypto.randomUUID()`)
|
|
189
|
-
2. Create a document handle with `createDocumentHandle`
|
|
190
|
-
3. Apply the `createDocument` action using the document handle, along with optional initial content
|
|
183
|
+
`useCreateDocument` handles the common case: it generates the document ID for you and returns the new document's handle, ready to pass to `useDocument`, `useEditDocument`, or your router.
|
|
191
184
|
|
|
192
185
|
```tsx
|
|
193
|
-
import {
|
|
186
|
+
import {useCreateDocument} from '@sanity/sdk-react'
|
|
194
187
|
|
|
195
188
|
function CreateArticleButton() {
|
|
196
|
-
const
|
|
189
|
+
const createArticle = useCreateDocument({documentType: 'article'})
|
|
197
190
|
|
|
198
|
-
const handleCreateArticle = () => {
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
191
|
+
const handleCreateArticle = async () => {
|
|
192
|
+
const handle = await createArticle({
|
|
193
|
+
title: 'New Article',
|
|
194
|
+
status: 'draft',
|
|
195
|
+
author: {_type: 'reference', _ref: 'author-123'},
|
|
203
196
|
})
|
|
204
197
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
title: 'New Article',
|
|
208
|
-
status: 'draft',
|
|
209
|
-
author: {_type: 'reference', _ref: 'author-123'},
|
|
210
|
-
}),
|
|
211
|
-
)
|
|
212
|
-
|
|
213
|
-
// Navigate to the new document
|
|
214
|
-
navigate(`/articles/${newId}`)
|
|
198
|
+
// Navigate to the new document using the returned handle
|
|
199
|
+
navigate(`/articles/${handle.documentId}`)
|
|
215
200
|
}
|
|
216
201
|
|
|
217
202
|
return <button onClick={handleCreateArticle}>Create Article</button>
|
|
218
203
|
}
|
|
219
204
|
```
|
|
220
205
|
|
|
206
|
+
To use a specific ID instead of a generated one, pass it on the handle (`useCreateDocument({documentType: 'article', documentId})`) or per call (`createArticle(initialValue, {documentId})`).
|
|
207
|
+
|
|
208
|
+
For atomic create-and-publish, or to create several documents in a single transaction, drop down to `useApplyDocumentActions` with the `createDocument` and `publishDocument` action creators (see [Batch Operations](#batch-operations) below).
|
|
209
|
+
|
|
221
210
|
#### Publishing Documents
|
|
222
211
|
|
|
223
212
|
```tsx
|
|
@@ -308,6 +297,27 @@ function ArticleList() {
|
|
|
308
297
|
|
|
309
298
|
---
|
|
310
299
|
|
|
300
|
+
### Viewport-Based Lazy Loading
|
|
301
|
+
|
|
302
|
+
Pass a `ref` to `useDocumentProjection` to fetch only when the element enters the viewport. Wrap the component in Suspense like any other data hook:
|
|
303
|
+
|
|
304
|
+
```tsx
|
|
305
|
+
import {useRef} from 'react'
|
|
306
|
+
|
|
307
|
+
function LazyArticleCard({handle}: {handle: DocumentHandle}) {
|
|
308
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
309
|
+
const {data} = useDocumentProjection({
|
|
310
|
+
...handle,
|
|
311
|
+
ref, // Only fetches when element is visible
|
|
312
|
+
projection: '{ title }',
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
return <div ref={ref}>{data?.title}</div>
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
---
|
|
320
|
+
|
|
311
321
|
### Draft/Published Model
|
|
312
322
|
|
|
313
323
|
Sanity has two document states:
|