@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.mjs CHANGED
@@ -211,6 +211,9 @@ function getPathKey(path) {
211
211
  function getInstructionTitle(instruction2) {
212
212
  return instruction2?.title ?? "Untitled";
213
213
  }
214
+ function isDefined(t) {
215
+ return t != null;
216
+ }
214
217
  function isPortableTextArray(type) {
215
218
  return type.of.find((t) => isType(t, "block"));
216
219
  }
@@ -397,6 +400,12 @@ function getTypeIcon(schemaType) {
397
400
  }
398
401
  return isType(schemaType, "slug") ? LinkIcon : isType(schemaType, "image") ? ImageIcon : schemaType.jsonType === "array" && isPortableTextArray(schemaType) ? BlockContentIcon : schemaType.jsonType === "array" ? OlistIcon : schemaType.jsonType === "object" ? BlockquoteIcon : schemaType.jsonType === "string" ? StringIcon : DocumentIcon;
399
402
  }
403
+ function asFieldRefsByTypePath(fieldRefs) {
404
+ return fieldRefs.reduce(
405
+ (acc, ref) => ({ ...acc, [ref.key]: ref }),
406
+ {}
407
+ );
408
+ }
400
409
  function getFieldRefsWithDocument(schemaType) {
401
410
  const fields = getFieldRefs(schemaType);
402
411
  return [
@@ -1026,7 +1035,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1026
1035
  } = useDocumentPane(), { draft, published, version } = editState || {}, assistableDocumentId = selectedReleaseId ? getVersionId(documentId, selectedReleaseId) : documentSchemaType.liveEdit ? documentId : getDraftId(documentId), documentIsNew = selectedReleaseId ? !version?._id : !draft?._id && !published?._id, documentIsAssistable = selectedReleaseId ? !!version : isDocAssistable(documentSchemaType, published, draft), { params } = useAiPaneRouter(), selectedPath = params[fieldPathParam], assistDocument = useStudioAssistDocument({
1027
1036
  documentId: assistableDocumentId,
1028
1037
  schemaType: documentSchemaType
1029
- }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId);
1038
+ }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId), fieldRefs = getFieldRefs(documentSchemaType), fieldRefsByTypePath = asFieldRefsByTypePath(fieldRefs);
1030
1039
  return useMemo(() => {
1031
1040
  const base = {
1032
1041
  assistableDocumentId,
@@ -1040,7 +1049,9 @@ function useAssistDocumentContextValue(documentId, documentType) {
1040
1049
  selectedPath,
1041
1050
  syntheticTasks,
1042
1051
  addSyntheticTask,
1043
- removeSyntheticTask
1052
+ removeSyntheticTask,
1053
+ fieldRefs,
1054
+ fieldRefsByTypePath
1044
1055
  };
1045
1056
  return assistDocument ? {
1046
1057
  ...base,
@@ -2870,12 +2881,12 @@ function useCustomFieldActions(props) {
2870
2881
  path: props.path
2871
2882
  });
2872
2883
  return useMemo(() => {
2873
- const title = fieldActions?.title, customActions = configActions?.map((node) => createSafeNode({
2884
+ const title = fieldActions?.title, customActions = configActions?.filter(isDefined).map((node) => createSafeNode({
2874
2885
  node,
2875
2886
  pushToast,
2876
2887
  addSyntheticTask,
2877
2888
  removeSyntheticTask
2878
- })), onlyGroups = customActions?.length && customActions?.every((node) => node.type === "group");
2889
+ })).filter(isDefined), onlyGroups = customActions?.length && customActions?.every((node) => node.type === "group");
2879
2890
  return (customActions?.length ? onlyGroups ? customActions : [
2880
2891
  {
2881
2892
  type: "group",
@@ -2892,12 +2903,13 @@ function createSafeNode(args) {
2892
2903
  case "action":
2893
2904
  return createSafeAction({ ...args, action: node });
2894
2905
  case "group":
2895
- return {
2906
+ const children = node.children?.filter(isDefined).map((child) => createSafeNode({ ...args, node: child })).filter(isDefined);
2907
+ return children?.length ? {
2896
2908
  ...node,
2897
2909
  renderAsButton: !1,
2898
2910
  expanded: !0,
2899
- children: node.children?.map((child) => createSafeNode({ ...args, node: child }))
2900
- };
2911
+ children
2912
+ } : void 0;
2901
2913
  case "divider":
2902
2914
  default:
2903
2915
  return node;
@@ -2955,7 +2967,8 @@ const assistFieldActions = {
2955
2967
  documentOnChange,
2956
2968
  documentSchemaType,
2957
2969
  selectedPath,
2958
- assistableDocumentId
2970
+ assistableDocumentId,
2971
+ fieldRefsByTypePath
2959
2972
  } = useAssistDocumentContext(), { value: docValue, formState } = useDocumentPane(), docValueRef = useRef(docValue), formStateRef = useRef(formState);
2960
2973
  formStateRef.current = formState;
2961
2974
  const currentUser = useCurrentUser(), isHidden = !assistDocument, pathKey = usePathKey(props.path), typePath = useTypePath(docValue, pathKey), assistDocumentId2 = assistDocument?._id, { requestRunInstruction } = useRequestRunInstruction({
@@ -3043,14 +3056,22 @@ const assistFieldActions = {
3043
3056
  path
3044
3057
  };
3045
3058
  }
3046
- ), []), customActions = useCustomFieldActions({
3059
+ ), []), parentSchemaType = useMemo(() => {
3060
+ if (props.path.length) {
3061
+ if (props.path.length === 1)
3062
+ return documentSchemaType;
3063
+ } else return;
3064
+ const parentPath = props.path.slice(0, -1), typePath2 = getTypePath(docValueRef.current, pathToString(parentPath));
3065
+ return typePath2 ? fieldRefsByTypePath[typePath2]?.schemaType : void 0;
3066
+ }, [fieldRefsByTypePath, props.path, documentSchemaType]), customActions = useCustomFieldActions({
3047
3067
  actionType: props.path.length ? "field" : "document",
3048
3068
  documentIdForAction: assistableDocumentId,
3049
3069
  schemaType,
3050
3070
  documentSchemaType,
3051
3071
  path: props.path,
3052
3072
  getDocumentValue,
3053
- getConditionalPaths
3073
+ getConditionalPaths,
3074
+ parentSchemaType
3054
3075
  }), manageInstructionsItem = useMemo(
3055
3076
  () => ({
3056
3077
  type: "action",