sadpandajoe commented on code in PR #41907: URL: https://github.com/apache/superset/pull/41907#discussion_r3952665064
########## superset-frontend/src/components/Chart/DrillDown/DrillDownHost.tsx: ########## @@ -0,0 +1,302 @@ +/** + * 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 { + ComponentType, + useCallback, + useMemo, + useRef, + useState, + useEffect, +} from 'react'; +import { + QueryData, + QueryFormData, + BinaryQueryObjectFilterClause, + FeatureFlag, + isFeatureEnabled, +} from '@superset-ui/core'; +import { css } from '@apache-superset/core/theme'; +import { ChartSource } from 'src/types/ChartSource'; +import { useDrillDownState } from './useDrillDownState'; +import { DrillDownBreadcrumb } from './DrillDownBreadcrumb'; +import type { ChartRendererProps } from '../ChartRenderer'; + +/** + * Hook payload contract: chart plugins call `onDrillDown(filters, label)` + * via the chart's hooks bag when the user clicks a data point and a + * drill-down hierarchy is configured. + */ +export type OnDrillDownHook = ( + filters: BinaryQueryObjectFilterClause[], + label: string, +) => void; + +/** + * Build the cross-filter clauses the dashboard data-mask expects from a set of + * drill-down filter clauses. Shared by the drill (onDrillDown) and the + * breadcrumb navigation (handleResetTo) paths so their cross-filter shape can + * never diverge. Special cases mirror the native ECharts cross-filter path: a + * temporal bucket click is passed through as a `TEMPORAL_RANGE`, and a null + * value becomes `IS NULL` (rather than `IN [null]`, which selects nothing). + */ +const toCrossFilterClauses = (filters: BinaryQueryObjectFilterClause[]) => + filters.map(f => { + if (f.op === 'TEMPORAL_RANGE') { + return { col: f.col, op: 'TEMPORAL_RANGE' as const, val: f.val }; + } + if (f.val == null) { + return { col: f.col, op: 'IS NULL' as const }; + } + return { + col: f.col, + op: 'IN' as const, + val: [f.val] as (string | number | boolean)[], + }; + }); + +interface DrillDownHostProps extends ChartRendererProps { + /** The wrapped renderer component */ + ChartRendererComponent: ComponentType< + ChartRendererProps & { onDrillDown?: OnDrillDownHook } + >; +} + +/** + * Wraps `<ChartRenderer>` with drill-down behavior. When the chart's + * form_data declares a `drilldown_hierarchy`, this host: + * + * 1. Tracks how deep the user has drilled (a stack of levels) + * 2. Computes "effective" form_data for the current level (swapping the + * grouping dimension — groupby or x_axis — and adding accumulated filters) + * 3. Re-fetches chart data for that level + * 4. Renders a breadcrumb above the chart for navigating back up + * + * If the chart has no hierarchy, this is a thin pass-through. + */ +export function DrillDownHost({ + ChartRendererComponent, + ...rendererProps +}: DrillDownHostProps) { + const { formData, queriesResponse } = rendererProps; + + const { + isDrilling, + drillStack, + selectedLeaf, + hierarchy, + effectiveFormData, + effectiveQueriesResponse, + isLoading, + error, + hasHierarchy, + drillDown, + resetTo, + } = useDrillDownState({ + chartId: rendererProps.chartId, + formData, + baseQueriesResponse: queriesResponse, + }); + + // Drill-down is a dashboard interaction gated behind the DRILL_DOWN feature + // flag. In Explore the control panel and the rendered chart would disagree + // (and clicks there mean something else), so only enable it when rendered + // inside a dashboard. + const drillEnabled = + isFeatureEnabled(FeatureFlag.DrillDown) && + hasHierarchy && + rendererProps.source === ChartSource.Dashboard; + + const onDrillDown = useMemo<OnDrillDownHook | undefined>(() => { + if (!drillEnabled) { + return undefined; + } + return (filters, label) => { + // Emit a cross-filter for the FULL drill path — every level reached so + // far plus this click — so other dashboard charts are scoped to exactly + // what the drilled chart shows, not just the deepest clicked column. + const pathFilters = [ + ...drillStack.flatMap(level => level.filters), + ...filters, + ]; + const pathLabels = [...drillStack.map(level => level.label), label]; + drillDown(filters, label); + if ( + rendererProps.emitCrossFilters && + rendererProps.actions?.updateDataMask + ) { + rendererProps.actions.updateDataMask(rendererProps.chartId, { + extraFormData: { + filters: toCrossFilterClauses(pathFilters), + }, + filterState: { + value: pathLabels, + selectedValues: pathLabels, + }, + }); + } + }; + }, [ + drillEnabled, + drillDown, + drillStack, + rendererProps.emitCrossFilters, + rendererProps.actions, + rendererProps.chartId, + ]); + + const overlayProps = useMemo<Partial<ChartRendererProps>>(() => { + if (!isDrilling) { + // At the base level, render the chart unchanged. + return {}; + } + return { + formData: effectiveFormData as QueryFormData, + queriesResponse: (effectiveQueriesResponse ?? null) as QueryData[] | null, + // Treat "no data yet" as loading. On the first render after a drill the + // fetch effect hasn't run (isLoading is still false, drillData is null); + // without this the renderer would briefly paint a "No results" state. + chartStatus: + isLoading || effectiveQueriesResponse == null ? 'loading' : 'rendered', Review Comment: After the final failed drill request, `effectiveQueriesResponse` is still null, so this keeps the chart in `loading` even though the hook has an error; ChartRenderer then renders no chart at all. Could the error case render a recoverable error/base state instead of leaving the chart body blank? -- 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]
