keep hidden events hidden across date-range changes

Clicking a legend item on the events chart toggled hidden via
chart.current.getDatasetMeta(idx).hidden, which lives on Chart.js's
per-dataset meta object. Each time chartData changed (date-range
switch, refetch, focusLabel update) the second useEffect in Chart.tsx
replaces datasets wholesale, Chart.js regenerates the meta, and the
hidden flags vanish, so previously-toggled-off events came back on
their own.

Lift the hidden state into React. EventsChart owns a Set<string> of
hidden labels and passes it down via a new optional hiddenLabels prop
on Chart, plus an onLegendClick callback for controlled toggling.
Chart re-applies hidden after the existing focusLabel pass so the set
survives every data refresh, and falls back to the original
meta-based behaviour when no callback is provided so other charts
(website overview, revenue) keep their existing semantics.

Verified with the seeded Demo SaaS data: hide signup_started in the
Last 24 hours view, switch to Last 7 days then Last 30 days, the
label stays greyed in the legend and absent from the bars; clicking
again restores it. State is component-scoped, so it intentionally
resets on reload or navigation away from the events page.
This commit is contained in:
Yan committed 2026-05-08 08:38:15 -04:00
1 parent a9508e7aae
commit 4f72d17faf
2 files changed
+37 -1

No files matched your search

+12
View File
@@ -26,6 +26,16 @@ export function EventsChart({ websiteId, focusLabel, limit }: EventsChartProps)
const { locale, dateLocale } = useLocale();
const { data, isLoading, error } = useWebsiteEventsSeriesQuery(websiteId, { limit });
const [label, setLabel] = useState<string>(focusLabel);
const [hiddenLabels, setHiddenLabels] = useState<Set<string>>(() => new Set());
const handleLegendClick = useCallback((legendLabel: string, willBeHidden: boolean) => {
setHiddenLabels(prev => {
const next = new Set(prev);
if (willBeHidden) next.add(legendLabel);
else next.delete(legendLabel);
return next;
});
}, []);
const chartData: any = useMemo(() => {
if (!data) return;
@@ -87,6 +97,8 @@ export function EventsChart({ websiteId, focusLabel, limit }: EventsChartProps)
stacked={true}
renderXLabel={renderXLabel}
height="400px"
hiddenLabels={hiddenLabels}
onLegendClick={handleLegendClick}
/>
)}
</LoadingPanel>