sadpandajoe commented on code in PR #41907:
URL: https://github.com/apache/superset/pull/41907#discussion_r3829024935


##########
superset-frontend/src/components/Chart/DrillDown/useDrillDownState.ts:
##########
@@ -0,0 +1,483 @@
+/**
+ * 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,
+  useLayoutEffect,
+  useMemo,
+  useRef,
+  useState,
+} from 'react';
+import { t } from '@apache-superset/core/translation';
+import {
+  BinaryQueryObjectFilterClause,
+  ensureIsArray,
+  getClientErrorObject,
+  QueryData,
+  QueryFormData,
+} from '@superset-ui/core';
+import { simpleFilterToAdhoc } from 'src/utils/simpleFilterToAdhoc';
+import {
+  getChartDataRequest,
+  handleChartDataResponse,
+} from 'src/components/Chart/chartAction';
+import { DrillDownLevel } from './types';
+
+/**
+ * The form-data field name that stores the ordered list of drill columns.
+ * The chart starts at hierarchy[0] and advances one level per click.
+ */
+const HIERARCHY_FIELD = 'drilldown_hierarchy';
+const HIERARCHY_FIELD_CAMEL = 'drilldownHierarchy';
+
+/**
+ * Default form-data field that holds the chart's grouping dimension.
+ * Most echarts plugins use 'groupby'; Sunburst uses 'columns'. The click
+ * handler can override this on a per-event basis.
+ */
+const DEFAULT_GROUPBY_FIELD = 'groupby';
+const DEFAULT_ADHOC_FILTERS_FIELD = 'adhoc_filters';
+
+/**
+ * Drill navigation is kept in a module-level store keyed by chart id so it
+ * survives incidental remounts of the chart component. On a dashboard, an
+ * unrelated filter change (e.g. removing another chart's cross-filter) can
+ * cause the grid to re-render and remount the chart, which would otherwise
+ * reset the local React state and make the breadcrumb vanish mid-drill,
+ * stranding the user with no way to navigate back up. The store is process
+ * memory only — a full page reload still starts fresh.
+ */
+interface StoredDrillState {
+  drillStack: DrillDownLevel[];
+  selectedLeaf?: string;
+  /** Filters for the value selected at the deepest level, if any. */
+  selectedLeafFilters?: BinaryQueryObjectFilterClause[];
+}
+const drillStateStore = new Map<string | number, StoredDrillState>();
+
+/**
+ * Clear persisted drill state. Without arguments clears everything (used by
+ * tests to isolate cases); with a chart id clears just that chart.
+ */
+export function clearDrillDownState(chartKey?: string | number): void {
+  if (chartKey === undefined) {
+    drillStateStore.clear();
+  } else {
+    drillStateStore.delete(chartKey);
+  }
+}
+
+interface UseDrillDownStateArgs {
+  /** Unique chart instance id (dashboard grid assigns one per slot). */
+  chartId: string | number;
+  formData: QueryFormData;
+  /** Original chart data, shown when the drill stack is empty */
+  baseQueriesResponse?: QueryData[] | null;
+}
+
+interface UseDrillDownStateResult {
+  /** True if the user has drilled at least one level deep */
+  isDrilling: boolean;
+  /** The breadcrumb path showing where the user is in the hierarchy */
+  drillStack: DrillDownLevel[];
+  /** Value selected at the deepest level */
+  selectedLeaf?: string;
+  /** The computed hierarchy of column names */
+  hierarchy: string[];
+  /** form_data adjusted for the current drill level */
+  effectiveFormData: QueryFormData;
+  /** Chart data for the current drill level (or base data when not drilling) 
*/
+  effectiveQueriesResponse: QueryData[] | null | undefined;
+  /** True while the next-level data is being fetched */
+  isLoading: boolean;
+  /** Error message if the drill query failed */
+  error?: string;
+  /** Whether the chart has a configured drill-down hierarchy */
+  hasHierarchy: boolean;
+  /**
+   * Push a new level onto the drill stack. Called from the chart's click
+   * handler with the filters that identify the clicked data point.
+   */
+  drillDown: (filters: BinaryQueryObjectFilterClause[], label: string) => void;
+  /** Truncate the drill stack to the given depth (0 = back to start) */
+  resetTo: (depth: number) => void;
+}
+
+/**
+ * Hook that manages a chart's drill-down state. Owns the drill stack,
+ * computes the effective form_data for the current level, fetches the
+ * data for that level, and exposes navigation helpers (drillDown / resetTo).
+ *
+ * The hook never mutates the upstream Redux store: closing or refreshing
+ * the dashboard wipes the drill state and restores the original chart.
+ */
+export function useDrillDownState({
+  chartId,
+  formData,
+  baseQueriesResponse,
+}: UseDrillDownStateArgs): UseDrillDownStateResult {
+  const chartKey = chartId;
+
+  // Drill state intentionally persists in drillStateStore across unmounts
+  // (dashboard virtualization scroll-out, tab switches, filter re-layouts) so
+  // it stays in sync with the cross-filter the drill emits into Redux. 
Evicting
+  // it on unmount previously left the emitted cross-filter orphaned — the 
drill
+  // appeared to reset while the filter lingered. The store is cleared on chart
+  // reconfigure (the layout effect below) and via clearDrillDownState.
+
+  const [drillStack, setDrillStack] = useState<DrillDownLevel[]>(
+    () =>
+      (chartKey != null
+        ? drillStateStore.get(chartKey)?.drillStack
+        : undefined) ?? [],
+  );
+  const [selectedLeaf, setSelectedLeaf] = useState<string | undefined>(() =>
+    chartKey != null ? drillStateStore.get(chartKey)?.selectedLeaf : undefined,
+  );
+  // Filters for the value picked at the deepest level. Applied to the drilled
+  // chart's own query so it narrows to the selected leaf (a single bar),
+  // independent of the dashboard's cross-filter scope config. Without this the
+  // drilled chart keeps showing the full leaf distribution and only charts 
that
+  // happen to include themselves in their cross-filter scope look "filtered".
+  const [selectedLeafFilters, setSelectedLeafFilters] = useState<
+    BinaryQueryObjectFilterClause[] | undefined
+  >(() =>
+    chartKey != null
+      ? drillStateStore.get(chartKey)?.selectedLeafFilters
+      : undefined,
+  );
+  const [drillData, setDrillData] = useState<QueryData[] | null>(null);
+  const [isLoading, setIsLoading] = useState(false);
+  const [error, setError] = useState<string | undefined>();
+
+  // Persist drill navigation synchronously so it survives remounts (see
+  // drillStateStore) without racing. Writing on a deferred effect would let a
+  // remount triggered by the same interaction (e.g. clearing a cross-filter)
+  // restore stale state before the effect runs, so the mutators below write
+  // through this helper immediately instead.
+  const persist = useCallback(
+    (
+      stack: DrillDownLevel[],
+      leaf: string | undefined,
+      leafFilters: BinaryQueryObjectFilterClause[] | undefined,
+    ) => {
+      if (chartKey == null) {
+        return;
+      }
+      if (stack.length === 0 && !leaf) {
+        drillStateStore.delete(chartKey);
+      } else {
+        drillStateStore.set(chartKey, {
+          drillStack: stack,
+          selectedLeaf: leaf,
+          selectedLeafFilters: leafFilters,
+        });
+      }
+    },
+    [chartKey],
+  );
+
+  // Reset only when the chart is actually reconfigured (chart id or viz type
+  // changes) — e.g. the dashboard owner edited the chart and saved. A ref
+  // guard ensures the initial mount (which restores persisted state) does not
+  // wipe it, and that incidental re-renders from filter changes don't either.
+  // useLayoutEffect runs synchronously before paint so the stale drill state
+  // is cleared without a visible flash when the chart is reconfigured.
+  const configKey = `${chartId}__${formData.viz_type}`;
+  const prevConfigKeyRef = useRef(configKey);
+  useLayoutEffect(() => {
+    if (prevConfigKeyRef.current === configKey) {
+      return;
+    }
+    prevConfigKeyRef.current = configKey;
+    if (chartKey != null) {
+      drillStateStore.delete(chartKey);
+    }
+    setDrillStack([]);
+    setSelectedLeaf(undefined);
+    setSelectedLeafFilters(undefined);
+    setDrillData(null);
+    setError(undefined);
+  }, [configKey, chartKey]);
+
+  const hierarchy = useMemo<string[]>(() => {
+    const fd = formData as Record<string, unknown>;
+    const xAxis = fd.x_axis ?? fd.xAxis;
+
+    // Primary source: the dedicated `drilldown_hierarchy` control. The chart's
+    // own primary dimension (x_axis for axis charts, the first groupby column
+    // for groupby charts) is the top level and is prepended automatically when
+    // the author lists only the deeper levels.
+    const drillLevels = ensureIsArray(
+      fd[HIERARCHY_FIELD] ?? fd[HIERARCHY_FIELD_CAMEL],
+    ) as string[];
+    if (drillLevels.length > 0) {
+      const xAxisStr = typeof xAxis === 'string' ? xAxis : undefined;
+      if (xAxisStr) {
+        return drillLevels.includes(xAxisStr)
+          ? drillLevels
+          : [xAxisStr, ...drillLevels];
+      }
+      const firstGroupby = ensureIsArray(fd[DEFAULT_GROUPBY_FIELD]).find(
+        col => typeof col === 'string',
+      ) as string | undefined;
+      if (firstGroupby && !drillLevels.includes(firstGroupby)) {
+        return [firstGroupby, ...drillLevels];
+      }
+      return drillLevels;
+    }
+
+    return [];
+  }, [formData]);
+
+  // A hierarchy needs at least two levels to be drillable; a single column
+  // (e.g. the author listed only the chart's own dimension) is a no-op that
+  // would otherwise hijack the normal cross-filter click without ever
+  // advancing.
+  const hasHierarchy = hierarchy.length >= 2;
+  const currentDepth = drillStack.length;
+
+  const effectiveFormData = useMemo<QueryFormData>(() => {
+    if (currentDepth === 0) {
+      return formData;
+    }
+    const nextColumn = hierarchy[currentDepth];
+
+    // Merge accumulated filters from every level into adhoc_filters.
+    const accumulatedFilters = drillStack.flatMap(level => level.filters);
+    const baseAdhoc = ensureIsArray(
+      (formData as Record<string, unknown>)[DEFAULT_ADHOC_FILTERS_FIELD],
+    );
+
+    const fdRecord = formData as Record<string, unknown>;
+
+    // Swap the field the hierarchy is anchored to. When the chart has an
+    // x-axis, the hierarchy is x-axis driven (the groupby, if any, is only a
+    // series breakdown and must be preserved), so swap x_axis. Only groupby-
+    // based charts (Pie/Funnel/…, no x_axis) swap the groupby.
+    const xAxisIsSet =
+      typeof fdRecord.x_axis === 'string' || typeof fdRecord.xAxis === 
'string';
+    const groupbyValue = fdRecord[DEFAULT_GROUPBY_FIELD];
+
+    const updated = { ...formData } as Record<string, unknown>;
+
+    if (xAxisIsSet) {
+      // Axis charts (Bar/Line/Area/…): advance the x-axis column.
+      if (typeof fdRecord.x_axis === 'string') {
+        updated.x_axis = nextColumn;
+      }
+      if (typeof fdRecord.xAxis === 'string') {
+        updated.xAxis = nextColumn;
+      }
+    } else {
+      // Groupby-based charts: advance the grouping dimension.
+      updated[DEFAULT_GROUPBY_FIELD] = Array.isArray(groupbyValue)
+        ? [nextColumn]
+        : nextColumn;
+    }
+
+    // At the deepest level a picked value narrows the chart to that single
+    // leaf (matching the breadcrumb selection), rather than showing the full
+    // leaf distribution. Only apply the leaf filters while a leaf is actually
+    // selected so that navigating back (resetTo) or drilling deeper never
+    // leaves stale leaf filters in effectiveFormData.
+    const leafFilters = selectedLeaf ? (selectedLeafFilters ?? []) : [];
+
+    updated[DEFAULT_ADHOC_FILTERS_FIELD] = [
+      ...baseAdhoc,
+      ...accumulatedFilters.map(f => simpleFilterToAdhoc(f)),

Review Comment:
   This conversion drops the `grain` attached to time-axis drill filters, so a 
bucketed time-series click becomes an exact raw-timestamp equality rather than 
the bucket predicate. Clicking a daily/monthly point can therefore return no 
next-level rows even though the source bucket contains data. Could the drill 
filter conversion preserve the time grain and add a bucketed time-axis 
regression?



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