fix(dashboard): stop the chart-row render loop on tab resume

A user reported the dashboard crashing on returning to a backgrounded
tab. The on-focus snapshot refetch re-rendered the page, and each chart
received a fresh inline `() => markChartReady(id)` handler every render;
the chart's data-ready notify effect, keyed on that ever-changing prop,
re-fired on every commit. That per-commit passive effect kept the chart
row's anchored popovers re-committing until the browser tripped its
render-depth guard and the page fell back to its error card.

Latch the notify behind a ref so it fires once, when the chart's initial
query settles, and depend on the load flag alone. The handler is read
through a ref so a late-arriving callback still fires without re-arming
the effect. Extract the pure fire-once decision into a small helper with
direct unit coverage (no DOM runner, per project convention).
This commit is contained in:
Marc-André Bombeck committed 2026-06-23 21:45:42 +02:00
1 parent 009d923c87
commit 2c112c3323
8 files changed
+210 -10

No files matched your search

@@ -0,0 +1,88 @@
/**
* v1.20.1 — regression cover for the dashboard chart-row render loop.
*
* A user reported the dashboard crashing (blank error card) on returning
* to a backgrounded tab. The snapshot query refetches on window focus,
* re-rendering the page; every chart was handed an inline
* `() => markChartReady(id)` closure, so the chart's `onDataReady` notify
* effect — keyed on that ever-changing prop — re-fired on every commit.
* That per-commit passive effect kept the tile strip's Radix-Popper
* anchors re-committing until React tripped its update-depth guard
* (minified React error #185).
*
* The fix latches the notify behind a ref so it fires exactly once on the
* first non-loading commit and depends on `isLoading` alone. This pins the
* pure decision and the charts' wiring so the latch cannot silently revert
* to the per-render notify.
*/
import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { shouldFireDataReady } from "@/lib/charts/data-ready-latch";
describe("shouldFireDataReady", () => {
it("holds while the chart's initial query is still loading", () => {
expect(shouldFireDataReady({ isLoading: true, alreadyFired: false })).toBe(
false,
);
});
it("fires once on the first non-loading commit", () => {
expect(shouldFireDataReady({ isLoading: false, alreadyFired: false })).toBe(
true,
);
});
it("never re-fires after it has fired once, even though the chart keeps re-rendering", () => {
// Models the tab-resume path: the parent re-renders repeatedly (a new
// `onDataReady` closure each time) while `isLoading` stays false. The
// latch must decline every subsequent commit so the notify cannot
// drive an unbounded render loop.
let fired = false;
let fireCount = 0;
for (let commit = 0; commit < 60; commit += 1) {
if (shouldFireDataReady({ isLoading: false, alreadyFired: fired })) {
fired = true;
fireCount += 1;
}
}
expect(fireCount).toBe(1);
});
it("a chart that never loaded data still fires once the moment it settles", () => {
// Loading first, then settled — fires on the settle commit, then holds.
expect(shouldFireDataReady({ isLoading: true, alreadyFired: false })).toBe(
false,
);
expect(shouldFireDataReady({ isLoading: false, alreadyFired: false })).toBe(
true,
);
expect(shouldFireDataReady({ isLoading: false, alreadyFired: true })).toBe(
false,
);
});
});
describe("chart notify wiring", () => {
const charts = [
"src/components/charts/health-chart.tsx",
"src/components/charts/mood-chart.tsx",
"src/components/charts/medication-compliance-chart.tsx",
];
for (const rel of charts) {
const src = readFileSync(join(process.cwd(), rel), "utf8");
it(`${rel} latches the data-ready notify once, not per render`, () => {
// Routes through the pure latch.
expect(src).toContain("shouldFireDataReady(");
// Reads the handler through a ref so a changing prop identity never
// re-arms the effect.
expect(src).toContain("onDataReadyRef.current");
// The notify effect depends on `isLoading` ALONE — never on the
// unstable `onDataReady` prop (the loop trigger).
expect(src).not.toMatch(/\}, \[isLoading, onDataReady\]\)/);
});
}
});
+35
View File
@@ -0,0 +1,35 @@
/**
* v1.20.1 — pure decision behind the charts' one-shot `onDataReady`
* notify.
*
* The dashboard's shared reveal gate (`useDashboardChartReveal`) listens
* for each chart reporting its data query settled. Every chart receives
* an inline `() => markChartReady(id)` closure from the dashboard, so the
* `onDataReady` prop is a NEW reference on every parent render. Keying the
* notify effect on that prop re-ran it on every commit — and a refetch on
* tab-resume (`refetchOnWindowFocus`) re-rendered the page repeatedly,
* which kept that per-commit passive effect firing and the chart row's
* Radix-Popper anchors re-committing until React tripped its update-depth
* guard (the minified React error #185 a user reported on returning to a
* backgrounded tab).
*
* The ready signal is monotonic: once a chart's initial query has settled
* it never un-settles (a later range-tab change opens a fresh cache entry,
* but the gate has long latched by then). So the notify must fire exactly
* once — on the first non-loading commit — and never again, regardless of
* the `onDataReady` prop identity.
*
* This helper is the pure core of that latch, exported so the gate has
* direct unit coverage without mounting React (project convention:
* SSR-only tests, no DOM runner). The chart wires it to a `useRef` flag.
*/
export function shouldFireDataReady(input: {
/** The chart's query is still on its initial load. */
isLoading: boolean;
/** Whether the notify has already fired this mount. */
alreadyFired: boolean;
}): boolean {
if (input.isLoading) return false;
if (input.alreadyFired) return false;
return true;
}