@sanity/sdk-react 0.0.0-alpha.9 → 0.0.0-rc.1

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.
Files changed (94) hide show
  1. package/README.md +33 -126
  2. package/dist/index.d.ts +4742 -2
  3. package/dist/index.js +1054 -2
  4. package/dist/index.js.map +1 -1
  5. package/package.json +19 -43
  6. package/src/_exports/index.ts +66 -10
  7. package/src/components/Login/LoginLinks.test.tsx +90 -0
  8. package/src/components/Login/LoginLinks.tsx +58 -0
  9. package/src/components/SDKProvider.test.tsx +79 -0
  10. package/src/components/SDKProvider.tsx +42 -0
  11. package/src/components/SanityApp.test.tsx +104 -2
  12. package/src/components/SanityApp.tsx +54 -17
  13. package/src/components/auth/AuthBoundary.test.tsx +2 -2
  14. package/src/components/auth/AuthBoundary.tsx +13 -3
  15. package/src/components/auth/Login.test.tsx +1 -1
  16. package/src/components/auth/Login.tsx +11 -26
  17. package/src/components/auth/LoginCallback.tsx +4 -7
  18. package/src/components/auth/LoginError.tsx +12 -8
  19. package/src/components/auth/LoginFooter.tsx +13 -20
  20. package/src/components/auth/LoginLayout.tsx +8 -9
  21. package/src/components/auth/authTestHelpers.tsx +1 -8
  22. package/src/components/utils.ts +22 -0
  23. package/src/context/SanityInstanceContext.ts +4 -0
  24. package/src/context/SanityProvider.test.tsx +1 -1
  25. package/src/context/SanityProvider.tsx +10 -8
  26. package/src/hooks/_synchronous-groq-js.mjs +4 -0
  27. package/src/hooks/auth/useAuthState.tsx +0 -2
  28. package/src/hooks/auth/useCurrentUser.tsx +26 -20
  29. package/src/hooks/auth/useDashboardOrganizationId.test.tsx +42 -0
  30. package/src/hooks/auth/useDashboardOrganizationId.tsx +29 -0
  31. package/src/hooks/client/useClient.ts +8 -30
  32. package/src/hooks/comlink/useFrameConnection.test.tsx +55 -10
  33. package/src/hooks/comlink/useFrameConnection.ts +39 -43
  34. package/src/hooks/comlink/useManageFavorite.test.ts +106 -0
  35. package/src/hooks/comlink/useManageFavorite.ts +101 -0
  36. package/src/hooks/comlink/useRecordDocumentHistoryEvent.test.ts +77 -0
  37. package/src/hooks/comlink/useRecordDocumentHistoryEvent.ts +79 -0
  38. package/src/hooks/comlink/useWindowConnection.test.ts +53 -12
  39. package/src/hooks/comlink/useWindowConnection.ts +69 -29
  40. package/src/hooks/context/useSanityInstance.test.tsx +1 -1
  41. package/src/hooks/context/useSanityInstance.ts +21 -5
  42. package/src/hooks/dashboard/useNavigateToStudioDocument.ts +97 -0
  43. package/src/hooks/dashboard/useStudioWorkspacesByResourceId.test.tsx +274 -0
  44. package/src/hooks/dashboard/useStudioWorkspacesByResourceId.ts +91 -0
  45. package/src/hooks/datasets/useDatasets.ts +37 -0
  46. package/src/hooks/document/useApplyActions.test.ts +5 -4
  47. package/src/hooks/document/useApplyActions.ts +55 -5
  48. package/src/hooks/document/useDocument.test.ts +2 -2
  49. package/src/hooks/document/useDocument.ts +90 -21
  50. package/src/hooks/document/useDocumentEvent.test.ts +13 -3
  51. package/src/hooks/document/useDocumentEvent.ts +36 -4
  52. package/src/hooks/document/useDocumentSyncStatus.test.ts +1 -1
  53. package/src/hooks/document/useDocumentSyncStatus.ts +26 -2
  54. package/src/hooks/document/useEditDocument.test.ts +55 -10
  55. package/src/hooks/document/useEditDocument.ts +159 -31
  56. package/src/hooks/document/usePermissions.ts +82 -0
  57. package/src/hooks/helpers/createCallbackHook.tsx +3 -2
  58. package/src/hooks/helpers/createStateSourceHook.test.tsx +66 -0
  59. package/src/hooks/helpers/createStateSourceHook.tsx +29 -10
  60. package/src/hooks/infiniteList/useInfiniteList.test.tsx +152 -0
  61. package/src/hooks/infiniteList/useInfiniteList.ts +174 -0
  62. package/src/hooks/paginatedList/usePaginatedList.test.tsx +259 -0
  63. package/src/hooks/paginatedList/usePaginatedList.ts +290 -0
  64. package/src/hooks/preview/usePreview.test.tsx +6 -6
  65. package/src/hooks/preview/usePreview.tsx +12 -9
  66. package/src/hooks/projection/useProjection.test.tsx +218 -0
  67. package/src/hooks/projection/useProjection.ts +147 -0
  68. package/src/hooks/projects/useProject.ts +45 -0
  69. package/src/hooks/projects/useProjects.ts +41 -0
  70. package/src/hooks/query/useQuery.test.tsx +188 -0
  71. package/src/hooks/query/useQuery.ts +103 -0
  72. package/src/hooks/users/useUsers.test.ts +163 -0
  73. package/src/hooks/users/useUsers.ts +107 -0
  74. package/src/utils/getEnv.ts +21 -0
  75. package/src/version.ts +8 -0
  76. package/dist/_chunks-es/context.js +0 -8
  77. package/dist/_chunks-es/context.js.map +0 -1
  78. package/dist/_chunks-es/useLogOut.js +0 -45
  79. package/dist/_chunks-es/useLogOut.js.map +0 -1
  80. package/dist/components.d.ts +0 -111
  81. package/dist/components.js +0 -153
  82. package/dist/components.js.map +0 -1
  83. package/dist/context.d.ts +0 -45
  84. package/dist/context.js +0 -5
  85. package/dist/context.js.map +0 -1
  86. package/dist/hooks.d.ts +0 -3532
  87. package/dist/hooks.js +0 -218
  88. package/dist/hooks.js.map +0 -1
  89. package/src/_exports/components.ts +0 -2
  90. package/src/_exports/context.ts +0 -2
  91. package/src/_exports/hooks.ts +0 -32
  92. package/src/hooks/client/useClient.test.tsx +0 -130
  93. package/src/hooks/documentCollection/useDocuments.test.ts +0 -130
  94. package/src/hooks/documentCollection/useDocuments.ts +0 -135
