@sanity/sdk-react 2.4.0 → 2.5.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
@@ -461,7 +461,53 @@ const useAgentPrompt = createCallbackHook(promptAdapter);
461
461
  function patchAdapter(instance, options) {
462
462
  return firstValueFrom(agentPatch(instance, options));
463
463
  }
464
- const useAgentPatch = createCallbackHook(patchAdapter), useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState);
464
+ const useAgentPatch = createCallbackHook(patchAdapter);
465
+ function useAgentResourceContext(options) {
466
+ const $ = c(9), {
467
+ projectId,
468
+ dataset,
469
+ documentId
470
+ } = options;
471
+ let t0;
472
+ $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = {
473
+ name: SDK_NODE_NAME,
474
+ connectTo: SDK_CHANNEL_NAME
475
+ }, $[0] = t0) : t0 = $[0];
476
+ const {
477
+ sendMessage
478
+ } = useWindowConnection(t0), lastContextRef = useRef(null);
479
+ let t1;
480
+ $[1] !== dataset || $[2] !== documentId || $[3] !== projectId || $[4] !== sendMessage ? (t1 = () => {
481
+ if (!projectId || !dataset) {
482
+ console.warn("[useAgentResourceContext] projectId and dataset are required", {
483
+ projectId,
484
+ dataset
485
+ });
486
+ return;
487
+ }
488
+ const contextKey = `${projectId}:${dataset}:${documentId || ""}`;
489
+ if (lastContextRef.current !== contextKey)
490
+ try {
491
+ const message = {
492
+ type: "dashboard/v1/events/agent/resource/update",
493
+ data: {
494
+ projectId,
495
+ dataset,
496
+ documentId
497
+ }
498
+ };
499
+ sendMessage(message.type, message.data), lastContextRef.current = contextKey;
500
+ } catch (t22) {
501
+ console.error("[useAgentResourceContext] Failed to update context:", t22);
502
+ }
503
+ }, $[1] = dataset, $[2] = documentId, $[3] = projectId, $[4] = sendMessage, $[5] = t1) : t1 = $[5];
504
+ const updateContext = t1;
505
+ let t2, t3;
506
+ $[6] !== updateContext ? (t2 = () => {
507
+ updateContext();
508
+ }, t3 = [updateContext], $[6] = updateContext, $[7] = t2, $[8] = t3) : (t2 = $[7], t3 = $[8]), useEffect(t2, t3);
509
+ }
510
+ const useAuthToken = createStateSourceHook(getTokenState), useCurrentUser = createStateSourceHook(getCurrentUserState);
465
511
  function useDashboardOrganizationId() {
466
512
  const $ = c(2), instance = useSanityInstance();
467
513
  let t0, t1;
@@ -832,7 +878,41 @@ const useDatasets = createStateSourceHook({
832
878
  ),
833
879
  suspender: resolveDatasets,
834
880
  getConfig: identity
835
- }), useApplyDocumentActions = createCallbackHook(applyDocumentActions), useDocumentValue = createStateSourceHook({
881
+ }), useApplyDocumentActions = () => {
882
+ const $ = c(2), instance = useSanityInstance();
883
+ let t0;
884
+ return $[0] !== instance ? (t0 = (actionOrActions, options) => {
885
+ const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions];
886
+ let projectId, dataset;
887
+ for (const action of actions)
888
+ if (action.projectId) {
889
+ if (projectId || (projectId = action.projectId), action.projectId !== projectId)
890
+ throw new Error(`Mismatched project IDs found in actions. All actions must belong to the same project. Found "${action.projectId}" but expected "${projectId}".`);
891
+ if (action.dataset && (dataset || (dataset = action.dataset), action.dataset !== dataset))
892
+ throw new Error(`Mismatched datasets found in actions. All actions must belong to the same dataset. Found "${action.dataset}" but expected "${dataset}".`);
893
+ }
894
+ if (projectId || dataset) {
895
+ const actualInstance = instance.match({
896
+ projectId,
897
+ dataset
898
+ });
899
+ if (!actualInstance)
900
+ throw new Error(`Could not find a matching Sanity instance for the requested action: ${JSON.stringify({
901
+ projectId,
902
+ dataset
903
+ }, null, 2)}.
904
+ Please ensure there is a ResourceProvider component with a matching configuration in the component hierarchy.`);
905
+ return applyDocumentActions(actualInstance, {
906
+ actions,
907
+ ...options
908
+ });
909
+ }
910
+ return applyDocumentActions(instance, {
911
+ actions,
912
+ ...options
913
+ });
914
+ }, $[0] = instance, $[1] = t0) : t0 = $[1], t0;
915
+ }, useDocumentValue = createStateSourceHook({
836
916
  // Pass options directly to getDocumentState
837
917
  getState: (instance, options) => getDocumentState(instance, options),
838
918
  // Pass options directly to getDocumentState for checking current value
@@ -871,8 +951,8 @@ function useDocumentEvent(options) {
871
951
  }
872
952
  function useDocumentPermissions(actionOrActions) {
873
953
  const $ = c(13);
874
- let t0;
875
- $[0] !== actionOrActions ? (t0 = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions], $[0] = actionOrActions, $[1] = t0) : t0 = $[1];
954
+ let t0, t1;
955
+ $[0] !== actionOrActions ? (t1 = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions], $[0] = actionOrActions, $[1] = t1) : t1 = $[1], t0 = t1;
876
956
  const actions = t0;
877
957
  let projectId, dataset;
878
958
  if ($[2] !== actions || $[3] !== dataset || $[4] !== projectId) {
@@ -886,20 +966,26 @@ function useDocumentPermissions(actionOrActions) {
886
966
  $[2] = actions, $[3] = dataset, $[4] = projectId, $[5] = projectId, $[6] = dataset;
887
967
  } else
888
968
  projectId = $[5], dataset = $[6];
889
- let t1;
890
- $[7] !== dataset || $[8] !== projectId ? (t1 = {
969
+ let t2;
970
+ $[7] !== dataset || $[8] !== projectId ? (t2 = {
891
971
  projectId,
892
972
  dataset
893
- }, $[7] = dataset, $[8] = projectId, $[9] = t1) : t1 = $[9];
894
- const instance = useSanityInstance(t1);
895
- if (getPermissionsState(instance, actionOrActions).getCurrent() === void 0)
896
- throw firstValueFrom(getPermissionsState(instance, actionOrActions).observable.pipe(filter(_temp$2)));
897
- let t2, t3;
898
- $[10] !== actionOrActions || $[11] !== instance ? (t3 = getPermissionsState(instance, actionOrActions), $[10] = actionOrActions, $[11] = instance, $[12] = t3) : t3 = $[12], t2 = t3;
973
+ }, $[7] = dataset, $[8] = projectId, $[9] = t2) : t2 = $[9];
974
+ const instance = useSanityInstance(t2);
975
+ if (getPermissionsState(instance, {
976
+ actions
977
+ }).getCurrent() === void 0)
978
+ throw firstValueFrom(getPermissionsState(instance, {
979
+ actions
980
+ }).observable.pipe(filter(_temp$2)));
981
+ let t3, t4;
982
+ $[10] !== actions || $[11] !== instance ? (t4 = getPermissionsState(instance, {
983
+ actions
984
+ }), $[10] = actions, $[11] = instance, $[12] = t4) : t4 = $[12], t3 = t4;
899
985
  const {
900
986
  subscribe,
901
987
  getCurrent
902
- } = t2;
988
+ } = t3;
903
989
  return useSyncExternalStore(subscribe, getCurrent);
904
990
  }
905
991
  function _temp$2(result) {
@@ -1320,7 +1406,7 @@ function useUsers(options) {
1320
1406
  loadMore
1321
1407
  };
1322
1408
  }
1323
- var version = "2.4.0";
1409
+ var version = "2.5.0";
1324
1410
  function getEnv(key) {
1325
1411
  if (typeof import.meta < "u" && import.meta.env)
1326
1412
  return import.meta.env[key];
@@ -1341,6 +1427,7 @@ export {
1341
1427
  useAgentGenerate,
1342
1428
  useAgentPatch,
1343
1429
  useAgentPrompt,
1430
+ useAgentResourceContext,
1344
1431
  useAgentTransform,
1345
1432
  useAgentTranslate,
1346
1433
  useApplyDocumentActions,