@sanity/sdk-react 2.6.0 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
2
  import { c } from "react-compiler-runtime";
3
3
  import { ClientError, CorsOriginError } from "@sanity/client";
4
- import { getAuthState, getNodeState, getIsInDashboardState, setAuthToken, AuthStateType, getLoginUrlState, observeOrganizationVerificationState, handleAuthCallback, logout, isProjectUserNotFoundClientError, getClientErrorApiDescription, getClientErrorApiBody, getCorsErrorProjectId, createSanityInstance, agentGenerate, agentTransform, agentTranslate, agentPrompt, agentPatch, getTokenState, getCurrentUserState, getDashboardOrganizationId, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, isDatasetSource, isMediaLibrarySource, isCanvasSource, 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
+ import { getAuthState, getNodeState, getIsInDashboardState, isStudioConfig, setAuthToken, AuthStateType, getLoginUrlState, observeOrganizationVerificationState, handleAuthCallback, logout, isProjectUserNotFoundClientError, getClientErrorApiDescription, getClientErrorApiBody, getCorsErrorProjectId, createSanityInstance, agentGenerate, agentTransform, agentTranslate, agentPrompt, agentPatch, getTokenState, getCurrentUserState, getDashboardOrganizationId, getClientState, getOrCreateController, getOrCreateChannel, releaseChannel, isDatasetSource, isMediaLibrarySource, isCanvasSource, 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";
5
5
  export * from "@sanity/sdk";
6
6
  import { createContext, useContext, useSyncExternalStore, useRef, useEffect, useState, Suspense, StrictMode, useCallback, useMemo, useInsertionEffect, useTransition } from "react";
7
7
  import { ErrorBoundary } from "react-error-boundary";
@@ -144,8 +144,8 @@ const ComlinkTokenRefreshProvider = (t0) => {
144
144
  } = t0, instance = useSanityInstance();
145
145
  let t1;
146
146
  t1 = getIsInDashboardState(instance).getCurrent();
147
- const isInDashboard = t1, studioModeEnabled = !!instance.config.studioMode?.enabled;
148
- if (isInDashboard && !studioModeEnabled) {
147
+ const isInDashboard = t1, isStudio = isStudioConfig(instance.config);
148
+ if (isInDashboard && !isStudio) {
149
149
  let t2;
150
150
  return $[0] !== children ? (t2 = /* @__PURE__ */ jsx(DashboardTokenRefresh, { children }), $[0] = children, $[1] = t2) : t2 = $[1], t2;
151
151
  }
@@ -360,7 +360,7 @@ function AuthBoundary(t0) {
360
360
  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;
361
361
  }
362
362
  function AuthSwitch(t0) {
363
- const $ = c(14);
363
+ const $ = c(16);
364
364
  let children, projectIds, props, t1, t2;
365
365
  $[0] !== t0 ? ({
366
366
  CallbackComponent: t1,
@@ -369,11 +369,14 @@ function AuthSwitch(t0) {
369
369
  projectIds,
370
370
  ...props
371
371
  } = t0, $[0] = t0, $[1] = children, $[2] = projectIds, $[3] = props, $[4] = t1, $[5] = t2) : (children = $[1], projectIds = $[2], props = $[3], t1 = $[4], t2 = $[5]);
372
- 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();
373
- let t3, t4;
374
- if ($[6] !== isLoggedOut || $[7] !== loginUrl || $[8] !== studioModeEnabled ? (t3 = () => {
375
- isLoggedOut && !isInIframe() && !studioModeEnabled && (window.location.href = loginUrl);
376
- }, t4 = [isLoggedOut, loginUrl, studioModeEnabled], $[6] = isLoggedOut, $[7] = loginUrl, $[8] = studioModeEnabled, $[9] = t3, $[10] = t4) : (t3 = $[9], t4 = $[10]), useEffect(t3, t4), verifyOrganization && orgError)
372
+ const CallbackComponent = t1 === void 0 ? LoginCallback : t1, verifyOrganization = t2 === void 0 ? !0 : t2, authState = useAuthState(), instance = useSanityInstance();
373
+ let t3;
374
+ $[6] !== instance.config ? (t3 = isStudioConfig(instance.config), $[6] = instance.config, $[7] = t3) : t3 = $[7];
375
+ const isStudio = t3, disableVerifyOrg = !verifyOrganization || isStudio || authState.type !== AuthStateType.LOGGED_IN, orgError = useVerifyOrgProjects(disableVerifyOrg, projectIds), isLoggedOut = authState.type === AuthStateType.LOGGED_OUT && !authState.isDestroyingSession, loginUrl = useLoginUrl();
376
+ let t4, t5;
377
+ if ($[8] !== isLoggedOut || $[9] !== isStudio || $[10] !== loginUrl ? (t4 = () => {
378
+ isLoggedOut && !isInIframe() && !isStudio && (window.location.href = loginUrl);
379
+ }, t5 = [isLoggedOut, loginUrl, isStudio], $[8] = isLoggedOut, $[9] = isStudio, $[10] = loginUrl, $[11] = t4, $[12] = t5) : (t4 = $[11], t5 = $[12]), useEffect(t4, t5), verifyOrganization && orgError)
377
380
  throw new ConfigurationError({
378
381
  message: orgError
379
382
  });
@@ -381,8 +384,8 @@ function AuthSwitch(t0) {
381
384
  case AuthStateType.ERROR:
382
385
  throw new AuthError(authState.error);
383
386
  case AuthStateType.LOGGING_IN: {
384
- let t5;
385
- return $[11] !== CallbackComponent || $[12] !== props ? (t5 = /* @__PURE__ */ jsx(CallbackComponent, { ...props }), $[11] = CallbackComponent, $[12] = props, $[13] = t5) : t5 = $[13], t5;
387
+ let t6;
388
+ return $[13] !== CallbackComponent || $[14] !== props ? (t6 = /* @__PURE__ */ jsx(CallbackComponent, { ...props }), $[13] = CallbackComponent, $[14] = props, $[15] = t6) : t6 = $[15], t6;
386
389
  }
387
390
  case AuthStateType.LOGGED_IN:
388
391
  return children;
@@ -392,6 +395,8 @@ function AuthSwitch(t0) {
392
395
  throw new Error(`Invalid auth state: ${authState.type}`);
393
396
  }
394
397
  }
398
+ const SDKStudioContext = createContext(null);
399
+ SDKStudioContext.displayName = "SDKStudioContext";
395
400
  const DEFAULT_FALLBACK = /* @__PURE__ */ jsx(Fragment, { children: "Warning: No fallback provided. Please supply a fallback prop to ensure proper Suspense handling." });
396
401
  function ResourceProvider(t0) {
397
402
  const $ = c(16);
@@ -453,26 +458,50 @@ function _temp$5(c2) {
453
458
  return c2.projectId;
454
459
  }
455
460
  const REDIRECT_URL = "https://sanity.io/welcome";
461
+ function deriveConfigFromWorkspace(workspace) {
462
+ return {
463
+ projectId: workspace.projectId,
464
+ dataset: workspace.dataset,
465
+ studio: {
466
+ auth: workspace.auth.token ? {
467
+ token: workspace.auth.token
468
+ } : void 0
469
+ }
470
+ };
471
+ }
456
472
  function SanityApp(t0) {
457
- const $ = c(15);
458
- let children, fallback, props, t1;
473
+ const $ = c(18);
474
+ let children, configProp, fallback, props;
459
475
  $[0] !== t0 ? ({
460
476
  children,
461
477
  fallback,
462
- config: t1,
478
+ config: configProp,
463
479
  ...props
464
- } = t0, $[0] = t0, $[1] = children, $[2] = fallback, $[3] = props, $[4] = t1) : (children = $[1], fallback = $[2], props = $[3], t1 = $[4]);
465
- let t2;
466
- $[5] !== t1 ? (t2 = t1 === void 0 ? [] : t1, $[5] = t1, $[6] = t2) : t2 = $[6];
467
- const config = t2;
468
- let t3, t4;
469
- $[7] !== config ? (t3 = () => {
480
+ } = t0, $[0] = t0, $[1] = children, $[2] = configProp, $[3] = fallback, $[4] = props) : (children = $[1], configProp = $[2], fallback = $[3], props = $[4]);
481
+ const studioWorkspace = useContext(SDKStudioContext);
482
+ let t1;
483
+ bb0: {
484
+ if (configProp) {
485
+ t1 = configProp;
486
+ break bb0;
487
+ }
488
+ if (studioWorkspace) {
489
+ let t23;
490
+ $[5] !== studioWorkspace ? (t23 = deriveConfigFromWorkspace(studioWorkspace), $[5] = studioWorkspace, $[6] = t23) : t23 = $[6], t1 = t23;
491
+ break bb0;
492
+ }
493
+ let t22;
494
+ $[7] === Symbol.for("react.memo_cache_sentinel") ? (t22 = [], $[7] = t22) : t22 = $[7], t1 = t22;
495
+ }
496
+ const resolvedConfig = t1;
497
+ let t2, t3;
498
+ $[8] !== configProp || $[9] !== resolvedConfig || $[10] !== studioWorkspace ? (t2 = () => {
470
499
  let timeout;
471
- const primaryConfig = Array.isArray(config) ? config[0] : config;
472
- return !isInIframe() && !isLocalUrl(window) && !primaryConfig?.studioMode?.enabled && (timeout = setTimeout(_temp$4, 1e3)), () => clearTimeout(timeout);
473
- }, t4 = [config], $[7] = config, $[8] = t3, $[9] = t4) : (t3 = $[8], t4 = $[9]), useEffect(t3, t4);
474
- let t5;
475
- return $[10] !== children || $[11] !== config || $[12] !== fallback || $[13] !== props ? (t5 = /* @__PURE__ */ jsx(SDKProvider, { ...props, fallback, config, children }), $[10] = children, $[11] = config, $[12] = fallback, $[13] = props, $[14] = t5) : t5 = $[14], t5;
500
+ const primaryConfig = Array.isArray(resolvedConfig) ? resolvedConfig[0] : resolvedConfig, shouldRedirectWithoutConfig = configProp === void 0 && !studioWorkspace && !primaryConfig;
501
+ return !isInIframe() && !isLocalUrl(window) && (shouldRedirectWithoutConfig || primaryConfig && !isStudioConfig(primaryConfig)) && (timeout = setTimeout(_temp$4, 1e3)), () => clearTimeout(timeout);
502
+ }, t3 = [configProp, resolvedConfig, studioWorkspace], $[8] = configProp, $[9] = resolvedConfig, $[10] = studioWorkspace, $[11] = t2, $[12] = t3) : (t2 = $[11], t3 = $[12]), useEffect(t2, t3);
503
+ let t4;
504
+ return $[13] !== children || $[14] !== fallback || $[15] !== props || $[16] !== resolvedConfig ? (t4 = /* @__PURE__ */ jsx(SDKProvider, { ...props, fallback, config: resolvedConfig, children }), $[13] = children, $[14] = fallback, $[15] = props, $[16] = resolvedConfig, $[17] = t4) : t4 = $[17], t4;
476
505
  }
477
506
  function _temp$4() {
478
507
  console.warn("Redirecting to core", REDIRECT_URL), window.location.replace(REDIRECT_URL);
@@ -620,21 +649,31 @@ function useDashboardNavigate(navigateFn) {
620
649
  }
621
650
  }, $[0] = navigateFn, $[1] = t0) : t0 = $[1], useWindowConnection(t0);
622
651
  }
623
- function useSource(options) {
652
+ function useNormalizedSourceOptions(options) {
653
+ const $ = c(6);
654
+ let rest, sourceName;
655
+ if ($[0] !== options ? ({
656
+ sourceName,
657
+ ...rest
658
+ } = options, $[0] = options, $[1] = rest, $[2] = sourceName) : (rest = $[1], sourceName = $[2]), sourceName && Object.hasOwn(options, "source"))
659
+ throw new Error(`Source name ${JSON.stringify(sourceName)} and source ${JSON.stringify(options.source)} cannot be used together.`);
624
660
  const sources = useContext(SourcesContext);
625
- if (!(!options.sourceName && !options.source)) {
626
- if (options.source)
627
- return options.source;
628
- if (options.sourceName && !Object.hasOwn(sources, options.sourceName))
629
- throw new Error(`There's no source named ${JSON.stringify(options.sourceName)} in context. Please use <SourceProvider>.`);
630
- return options.sourceName ? sources[options.sourceName] : void 0;
631
- }
661
+ let resolvedSource;
662
+ if (options.source && (resolvedSource = options.source), sourceName && !Object.hasOwn(sources, sourceName))
663
+ throw new Error(`There's no source named ${JSON.stringify(sourceName)} in context. Please use <SourceProvider>.`);
664
+ sourceName && sources[sourceName] && (resolvedSource = sources[sourceName]);
665
+ let t0;
666
+ return $[3] !== resolvedSource || $[4] !== rest ? (t0 = {
667
+ ...rest,
668
+ source: resolvedSource
669
+ }, $[3] = resolvedSource, $[4] = rest, $[5] = t0) : t0 = $[5], t0;
632
670
  }
633
671
  function useResourceIdFromDocumentHandle(documentHandle) {
634
- const $ = c(3), source = useSource(documentHandle), {
672
+ const $ = c(3), options = useNormalizedSourceOptions(documentHandle), {
635
673
  projectId,
636
- dataset
637
- } = documentHandle;
674
+ dataset,
675
+ source
676
+ } = options;
638
677
  let resourceId = "", resourceType;
639
678
  projectId && dataset && (resourceId = `${projectId}.${dataset}`), source && (isDatasetSource(source) ? (resourceId = `${source.projectId}.${source.dataset}`, resourceType = void 0) : isMediaLibrarySource(source) ? (resourceId = source.mediaLibraryId, resourceType = "media-library") : isCanvasSource(source) && (resourceId = source.canvasId, resourceType = "canvas"));
640
679
  let t0;
@@ -660,13 +699,8 @@ function useDispatchIntent(params) {
660
699
  try {
661
700
  if (!action && !intentId)
662
701
  throw new Error("useDispatchIntent: Either `action` or `intentId` must be provided.");
663
- const {
664
- projectId,
665
- dataset,
666
- sourceName
667
- } = documentHandle;
668
- if (action && intentId && console.warn("useDispatchIntent: Both `action` and `intentId` were provided. Using `intentId` and ignoring `action`."), !sourceName && (!projectId || !dataset))
669
- throw new Error("useDispatchIntent: Either `sourceName` or both `projectId` and `dataset` must be provided in documentHandle.");
702
+ if (action && intentId && console.warn("useDispatchIntent: Both `action` and `intentId` were provided. Using `intentId` and ignoring `action`."), !resource.id)
703
+ throw new Error("useDispatchIntent: Unable to determine resource. Either `source`, `sourceName`, or both `projectId` and `dataset` must be provided in documentHandle.");
670
704
  const message = {
671
705
  type: "dashboard/v1/events/intents/dispatch-intent",
672
706
  data: {
@@ -1076,7 +1110,7 @@ function _temp$1(key) {
1076
1110
  return !ignoredKeys.includes(key);
1077
1111
  }
1078
1112
  function useQuery(options) {
1079
- const instance = useSanityInstance(options), source = useSource(options), [isPending, startTransition] = useTransition(), queryKey = getQueryKey(options), [deferredQueryKey, setDeferredQueryKey] = useState(queryKey), ref = useRef(new AbortController());
1113
+ const instance = useSanityInstance(options), normalized = useNormalizedSourceOptions(options), [isPending, startTransition] = useTransition(), queryKey = getQueryKey(normalized), [deferredQueryKey, setDeferredQueryKey] = useState(queryKey), ref = useRef(new AbortController());
1080
1114
  useEffect(() => {
1081
1115
  queryKey !== deferredQueryKey && startTransition(() => {
1082
1116
  ref && !ref.current.signal.aborted && (ref.current.abort(), ref.current = new AbortController()), setDeferredQueryKey(queryKey);
@@ -1087,16 +1121,12 @@ function useQuery(options) {
1087
1121
  subscribe
1088
1122
  } = useMemo(() => {
1089
1123
  const deferred = parseQueryKey(deferredQueryKey);
1090
- return getQueryState(instance, {
1091
- ...deferred,
1092
- source
1093
- });
1094
- }, [instance, deferredQueryKey, source]);
1124
+ return getQueryState(instance, deferred);
1125
+ }, [instance, deferredQueryKey]);
1095
1126
  if (getCurrent() === void 0) {
1096
1127
  const currentSignal = ref.current.signal, deferred_0 = parseQueryKey(deferredQueryKey);
1097
1128
  throw resolveQuery(instance, {
1098
1129
  ...deferred_0,
1099
- source,
1100
1130
  signal: currentSignal
1101
1131
  });
1102
1132
  }
@@ -1349,13 +1379,13 @@ function useDocumentProjection({
1349
1379
  projection,
1350
1380
  ...docHandle
1351
1381
  }) {
1352
- const instance = useSanityInstance(docHandle), normalizedProjection = useMemo(() => projection.trim(), [projection]), stateSource = useMemo(() => getProjectionState(instance, {
1353
- ...docHandle,
1382
+ const instance = useSanityInstance(docHandle), normalizedProjection = useMemo(() => projection.trim(), [projection]), normalizedDocHandle = useNormalizedSourceOptions(docHandle), stateSource = useMemo(() => getProjectionState(instance, {
1383
+ ...normalizedDocHandle,
1354
1384
  projection: normalizedProjection
1355
- }), [instance, normalizedProjection, docHandle]);
1385
+ }), [instance, normalizedDocHandle, normalizedProjection]);
1356
1386
  if (stateSource.getCurrent()?.data === null)
1357
1387
  throw resolveProjection(instance, {
1358
- ...docHandle,
1388
+ ...normalizedDocHandle,
1359
1389
  projection: normalizedProjection
1360
1390
  });
1361
1391
  const subscribe = useCallback((onStoreChanged) => {
@@ -1445,7 +1475,7 @@ function useUsers(options) {
1445
1475
  loadMore
1446
1476
  };
1447
1477
  }
1448
- var version = "2.6.0";
1478
+ var version = "2.7.0";
1449
1479
  function getEnv(key) {
1450
1480
  if (typeof import.meta < "u" && import.meta.env)
1451
1481
  return import.meta.env[key];
@@ -1461,6 +1491,7 @@ export {
1461
1491
  REACT_SDK_VERSION,
1462
1492
  ResourceProvider,
1463
1493
  SDKProvider,
1494
+ SDKStudioContext,
1464
1495
  SanityApp,
1465
1496
  renderSanityApp,
1466
1497
  useActiveReleases,