@sanity/sdk-react 0.0.0-alpha.16 → 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.
package/dist/hooks.js CHANGED
@@ -1,87 +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
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 { useState, useMemo, useEffect, useCallback, useRef, useInsertionEffect, useSyncExternalStore, useTransition } from "react";
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
7
  const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState), useClient = createStateSourceHook({
7
8
  getState: getClientState,
8
9
  getResourceId: (e) => e.resourceId
9
10
  });
10
11
  function useFrameConnection(options) {
11
- const { onMessage, targetOrigin, name, connectTo, heartbeat } = options, instance = useSanityInstance(), [status, setStatus] = useState("idle"), controller = useMemo(
12
- () => getOrCreateController(instance, targetOrigin),
13
- [instance, targetOrigin]
14
- ), channel = useMemo(
15
- () => getOrCreateChannel(instance, {
16
- name,
17
- connectTo,
18
- heartbeat
19
- }),
20
- [instance, name, connectTo, heartbeat]
21
- );
22
- 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) => {
23
31
  setStatus(event.status);
24
- }) : void 0, [channel]), useEffect(() => {
25
- 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;
26
37
  const unsubscribers = [];
27
- return Object.entries(onMessage).forEach(([type, handler]) => {
28
- const unsubscribe = channel.on(type, handler);
29
- 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);
30
41
  }), () => {
31
- unsubscribers.forEach((unsub) => unsub());
42
+ unsubscribers.forEach(_temp$5);
32
43
  };
33
- }, [channel, onMessage]);
34
- const connect = useCallback(
35
- (frameWindow) => {
36
- const removeTarget = controller?.addTarget(frameWindow);
37
- return () => {
38
- removeTarget?.();
39
- };
40
- },
41
- [controller]
42
- ), sendMessage = useCallback(
43
- (type, data) => {
44
- channel?.post(type, data);
45
- },
46
- [channel]
47
- );
48
- 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 = () => () => {
49
60
  releaseChannel(instance, name);
50
- }, [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 = {
51
64
  connect,
52
65
  sendMessage,
53
66
  status
54
- };
67
+ }, $[23] = connect, $[24] = sendMessage, $[25] = status, $[26] = t12) : t12 = $[26], t12;
68
+ }
69
+ function _temp$5(unsub) {
70
+ return unsub();
55
71
  }
56
72
  function useWindowConnection(options) {
57
- const { name, onMessage, connectTo } = options, instance = useSanityInstance(), [status, setStatus] = useState("idle"), node = useMemo(
58
- () => getOrCreateNode(instance, { name, connectTo }),
59
- [instance, name, connectTo]
60
- );
61
- 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) => {
62
86
  setStatus(newStatus);
63
- }), [node, instance, name]), useEffect(() => {
64
- 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;
65
94
  const unsubscribers = [];
66
- return Object.entries(onMessage).forEach(([type, handler]) => {
67
- const unsubscribe = node.on(type, handler);
68
- 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);
69
98
  }), () => {
70
- unsubscribers.forEach((unsub) => unsub());
99
+ unsubscribers.forEach(_temp$4);
71
100
  };
72
- }, [node, onMessage]);
73
- const sendMessage = useCallback(
74
- (type, data) => {
75
- node?.post(type, data);
76
- },
77
- [node]
78
- );
79
- 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 = () => () => {
80
109
  releaseNode(instance, name);
81
- }, [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 = {
82
113
  sendMessage,
83
114
  status
84
- };
115
+ }, $[20] = sendMessage, $[21] = status, $[22] = t9) : t9 = $[22], t9;
116
+ }
117
+ function _temp$4(unsub) {
118
+ return unsub();
85
119
  }
