alexandrusoare commented on code in PR #35683:
URL: https://github.com/apache/superset/pull/35683#discussion_r2447503728


##########
superset-frontend/plugins/plugin-chart-ag-grid-table/src/utils/useAgGridFilters.ts:
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { useCallback, useEffect, useMemo, type RefObject } from 'react';
+import { debounce, isEqual } from 'lodash';
+import type { FilterChangedEvent, GridApi } from 'ag-grid-community';
+import {
+  type AgGridFilterModel,
+  type AgGridSimpleFilter,
+  type AgGridCompoundFilter,
+} from './agGridFilterConverter';
+
+const FILTER_DEBOUNCE_DURATION = 500;
+
+interface FilterInputPosition {
+  lastFilteredColumn?: string;
+  lastFilteredInputPosition?: 'first' | 'second';
+}
+
+const detectLastFilteredColumn = (
+  filterModel: AgGridFilterModel,
+  previousModel: AgGridFilterModel,
+): FilterInputPosition => {
+  const allColumns = new Set([
+    ...Object.keys(filterModel),
+    ...Object.keys(previousModel),
+  ]);
+
+  for (const colId of allColumns) {
+    if (!isEqual(filterModel[colId], previousModel[colId])) {
+      const activeElement = document.activeElement as HTMLElement;
+      const isInputOrTextarea =
+        activeElement?.tagName === 'INPUT' ||
+        activeElement?.tagName === 'TEXTAREA';
+
+      let lastFilteredInputPosition: 'first' | 'second' | undefined;
+
+      if (isInputOrTextarea) {
+        const filterBody = activeElement.closest('.ag-filter-body');
+
+        if (filterBody) {
+          const prevSibling =
+            filterBody?.previousElementSibling?.previousElementSibling;
+
+          if (
+            prevSibling &&
+            prevSibling.classList.contains('ag-filter-condition')
+          ) {
+            lastFilteredInputPosition = 'second';
+          } else {
+            const nextSibling = filterBody.nextElementSibling;
+            if (
+              !nextSibling ||
+              nextSibling.classList.contains('ag-filter-condition')
+            ) {
+              lastFilteredInputPosition = 'first';
+            }

Review Comment:
   I see 'first' and 'second', check if it s an already existing enum for these 
to make it easiear



##########
superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx:
##########
@@ -102,17 +141,33 @@ const CustomHeader: React.FC<CustomHeaderParams> = ({
     else clearSort();
   };
 
-  const handleFilterClick = async (e: React.MouseEvent) => {
-    e.stopPropagation();
-    setFilterVisible(!isFilterVisible);
-
-    const filterInstance = await api.getColumnFilterInstance<any>(column);
-    const filterEl = filterInstance?.eGui;
-    if (filterEl && filterRef.current) {
-      filterRef.current.innerHTML = '';
-      filterRef.current.appendChild(filterEl);
-    }
-  };
+  const handleFilterClick = useCallback(
+    async (e?: React.MouseEvent | undefined) => {
+      if (e) {
+        e?.stopPropagation();
+      }
+      setFilterVisible(!isFilterVisible);
+
+      const filterInstance = await api.getColumnFilterInstance<any>(column);
+      const filterEl = filterInstance?.eGui;
+      if (filterEl && filterRef.current) {
+        filterRef.current.innerHTML = '';
+        filterRef.current.appendChild(filterEl);
+      }
+    },
+    [isFilterVisible, api, column],
+  );
+
+  useEffect(() => {
+    const timeoutId = setTimeout(() => {
+      if (lastFilteredColumn === colId && !isFilterVisible) {
+        handleFilterClick();
+        focusFilterInput(lastFilteredInputPosition);
+      }
+    }, 200);
+

Review Comment:
   What s the purpose of this delay?



##########
superset-frontend/plugins/plugin-chart-ag-grid-table/src/utils/useAgGridFilters.ts:
##########
@@ -0,0 +1,273 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { useCallback, useEffect, useMemo, type RefObject } from 'react';
+import { debounce, isEqual } from 'lodash';
+import type { FilterChangedEvent, GridApi } from 'ag-grid-community';
+import {
+  type AgGridFilterModel,
+  type AgGridSimpleFilter,
+  type AgGridCompoundFilter,
+} from './agGridFilterConverter';
+
+const FILTER_DEBOUNCE_DURATION = 500;

Review Comment:
   There is already a delay const for this, which is used for debouncing in 
general



##########
superset-frontend/plugins/plugin-chart-ag-grid-table/test/tsconfig.json:
##########
@@ -0,0 +1,9 @@
+{
+  "compilerOptions": {
+    "composite": false,
+    "emitDeclarationOnly": false,
+    "rootDir": "../../../"
+  },
+  "extends": "../../../tsconfig.json",
+  "include": ["**/*", "../types/**/*", "../../../types/**/*"]

Review Comment:
   I don t think there s a need for a tsconfig.json for a test folder, is this 
pattern going on for other plugins as well?



##########
superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx:
##########
@@ -102,17 +141,33 @@ const CustomHeader: React.FC<CustomHeaderParams> = ({
     else clearSort();
   };
 
-  const handleFilterClick = async (e: React.MouseEvent) => {
-    e.stopPropagation();
-    setFilterVisible(!isFilterVisible);
-
-    const filterInstance = await api.getColumnFilterInstance<any>(column);
-    const filterEl = filterInstance?.eGui;
-    if (filterEl && filterRef.current) {
-      filterRef.current.innerHTML = '';
-      filterRef.current.appendChild(filterEl);
-    }
-  };
+  const handleFilterClick = useCallback(
+    async (e?: React.MouseEvent | undefined) => {
+      if (e) {
+        e?.stopPropagation();
+      }
+      setFilterVisible(!isFilterVisible);
+
+      const filterInstance = await api.getColumnFilterInstance<any>(column);

Review Comment:
   Is there an interface more appropriate for this rather than any?



##########
superset-frontend/plugins/plugin-chart-ag-grid-table/src/AgGridTable/components/CustomHeader.tsx:
##########
@@ -70,7 +76,40 @@ const CustomHeader: React.FC<CustomHeaderParams> = ({
   const [isFilterVisible, setFilterVisible] = useState(false);
   const [isMenuVisible, setMenuVisible] = useState(false);
   const filterRef = useRef<HTMLDivElement>(null);
-  const isFilterActive = column?.isFilterActive();
+  const filterButtonRef = useRef<HTMLDivElement>(null);
+  // Use activeFilterColumns from context as reliable backup for AG Grid's 
isFilterActive
+  const isFilterActive =
+    column?.isFilterActive() ||
+    (colId ? activeFilterColumns?.has(colId) : false);
+
+  const focusFilterInput = useCallback((position?: 'first' | 'second') => {
+    setTimeout(() => {
+      if (!filterRef.current) return;
+
+      const filterBodies =
+        filterRef.current.querySelectorAll<HTMLElement>('.ag-filter-body');
+
+      if (filterBodies.length === 0) return;
+
+      let targetFilterBody: HTMLElement | null = null;
+
+      if (position === 'second' && filterBodies?.length > 1) {
+        targetFilterBody = filterBodies[1];
+      } else if (position === 'first' && filterBodies?.length > 0) {
+        targetFilterBody = filterBodies[0];
+      }
+
+      if (!targetFilterBody) return;
+
+      const input = targetFilterBody.querySelector<HTMLInputElement>(
+        'input:not([type="checkbox"]):not([type="radio"]), textarea',
+      );
+
+      if (input) {
+        input.focus();
+      }
+    }, 100);

Review Comment:
   There are some already declared constants `SLOW_DELAY` `FAST_DELAY`, but I 
am not sure what s the purpose of the delay. Could you provide more details?



-- 
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