@sanity/assist 4.3.1 → 4.4.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
@@ -204,6 +204,9 @@ function getPathKey(path) {
204
204
  function getInstructionTitle(instruction2) {
205
205
  return instruction2?.title ?? "Untitled";
206
206
  }
207
+ function isDefined(t) {
208
+ return t != null;
209
+ }
207
210
  function isPortableTextArray(type) {
208
211
  return type.of.find((t) => isType(t, "block"));
209
212
  }
@@ -390,6 +393,12 @@ function getTypeIcon(schemaType) {
390
393
  }
391
394
  return isType(schemaType, "slug") ? icons.LinkIcon : isType(schemaType, "image") ? icons.ImageIcon : schemaType.jsonType === "array" && isPortableTextArray(schemaType) ? icons.BlockContentIcon : schemaType.jsonType === "array" ? icons.OlistIcon : schemaType.jsonType === "object" ? icons.BlockquoteIcon : schemaType.jsonType === "string" ? icons.StringIcon : icons.DocumentIcon;
392
395
  }
396
+ function asFieldRefsByTypePath(fieldRefs) {
397
+ return fieldRefs.reduce(
398
+ (acc, ref) => ({ ...acc, [ref.key]: ref }),
399
+ {}
400
+ );
401
+ }
393
402
  function getFieldRefsWithDocument(schemaType) {
394
403
  const fields = getFieldRefs(schemaType);
395
404
  return [
@@ -1019,7 +1028,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1019
1028
  } = structure.useDocumentPane(), { draft, published, version } = editState || {}, assistableDocumentId = selectedReleaseId ? sanity.getVersionId(documentId, selectedReleaseId) : documentSchemaType.liveEdit ? documentId : sanity.getDraftId(documentId), documentIsNew = selectedReleaseId ? !version?._id : !draft?._id && !published?._id, documentIsAssistable = selectedReleaseId ? !!version : isDocAssistable(documentSchemaType, published, draft), { params } = useAiPaneRouter(), selectedPath = params[fieldPathParam], assistDocument = useStudioAssistDocument({
1020
1029
  documentId: assistableDocumentId,
1021
1030
  schemaType: documentSchemaType
1022
- }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId);
1031
+ }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId), fieldRefs = getFieldRefs(documentSchemaType), fieldRefsByTypePath = asFieldRefsByTypePath(fieldRefs);
1023
1032
  return react.useMemo(() => {
1024
1033
  const base = {
1025
1034
  assistableDocumentId,
@@ -1033,7 +1042,9 @@ function useAssistDocumentContextValue(documentId, documentType) {
1033
1042
  selectedPath,
1034
1043
  syntheticTasks,
1035
1044
  addSyntheticTask,
1036
- removeSyntheticTask
1045
+ removeSyntheticTask,
1046
+ fieldRefs,
1047
+ fieldRefsByTypePath
1037
1048
  };
1038
1049
  return assistDocument ? {
1039
1050
  ...base,
@@ -2863,12 +2874,12 @@ function useCustomFieldActions(props) {
2863
2874
  path: props.path
2864
2875
  });
2865
2876
  return react.useMemo(() => {
2866
- const title = fieldActions?.title, customActions = configActions?.map((node) => createSafeNode({
2877
+ const title = fieldActions?.title, customActions = configActions?.filter(isDefined).map((node) => createSafeNode({
2867
2878
  node,
2868
2879
  pushToast,
2869
2880
  addSyntheticTask,
2870
2881
  removeSyntheticTask
2871
- })), onlyGroups = customActions?.length && customActions?.every((node) => node.type === "group");
2882
+ })).filter(isDefined), onlyGroups = customActions?.length && customActions?.every((node) => node.type === "group");
2872
2883
  return (customActions?.length ? onlyGroups ? customActions : [
2873
2884
  {
2874
2885
  type: "group",
@@ -2885,12 +2896,13 @@ function createSafeNode(args) {
2885
2896
  case "action":
2886
2897
  return createSafeAction({ ...args, action: node });
2887
2898
  case "group":
2888
- return {
2899
+ const children = node.children?.filter(isDefined).map((child) => createSafeNode({ ...args, node: child })).filter(isDefined);
2900
+ return children?.length ? {
2889
2901
  ...node,
2890
2902
  renderAsButton: !1,
2891
2903
  expanded: !0,
2892
- children: node.children?.map((child) => createSafeNode({ ...args, node: child }))
2893
- };
2904
+ children
2905
+ } : void 0;
2894
2906
  case "divider":
2895
2907
  default:
2896
2908
  return node;
@@ -2948,7 +2960,8 @@ const assistFieldActions = {
2948
2960
  documentOnChange,
2949
2961
  documentSchemaType,
2950
2962
  selectedPath,
2951
- assistableDocumentId
2963
+ assistableDocumentId,
2964
+ fieldRefsByTypePath
2952
2965
  } = useAssistDocumentContext(), { value: docValue, formState } = structure.useDocumentPane(), docValueRef = react.useRef(docValue), formStateRef = react.useRef(formState);
2953
2966
  formStateRef.current = formState;
2954
2967
  const currentUser = sanity.useCurrentUser(), isHidden = !assistDocument, pathKey = usePathKey(props.path), typePath = useTypePath(docValue, pathKey), assistDocumentId2 = assistDocument?._id, { requestRunInstruction } = useRequestRunInstruction({
@@ -3036,14 +3049,22 @@ const assistFieldActions = {
3036
3049
  path
3037
3050
  };
3038
3051
  }
3039
- ), []), customActions = useCustomFieldActions({
3052
+ ), []), parentSchemaType = react.useMemo(() => {
3053
+ if (props.path.length) {
3054
+ if (props.path.length === 1)
3055
+ return documentSchemaType;
3056
+ } else return;
3057
+ const parentPath = props.path.slice(0, -1), typePath2 = getTypePath(docValueRef.current, sanity.pathToString(parentPath));
3058
+ return typePath2 ? fieldRefsByTypePath[typePath2]?.schemaType : void 0;
3059
+ }, [fieldRefsByTypePath, props.path, documentSchemaType]), customActions = useCustomFieldActions({
3040
3060
  actionType: props.path.length ? "field" : "document",
3041
3061
  documentIdForAction: assistableDocumentId,
3042
3062
  schemaType,
3043
3063
  documentSchemaType,
3044
3064
  path: props.path,
3045
3065
  getDocumentValue,
3046
- getConditionalPaths
3066
+ getConditionalPaths,
3067
+ parentSchemaType
3047
3068
  }), manageInstructionsItem = react.useMemo(
3048
3069
  () => ({
3049
3070
  type: "action",