@@ -1,135 +0,0 @@
1
- import {createDocumentListStore, type DocumentHandle, type DocumentListOptions} from '@sanity/sdk'
2
- import {useCallback, useEffect, useState, useSyncExternalStore} from 'react'
3
-
4
- import {useSanityInstance} from '../context/useSanityInstance'
5
-
6
- /**
7
- * @public
8
- */
9
- export interface DocumentCollection {
10
- /** Retrieve more documents matching the provided options */
11
- loadMore: () => void
12
- /** The retrieved document handles of the documents matching the provided options */
13
- results: DocumentHandle[]
14
- /** Whether a retrieval of documents is in flight */
15
- isPending: boolean
16
- /** Whether more documents exist that match the provided options than have been retrieved */
17
- hasMore: boolean
18
- /** The total number of documents in the collection */
19
- count: number
20
- }
21
-
22
- type DocumentListStore = ReturnType<typeof createDocumentListStore>
23
- type DocumentListState = ReturnType<DocumentListStore['getState']>['getCurrent']
24
- const STABLE_EMPTY = {
25
- results: [],
26
- isPending: false,
27
- hasMore: false,
28
- count: 0,
29
- }
30
-
31
- /**
32
- * @public
33
- *
34
- * The `useDocuments` hook retrieves and provides access to a live collection of documents, optionally filtered, sorted, and matched to a given Content Lake perspective.
35
- * Because the returned document collection is live, the results will update in real time until the component invoking the hook is unmounted.
36
- *
37
- * @param options - Options for narrowing and sorting the document collection
38
- * @returns The collection of documents matching the provided options (if any), as well as properties describing the collection and a function to load more.
39
- *
40
- * @example Retrieving all documents of type 'movie'
41
- * ```
42
- * const { results, isPending } = useDocuments({ filter: '_type == "movie"' })
43
- *
44
- * return (
45
- * <div>
46
- * <h1>Movies</h1>
47
- * {results && (
48
- * <ul>
49
- * {results.map(movie => (<li key={movie._id}>…</li>))}
50
- * </ul>
51
- * )}
52
- * {isPending && <div>Loading movies…</div>}
53
- * </div>
54
- * )
55
- * ```
56
- *
57
- * @example Retrieving all movies released since 1980, sorted by director’s last name
58
- * ```
59
- * const { results } = useDocuments({
60
- * filter: '_type == "movie" && releaseDate >= "1980-01-01"',
61
- * sort: [
62
- * {
63
- * // Expand the `director` reference field with the dereferencing operator `->`
64
- * field: 'director->lastName',
65
- * sort: 'asc',
66
- * },
67
- * ],
68
- * })
69
- *
70
- * return (
71
- * <div>
72
- * <h1>Movies released since 1980</h1>
73
- * {results && (
74
- * <ol>
75
- * {results.map(movie => (<li key={movie._id}>…</li>))}
76
- * </ol>
77
- * )}
78
- * </div>
79
- * )
80
- * ```
81
- */
82
- export function useDocuments(options: DocumentListOptions = {}): DocumentCollection {
83
- const instance = useSanityInstance()
84
-
85
- // NOTE: useState is used because it guaranteed to return a stable reference
86
- // across renders
87
- const [ref] = useState<{
88
- storeInstance: DocumentListStore | null
89
- getCurrent: DocumentListState
90
- initialOptions: DocumentListOptions
91
- }>(() => ({
92
- storeInstance: null,
93
- getCurrent: () => STABLE_EMPTY,
94
- initialOptions: options,
95
- }))
96
-
97
- // serialize options to ensure it only calls `setOptions` when the values
98
- // themselves changes (in cases where devs put config inline)
99
- const serializedOptions = JSON.stringify(options)
100
- useEffect(() => {
101
- ref.storeInstance?.setOptions(JSON.parse(serializedOptions))
102
- }, [ref, serializedOptions])
103
-
104
- const subscribe = useCallback(
105
- (onStoreChanged: () => void) => {
106
- // to match the lifecycle of `useSyncExternalState`, we create the store
107
- // instance after subscribe and mutate the ref to connect everything
108
- ref.storeInstance = createDocumentListStore(instance)
109
- ref.storeInstance.setOptions(ref.initialOptions)
110
- const state = ref.storeInstance.getState()
111
- ref.getCurrent = state.getCurrent
112
- const unsubscribe = state.subscribe(onStoreChanged)
113
-
114
- return () => {
115
- // unsubscribe to clean up the state subscriptions
116
- unsubscribe()
117
- // dispose of the instance
118
- ref.storeInstance?.dispose()
119
- }
120
- },
121
- [instance, ref],
122
- )
123
-
124
- const getSnapshot = useCallback(() => {
125
- return ref.getCurrent()
126
- }, [ref])
127
-
128
- const state = useSyncExternalStore(subscribe, getSnapshot)
129
-
130
- const loadMore = useCallback(() => {
131
- ref.storeInstance?.loadMore()
132
- }, [ref])
133
-
134
- return {loadMore, ...state}
135
- }