@sanity/sdk-react 2.2.0 → 2.3.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 (33) hide show
  1. package/dist/index.d.ts +2 -3
  2. package/dist/index.js +101 -26
  3. package/dist/index.js.map +1 -1
  4. package/package.json +7 -6
  5. package/src/components/auth/AuthBoundary.test.tsx +33 -20
  6. package/src/components/auth/AuthBoundary.tsx +20 -5
  7. package/src/components/auth/LoginError.tsx +9 -12
  8. package/src/components/errors/CorsErrorComponent.test.tsx +48 -0
  9. package/src/components/errors/CorsErrorComponent.tsx +37 -0
  10. package/src/components/errors/Error.styles.ts +35 -0
  11. package/src/components/errors/Error.tsx +40 -0
  12. package/src/context/ComlinkTokenRefresh.test.tsx +87 -38
  13. package/src/context/ComlinkTokenRefresh.tsx +2 -1
  14. package/src/hooks/auth/useDashboardOrganizationId.test.tsx +16 -7
  15. package/src/hooks/auth/useVerifyOrgProjects.test.tsx +56 -14
  16. package/src/hooks/dashboard/{useManageFavorite.test.ts → useManageFavorite.test.tsx} +99 -44
  17. package/src/hooks/document/{useDocument.test.ts → useDocument.test.tsx} +25 -22
  18. package/src/hooks/document/{useDocumentEvent.test.ts → useDocumentEvent.test.tsx} +17 -16
  19. package/src/hooks/document/{useDocumentPermissions.test.ts → useDocumentPermissions.test.tsx} +101 -40
  20. package/src/hooks/document/{useEditDocument.test.ts → useEditDocument.test.tsx} +52 -22
  21. package/src/hooks/documents/useDocuments.test.tsx +63 -25
  22. package/src/hooks/helpers/createCallbackHook.test.tsx +41 -37
  23. package/src/hooks/paginatedDocuments/usePaginatedDocuments.test.tsx +2 -2
  24. package/src/hooks/presence/usePresence.test.tsx +9 -6
  25. package/src/hooks/preview/useDocumentPreview.test.tsx +15 -16
  26. package/src/hooks/projection/useDocumentProjection.test.tsx +23 -38
  27. package/src/hooks/projection/useDocumentProjection.ts +3 -8
  28. package/src/hooks/query/useQuery.test.tsx +18 -10
  29. package/src/hooks/releases/useActiveReleases.test.tsx +25 -21
  30. package/src/hooks/releases/usePerspective.test.tsx +16 -22
  31. package/src/hooks/users/useUser.test.tsx +32 -15
  32. package/src/hooks/users/useUsers.test.tsx +19 -11
  33. package/src/hooks/_synchronous-groq-js.mjs +0 -4
package/dist/index.d.ts CHANGED
@@ -42,7 +42,6 @@ import {SanityUser} from '@sanity/sdk'
42
42
  import {SortOrderingItem} from '@sanity/types'
43
43
  import {StudioResource} from '@sanity/message-protocol'
44
44
  import {UserPresence} from '@sanity/sdk'
45
- import {ValidProjection} from '@sanity/sdk'
46
45
  import {WindowMessage} from '@sanity/sdk'
47
46
 
48
47
  /**
@@ -1406,7 +1405,7 @@ export declare interface useDocumentPreviewResults {
1406
1405
  * ```
1407
1406
  */
1408
1407
  export declare function useDocumentProjection<
1409
- TProjection extends ValidProjection = ValidProjection,
1408
+ TProjection extends string = string,
1410
1409
  TDocumentType extends string = string,
1411
1410
  TDataset extends string = string,
1412
1411
  TProjectId extends string = string,
