Address review: label key, gate every feed, retire the dead switch

Five points from the review, in the order they were raised.

1. The RMSSD marker did not survive. The tile label paints truncated,
uppercase and on one line, so an appended `· RMSSD` was the first thing
dropped in the longer locales — the part a reader needs to tell a 40 ms
RMSSD night from a collapsed SDNN one. `measurements.typeHrvRmssd` already
exists in all seven bundles, so each series now carries its own key.
`pickHrvSummary` returns that key beside the series it chose, which keeps
one mapping from series to label rather than a conditional at the call site.

2. The module gate bit on one path only. `gateSummariesByModules` was
private to the snapshot builder, and with the snapshot flag off the tiles
are fed by `/api/analytics`, which filtered nothing — so recovery-off plus
RMSSD-only still painted the tile, on the exact path the helper's docblock
promised it could not. The filter moves next to the map it reads, and both
analytics slices apply it. Gated after the cache read, so the cached cell
stays module-independent and toggling a module takes effect on the next
request instead of waiting out the TTL.

3. The same class one step on: recovery off plus RMSSD only left a switch
that was on, a tile that could never paint, and no explanation. The repo
already calls that a dead switch and hides the row; HRV is only dead
sometimes, so the answer needs one fact the Settings screen does not have.
The server resolves it — `unavailableWidgetIds` on the widgets payload —
and Settings drops those rows. An account with SDNN keeps the toggle: its
tile still works with recovery off, because SDNN is a vital no module owns.
The SDNN probe is an index-only existence check that runs only when
recovery is off, outside the layout cache so the row goes the moment the
module does.

4. The brittle test sliced from the helper to the end of the file, so any
function added below it would have gone red pointing at HRV. Bounded to the
function's own body, with a guard on the bound itself. The three wiring
assertions matched formatted source text — they would have passed just as
well if the tile never rendered — and are replaced by a test that renders
the tile through a real TrendCard and asserts the reading is on screen.
One negative source check is kept, naming the expression the bug lived on
rather than the formatting of its replacement.

5. Rebased onto v1.38.6; the changelog entry sits under [Unreleased].

Not verified here: `pnpm db:generate` cannot run in this container (the
Prisma engine download is refused), so snapshot.test.ts, the widgets route
test and the analytics route test could not load, and typecheck is
incomplete. Lint, format and the HRV suite (17 tests) pass.
This commit is contained in:
Antiheld86 committed 2026-09-03 07:25:58 +00:00
1 parent 4594b664e3
commit 6aac2070b4
11 files changed
+551 -235

No files matched your search

