@sanity/sdk-react 0.0.0-alpha.15 → 0.0.0-alpha.17

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 (49) hide show
  1. package/dist/_chunks-es/context.js +9 -1
  2. package/dist/_chunks-es/context.js.map +1 -1
  3. package/dist/_chunks-es/useLogOut.js +28 -15
  4. package/dist/_chunks-es/useLogOut.js.map +1 -1
  5. package/dist/components.d.ts +37 -9
  6. package/dist/components.js +171 -96
  7. package/dist/components.js.map +1 -1
  8. package/dist/hooks.d.ts +417 -145
  9. package/dist/hooks.js +470 -226
  10. package/dist/hooks.js.map +1 -1
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +10 -2
  13. package/dist/index.js.map +1 -1
  14. package/package.json +7 -4
  15. package/src/_exports/hooks.ts +15 -6
  16. package/src/_exports/index.ts +1 -10
  17. package/src/components/SDKProvider.test.tsx +3 -3
  18. package/src/components/SDKProvider.tsx +18 -7
  19. package/src/components/SanityApp.test.tsx +5 -5
  20. package/src/components/SanityApp.tsx +38 -24
  21. package/src/hooks/_synchronous-groq-js.mjs +4 -0
  22. package/src/hooks/client/useClient.ts +4 -1
  23. package/src/hooks/context/useSanityInstance.ts +1 -1
  24. package/src/hooks/datasets/useDatasets.ts +26 -1
  25. package/src/hooks/document/useApplyActions.test.ts +5 -4
  26. package/src/hooks/document/useApplyActions.ts +10 -4
  27. package/src/hooks/document/useDocument.ts +3 -3
  28. package/src/hooks/document/useDocumentEvent.ts +2 -0
  29. package/src/hooks/document/useDocumentSyncStatus.ts +2 -1
  30. package/src/hooks/document/useEditDocument.ts +31 -33
  31. package/src/hooks/helpers/createCallbackHook.tsx +3 -2
  32. package/src/hooks/helpers/createStateSourceHook.tsx +6 -10
  33. package/src/hooks/infiniteList/useInfiniteList.test.tsx +152 -0
  34. package/src/hooks/infiniteList/useInfiniteList.ts +174 -0
  35. package/src/hooks/paginatedList/usePaginatedList.test.tsx +259 -0
  36. package/src/hooks/paginatedList/usePaginatedList.ts +290 -0
  37. package/src/hooks/projection/useProjection.ts +3 -3
  38. package/src/hooks/projects/useProject.ts +25 -1
  39. package/src/hooks/projects/useProjects.ts +33 -11
  40. package/src/hooks/query/useQuery.test.tsx +188 -0
  41. package/src/hooks/query/useQuery.ts +103 -0
  42. package/src/hooks/users/useUsers.ts +2 -2
  43. package/src/utils/getEnv.ts +21 -0
  44. package/src/version.ts +8 -0
  45. package/src/hooks/documentCollection/types.ts +0 -19
  46. package/src/hooks/documentCollection/useDocuments.test.ts +0 -130
  47. package/src/hooks/documentCollection/useDocuments.ts +0 -126
  48. package/src/hooks/documentCollection/useSearch.test.ts +0 -100
  49. package/src/hooks/documentCollection/useSearch.ts +0 -75
package/dist/hooks.js CHANGED
@@ -1,84 +1,121 @@
1
1
  import { createStateSourceHook, useSanityInstance, createCallbackHook } from "./_chunks-es/useLogOut.js";
2
2
  import { useAuthState, useHandleCallback, useLogOut, useLoginUrls } from "./_chunks-es/useLogOut.js";
3
- import { getTokenState, getCurrentUserState, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, resolveDatasets, getDatasetsState, applyActions, getResourceId, resolveDocument, getDocumentState, subscribeDocumentEvents, getDocumentSyncStatus, editDocument, getPermissionsState, createDocumentListStore, getPreviewState, resolvePreview, resolveProject, getProjectState, resolveProjects, getProjectsState, createUsersStore, getProjectionState, resolveProjection } from "@sanity/sdk";
4
- import { useState, useMemo, useEffect, useCallback, useRef, useInsertionEffect, useSyncExternalStore } from "react";
3
+ import { getTokenState, getCurrentUserState, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, getOrCreateNode, releaseNode, resolveDatasets, getDatasetsState, applyActions, getResourceId, resolveDocument, getDocumentState, subscribeDocumentEvents, getDocumentSyncStatus, editDocument, getPermissionsState, getQueryKey, parseQueryKey, getQueryState, resolveQuery, getPreviewState, resolvePreview, getProjectionState, resolveProjection, resolveProject, getProjectState, resolveProjects, getProjectsState, createUsersStore } from "@sanity/sdk";
4
+ import { c } from "react/compiler-runtime";
5
+ import { useState, useEffect, useRef, useInsertionEffect, useSyncExternalStore, useTransition, useMemo } from "react";
5
6
  import { firstValueFrom, filter, Observable, startWith, distinctUntilChanged, switchMap, EMPTY } from "rxjs";