86
120
  const useDatasets = createStateSourceHook({
87
121
  // remove `undefined` since we're suspending when that is the case
@@ -89,10 +123,10 @@ const useDatasets = createStateSourceHook({
89
123
  shouldSuspend: (instance) => getDatasetsState(instance).getCurrent() === void 0,
90
124
  suspender: resolveDatasets
91
125
  });
92
- function useApplyActions() {
93
- return _useApplyActions();
126
+ function useApplyActions(resourceId) {
127
+ return _useApplyActions(resourceId)();
94
128
  }
95
- const _useApplyActions = createCallbackHook(applyActions);
129
+ const _useApplyActions = (resourceId) => createCallbackHook(applyActions, resourceId);
96
130
  function useDocument(doc, path) {
97
131
  return _useDocument(doc, path);
98
132
  }
@@ -103,254 +137,365 @@ const _useDocument = createStateSourceHook({
103
137
  getResourceId: (doc) => getResourceId(doc.resourceId)
104
138
  });
105
139
  function useDocumentEvent(handler, doc) {
106
- const ref = useRef(handler);
107
- useInsertionEffect(() => {
140
+ const $ = c(8), ref = useRef(handler);
141
+ let t0;
142
+ $[0] !== handler ? (t0 = () => {
108
143
  ref.current = handler;
109
- });
110
- const stableHandler = useCallback((documentEvent) => ref.current(documentEvent), []), instance = useSanityInstance(getResourceId(doc.resourceId));
111
- 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);
112
153
  }
113
154
  const useDocumentSyncStatus = createStateSourceHook(getDocumentSyncStatus), ignoredKeys = ["_id", "_type", "_createdAt", "_updatedAt", "_rev"];
114
155
  function useEditDocument(doc, path) {
115
- const documentId = doc._id, instance = useSanityInstance(getResourceId(doc.resourceId)), apply = useApplyActions();
116
- if (!useCallback(
117
- () => getDocumentState(instance, documentId).getCurrent() !== void 0,
118
- [instance, documentId]
119
- )()) throw resolveDocument(instance, documentId);
120
- return useCallback(
121
- (updater) => {
122
- if (path) {
123
- const nextValue2 = typeof updater == "function" ? updater(getDocumentState(instance, documentId, path).getCurrent()) : updater;
124
- 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]
125
181
  }
126
- const current = getDocumentState(instance, documentId).getCurrent(), nextValue = typeof updater == "function" ? updater(current) : updater;
127
- if (typeof nextValue != "object" || !nextValue)
128
- throw new Error(
129
- "No path was provided to `useEditDocument` and the value provided was not a document object."
130
- );
131
- const editActions = Object.keys({ ...current, ...nextValue }).filter((key) => !ignoredKeys.includes(key)).filter((key) => current?.[key] !== nextValue[key]).map(
132
- (key) => key in nextValue ? editDocument(doc, { set: { [key]: nextValue[key] } }) : editDocument(doc, { unset: [key] })
133
- );
134
- return apply(editActions);
135
- },
136
- // eslint-disable-next-line react-hooks/exhaustive-deps
137
- [apply, doc._id, instance, path]
138
- );
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);
139
190
  }
140
191
  function usePermissions(actions) {
192
+ const $ = c(5);
141
193
  if (Array.isArray(actions)) {
142
- const resourceIds = actions.map((action) => action.resourceId);
194
+ const resourceIds = actions.map(_temp$2);
143
195
  if (new Set(resourceIds).size !== 1)
144
196
  throw new Error("All actions must have the same resourceId");
145
197
  }
146
- const resourceId = Array.isArray(actions) ? getResourceId(actions[0].resourceId) : getResourceId(actions.resourceId), instance = useSanityInstance(resourceId);
147
- if (!useCallback(
148
- () => getPermissionsState(instance, actions).getCurrent() !== void 0,
149
- [actions, instance]
150
- )())
151
- throw firstValueFrom(
152
- getPermissionsState(instance, actions).observable.pipe(
153
- filter((result) => result !== void 0)
154
- )
155
- );
156
- const { subscribe, getCurrent } = useMemo(
157
- () => getPermissionsState(instance, actions),
158
- [actions, instance]
159
- );
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;
160
209
  return useSyncExternalStore(subscribe, getCurrent);
161
210
  }
211
+ function _temp2$2(result) {
212
+ return result !== void 0;
213
+ }
214
+ function _temp$2(action) {
215
+ return action.resourceId;
216
+ }
162
217
  function useQuery(query, options) {
163
- const instance = useSanityInstance(options?.resourceId), [isPending, startTransition] = useTransition(), queryKey = getQueryKey(query, options), [deferredQueryKey, setDeferredQueryKey] = useState(queryKey), deferred = useMemo(() => parseQueryKey(deferredQueryKey), [deferredQueryKey]), ref = useRef(null);
164
- ref.current === null && (ref.current = new AbortController()), useEffect(() => {
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());
219
+ useEffect(() => {
165
220
  queryKey !== deferredQueryKey && startTransition(() => {
166
- ref.current && !ref.current.signal.aborted && (ref.current.abort(), ref.current = new AbortController()), setDeferredQueryKey(queryKey);
221
+ ref && !ref.signal.aborted && (ref.abort(), setRef(new AbortController())), setDeferredQueryKey(queryKey);
167
222
  });
168
- }, [deferredQueryKey, queryKey]);
169
- const { getCurrent, subscribe } = useMemo(
170
- () => getQueryState(instance, deferred.query, deferred.options),
171
- [instance, deferred]
172
- );
223
+ }, [deferredQueryKey, queryKey, ref]);
224
+ const {
225
+ getCurrent,
226
+ subscribe
227
+ } = useMemo(() => getQueryState(instance, deferred.query, deferred.options), [instance, deferred]);
173
228
  if (getCurrent() === void 0)
174
- throw resolveQuery(instance, deferred.query, { ...deferred.options, signal: ref.current.signal });
175
- return { data: useSyncExternalStore(subscribe, getCurrent), isPending };
176
- }
177
- const DEFAULT_PAGE_SIZE = 25, DEFAULT_PERSPECTIVE$1 = "drafts";
178
- function useInfiniteList({
179
- pageSize = DEFAULT_PAGE_SIZE,
180
- params,
181
- search,
182
- filter: filter2,
183
- orderings,
184
- ...options
185
- }) {
186
- const perspective = options.perspective ?? DEFAULT_PERSPECTIVE$1, [limit, setLimit] = useState(pageSize), key = JSON.stringify({ filter: filter2, search, params, orderings, pageSize });
187
- useEffect(() => {
188
- setLimit(pageSize);
189
- }, [key, pageSize]);
190
- const filterClause = useMemo(() => {
191
- const conditions = [];
192
- return search?.trim() && conditions.push(`[@] match text::query("${search.trim()}")`), filter2 && conditions.push(`(${filter2})`), conditions.length ? `[${conditions.join(" && ")}]` : "";
193
- }, [filter2, search]), orderClause = orderings ? `| order(${orderings.map(
194
- (ordering) => [ordering.field, ordering.direction.toLowerCase()].map((str) => str.trim()).filter(Boolean).join(" ")
195
- ).join(",")})` : "", dataQuery = `*${filterClause}${orderClause}[0...${limit}]{_id,_type}`, countQuery = `count(*${filterClause})`, {
196
- data: { count, data },
229
+ throw resolveQuery(instance, deferred.query, {
230
+ ...deferred.options,
231
+ signal: ref.signal
232
+ });
233
+ return {
234
+ data: useSyncExternalStore(subscribe, getCurrent),
197
235
  isPending
198
- } = useQuery(`{"count":${countQuery},"data":${dataQuery}}`, {
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 = {
199
272
  ...options,
200
273
  params,
201
274
  perspective
202
- }), hasMore = data.length < count, loadMore = useCallback(() => {
203
- setLimit((prev) => Math.min(prev + pageSize, count));
204
- }, [count, pageSize]);
205
- return useMemo(
206
- () => ({ data, hasMore, count, isPending, loadMore }),
207
- [data, hasMore, count, isPending, loadMore]
208
- );
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;
296
+ }
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();
209
302
  }
210
303
  const DEFAULT_PERSPECTIVE = "drafts";
211
- function usePaginatedList({
212
- filter: filter2 = "",
213
- pageSize = 25,
214
- params = {},
215
- orderings,
216
- search,
217
- ...options
218
- } = {}) {
219
- const [pageIndex, setPageIndex] = useState(0), key = JSON.stringify({ filter: filter2, search, params, orderings, pageSize });
220
- useEffect(() => {
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 = () => {
221
332
  setPageIndex(0);
222
- }, [key]);
223
- const startIndex = pageIndex * pageSize, endIndex = (pageIndex + 1) * pageSize, perspective = options.perspective ?? DEFAULT_PERSPECTIVE, filterClause = useMemo(() => {
224
- const conditions = [];
225
- return search?.trim() && conditions.push(`[@] match text::query("${search.trim()}")`), filter2 && conditions.push(`(${filter2})`), conditions.length ? `[${conditions.join(" && ")}]` : "";
226
- }, [filter2, search]), orderClause = orderings ? `| order(${orderings.map(
227
- (ordering) => [ordering.field, ordering.direction.toLowerCase()].map((str) => str.trim()).filter(Boolean).join(" ")
228
- ).join(",")})` : "", dataQuery = `*${filterClause}${orderClause}[${startIndex}...${endIndex}]{_id,_type}`, countQuery = `count(*${filterClause})`, {
229
- data: { data, count },
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 = {
343
+ ...options,
344
+ perspective,
345
+ params
346
+ }, $[20] = options, $[21] = params, $[22] = perspective, $[23] = t10) : t10 = $[23];
347
+ const {
348
+ data: t11,
230
349
  isPending
231
- } = useQuery(
232
- `{"data":${dataQuery},"count":${countQuery}}`,
233
- {
234
- ...options,
235
- perspective,
236
- params
237
- }
238
- ), totalPages = Math.ceil(count / pageSize), currentPage = pageIndex + 1, firstPage = useCallback(() => setPageIndex(0), []), previousPage = useCallback(() => setPageIndex((prev) => Math.max(prev - 1, 0)), []), nextPage = useCallback(
239
- () => setPageIndex((prev) => Math.min(prev + 1, totalPages - 1)),
240
- [totalPages]
241
- ), lastPage = useCallback(() => setPageIndex(totalPages - 1), [totalPages]), goToPage = useCallback(
242
- (pageNumber) => {
243
- pageNumber < 1 || pageNumber > totalPages || setPageIndex(pageNumber - 1);
244
- },
245
- [totalPages]
246
- ), hasFirstPage = pageIndex > 0, hasPreviousPage = pageIndex > 0, hasNextPage = pageIndex < totalPages - 1, hasLastPage = pageIndex < totalPages - 1;
247
- return useMemo(
248
- () => ({
249
- data,
250
- isPending,
251
- pageSize,
252
- currentPage,
253
- totalPages,
254
- startIndex,
255
- endIndex,
256
- count,
257
- firstPage,
258
- hasFirstPage,
259
- previousPage,
260
- hasPreviousPage,
261
- nextPage,
262
- hasNextPage,
263
- lastPage,
264
- hasLastPage,
265
- goToPage
266
- }),
267
- [
268
- data,
269
- isPending,
270
- pageSize,
271
- currentPage,
272
- totalPages,
273
- startIndex,
274
- endIndex,
275
- count,
276
- firstPage,
277
- hasFirstPage,
278
- previousPage,
279
- hasPreviousPage,
280
- nextPage,
281
- hasNextPage,
282
- lastPage,
283
- hasLastPage,
284
- goToPage
285
- ]
286
- );
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;
287
391
  }
288
- function usePreview({ document: { _id, _type }, ref }) {
289
- const instance = useSanityInstance(), stateSource = useMemo(
290
- () => getPreviewState(instance, { document: { _id, _type } }),
291
- [instance, _id, _type]
292
- ), subscribe = useCallback(
293
- (onStoreChanged) => {
294
- const subscription = new Observable((observer) => {
295
- if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
296
- return;
297
- const intersectionObserver = new IntersectionObserver(
298
- ([entry]) => observer.next(entry.isIntersecting),
299
- { rootMargin: "0px", threshold: 0 }
300
- );
301
- return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
302
- }).pipe(
303
- startWith(!1),
304
- distinctUntilChanged(),
305
- switchMap(
306
- (isVisible) => isVisible ? new Observable((obs) => stateSource.subscribe(() => obs.next())) : EMPTY
307
- )
308
- ).subscribe({ next: onStoreChanged });
309
- return () => subscription.unsubscribe();
310
- },
311
- [stateSource, ref]
312
- ), getSnapshot = useCallback(() => {
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 = () => {
313
438
  const currentState = stateSource.getCurrent();
314
- if (currentState.results === null) throw resolvePreview(instance, { document: { _id, _type } });
439
+ if (currentState.results === null)
440
+ throw resolvePreview(instance, {
441
+ document: {
442
+ _id,
443
+ _type
444
+ }
445
+ });
315
446
  return currentState;
316
- }, [_id, _type, instance, stateSource]);
317
- return useSyncExternalStore(subscribe, getSnapshot);
447
+ }, $[7] = _id, $[8] = _type, $[9] = instance, $[10] = stateSource, $[11] = t5) : t5 = $[11], useSyncExternalStore(subscribe, t5);
318
448
  }
319
- function useProjection({
320
- document: { _id, _type },
321
- projection,
322
- ref
323
- }) {
324
- const instance = useSanityInstance(), stateSource = useMemo(
325
- () => getProjectionState(instance, { document: { _id, _type }, projection }),
326
- [instance, _id, _type, projection]
327
- ), subscribe = useCallback(
328
- (onStoreChanged) => {
329
- const subscription = new Observable((observer) => {
330
- if (typeof IntersectionObserver > "u" || typeof HTMLElement > "u")
331
- return;
332
- const intersectionObserver = new IntersectionObserver(
333
- ([entry]) => observer.next(entry.isIntersecting),
334
- { rootMargin: "0px", threshold: 0 }
335
- );
336
- return ref?.current && ref.current instanceof HTMLElement && intersectionObserver.observe(ref.current), () => intersectionObserver.disconnect();
337
- }).pipe(
338
- startWith(!1),
339
- distinctUntilChanged(),
340
- switchMap(
341
- (isVisible) => isVisible ? new Observable((obs) => stateSource.subscribe(() => obs.next())) : EMPTY
342
- )
343
- ).subscribe({ next: onStoreChanged });
344
- 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
345
463
  },
346
- [stateSource, ref]
347
- ), 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 = () => {
348
488
  const currentState = stateSource.getCurrent();
349
489
  if (currentState.results === null)
350
- throw resolveProjection(instance, { document: { _id, _type }, projection });
490
+ throw resolveProjection(instance, {
491
+ document: {
492
+ _id,
493
+ _type
494
+ },
495
+ projection
496
+ });
351
497
  return currentState;
352
- }, [_id, _type, projection, instance, stateSource]);
353
- return useSyncExternalStore(subscribe, getSnapshot);
498
+ }, $[8] = _id, $[9] = _type, $[10] = instance, $[11] = projection, $[12] = stateSource, $[13] = t5) : t5 = $[13], useSyncExternalStore(subscribe, t5);
354
499
  }