@@ -1470,7 +1469,7 @@ export declare function useDocumentProjection<TData extends object>(
1470
1469
  * @category Types
1471
1470
  */
1472
1471
  export declare interface useDocumentProjectionOptions<
1473
- TProjection extends ValidProjection = ValidProjection,
1472
+ TProjection extends string = string,
1474
1473
  TDocumentType extends string = string,
1475
1474
  TDataset extends string = string,
1476
1475
  TProjectId extends string = string,
package/dist/index.js CHANGED
@@ -1,12 +1,12 @@
1
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
2
  import { c } from "react-compiler-runtime";
3
- import { getAuthState, getNodeState, getIsInDashboardState, setAuthToken, AuthStateType, getLoginUrlState, observeOrganizationVerificationState, handleAuthCallback, logout, createSanityInstance, getTokenState, getCurrentUserState, getDashboardOrganizationId, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, getFavoritesState, resolveFavoritesState, resolveDatasets, getDatasetsState, applyDocumentActions, resolveDocument, getDocumentState, subscribeDocumentEvents, getPermissionsState, getDocumentSyncStatus, editDocument, getQueryKey, parseQueryKey, getQueryState, resolveQuery, createGroqSearchFilter, getPresence, getPreviewState, resolvePreview, getProjectionState, resolveProjection, resolveProject, getProjectState, resolveProjects, getProjectsState, getActiveReleasesState, getPerspectiveState, getUsersKey, parseUsersKey, getUsersState, resolveUsers, loadMoreUsers } from "@sanity/sdk";
3
+ import { ClientError, CorsOriginError } from "@sanity/client";
4
+ import { getAuthState, getNodeState, getIsInDashboardState, setAuthToken, AuthStateType, getLoginUrlState, observeOrganizationVerificationState, handleAuthCallback, logout, getCorsErrorProjectId, createSanityInstance, getTokenState, getCurrentUserState, getDashboardOrganizationId, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, getFavoritesState, resolveFavoritesState, resolveDatasets, getDatasetsState, applyDocumentActions, resolveDocument, getDocumentState, subscribeDocumentEvents, getPermissionsState, getDocumentSyncStatus, editDocument, getQueryKey, parseQueryKey, getQueryState, resolveQuery, createGroqSearchFilter, getPresence, getPreviewState, resolvePreview, getProjectionState, resolveProjection, resolveProject, getProjectState, resolveProjects, getProjectsState, getActiveReleasesState, getPerspectiveState, getUsersKey, parseUsersKey, getUsersState, resolveUsers, loadMoreUsers } from "@sanity/sdk";
4
5
  export * from "@sanity/sdk";
5
6
  import { createContext, useContext, useSyncExternalStore, useRef, useEffect, useState, Suspense, useMemo, useCallback, useInsertionEffect, useTransition } from "react";
6
7
  import { ErrorBoundary } from "react-error-boundary";
7
8
  import { SDK_CHANNEL_NAME, SDK_NODE_NAME } from "@sanity/message-protocol";
8
9
  import { firstValueFrom, filter, identity, Observable, startWith, distinctUntilChanged, switchMap, EMPTY } from "rxjs";
9
- import { ClientError } from "@sanity/client";
10
10
  import { pick } from "lodash-es";
11
11
  const SanityInstanceContext = createContext(null), useSanityInstance = (config) => {
12
12
  const $ = c(3), instance = useContext(SanityInstanceContext);
@@ -142,7 +142,9 @@ const ComlinkTokenRefreshProvider = (t0) => {
142
142
  children
143
143
  } = t0, instance = useSanityInstance();
144
144
  let t1;
145
- if (t1 = getIsInDashboardState(instance).getCurrent(), t1) {
145
+ t1 = getIsInDashboardState(instance).getCurrent();
146
+ const isInDashboard = t1, studioModeEnabled = !!instance.config.studioMode?.enabled;
147
+ if (isInDashboard && !studioModeEnabled) {
146
148
  let t2;
147
149
  return $[0] !== children ? (t2 = /* @__PURE__ */ jsx(DashboardTokenRefresh, { children }), $[0] = children, $[1] = t2) : t2 = $[1], t2;
148
150
  }
@@ -174,6 +176,88 @@ function useVerifyOrgProjects(t0, projectIds) {
174
176
  };
175
177
  }, t2 = [instance, disabled, error, projectIds], $[0] = disabled, $[1] = error, $[2] = instance, $[3] = projectIds, $[4] = t1, $[5] = t2) : (t1 = $[4], t2 = $[5]), useEffect(t1, t2), error;
176
178
  }
