msyavuz commented on code in PR #33960:
URL: https://github.com/apache/superset/pull/33960#discussion_r2194574042


##########
superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/utils.ts:
##########
@@ -49,6 +50,39 @@ export const hasCircularDependency = (
   return false;
 };
 
+/**
+ * Helper function to find dependency validation errors in form fields.
+ * This function improves code maintainability by centralizing the logic
+ * for detecting cyclic dependency errors and extracting the problematic 
filter ID.
+ *
+ * @param fields - Array of form field error objects from Ant Design Form
+ * @returns Object containing error status and filter ID, or null if no 
dependency errors
+ */
+export const findDependencyError = (fields: any[]) => {
+  // Form field structure constants for better readability
+  const FORM_FIELD_FILTERS_INDEX = 0;
+  const FORM_FIELD_DEPENDENCIES_INDEX = 2;
+  const FILTER_ID_INDEX = 1;
+
+  // Find the first field with dependency validation errors
+  const errorField = fields.find(
+    field =>
+      field.name?.[FORM_FIELD_FILTERS_INDEX] === 'filters' &&
+      field.name?.[FORM_FIELD_DEPENDENCIES_INDEX] === 'dependencies' &&
+      field.errors?.length > 0,
+  );
+
+  if (errorField) {
+    return {
+      hasError: true,
+      filterId: errorField.name[FILTER_ID_INDEX] as string,
+      errors: errorField.errors,
+    };
+  }
+
+  return null;
+};

Review Comment:
   ```suggestion
   interface FieldError {
     name: (string | number)[];
     errors: string[];
     warnings: string[];
   }
   
   /**
    * Helper function to find dependency validation errors in form fields.
    * This function improves code maintainability by centralizing the logic
    * for detecting cyclic dependency errors and extracting the problematic 
filter ID.
    *
    * @param fields - Array of form field error objects from Ant Design Form
    * @returns Object containing error status and filter ID, or null if no 
dependency errors
    */
   export const findDependencyError = (fields: FieldError[]) => {
     const FILTER_ID_INDEX = 1;
   
     const errorField = fields.find(({ name, errors }) => {
       const [path, subpath, id] = name;
       return (
         path === 'filters' &&
         subpath === 'dependencies' &&
         typeof id === 'string' &&
         errors?.length > 0
       );
     });
   
     if (!errorField) {
       return null;
     }
   
     const filterId = errorField.name[FILTER_ID_INDEX];
   
     return {
       hasError: true,
       filterId: filterId as string,
       errors: errorField.errors,
     };
   };
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to