355
500
  const useProject = createStateSourceHook({
356
501
  // remove `undefined` since we're suspending when that is the case
@@ -364,24 +509,38 @@ const useProject = createStateSourceHook({
364
509
  suspender: resolveProjects
365
510
  });
366
511
  function useUsers(params) {
367
- const instance = useSanityInstance(params.resourceId), [store] = useState(() => createUsersStore(instance));
368
- 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 = () => {
369
518
  store.setOptions({
370
519
  resourceType: params.resourceType,
371
520
  resourceId: params.resourceId
372
521
  });
373
- }, [params.resourceType, params.resourceId, store]);
374
- const subscribe = useCallback(
375
- (onStoreChanged) => {
376
- store.getState().getCurrent().initialFetchCompleted === !1 && store.resolveUsers();
377
- const unsubscribe = store.getState().subscribe(onStoreChanged);
378
- return () => {
379
- unsubscribe(), store.dispose();
380
- };
381
- },
382
- [store]
383
- ), getSnapshot = useCallback(() => store.getState().getCurrent(), [store]), { users, hasMore } = useSyncExternalStore(subscribe, getSnapshot) || {};
384
- return { users, hasMore, loadMore: store.loadMore };
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;
385
544
  }
386
545
  export {
387
546
  useApplyActions,