+7
View File
@@ -709,6 +709,13 @@ export function mergePreservedLayoutFields(
*/
export type DashboardLayoutWithToken = DashboardLayout & {
updatedAt?: string;
/**
* Widget ids the server resolved as unavailable for this account — a
* toggle that cannot do anything, which Settings must not offer. See
* `unavailableWidgetIds`. Optional and additive: a client that ignores
* it behaves exactly as before.
*/
unavailableWidgetIds?: string[];
};
const DASHBOARD_LAYOUT_VERSION = 1;
+15 -31
View File
@@ -130,7 +130,8 @@ export {
} from "@/lib/dashboard/widget-modules";
import {
WIDGET_MODULE_BY_ID,
disabledSummaryTypes,
gateSummariesByModules,
unavailableWidgetIds,
} from "@/lib/dashboard/widget-modules";
const GLUCOSE_CONTEXTS = [
@@ -1078,7 +1079,16 @@ function isThickPhaseWarm(coverage: RollupCoverageMap): boolean {
function gateLayoutByModules(
layout: DashboardLayout,
modules: Record<ModuleKey, boolean>,
summaries: Record<string, DataSummary>,
): DashboardLayout {
// Widgets that cannot paint for THIS account, on the same rule the
// Settings screen hides their toggle with — so the layout the dashboard
// renders and the switches the user is offered never disagree.
const unavailable = new Set(
unavailableWidgetIds(modules, {
hasSdnn: (summaries.HEART_RATE_VARIABILITY?.count ?? 0) > 0,
}),
);
return {
...layout,
widgets: layout.widgets.map((w) => {
@@ -1086,40 +1096,14 @@ function gateLayoutByModules(
if (moduleKey && modules[moduleKey] === false) {
return { ...w, visible: false, tileVisible: false };
}
if (unavailable.has(w.id)) {
return { ...w, visible: false, tileVisible: false };
}
return w;
}),
};
}
/**
* v1.18.0 — strip disabled-module measurement types from the slim slice
* so neither `tiles.summaries` / `tiles.lastSeenByType` nor the derived
* `metricStates` carry data for a module the user turned off. Returns a
* shallow copy; the inputs are not mutated.
*/
function gateSummariesByModules(
summaries: Record<string, DataSummary>,
lastSeenByType: Record<string, { lastSeenAt: string } | null>,
modules: Record<ModuleKey, boolean>,
): {
summaries: Record<string, DataSummary>;
lastSeenByType: Record<string, { lastSeenAt: string } | null>;
} {
// The drop decision is shared with `GET /api/dashboard/summary` — see
// `disabledSummaryTypes`. Only the filtering below is snapshot-shaped.
const dropped = disabledSummaryTypes(modules);
if (dropped.size === 0) return { summaries, lastSeenByType };
const outSummaries: Record<string, DataSummary> = {};
for (const [type, summary] of Object.entries(summaries)) {
if (!dropped.has(type)) outSummaries[type] = summary;
}
const outLastSeen: Record<string, { lastSeenAt: string } | null> = {};
for (const [type, slot] of Object.entries(lastSeenByType)) {
if (!dropped.has(type)) outLastSeen[type] = slot;
}
return { summaries: outSummaries, lastSeenByType: outLastSeen };
}
/**
* v1.21.2 (A4) — map the machine-derived coach memory block onto the briefing
* card's `{ recall, forward }` shape, both already-localised. `recall` is the
@@ -1372,7 +1356,7 @@ export async function buildDashboardSnapshot(
});
}
const layout = gateLayoutByModules(storedLayout, modules);
const layout = gateLayoutByModules(storedLayout, modules, slim.summaries);
// v1.18.0 — the Daily Briefing is the dashboard's AI-narrative surface,
// so the `insights` module gates it alongside the operator briefing
// flag + per-user coach opt-out. Disabling `insights` yields the same
+66
View File
@@ -113,6 +113,72 @@ export function disabledSummaryTypes(
return dropped;
}
/**
* Strip disabled-module types from a summaries slice. Lifted out of
* `@/lib/dashboard/snapshot`, where it was private, once a second
* dashboard feed needed it: with `NEXT_PUBLIC_DASHBOARD_SNAPSHOT=false`
* the tile strip hydrates from `/api/analytics` instead of the snapshot,
* and that route filtered nothing — so a module the user had turned off
* still reached the client, and the tile it owns still painted. The gate
* has to sit on every feed that carries summaries, not just the default
* one, or it is only as strong as a rollout flag.
*
* Pure over its inputs and browser-safe (string keys + the map above), so
* the snapshot builder, the analytics route and any future feed share one
* implementation rather than each carrying a copy that can drift.
* Returns shallow copies; the inputs are not mutated.
*/
/**
* Widget ids whose toggle cannot do anything for THIS account, and so must
* not be offered at all. `WIDGET_MODULE_BY_ID` above answers the same
* question for widgets a module owns outright; this answers it for the one
* widget whose deadness also depends on the account's data.
*
* `hrv` is that widget. The tile takes SDNN when the account has it and
* falls back to nightly RMSSD otherwise (see `pickHrvSummary`). RMSSD is
* recovery-owned, so with Recovery off it is stripped from every feed —
* which leaves a ring / strap account, whose only HRV is RMSSD, holding a
* switch that is on and a tile that can never paint, with nothing saying
* why. That is precisely the defect the RMSSD fallback exists to remove, so
* it must not survive one step further on. An account WITH SDNN keeps the
* row: its tile still works with Recovery off, because SDNN is a plain
* vital that no module owns.
*
* Decided from server-resolved facts and handed to the client, so the
* Settings screen does not have to fetch dashboard data to find out.
*/
export function unavailableWidgetIds(
modules: Partial<Record<ModuleKey, boolean>>,
facts: { hasSdnn: boolean },
): string[] {
const out: string[] = [];
if (modules.recovery === false && !facts.hasSdnn) out.push("hrv");
return out;
}
export function gateSummariesByModules<
S extends Record<string, unknown>,
L extends Record<string, unknown>,
>(
summaries: S,
lastSeenByType: L,
modules: Partial<Record<ModuleKey, boolean>>,
): { summaries: S; lastSeenByType: L } {
const dropped = disabledSummaryTypes(modules);
if (dropped.size === 0) return { summaries, lastSeenByType };
const outSummaries = {} as S;
for (const [type, summary] of Object.entries(summaries)) {
if (!dropped.has(type))
(outSummaries as Record<string, unknown>)[type] = summary;
}
const outLastSeen = {} as L;
for (const [type, slot] of Object.entries(lastSeenByType)) {
if (!dropped.has(type))
(outLastSeen as Record<string, unknown>)[type] = slot;
}
return { summaries: outSummaries, lastSeenByType: outLastSeen };
}
/**
* iOS `MetricKind` (the `kind` on a `GET /api/dashboard/summary` metric
* card) → the `MeasurementType` it is built from. The summary route emits