Skip to content

Commit cdf23a0

Browse files
sadpandajoeclaude
andauthored
fix(dashboard): don't apply cross-filters from charts scoped to no other chart (#46202)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 01b642d commit cdf23a0

12 files changed

Lines changed: 381 additions & 11 deletions

File tree

‎superset-frontend/src/components/Chart/Chart.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export interface ChartProps {
9090
datasetsStatus?: 'loading' | 'error' | 'complete';
9191
isInView?: boolean;
9292
emitCrossFilters?: boolean;
93+
/** False when the chart's cross-filter scope reaches no other chart */
94+
hasCrossFilterTargets?: boolean;
9395
onChartStateChange?: (chartState: AgGridChartState) => void;
9496
/** Whether to suppress the loading spinner (during auto-refresh) */
9597
suppressLoadingSpinner?: boolean;
@@ -212,6 +214,7 @@ function Chart({
212214
triggerQuery,
213215
postTransformProps,
214216
emitCrossFilters,
217+
hasCrossFilterTargets,
215218
onChartStateChange,
216219
suppressLoadingSpinner,
217220
filterState,
@@ -421,6 +424,7 @@ function Chart({
421424
ownState={ownState}
422425
postTransformProps={postTransformProps}
423426
emitCrossFilters={emitCrossFilters}
427+
hasCrossFilterTargets={hasCrossFilterTargets}
424428
onChartStateChange={onChartStateChange}
425429
latestQueryFormData={latestQueryFormData}
426430
filterState={filterState}
@@ -447,6 +451,7 @@ function Chart({
447451
emitCrossFilters,
448452
filterState,
449453
formData,
454+
hasCrossFilterTargets,
450455
height,
451456
initialValues,
452457
latestQueryFormData,

‎superset-frontend/src/components/Chart/ChartContextMenu/ChartContextMenu.test.tsx‎

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import { useRef, useState } from 'react';
2020
import {
2121
Behavior,
2222
ChartMetadata,
23-
DataMask,
24-
getChartMetadataRegistry,
2523
ContextMenuFilters,
24+
DataMask,
2625
FeatureFlag,
26+
getChartMetadataRegistry,
2727
VizType,
2828
} from '@superset-ui/core';
29-
import { render, screen, waitFor } from 'spec/helpers/testing-library';
29+
import { render, screen, waitFor, within } from 'spec/helpers/testing-library';
3030
import userEvent from '@testing-library/user-event';
3131
import * as dataMaskActions from 'src/dataMask/actions';
3232
import getFormDataWithExtraFilters from 'src/dashboard/util/charts/getFormDataWithExtraFilters';
@@ -127,11 +127,15 @@ const TestWrapper = ({
127127
);
128128
};
129129

130-
const setup = (openFilters?: ContextMenuFilters) =>
130+
const setup = (
131+
openFilters?: ContextMenuFilters,
132+
dashboardInfo: Record<string, unknown> = {},
133+
) =>
131134
render(<TestWrapper openFilters={openFilters} />, {
132135
useRedux: true,
133136
initialState: {
134137
...mockState,
138+
dashboardInfo: { ...mockState.dashboardInfo, ...dashboardInfo },
135139
user: {
136140
...mockState.user,
137141
roles: {
@@ -379,6 +383,87 @@ test('semantic-view datasource resolves via the structure endpoint and renders t
379383
});
380384
});
381385

386+
const crossFilterMenuFilters = (
387+
isCurrentValueSelected: boolean,
388+
): ContextMenuFilters => ({
389+
crossFilter: {
390+
dataMask: {
391+
extraFormData: { filters: [{ col: 'gender', op: 'IN', val: ['boy'] }] },
392+
filterState: { value: ['boy'] },
393+
},
394+
isCurrentValueSelected,
395+
},
396+
});
397+
398+
const crossFilterDashboardInfo = (chartsInScope: number[]) => ({
399+
crossFiltersEnabled: true,
400+
metadata: {
401+
...mockState.dashboardInfo.metadata,
402+
chart_configuration: {
403+
[sliceId]: {
404+
id: sliceId,
405+
crossFilters: { scope: 'global', chartsInScope },
406+
},
407+
},
408+
},
409+
});
410+
411+
const openCrossFilterMenuItem = async (label: string) => {
412+
await userEvent.click(screen.getByTestId('open-context-menu'));
413+
return (await screen.findByText(label)).closest('[role="menuitem"]');
414+
};
415+
416+
const registerInteractivePie = () => {
417+
if (!getChartMetadataRegistry().has(VizType.Pie)) {
418+
getChartMetadataRegistry().registerValue(
419+
VizType.Pie,
420+
new ChartMetadata({
421+
name: 'Pie',
422+
thumbnail: '',
423+
behaviors: [Behavior.InteractiveChart],
424+
}),
425+
);
426+
}
427+
};
428+
429+
test('Add cross-filter is enabled when the chart cross-filter scope has targets', async () => {
430+
registerInteractivePie();
431+
setup(crossFilterMenuFilters(false), crossFilterDashboardInfo([sliceId + 1]));
432+
433+
const item = await openCrossFilterMenuItem('Add cross-filter');
434+
expect(item).not.toHaveAttribute('aria-disabled', 'true');
435+
});
436+
437+
test('Add cross-filter is disabled when the chart cross-filter scope has no targets', async () => {
438+
registerInteractivePie();
439+
setup(crossFilterMenuFilters(false), crossFilterDashboardInfo([]));
440+
441+
const item = await openCrossFilterMenuItem('Add cross-filter');
442+
expect(item).toHaveAttribute('aria-disabled', 'true');
443+
444+
await userEvent.hover(
445+
within(item as HTMLElement).getByTestId('tooltip-trigger'),
446+
);
447+
expect(
448+
await screen.findByText(
449+
"This chart's cross-filtering scope doesn't include any other charts.",
450+
),
451+
).toBeInTheDocument();
452+
expect(
453+
screen.queryByText(
454+
'You can also just click on the chart to apply cross-filter.',
455+
),
456+
).not.toBeInTheDocument();
457+
});
458+
459+
test('Remove cross-filter stays enabled when the chart cross-filter scope has no targets', async () => {
460+
registerInteractivePie();
461+
setup(crossFilterMenuFilters(true), crossFilterDashboardInfo([]));
462+
463+
const item = await openCrossFilterMenuItem('Remove cross-filter');
464+
expect(item).not.toHaveAttribute('aria-disabled', 'true');
465+
});
466+
382467
test('SQL context-menu cross-filter is excluded by semantic dashboard assembly', async () => {
383468
const registry = getChartMetadataRegistry();
384469
const previous = registry.get(VizType.Pie);

‎superset-frontend/src/components/Chart/ChartContextMenu/ChartContextMenu.tsx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { usePermissions } from 'src/hooks/usePermissions';
4949
import { Dropdown } from '@superset-ui/core/components';
5050
import { updateDataMask } from 'src/dataMask/actions';
5151
import { releaseFilterKeyPublish } from 'src/dashboard/util/sharedFilterStateHold';
52+
import { selectHasCrossFilterTargets } from 'src/dashboard/util/crossFilters';
5253
import DrillByModal from 'src/components/Chart/DrillBy/DrillByModal';
5354
import { useDatasetDrillInfo } from 'src/hooks/apiResources/datasets';
5455
import { ResourceStatus } from 'src/hooks/apiResources/apiResources';
@@ -106,6 +107,9 @@ const ChartContextMenu = (
106107
const crossFiltersEnabled = useSelector<RootState, boolean>(
107108
({ dashboardInfo }) => dashboardInfo.crossFiltersEnabled,
108109
);
110+
const hasCrossFilterTargets = useSelector<RootState, boolean>(state =>
111+
selectHasCrossFilterTargets(state, id),
112+
);
109113
const dashboardId = useSelector<RootState, number>(
110114
({ dashboardInfo }) => dashboardInfo.id,
111115
);
@@ -301,18 +305,20 @@ const ChartContextMenu = (
301305
});
302306

303307
if (showCrossFilters) {
308+
// Removing an applied cross-filter stays possible when the scope is empty
304309
const isCrossFilterDisabled =
305310
!isCrossFilteringSupportedByChart ||
306311
!crossFiltersEnabled ||
307-
!filters?.crossFilter;
312+
!filters?.crossFilter ||
313+
(!hasCrossFilterTargets && !filters.crossFilter.isCurrentValueSelected);
308314

309315
let crossFilteringTooltipTitle: ReactNode = null;
310316
if (!isCrossFilterDisabled) {
311317
crossFilteringTooltipTitle = (
312318
<>
313319
<div>
314320
{t(
315-
'Cross-filter will be applied to all of the charts that use this dataset.',
321+
"Cross-filter will be applied to the charts in this chart's cross-filtering scope.",
316322
)}
317323
</div>
318324
<div>
@@ -340,6 +346,16 @@ const ChartContextMenu = (
340346
<div>{t(`You can't apply cross-filter on this data point.`)}</div>
341347
</>
342348
);
349+
} else if (!hasCrossFilterTargets) {
350+
crossFilteringTooltipTitle = (
351+
<>
352+
<div>
353+
{t(
354+
"This chart's cross-filtering scope doesn't include any other charts.",
355+
)}
356+
</div>
357+
</>
358+
);
343359
}
344360

345361
menuItems.push(

‎superset-frontend/src/components/Chart/ChartRenderer.test.tsx‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,69 @@ test('semantic member filters still carry the source view identity', () => {
805805
});
806806
});
807807

808+
const appliedCrossFilter: DataMask = {
809+
extraFormData: { filters: [{ col: 'gender', op: 'IN', val: ['boy'] }] },
810+
filterState: { value: ['boy'] },
811+
};
812+
813+
test.each<[string, DataMask, DataMask | undefined]>([
814+
['drops an applied cross-filter', appliedCrossFilter, undefined],
815+
[
816+
'keeps the own state of a mask that also applies a cross-filter',
817+
{ ...appliedCrossFilter, ownState: { page: 2 } },
818+
{ ownState: { page: 2 } },
819+
],
820+
[
821+
'passes a cleared cross-filter through',
822+
{ extraFormData: { filters: [] }, filterState: { value: null } },
823+
{ extraFormData: { filters: [] }, filterState: { value: null } },
824+
],
825+
[
826+
'passes an own-state-only mask through',
827+
{ ownState: { page: 2 } },
828+
{ ownState: { page: 2 } },
829+
],
830+
])(
831+
'a chart whose cross-filter scope has no targets %s',
832+
(_name, mask, expected) => {
833+
const updateDataMask = jest.fn();
834+
render(
835+
<ChartRenderer
836+
{...requiredProps}
837+
hasCrossFilterTargets={false}
838+
formData={{ ...requiredProps.formData, testDataMask: mask }}
839+
actions={{ ...requiredProps.actions, updateDataMask }}
840+
/>,
841+
);
842+
fireEvent.click(screen.getByRole('button', { name: 'Emit test mask' }));
843+
if (expected) {
844+
expect(updateDataMask).toHaveBeenCalledWith(
845+
requiredProps.chartId,
846+
expected,
847+
);
848+
} else {
849+
expect(updateDataMask).not.toHaveBeenCalled();
850+
}
851+
},
852+
);
853+
854+
test('a chart whose cross-filter scope has targets applies cross-filters', () => {
855+
const updateDataMask = jest.fn();
856+
render(
857+
<ChartRenderer
858+
{...requiredProps}
859+
hasCrossFilterTargets
860+
formData={{ ...requiredProps.formData, testDataMask: appliedCrossFilter }}
861+
actions={{ ...requiredProps.actions, updateDataMask }}
862+
/>,
863+
);
864+
fireEvent.click(screen.getByRole('button', { name: 'Emit test mask' }));
865+
expect(updateDataMask).toHaveBeenCalledWith(
866+
requiredProps.chartId,
867+
appliedCrossFilter,
868+
);
869+
});
870+
808871
test('a SQL chart click mask is excluded from a versioned semantic receiver', () => {
809872
const mask: DataMask = {
810873
extraFormData: {

‎superset-frontend/src/components/Chart/ChartRenderer.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ export interface ChartRendererProps {
149149
postTransformProps?: (props: JsonObject) => JsonObject;
150150
source?: ChartSource;
151151
emitCrossFilters?: boolean;
152+
/** False when the chart's cross-filter scope reaches no other chart */
153+
hasCrossFilterTargets?: boolean;
152154
cacheBusterProp?: string;
153155
onChartStateChange?: (chartState: AgGridChartState) => void;
154156
suppressLoadingSpinner?: boolean;
@@ -265,6 +267,7 @@ function ChartRendererComponent({
265267
postTransformProps,
266268
source,
267269
emitCrossFilters,
270+
hasCrossFilterTargets = true,
268271
onChartStateChange,
269272
asyncModeOverride,
270273
} = restProps;
@@ -516,8 +519,23 @@ function ChartRendererComponent({
516519
// otherwise do under the spinner as soon as its query starts.
517520
const dataMaskSourceRef = useRef(latestQueryFormData ?? formData);
518521
dataMaskSourceRef.current = latestQueryFormData ?? formData;
522+
const hasCrossFilterTargetsRef = useRef(hasCrossFilterTargets);
523+
hasCrossFilterTargetsRef.current = hasCrossFilterTargets;
519524
const setDataMaskCallback = useCallback(
520-
(dataMask: DataMask) => {
525+
(incomingDataMask: DataMask) => {
526+
let dataMask = incomingDataMask;
527+
// A cross-filter from a chart whose scope reaches no other chart would
528+
// filter nothing, so only clearing a cross-filter or own state goes through.
529+
if (
530+
!hasCrossFilterTargetsRef.current &&
531+
incomingDataMask.filterState?.value !== null &&
532+
incomingDataMask.filterState?.value !== undefined
533+
) {
534+
if (incomingDataMask.ownState === undefined) {
535+
return;
536+
}
537+
dataMask = { ownState: incomingDataMask.ownState };
538+
}
521539
if (
522540
dataMask.filterState !== undefined ||
523541
dataMask.extraFormData !== undefined

‎superset-frontend/src/dashboard/components/gridComponents/Chart/Chart.test.tsx‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,53 @@ test('passes the form data of the last request to ChartContainer next to the cur
12881288
);
12891289
});
12901290

1291+
const stateWithCrossFilterScope = (chartsInScope?: number[]) => ({
1292+
...defaultState,
1293+
dashboardInfo: {
1294+
...defaultState.dashboardInfo,
1295+
crossFiltersEnabled: true,
1296+
metadata: chartsInScope
1297+
? {
1298+
chart_configuration: {
1299+
[queryId]: {
1300+
id: queryId,
1301+
crossFilters: { scope: 'global', chartsInScope },
1302+
},
1303+
},
1304+
}
1305+
: {},
1306+
},
1307+
});
1308+
1309+
test('keeps cross-filter rendering on but reports no targets when the chart cross-filter scope targets no other chart', () => {
1310+
// emitCrossFilters drives plugin rendering (e.g. Table V2's numbered column
1311+
// and cell truncation), so it must keep following the dashboard setting.
1312+
setup({}, stateWithCrossFilterScope([]));
1313+
expect(capturedChartContainerProps.emitCrossFilters).toBe(true);
1314+
expect(capturedChartContainerProps.hasCrossFilterTargets).toBe(false);
1315+
1316+
setup({}, stateWithCrossFilterScope([queryId]));
1317+
expect(capturedChartContainerProps.emitCrossFilters).toBe(true);
1318+
expect(capturedChartContainerProps.hasCrossFilterTargets).toBe(false);
1319+
});
1320+
1321+
test('reports cross-filter targets when the chart cross-filter scope targets another chart', () => {
1322+
setup({}, stateWithCrossFilterScope([queryId + 1]));
1323+
expect(capturedChartContainerProps.emitCrossFilters).toBe(true);
1324+
expect(capturedChartContainerProps.hasCrossFilterTargets).toBe(true);
1325+
});
1326+
1327+
test('reports cross-filter targets when the chart has no cross-filter configuration', () => {
1328+
setup({}, stateWithCrossFilterScope());
1329+
expect(capturedChartContainerProps.emitCrossFilters).toBe(true);
1330+
expect(capturedChartContainerProps.hasCrossFilterTargets).toBe(true);
1331+
});
1332+
1333+
test('does not emit cross-filters when cross-filtering is disabled on the dashboard', () => {
1334+
setup({}, defaultState);
1335+
expect(capturedChartContainerProps.emitCrossFilters).toBe(false);
1336+
});
1337+
12911338
test('dashboard Chart omits SQL cross-filter using the emitter datasource in Redux', () => {
12921339
setup(
12931340
{},

0 commit comments

Comments
 (0)