179
+ const FONT_SANS_SERIF = "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Helvetica, Arial, system-ui, sans-serif", FONT_MONOSPACE = "-apple-system-ui-monospace, 'SF Mono', Menlo, Monaco, Consolas, monospace", styles = {
180
+ container: {
181
+ padding: "28px",
182
+ fontFamily: FONT_SANS_SERIF,
183
+ display: "flex",
184
+ flexDirection: "column",
185
+ gap: "21px",
186
+ fontSize: "14px"
187
+ },
188
+ heading: {
189
+ margin: 0,
190
+ fontSize: "28px",
191
+ fontWeight: 700
192
+ },
193
+ paragraph: {
194
+ margin: 0
195
+ },
196
+ link: {
197
+ appearance: "none",
198
+ background: "transparent",
199
+ border: 0,
200
+ padding: 0,
201
+ font: "inherit",
202
+ textDecoration: "underline",
203
+ cursor: "pointer"
204
+ },
205
+ code: {
206
+ fontFamily: FONT_MONOSPACE
207
+ }
208
+ };
209
+ function Error$1(t0) {
210
+ const $ = c(13), {
211
+ heading,
212
+ description,
213
+ code,
214
+ cta
215
+ } = t0;
216
+ let t1;
217
+ $[0] !== heading ? (t1 = /* @__PURE__ */ jsx("h1", { style: styles.heading, children: heading }), $[0] = heading, $[1] = t1) : t1 = $[1];
218
+ let t2;
219
+ $[2] !== description ? (t2 = description && /* @__PURE__ */ jsx("p", { style: styles.paragraph, dangerouslySetInnerHTML: {
220
+ __html: description
221
+ } }), $[2] = description, $[3] = t2) : t2 = $[3];
222
+ let t3;
223
+ $[4] !== code ? (t3 = code && /* @__PURE__ */ jsx("code", { style: styles.code, children: code }), $[4] = code, $[5] = t3) : t3 = $[5];
224
+ let t4;
225
+ $[6] !== cta ? (t4 = cta && (cta.href || cta.onClick) && /* @__PURE__ */ jsx("p", { style: styles.paragraph, children: cta.href ? /* @__PURE__ */ jsx("a", { style: styles.link, href: cta.href, target: "_blank", rel: "noopener noreferrer", children: cta.text }) : /* @__PURE__ */ jsx("button", { style: styles.link, onClick: cta.onClick, children: cta.text }) }), $[6] = cta, $[7] = t4) : t4 = $[7];
226
+ let t5;
227
+ return $[8] !== t1 || $[9] !== t2 || $[10] !== t3 || $[11] !== t4 ? (t5 = /* @__PURE__ */ jsxs("div", { style: styles.container, children: [
228
+ t1,
229
+ t2,
230
+ t3,
231
+ t4
232
+ ] }), $[8] = t1, $[9] = t2, $[10] = t3, $[11] = t4, $[12] = t5) : t5 = $[12], t5;
233
+ }
234
+ function CorsErrorComponent(t0) {
235
+ const $ = c(6), {
236
+ projectId,
237
+ error
238
+ } = t0, origin = window.location.origin;
239
+ let t1;
240
+ const t2 = `https://sanity.io/manage/project/${projectId}/api`;
241
+ let t3;
242
+ if ($[0] !== t2) {
243
+ const url = new URL(t2);
244
+ url.searchParams.set("cors", "add"), url.searchParams.set("origin", origin), url.searchParams.set("credentials", "include"), t3 = url.toString(), $[0] = t2, $[1] = t3;
245
+ } else
246
+ t3 = $[1];
247
+ t1 = t3;
248
+ const corsUrl = t1;
249
+ let t4;
250
+ return $[2] !== corsUrl || $[3] !== error?.message || $[4] !== projectId ? (t4 = /* @__PURE__ */ jsx(Error$1, { heading: "Before you continue\u2026", ...projectId ? {
251
+ description: "To access your content, you need to <strong>add the following URL as a CORS origin</strong> to your Sanity project.",
252
+ code: origin,
253
+ cta: {
254
+ text: "Manage CORS configuration",
255
+ href: corsUrl
256
+ }
257
+ } : {
258
+ description: error?.message
259
+ } }), $[2] = corsUrl, $[3] = error?.message, $[4] = projectId, $[5] = t4) : t4 = $[5], t4;
260
+ }
177
261
  function isInIframe() {
178
262
  return typeof window < "u" && window.self !== window.top;
179
263
  }