6
- const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState), useClient = createStateSourceHook(getClientState);
7
+ const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState), useClient = createStateSourceHook({
8
+ getState: getClientState,
9
+ getResourceId: (e) => e.resourceId
10
+ });
7
11
  function useFrameConnection(options) {
8
- const { onMessage, targetOrigin, name, connectTo, heartbeat } = options, instance = useSanityInstance(), [status, setStatus] = useState("idle"), controller = useMemo(
9
- () => getOrCreateController(instance, targetOrigin),
10
- [instance, targetOrigin]
11
- ), channel = useMemo(
12
- () => getOrCreateChannel(instance, {
13
- name,
14
- connectTo,
15
- heartbeat
16
- }),
17
- [instance, name, connectTo, heartbeat]
18
- );
19
- useEffect(() => channel ? channel.onStatus((event) => {
12
+ const $ = c(27), {
13
+ onMessage,
14
+ targetOrigin,
15
+ name,
16
+ connectTo,
17
+ heartbeat
18
+ } = options, instance = useSanityInstance(), [status, setStatus] = useState("idle");
19
+ let t0, t1;
20
+ $[0] !== instance || $[1] !== targetOrigin ? (t1 = getOrCreateController(instance, targetOrigin), $[0] = instance, $[1] = targetOrigin, $[2] = t1) : t1 = $[2], t0 = t1;
21
+ const controller = t0;
22
+ let t2, t3;
23
+ $[3] !== connectTo || $[4] !== heartbeat || $[5] !== instance || $[6] !== name ? (t3 = getOrCreateChannel(instance, {
24
+ name,
25
+ connectTo,
26
+ heartbeat
27
+ }), $[3] = connectTo, $[4] = heartbeat, $[5] = instance, $[6] = name, $[7] = t3) : t3 = $[7], t2 = t3;
28
+ const channel = t2;
29
+ let t4, t5;
30
+ $[8] !== channel ? (t4 = () => channel ? channel.onStatus((event) => {
20
31
  setStatus(event.status);
21
- }) : void 0, [channel]), useEffect(() => {
22
- if (!channel || !onMessage) return;
32
+ }) : void 0, t5 = [channel], $[8] = channel, $[9] = t4, $[10] = t5) : (t4 = $[9], t5 = $[10]), useEffect(t4, t5);
33
+ let t6, t7;
34
+ $[11] !== channel || $[12] !== onMessage ? (t6 = () => {
35
+ if (!channel || !onMessage)
36
+ return;
23
37
  const unsubscribers = [];
24
- return Object.entries(onMessage).forEach(([type, handler]) => {
25
- const unsubscribe = channel.on(type, handler);
26
- unsubscribers.push(unsubscribe);
38
+ return Object.entries(onMessage).forEach((t82) => {
39
+ const [type, handler] = t82, unsubscribe_0 = channel.on(type, handler);
40
+ unsubscribers.push(unsubscribe_0);
27
41
  }), () => {
28
- unsubscribers.forEach((unsub) => unsub());
42
+ unsubscribers.forEach(_temp$5);
29
43
  };
30
- }, [channel, onMessage]);
31
- const connect = useCallback(
32
- (frameWindow) => {
33
- const removeTarget = controller?.addTarget(frameWindow);
34
- return () => {
35
- removeTarget?.();
36
- };
37
- },
38
- [controller]
39
- ), sendMessage = useCallback(
40
- (type, data) => {
41
- channel?.post(type, data);
42
- },
43
- [channel]
44
- );
45
- return useEffect(() => () => {
44
+ }, t7 = [channel, onMessage], $[11] = channel, $[12] = onMessage, $[13] = t6, $[14] = t7) : (t6 = $[13], t7 = $[14]), useEffect(t6, t7);
45
+ let t8;
46
+ $[15] !== controller ? (t8 = (frameWindow) => {
47
+ const removeTarget = controller?.addTarget(frameWindow);
48
+ return () => {
49
+ removeTarget?.();
50
+ };
51
+ }, $[15] = controller, $[16] = t8) : t8 = $[16];
52
+ const connect = t8;
53
+ let t9;
54
+ $[17] !== channel ? (t9 = (type_0, data) => {
55
+ channel?.post(type_0, data);
56
+ }, $[17] = channel, $[18] = t9) : t9 = $[18];
57
+ const sendMessage = t9;
58
+ let t10, t11;
59
+ $[19] !== instance || $[20] !== name ? (t10 = () => () => {
46
60
  releaseChannel(instance, name);
47
- }, [name, instance]), {
61
+ }, t11 = [name, instance], $[19] = instance, $[20] = name, $[21] = t10, $[22] = t11) : (t10 = $[21], t11 = $[22]), useEffect(t10, t11);
62
+ let t12;
63
+ return $[23] !== connect || $[24] !== sendMessage || $[25] !== status ? (t12 = {
48
64
  connect,
49
65
  sendMessage,
50
66
  status
51
- };
67
+ }, $[23] = connect, $[24] = sendMessage, $[25] = status, $[26] = t12) : t12 = $[26], t12;
68
+ }
69
+ function _temp$5(unsub) {
70
+ return unsub();
52
71
  }
53
72
  function useWindowConnection(options) {
54
- const { name, onMessage, connectTo } = options, instance = useSanityInstance(), [status, setStatus] = useState("idle"), node = useMemo(
55
- () => getOrCreateNode(instance, { name, connectTo }),
56
- [instance, name, connectTo]
57
- );
58
- useEffect(() => node.onStatus((newStatus) => {
73
+ const $ = c(23), {
74
+ name,
75
+ onMessage,
76
+ connectTo
77
+ } = options, instance = useSanityInstance(), [status, setStatus] = useState("idle");
78
+ let t0, t1;
79
+ $[0] !== connectTo || $[1] !== instance || $[2] !== name ? (t1 = getOrCreateNode(instance, {
80
+ name,
81
+ connectTo
82
+ }), $[0] = connectTo, $[1] = instance, $[2] = name, $[3] = t1) : t1 = $[3], t0 = t1;
83
+ const node = t0;
84
+ let t2;
85
+ $[4] !== node ? (t2 = () => node.onStatus((newStatus) => {
59
86
  setStatus(newStatus);
60
- }), [node, instance, name]), useEffect(() => {
61
- if (!onMessage) return;
87
+ }), $[4] = node, $[5] = t2) : t2 = $[5];
88
+ let t3;
89
+ $[6] !== instance || $[7] !== name || $[8] !== node ? (t3 = [node, instance, name], $[6] = instance, $[7] = name, $[8] = node, $[9] = t3) : t3 = $[9], useEffect(t2, t3);
90
+ let t4, t5;
91
+ $[10] !== node || $[11] !== onMessage ? (t4 = () => {
92
+ if (!onMessage)
93
+ return;
62
94
  const unsubscribers = [];
63
- return Object.entries(onMessage).forEach(([type, handler]) => {
64
- const unsubscribe = node.on(type, handler);
65
- unsubscribers.push(unsubscribe);
95
+ return Object.entries(onMessage).forEach((t62) => {
96
+ const [type, handler] = t62, unsubscribe_0 = node.on(type, handler);
97
+ unsubscribers.push(unsubscribe_0);
66
98
  }), () => {
67
- unsubscribers.forEach((unsub) => unsub());
99
+ unsubscribers.forEach(_temp$4);
68
100
  };
69
- }, [node, onMessage]);
70
- const sendMessage = useCallback(
71
- (type, data) => {
72
- node?.post(type, data);
73
- },
74
- [node]
75
- );
76
- return useEffect(() => () => {
101
+ }, t5 = [node, onMessage], $[10] = node, $[11] = onMessage, $[12] = t4, $[13] = t5) : (t4 = $[12], t5 = $[13]), useEffect(t4, t5);
102
+ let t6;
103
+ $[14] !== node ? (t6 = (type_0, data) => {
104
+ node?.post(type_0, data);
105
+ }, $[14] = node, $[15] = t6) : t6 = $[15];
106
+ const sendMessage = t6;
107
+ let t7, t8;
108
+ $[16] !== instance || $[17] !== name ? (t7 = () => () => {
77
109
  releaseNode(instance, name);
78
- }, [instance, name]), {
110
+ }, t8 = [instance, name], $[16] = instance, $[17] = name, $[18] = t7, $[19] = t8) : (t7 = $[18], t8 = $[19]), useEffect(t7, t8);
111
+ let t9;
112
+ return $[20] !== sendMessage || $[21] !== status ? (t9 = {
79
113
  sendMessage,
80
114
  status
81
- };
115
+ }, $[20] = sendMessage, $[21] = status, $[22] = t9) : t9 = $[22], t9;
116
+ }
117
+ function _temp$4(unsub) {
118
+ return unsub();
82
119
  }
83
120
  const useDatasets = createStateSourceHook({
84
121
  // remove `undefined` since we're suspending when that is the case
@@ -86,10 +123,10 @@ const useDatasets = createStateSourceHook({
86
123
  shouldSuspend: (instance) => getDatasetsState(instance).getCurrent() === void 0,
87
124
  suspender: resolveDatasets
88
125
  });
89
- function useApplyActions() {
90
- return _useApplyActions();
126
+ function useApplyActions(resourceId) {
127
+ return _useApplyActions(resourceId)();
91
128
  }
92
- const _useApplyActions = createCallbackHook(applyActions);
129
+ const _useApplyActions = (resourceId) => createCallbackHook(applyActions, resourceId);
93
130
  function useDocument(doc, path) {
94
131
  return _useDocument(doc, path);
95
132
  }
@@ -100,137 +137,365 @@ const _useDocument = createStateSourceHook({
100
137
  getResourceId: (doc) => getResourceId(doc.resourceId)
101
138
  });
102
139
  function useDocumentEvent(handler, doc) {
103
- const ref = useRef(handler);
104
- useInsertionEffect(() => {
140
+ const $ = c(8), ref = useRef(handler);
141
+ let t0;
142
+ $[0] !== handler ? (t0 = () => {
105
143
  ref.current = handler;
106
- });
107
- const stableHandler = useCallback((documentEvent) => ref.current(documentEvent), []), instance = useSanityInstance(getResourceId(doc.resourceId));
108
- useEffect(() => subscribeDocumentEvents(instance, stableHandler), [instance, stableHandler]);
144
+ }, $[0] = handler, $[1] = t0) : t0 = $[1], useInsertionEffect(t0);
145
+ let t1;
146
+ $[2] === Symbol.for("react.memo_cache_sentinel") ? (t1 = (documentEvent) => ref.current(documentEvent), $[2] = t1) : t1 = $[2];
147
+ const stableHandler = t1;
148
+ let t2;
149
+ $[3] !== doc.resourceId ? (t2 = getResourceId(doc.resourceId), $[3] = doc.resourceId, $[4] = t2) : t2 = $[4];
150
+ const instance = useSanityInstance(t2);
151
+ let t3, t4;
152
+ $[5] !== instance ? (t3 = () => subscribeDocumentEvents(instance, stableHandler), t4 = [instance, stableHandler], $[5] = instance, $[6] = t3, $[7] = t4) : (t3 = $[6], t4 = $[7]), useEffect(t3, t4);
109
153
  }
110
154
  const useDocumentSyncStatus = createStateSourceHook(getDocumentSyncStatus), ignoredKeys = ["_id", "_type", "_createdAt", "_updatedAt", "_rev"];
111
155
  function useEditDocument(doc, path) {
112
- const documentId = doc._id, instance = useSanityInstance(getResourceId(doc.resourceId)), apply = useApplyActions();
113
- if (!useCallback(
114
- () => getDocumentState(instance, documentId).getCurrent() !== void 0,
115
- [instance, documentId]
116
- )()) throw resolveDocument(instance, documentId);
117
- return useCallback(
118
- (updater) => {
119
- if (path) {
120
- const nextValue2 = typeof updater == "function" ? updater(getDocumentState(instance, documentId, path).getCurrent()) : updater;
121
- return apply(editDocument(doc, { set: { [path]: nextValue2 } }));
156
+ const $ = c(8);
157
+ let t0;
158
+ $[0] !== doc.resourceId ? (t0 = getResourceId(doc.resourceId), $[0] = doc.resourceId, $[1] = t0) : t0 = $[1];
159
+ const resourceId = t0, documentId = doc._id, instance = useSanityInstance(resourceId), apply = useApplyActions(resourceId);
160
+ if (getDocumentState(instance, documentId).getCurrent() === void 0)
161
+ throw resolveDocument(instance, documentId);
162
+ let t1;
163
+ return $[2] !== apply || $[3] !== doc || $[4] !== documentId || $[5] !== instance || $[6] !== path ? (t1 = (updater) => {
164
+ if (path) {
165
+ const nextValue = typeof updater == "function" ? updater(getDocumentState(instance, documentId, path).getCurrent()) : updater;
166
+ return apply(editDocument(doc, {
167
+ set: {
168
+ [path]: nextValue
169
+ }
170
+ }));
171
+ }
172
+ const current = getDocumentState(instance, documentId).getCurrent(), nextValue_0 = typeof updater == "function" ? updater(current) : updater;
173
+ if (typeof nextValue_0 != "object" || !nextValue_0)
174
+ throw new Error("No path was provided to `useEditDocument` and the value provided was not a document object.");
175
+ const editActions = Object.keys({
176
+ ...current,
177
+ ...nextValue_0
178
+ }).filter(_temp$3).filter((key_0) => current?.[key_0] !== nextValue_0[key_0]).map((key_1) => key_1 in nextValue_0 ? editDocument(doc, {
179
+ set: {
180
+ [key_1]: nextValue_0[key_1]
122
181
  }
123
- const current = getDocumentState(instance, documentId).getCurrent(), nextValue = typeof updater == "function" ? updater(current) : updater;
124
- if (typeof nextValue != "object" || !nextValue)
125
- throw new Error(
126
- "No path was provided to `useEditDocument` and the value provided was not a document object."
127
- );
128
- const editActions = Object.keys({ ...current, ...nextValue }).filter((key) => !ignoredKeys.includes(key)).filter((key) => current?.[key] !== nextValue[key]).map(
129
- (key) => key in nextValue ? editDocument(doc, { set: { [key]: nextValue[key] } }) : editDocument(doc, { unset: [key] })
130
- );
131
- return apply(editActions);
132
- },
133
- // eslint-disable-next-line react-hooks/exhaustive-deps
134
- [apply, doc._id, instance, path]
135
- );
182
+ }) : editDocument(doc, {
183
+ unset: [key_1]
184
+ }));
185
+ return apply(editActions);
186
+ }, $[2] = apply, $[3] = doc, $[4] = documentId, $[5] = instance, $[6] = path, $[7] = t1) : t1 = $[7], t1;
187
+ }
188
+ function _temp$3(key) {
189
+ return !ignoredKeys.includes(key);
136
190
  }
137
191
  function usePermissions(actions) {
192
+ const $ = c(5);
138
193
  if (Array.isArray(actions)) {
139
- const resourceIds = actions.map((action) => action.resourceId);
194
+ const resourceIds = actions.map(_temp$2);
140
195
  if (new Set(resourceIds).size !== 1)
141
196
  throw new Error("All actions must have the same resourceId");
142
197
  }
143
- const resourceId = Array.isArray(actions) ? getResourceId(actions[0].resourceId) : getResourceId(actions.resourceId), instance = useSanityInstance(resourceId);
144
- if (!useCallback(
145
- () => getPermissionsState(instance, actions).getCurrent() !== void 0,
146
- [actions, instance]
147
- )())
148
- throw firstValueFrom(
149
- getPermissionsState(instance, actions).observable.pipe(
150
- filter((result) => result !== void 0)
151
- )
152
- );
153
- const { subscribe, getCurrent } = useMemo(
154
- () => getPermissionsState(instance, actions),
155
- [actions, instance]
156
- );
198
+ let t0;
199
+ $[0] !== actions ? (t0 = Array.isArray(actions) ? getResourceId(actions[0].resourceId) : getResourceId(actions.resourceId), $[0] = actions, $[1] = t0) : t0 = $[1];
200
+ const instance = useSanityInstance(t0);
201
+ if (getPermissionsState(instance, actions).getCurrent() === void 0)
202
+ throw firstValueFrom(getPermissionsState(instance, actions).observable.pipe(filter(_temp2$2)));
203
+ let t1, t2;
204
+ $[2] !== actions || $[3] !== instance ? (t2 = getPermissionsState(instance, actions), $[2] = actions, $[3] = instance, $[4] = t2) : t2 = $[4], t1 = t2;
205
+ const {
206
+ subscribe,
207
+ getCurrent
208
+ } = t1;
157
209
  return useSyncExternalStore(subscribe, getCurrent);
158
210
  }
159
- const STABLE_EMPTY = {
160
- results: [],
161
- isPending: !1,
162
- hasMore: !1,
163
- count: 0
164
- };
165
- function useDocuments(options = {}) {
166
- const instance = useSanityInstance(options.resourceId), [ref] = useState(() => ({
167
- storeInstance: null,
168
- getCurrent: () => STABLE_EMPTY,
169
- initialOptions: options
170
- })), serializedOptions = JSON.stringify(options);
211
+ function _temp2$2(result) {
212
+ return result !== void 0;
213
+ }
214
+ function _temp$2(action) {
215
+ return action.resourceId;
216
+ }
217
+ function useQuery(query, options) {
218
+ const instance = useSanityInstance(options?.resourceId), [isPending, startTransition] = useTransition(), queryKey = getQueryKey(query, options), [deferredQueryKey, setDeferredQueryKey] = useState(queryKey), deferred = useMemo(() => parseQueryKey(deferredQueryKey), [deferredQueryKey]), [ref, setRef] = useState(new AbortController());
171
219
  useEffect(() => {
172
- ref.storeInstance?.setOptions(JSON.parse(serializedOptions));
173
- }, [ref, serializedOptions]);
174
- const subscribe = useCallback(
175
- (onStoreChanged) => {
176
- ref.storeInstance = createDocumentListStore(instance), ref.storeInstance.setOptions(ref.initialOptions);
177
- const state2 = ref.storeInstance.getState();
178
- ref.getCurrent = state2.getCurrent;
179
- const unsubscribe = state2.subscribe(onStoreChanged);
180
- return () => {
181
- unsubscribe(), ref.storeInstance?.dispose();
182
- };
183
- },
184
- [instance, ref]
185
- ), getSnapshot = useCallback(() => ref.getCurrent(), [ref]), state = useSyncExternalStore(subscribe, getSnapshot);
186
- return { loadMore: useCallback(() => {
187
- ref.storeInstance?.loadMore();
188
- }, [ref]), ...state };
220
+ queryKey !== deferredQueryKey && startTransition(() => {
221
+ ref && !ref.signal.aborted && (ref.abort(), setRef(new AbortController())), setDeferredQueryKey(queryKey);
222
+ });
223
+ }, [deferredQueryKey, queryKey, ref]);
224
+ const {
225
+ getCurrent,
226
+ subscribe
227
+ } = useMemo(() => getQueryState(instance, deferred.query, deferred.options), [instance, deferred]);
228
+ if (getCurrent() === void 0)
229
+ throw resolveQuery(instance, deferred.query, {
230
+ ...deferred.options,
231
+ signal: ref.signal
232
+ });
233
+ return {
234
+ data: useSyncExternalStore(subscribe, getCurrent),
235
+ isPending
236
+ };
237
+ }
238
+ const DEFAULT_BATCH_SIZE = 25, DEFAULT_PERSPECTIVE$1 = "drafts";
239
+ function useInfiniteList(t0) {
240
+ const $ = c(31);
241
+ let filter2, options, orderings, params, search, t1;
242
+ $[0] !== t0 ? ({
243
+ batchSize: t1,
244
+ params,
245
+ search,
246
+ filter: filter2,
247
+ orderings,
248
+ ...options
249
+ } = t0, $[0] = t0, $[1] = filter2, $[2] = options, $[3] = orderings, $[4] = params, $[5] = search, $[6] = t1) : (filter2 = $[1], options = $[2], orderings = $[3], params = $[4], search = $[5], t1 = $[6]);
250
+ const batchSize = t1 === void 0 ? DEFAULT_BATCH_SIZE : t1, perspective = options.perspective ?? DEFAULT_PERSPECTIVE$1, [limit, setLimit] = useState(batchSize);
251
+ let t2;
252
+ $[7] !== batchSize || $[8] !== filter2 || $[9] !== orderings || $[10] !== params || $[11] !== search ? (t2 = JSON.stringify({
253
+ filter: filter2,
254
+ search,
255
+ params,
256
+ orderings,
257
+ batchSize
258
+ }), $[7] = batchSize, $[8] = filter2, $[9] = orderings, $[10] = params, $[11] = search, $[12] = t2) : t2 = $[12];
259
+ const key = t2;
260
+ let t3;
261
+ $[13] !== batchSize ? (t3 = () => {
262
+ setLimit(batchSize);
263
+ }, $[13] = batchSize, $[14] = t3) : t3 = $[14];
264
+ let t4;
265
+ $[15] !== batchSize || $[16] !== key ? (t4 = [key, batchSize], $[15] = batchSize, $[16] = key, $[17] = t4) : t4 = $[17], useEffect(t3, t4);
266
+ let t5;
267
+ const conditions = [];
268
+ search?.trim() && conditions.push(`[@] match text::query("${search.trim()}")`), filter2 && conditions.push(`(${filter2})`), t5 = conditions.length ? `[${conditions.join(" && ")}]` : "";
269
+ const filterClause = t5, orderClause = orderings ? `| order(${orderings.map(_temp2$1).join(",")})` : "", dataQuery = `*${filterClause}${orderClause}[0...${limit}]{_id,_type}`, countQuery = `count(*${filterClause})`;
270
+ let t6;
271
+ $[18] !== options || $[19] !== params || $[20] !== perspective ? (t6 = {
272
+ ...options,
273
+ params,
274
+ perspective
275
+ }, $[18] = options, $[19] = params, $[20] = perspective, $[21] = t6) : t6 = $[21];
276
+ const {
277
+ data: t7,
278
+ isPending
279
+ } = useQuery(`{"count":${countQuery},"data":${dataQuery}}`, t6), {
280
+ count,
281
+ data
282
+ } = t7, hasMore = data.length < count;
283
+ let t8;
284
+ $[22] !== batchSize || $[23] !== count ? (t8 = () => {
285
+ setLimit((prev) => Math.min(prev + batchSize, count));
286
+ }, $[22] = batchSize, $[23] = count, $[24] = t8) : t8 = $[24];
287
+ const loadMore = t8;
288
+ let t9, t10;
289
+ return $[25] !== count || $[26] !== data || $[27] !== hasMore || $[28] !== isPending || $[29] !== loadMore ? (t10 = {
290
+ data,
291
+ hasMore,
292
+ count,
293
+ isPending,
294
+ loadMore
295
+ }, $[25] = count, $[26] = data, $[27] = hasMore, $[28] = isPending, $[29] = loadMore, $[30] = t10) : t10 = $[30], t9 = t10, t9;
189
296
  }
190
- function useSearch({
191
- query,
192
- filter: additionalFilter,
193
- ...options
194
- } = {}) {
195
- const filter2 = useMemo(() => {
196
- const conditions = [];
197
- return query?.trim() && conditions.push(`[@] match text::query("${query.trim()}")`), additionalFilter && conditions.push(`(${additionalFilter})`), conditions.length ? conditions.join(" && ") : "";
198
- }, [query, additionalFilter]);
199
- return useDocuments({
297
+ function _temp2$1(ordering) {
298
+ return [ordering.field, ordering.direction.toLowerCase()].map(_temp$1).filter(Boolean).join(" ");
299
+ }
300
+ function _temp$1(str) {
301
+ return str.trim();
302
+ }
303
+ const DEFAULT_PERSPECTIVE = "drafts";
304
+ function usePaginatedList(t0) {
305
+ const $ = c(48);
306
+ let t1;
307
+ $[0] !== t0 ? (t1 = t0 === void 0 ? {} : t0, $[0] = t0, $[1] = t1) : t1 = $[1];
308
+ let options, orderings, search, t2, t3, t4;
309
+ $[2] !== t1 ? ({
310
+ filter: t2,
311
+ pageSize: t3,
312
+ params: t4,
313
+ orderings,
314
+ search,
315
+ ...options
316
+ } = t1, $[2] = t1, $[3] = options, $[4] = orderings, $[5] = search, $[6] = t2, $[7] = t3, $[8] = t4) : (options = $[3], orderings = $[4], search = $[5], t2 = $[6], t3 = $[7], t4 = $[8]);
317
+ const filter2 = t2 === void 0 ? "" : t2, pageSize = t3 === void 0 ? 25 : t3;
318
+ let t5;
319
+ $[9] !== t4 ? (t5 = t4 === void 0 ? {} : t4, $[9] = t4, $[10] = t5) : t5 = $[10];
320
+ const params = t5, [pageIndex, setPageIndex] = useState(0);
321
+ let t6;
322
+ $[11] !== filter2 || $[12] !== orderings || $[13] !== pageSize || $[14] !== params || $[15] !== search ? (t6 = JSON.stringify({
323
+ filter: filter2,
324
+ search,
325
+ params,
326
+ orderings,
327
+ pageSize
328
+ }), $[11] = filter2, $[12] = orderings, $[13] = pageSize, $[14] = params, $[15] = search, $[16] = t6) : t6 = $[16];
329
+ const key = t6;
330
+ let t7;
331
+ $[17] === Symbol.for("react.memo_cache_sentinel") ? (t7 = () => {
332
+ setPageIndex(0);
333
+ }, $[17] = t7) : t7 = $[17];
334
+ let t8;
335
+ $[18] !== key ? (t8 = [key], $[18] = key, $[19] = t8) : t8 = $[19], useEffect(t7, t8);
336
+ const startIndex = pageIndex * pageSize, endIndex = (pageIndex + 1) * pageSize, perspective = options.perspective ?? DEFAULT_PERSPECTIVE;
337
+ let t9;
338
+ const conditions = [];
339
+ search?.trim() && conditions.push(`[@] match text::query("${search.trim()}")`), filter2 && conditions.push(`(${filter2})`), t9 = conditions.length ? `[${conditions.join(" && ")}]` : "";
340
+ const filterClause = t9, orderClause = orderings ? `| order(${orderings.map(_temp2).join(",")})` : "", dataQuery = `*${filterClause}${orderClause}[${startIndex}...${endIndex}]{_id,_type}`, countQuery = `count(*${filterClause})`;
341
+ let t10;
342
+ $[20] !== options || $[21] !== params || $[22] !== perspective ? (t10 = {
200
343
  ...options,
201
- filter: filter2
202
- });
344
+ perspective,
345
+ params
346
+ }, $[20] = options, $[21] = params, $[22] = perspective, $[23] = t10) : t10 = $[23];
347
+ const {
348
+ data: t11,
349
+ isPending
350
+ } = useQuery(`{"data":${dataQuery},"count":${countQuery}}`, t10), {
351
+ data,
352
+ count
353
+ } = t11, totalPages = Math.ceil(count / pageSize), currentPage = pageIndex + 1;
354
+ let t12;
355
+ $[24] === Symbol.for("react.memo_cache_sentinel") ? (t12 = () => setPageIndex(0), $[24] = t12) : t12 = $[24];
356
+ const firstPage = t12;
357
+ let t13;
358
+ $[25] === Symbol.for("react.memo_cache_sentinel") ? (t13 = () => setPageIndex(_temp3), $[25] = t13) : t13 = $[25];
359
+ const previousPage = t13;
360
+ let t14;
361
+ $[26] !== totalPages ? (t14 = () => setPageIndex((prev_0) => Math.min(prev_0 + 1, totalPages - 1)), $[26] = totalPages, $[27] = t14) : t14 = $[27];
362
+ const nextPage = t14;
363
+ let t15;
364
+ $[28] !== totalPages ? (t15 = () => setPageIndex(totalPages - 1), $[28] = totalPages, $[29] = t15) : t15 = $[29];
365
+ const lastPage = t15;
366
+ let t16;
367
+ $[30] !== totalPages ? (t16 = (pageNumber) => {
368
+ pageNumber < 1 || pageNumber > totalPages || setPageIndex(pageNumber - 1);
369
+ }, $[30] = totalPages, $[31] = t16) : t16 = $[31];
370
+ const goToPage = t16, hasFirstPage = pageIndex > 0, hasPreviousPage = pageIndex > 0, hasNextPage = pageIndex < totalPages - 1, hasLastPage = pageIndex < totalPages - 1;
371
+ let t17, t18;
372
+ return $[32] !== count || $[33] !== currentPage || $[34] !== data || $[35] !== endIndex || $[36] !== goToPage || $[37] !== hasFirstPage || $[38] !== hasLastPage || $[39] !== hasNextPage || $[40] !== hasPreviousPage || $[41] !== isPending || $[42] !== lastPage || $[43] !== nextPage || $[44] !== pageSize || $[45] !== startIndex || $[46] !== totalPages ? (t18 = {
373
+ data,
374
+ isPending,
375
+ pageSize,
376
+ currentPage,
377
+ totalPages,
378
+ startIndex,
379
+ endIndex,
380
+ count,
381
+ firstPage,
382
+ hasFirstPage,
383
+ previousPage,
384
+ hasPreviousPage,
385
+ nextPage,
386
+ hasNextPage,
387
+ lastPage,
388
+ hasLastPage,
389
+ goToPage
390
+ }, $[32] = count, $[33] = currentPage, $[34] = data, $[35] = endIndex, $[36] = goToPage, $[37] = hasFirstPage, $[38] = hasLastPage, $[39] = hasNextPage, $[40] = hasPreviousPage, $[41] = isPending, $[42] = lastPage, $[43] = nextPage, $[44] = pageSize, $[45] = startIndex, $[46] = totalPages, $[47] = t18) : t18 = $[47], t17 = t18, t17;
391
+ }
392
+ function _temp3(prev) {
393
+ return Math.max(prev - 1, 0);
394
+ }
395
+ function _temp2(ordering) {
396
+ return [ordering.field, ordering.direction.toLowerCase()].map(_temp).filter(Boolean).join(" ");
397
+ }
398
+ function _temp(str) {
399
+ return str.trim();
400
+ }
401
+ function usePreview(t0) {
402
+ const $ = c(12), {
403
+ document: t1,
404
+ ref
405
+ } = t0, {
406
+ _id,
407
+ _type
408
+ } = t1, instance = useSanityInstance();
409
+ let t2, t3;
410
+ $[0] !== _id || $[1] !== _type || $[2] !== instance ? (t3 = getPreviewState(instance, {
411
+ document: {
412
+ _id,
413
+ _type
414
+ }
415
+ }), $[0] = _id, $[1] = _type, $[2] = instance, $[3] = t3) : t3 = $[3], t2 = t3;
416
+ const stateSource = t2;
417
+ let t4;
418
+ $[4] !== ref || $[5] !== stateSource ? (t4 = (onStoreChanged) => {
419
+ const subscription = new Observable((observer) => {
420
+ if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
421
+ return;
422
+ const intersectionObserver = new IntersectionObserver((t52) => {
423
+ const [entry] = t52;
424
+ return observer.next(entry.isIntersecting);
425
+ }, {
426
+ rootMargin: "0px",
427
+ threshold: 0
428
+ });
429
+ return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
430
+ }).pipe(startWith(!1), distinctUntilChanged(), switchMap((isVisible) => isVisible ? new Observable((obs) => stateSource.subscribe(() => obs.next())) : EMPTY)).subscribe({
431
+ next: onStoreChanged
432
+ });
433
+ return () => subscription.unsubscribe();
434
+ }, $[4] = ref, $[5] = stateSource, $[6] = t4) : t4 = $[6];
435
+ const subscribe = t4;
436
+ let t5;
437
+ return $[7] !== _id || $[8] !== _type || $[9] !== instance || $[10] !== stateSource ? (t5 = () => {
438
+ const currentState = stateSource.getCurrent();
439
+ if (currentState.results === null)
440
+ throw resolvePreview(instance, {
441
+ document: {
442
+ _id,
443
+ _type
444
+ }
445
+ });
446
+ return currentState;
447
+ }, $[7] = _id, $[8] = _type, $[9] = instance, $[10] = stateSource, $[11] = t5) : t5 = $[11], useSyncExternalStore(subscribe, t5);
203
448
  }
204
- function usePreview({ document: { _id, _type }, ref }) {
205
- const instance = useSanityInstance(), stateSource = useMemo(
206
- () => getPreviewState(instance, { document: { _id, _type } }),
207
- [instance, _id, _type]
208
- ), subscribe = useCallback(
209
- (onStoreChanged) => {
210
- const subscription = new Observable((observer) => {
211
- if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
212
- return;
213
- const intersectionObserver = new IntersectionObserver(
214
- ([entry]) => observer.next(entry.isIntersecting),
215
- { rootMargin: "0px", threshold: 0 }
216
- );
217
- return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
218
- }).pipe(
219
- startWith(!1),
220
- distinctUntilChanged(),
221
- switchMap(
222
- (isVisible) => isVisible ? new Observable((obs) => stateSource.subscribe(() => obs.next())) : EMPTY
223
- )
224
- ).subscribe({ next: onStoreChanged });
225
- return () => subscription.unsubscribe();
449
+ function useProjection(t0) {
450
+ const $ = c(14), {
451
+ document: t1,
452
+ projection,
453
+ ref
454
+ } = t0, {
455
+ _id,
456
+ _type
457
+ } = t1, instance = useSanityInstance();
458
+ let t2, t3;
459
+ $[0] !== _id || $[1] !== _type || $[2] !== instance || $[3] !== projection ? (t3 = getProjectionState(instance, {
460
+ document: {
461
+ _id,
462
+ _type
226
463
  },
227
- [stateSource, ref]
228
- ), getSnapshot = useCallback(() => {
464
+ projection
465
+ }), $[0] = _id, $[1] = _type, $[2] = instance, $[3] = projection, $[4] = t3) : t3 = $[4], t2 = t3;
466
+ const stateSource = t2;
467
+ let t4;
468
+ $[5] !== ref || $[6] !== stateSource ? (t4 = (onStoreChanged) => {
469
+ const subscription = new Observable((observer) => {
470
+ if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
471
+ return;
472
+ const intersectionObserver = new IntersectionObserver((t52) => {
473
+ const [entry] = t52;
474
+ return observer.next(entry.isIntersecting);
475
+ }, {
476
+ rootMargin: "0px",
477
+ threshold: 0
478
+ });
479
+ return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
480
+ }).pipe(startWith(!1), distinctUntilChanged(), switchMap((isVisible) => isVisible ? new Observable((obs) => stateSource.subscribe(() => obs.next())) : EMPTY)).subscribe({
481
+ next: onStoreChanged
482
+ });
483
+ return () => subscription.unsubscribe();
484
+ }, $[5] = ref, $[6] = stateSource, $[7] = t4) : t4 = $[7];
485
+ const subscribe = t4;
486
+ let t5;
487
+ return $[8] !== _id || $[9] !== _type || $[10] !== instance || $[11] !== projection || $[12] !== stateSource ? (t5 = () => {
229
488
  const currentState = stateSource.getCurrent();
230
- if (currentState.results === null) throw resolvePreview(instance, { document: { _id, _type } });
489
+ if (currentState.results === null)
490
+ throw resolveProjection(instance, {
491
+ document: {
492
+ _id,
493
+ _type
494
+ },
495
+ projection
496
+ });
231
497
  return currentState;
232
- }, [_id, _type, instance, stateSource]);
233
- return useSyncExternalStore(subscribe, getSnapshot);
498
+ }, $[8] = _id, $[9] = _type, $[10] = instance, $[11] = projection, $[12] = stateSource, $[13] = t5) : t5 = $[13], useSyncExternalStore(subscribe, t5);
234
499
  }
235
500
  const useProject = createStateSourceHook({
236
501
  // remove `undefined` since we're suspending when that is the case
@@ -244,60 +509,38 @@ const useProject = createStateSourceHook({
244
509
  suspender: resolveProjects
245
510
  });
246
511
  function useUsers(params) {
247
- const instance = useSanityInstance(params.resourceId), [store] = useState(() => createUsersStore(instance));
248
- useEffect(() => {
512
+ const $ = c(15), instance = useSanityInstance(params.resourceId);
513
+ let t0;
514
+ $[0] !== instance ? (t0 = () => createUsersStore(instance), $[0] = instance, $[1] = t0) : t0 = $[1];
515
+ const [store] = useState(t0);
516
+ let t1, t2;
517
+ $[2] !== params.resourceId || $[3] !== params.resourceType || $[4] !== store ? (t1 = () => {
249
518
  store.setOptions({
250
519
  resourceType: params.resourceType,
251
520
  resourceId: params.resourceId
252
521
  });
253
- }, [params.resourceType, params.resourceId, store]);
254
- const subscribe = useCallback(
255
- (onStoreChanged) => {
256
- store.getState().getCurrent().initialFetchCompleted === !1 && store.resolveUsers();
257
- const unsubscribe = store.getState().subscribe(onStoreChanged);
258
- return () => {
259
- unsubscribe(), store.dispose();
260
- };
261
- },
262
- [store]
263
- ), getSnapshot = useCallback(() => store.getState().getCurrent(), [store]), { users, hasMore } = useSyncExternalStore(subscribe, getSnapshot) || {};
264
- return { users, hasMore, loadMore: store.loadMore };
265
- }
266
- function useProjection({
267
- document: { _id, _type },
268
- projection,
269
- ref
270
- }) {
271
- const instance = useSanityInstance(), stateSource = useMemo(
272
- () => getProjectionState(instance, { document: { _id, _type }, projection }),
273
- [instance, _id, _type, projection]
274
- ), subscribe = useCallback(
275
- (onStoreChanged) => {
276
- const subscription = new Observable((observer) => {
277
- if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
278
- return;
279
- const intersectionObserver = new IntersectionObserver(
280
- ([entry]) => observer.next(entry.isIntersecting),
281
- { rootMargin: "0px", threshold: 0 }
282
- );
283
- return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
284
- }).pipe(
285
- startWith(!1),
286
- distinctUntilChanged(),
287
- switchMap(
288
- (isVisible) => isVisible ? new Observable((obs) => stateSource.subscribe(() => obs.next())) : EMPTY
289
- )
290
- ).subscribe({ next: onStoreChanged });
291
- return () => subscription.unsubscribe();
292
- },
293
- [stateSource, ref]
294
- ), getSnapshot = useCallback(() => {
295
- const currentState = stateSource.getCurrent();
296
- if (currentState.results === null)
297
- throw resolveProjection(instance, { document: { _id, _type }, projection });
298
- return currentState;
299
- }, [_id, _type, projection, instance, stateSource]);
300
- return useSyncExternalStore(subscribe, getSnapshot);
522
+ }, t2 = [params.resourceType, params.resourceId, store], $[2] = params.resourceId, $[3] = params.resourceType, $[4] = store, $[5] = t1, $[6] = t2) : (t1 = $[5], t2 = $[6]), useEffect(t1, t2);
523
+ let t3;
524
+ $[7] !== store ? (t3 = (onStoreChanged) => {
525
+ store.getState().getCurrent().initialFetchCompleted === !1 && store.resolveUsers();
526
+ const unsubscribe = store.getState().subscribe(onStoreChanged);
527
+ return () => {
528
+ unsubscribe(), store.dispose();
529
+ };
530
+ }, $[7] = store, $[8] = t3) : t3 = $[8];
531
+ const subscribe = t3;
532
+ let t4;
533
+ $[9] !== store ? (t4 = () => store.getState().getCurrent(), $[9] = store, $[10] = t4) : t4 = $[10];
534
+ const getSnapshot = t4, {
535
+ users,
536
+ hasMore
537
+ } = useSyncExternalStore(subscribe, getSnapshot) || {};
538
+ let t5;
539
+ return $[11] !== hasMore || $[12] !== store.loadMore || $[13] !== users ? (t5 = {
540
+ users,
541
+ hasMore,
542
+ loadMore: store.loadMore
543
+ }, $[11] = hasMore, $[12] = store.loadMore, $[13] = users, $[14] = t5) : t5 = $[14], t5;
301
544
  }
302
545
  export {
303
546
  useApplyActions,
@@ -309,19 +552,20 @@ export {
309
552
  useDocument,
310
553
  useDocumentEvent,
311
554
  useDocumentSyncStatus,
312
- useDocuments,
313
555
  useEditDocument,
314
556
  useFrameConnection,
315
557
  useHandleCallback,
558
+ useInfiniteList,
316
559
  useLogOut,
317
560
  useLoginUrls,
561
+ usePaginatedList,
318
562
  usePermissions,
319
563
  usePreview,
320
564
  useProject,
321
565
  useProjection,
322
566
  useProjects,
567
+ useQuery,
323
568
  useSanityInstance,
324
- useSearch,
325
569
  useUsers,
326
570
  useWindowConnection
327
571
  };