@@ -213,7 +297,7 @@ function _temp$5(replacementLocation) {
213
297
  }
214
298
  const useLogOut = createCallbackHook(logout);
215
299
  function LoginError(t0) {
216
- const $ = c(23), {
300
+ const $ = c(17), {
217
301
  error,
218
302
  resetErrorBoundary
219
303
  } = t0;
@@ -241,21 +325,12 @@ function LoginError(t0) {
241
325
  $[7] !== authState || $[8] !== error || $[9] !== handleRetry ? (t3 = [authState, handleRetry, error], $[7] = authState, $[8] = error, $[9] = handleRetry, $[10] = t3) : t3 = $[10], useEffect(t2, t3);
242
326
  const t4 = error instanceof AuthError ? "Authentication Error" : "Configuration Error";
243
327
  let t5;
244
- $[11] !== t4 ? (t5 = /* @__PURE__ */ jsx("h2", { className: "sc-login-error__title", children: t4 }), $[11] = t4, $[12] = t5) : t5 = $[12];
328
+ $[11] !== handleRetry ? (t5 = {
329
+ text: "Retry",
330
+ onClick: handleRetry
331
+ }, $[11] = handleRetry, $[12] = t5) : t5 = $[12];
245
332
  let t6;
246
- $[13] !== authErrorMessage ? (t6 = /* @__PURE__ */ jsx("p", { className: "sc-login-error__description", children: authErrorMessage }), $[13] = authErrorMessage, $[14] = t6) : t6 = $[14];
247
- let t7;
248
- $[15] !== t5 || $[16] !== t6 ? (t7 = /* @__PURE__ */ jsxs("div", { className: "sc-login-error__content", children: [
249
- t5,
250
- t6
251
- ] }), $[15] = t5, $[16] = t6, $[17] = t7) : t7 = $[17];
252
- let t8;
253
- $[18] !== handleRetry ? (t8 = /* @__PURE__ */ jsx("button", { className: "sc-login-error__button", onClick: handleRetry, children: "Retry" }), $[18] = handleRetry, $[19] = t8) : t8 = $[19];
254
- let t9;
255
- return $[20] !== t7 || $[21] !== t8 ? (t9 = /* @__PURE__ */ jsxs("div", { className: "sc-login-error", children: [
256
- t7,
257
- t8
258
- ] }), $[20] = t7, $[21] = t8, $[22] = t9) : t9 = $[22], t9;
333
+ return $[13] !== authErrorMessage || $[14] !== t4 || $[15] !== t5 ? (t6 = /* @__PURE__ */ jsx(Error$1, { heading: t4, description: authErrorMessage, cta: t5 }), $[13] = authErrorMessage, $[14] = t4, $[15] = t5, $[16] = t6) : t6 = $[16], t6;
259
334
  }
260
335
  if (isInIframe() && !document.querySelector("[data-sanity-core]")) {
261
336
  const parsedUrl = new URL(window.location.href), mode = new URLSearchParams(parsedUrl.hash.slice(1)).get("mode"), script = document.createElement("script");
@@ -271,7 +346,7 @@ function AuthBoundary(t0) {
271
346
  const LoginErrorComponent = t1 === void 0 ? LoginError : t1;
272
347
  let t2, t3;
273
348
  $[3] !== LoginErrorComponent ? (t3 = function(fallbackProps) {
274
- return /* @__PURE__ */ jsx(LoginErrorComponent, { ...fallbackProps });
349
+ return fallbackProps.error instanceof CorsOriginError ? /* @__PURE__ */ jsx(CorsErrorComponent, { ...fallbackProps, projectId: getCorsErrorProjectId(fallbackProps.error) }) : /* @__PURE__ */ jsx(LoginErrorComponent, { ...fallbackProps });
275
350
  }, $[3] = LoginErrorComponent, $[4] = t3) : t3 = $[4], t2 = t3;
276
351
  const FallbackComponent = t2;
277
352
  let t4;
@@ -280,7 +355,7 @@ function AuthBoundary(t0) {
280
355
  return $[7] !== FallbackComponent || $[8] !== t4 ? (t5 = /* @__PURE__ */ jsx(ComlinkTokenRefreshProvider, { children: /* @__PURE__ */ jsx(ErrorBoundary, { FallbackComponent, children: t4 }) }), $[7] = FallbackComponent, $[8] = t4, $[9] = t5) : t5 = $[9], t5;
281
356
  }
282
357
  function AuthSwitch(t0) {
283
- const $ = c(13);
358
+ const $ = c(14);
284
359
  let children, projectIds, props, t1, t2;
285
360
  $[0] !== t0 ? ({
286
361
  CallbackComponent: t1,
@@ -289,11 +364,11 @@ function AuthSwitch(t0) {
289
364
  projectIds,
290
365
  ...props
291
366
  } = t0, $[0] = t0, $[1] = children, $[2] = projectIds, $[3] = props, $[4] = t1, $[5] = t2) : (children = $[1], projectIds = $[2], props = $[3], t1 = $[4], t2 = $[5]);
292
- const CallbackComponent = t1 === void 0 ? LoginCallback : t1, verifyOrganization = t2 === void 0 ? !0 : t2, authState = useAuthState(), orgError = useVerifyOrgProjects(!verifyOrganization, projectIds), isLoggedOut = authState.type === AuthStateType.LOGGED_OUT && !authState.isDestroyingSession, loginUrl = useLoginUrl();
367
+ const CallbackComponent = t1 === void 0 ? LoginCallback : t1, verifyOrganization = t2 === void 0 ? !0 : t2, authState = useAuthState(), studioModeEnabled = useSanityInstance().config.studioMode?.enabled, disableVerifyOrg = !verifyOrganization || studioModeEnabled || authState.type !== AuthStateType.LOGGED_IN, orgError = useVerifyOrgProjects(disableVerifyOrg, projectIds), isLoggedOut = authState.type === AuthStateType.LOGGED_OUT && !authState.isDestroyingSession, loginUrl = useLoginUrl();
293
368
  let t3, t4;
294
- if ($[6] !== isLoggedOut || $[7] !== loginUrl ? (t3 = () => {
295
- isLoggedOut && !isInIframe() && (window.location.href = loginUrl);
296
- }, t4 = [isLoggedOut, loginUrl], $[6] = isLoggedOut, $[7] = loginUrl, $[8] = t3, $[9] = t4) : (t3 = $[8], t4 = $[9]), useEffect(t3, t4), verifyOrganization && orgError)
369
+ if ($[6] !== isLoggedOut || $[7] !== loginUrl || $[8] !== studioModeEnabled ? (t3 = () => {
370
+ isLoggedOut && !isInIframe() && !studioModeEnabled && (window.location.href = loginUrl);
371
+ }, t4 = [isLoggedOut, loginUrl, studioModeEnabled], $[6] = isLoggedOut, $[7] = loginUrl, $[8] = studioModeEnabled, $[9] = t3, $[10] = t4) : (t3 = $[9], t4 = $[10]), useEffect(t3, t4), verifyOrganization && orgError)
297
372
  throw new ConfigurationError({
298
373
  message: orgError
299
374
  });
@@ -302,7 +377,7 @@ function AuthSwitch(t0) {
302
377
  throw new AuthError(authState.error);
303
378
  case AuthStateType.LOGGING_IN: {
304
379
  let t5;
305
- return $[10] !== CallbackComponent || $[11] !== props ? (t5 = /* @__PURE__ */ jsx(CallbackComponent, { ...props }), $[10] = CallbackComponent, $[11] = props, $[12] = t5) : t5 = $[12], t5;
380
+ return $[11] !== CallbackComponent || $[12] !== props ? (t5 = /* @__PURE__ */ jsx(CallbackComponent, { ...props }), $[11] = CallbackComponent, $[12] = props, $[13] = t5) : t5 = $[13], t5;
306
381
  }
307
382
  case AuthStateType.LOGGED_IN:
308
383
  return children;
@@ -1158,7 +1233,7 @@ function useUsers(options) {
1158
1233
  loadMore
1159
1234
  };
1160
1235
  }
1161
- var version = "2.2.0";
1236
+ var version = "2.3.1";
1162
1237
  function getEnv(key) {
1163
1238
  if (typeof import.meta < "u" && import.meta.env)
1164
1239
  return import.meta.env[key];