mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
feat(dashboard): let the top card be switched off
The layout settings gain a switch for the dashboard's top card (the Today card above the tiles). Stored server-side on the layout blob as `todayCardVisible`: missing means shown, only an explicit false hides it, the default is omitted from storage, a save that omits the field keeps the stored choice, and a layout reset shows the card again. Hidden, the card leaves no skeleton or error card behind. The flag rides the account-settings backup with the rest of the layout. The field is deliberately not `heroVisible`, the retired opt-in hero's flag that older stored layouts may still carry. An end-to-end journey on its own account switches the card off and on and walks the score histories' range tabs.
This commit is contained in:
1 parent
62140a4aee
commit
668f568289
21 files changed
+714
-10
No files matched your search
@@ -42,6 +42,17 @@ describe("dashboard hero — page wiring", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("hides the top card, its skeleton and its error card only on an explicit false", () => {
|
||||
// One gate in front of the whole three-way branch, so switching the card
|
||||
// off leaves no reserve and no retry card where it stood.
|
||||
const gate = src.indexOf("layout.todayCardVisible === false ? null :");
|
||||
expect(gate).toBeGreaterThan(-1);
|
||||
const hero = src.indexOf("<TodayHero", gate);
|
||||
const skeleton = src.indexOf("<TodayHeroSkeleton", gate);
|
||||
expect(hero).toBeGreaterThan(gate);
|
||||
expect(skeleton).toBeGreaterThan(hero);
|
||||
});
|
||||
|
||||
it("no longer references the retired legacy hero", () => {
|
||||
expect(src).not.toContain("DashboardHero");
|
||||
expect(src).not.toContain("DashboardHeroSkeleton");
|
||||
|
||||
@@ -929,7 +929,9 @@ describe("DELETE /api/dashboard/widgets — web-owned reset scope", () => {
|
||||
selectedScoreRings: ["READINESS", "SLEEP_SCORE"],
|
||||
heroRingOrder: ["SLEEP_SCORE", "HEALTH_SCORE", "READINESS"],
|
||||
enabledHeroItemKinds: ["milestone"],
|
||||
todayCardVisible: false,
|
||||
});
|
||||
expect(stored.todayCardVisible).toBe(false);
|
||||
const advanced = new Date("2026-07-24T12:00:00.000Z");
|
||||
transactionUser.findUnique.mockResolvedValue({
|
||||
dashboardWidgetsJson: stored,
|
||||
@@ -958,6 +960,10 @@ describe("DELETE /api/dashboard/widgets — web-owned reset scope", () => {
|
||||
expect(
|
||||
Object.prototype.hasOwnProperty.call(persisted, "enabledHeroItemKinds"),
|
||||
).toBe(false);
|
||||
// A reset brings a hidden top card back; shown is stored by omission.
|
||||
expect(
|
||||
Object.prototype.hasOwnProperty.call(persisted, "todayCardVisible"),
|
||||
).toBe(false);
|
||||
expect(persisted.comparisonBaseline).toBe("lastYear");
|
||||
expect(persisted.chartOverlayPrefs).toEqual({
|
||||
weight: {
|
||||
@@ -1076,6 +1082,96 @@ describe("dashboard widgets — hero primary content", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("dashboard widgets — the top card switch", () => {
|
||||
const weightOnly = [
|
||||
{ id: "weight" as const, visible: true, tileVisible: true, order: 0 },
|
||||
];
|
||||
|
||||
function persistedLayout() {
|
||||
const updateArg = vi.mocked(prisma.user.update).mock
|
||||
.calls[0]?.[0] as unknown as {
|
||||
data: { dashboardWidgetsJson: { todayCardVisible?: boolean } };
|
||||
};
|
||||
return updateArg.data.dashboardWidgetsJson;
|
||||
}
|
||||
|
||||
it("accepts and persists todayCardVisible: false", async () => {
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
dashboardWidgetsJson: null,
|
||||
} as never);
|
||||
vi.mocked(prisma.user.update).mockResolvedValue({} as never);
|
||||
|
||||
const res = await callPut(
|
||||
makeReq({ version: 1, widgets: weightOnly, todayCardVisible: false }),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(persistedLayout().todayCardVisible).toBe(false);
|
||||
const body = (await res.json()) as {
|
||||
data: { todayCardVisible?: boolean };
|
||||
};
|
||||
expect(body.data.todayCardVisible).toBe(false);
|
||||
});
|
||||
|
||||
it("rejects a non-boolean with 422", async () => {
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
dashboardWidgetsJson: null,
|
||||
} as never);
|
||||
vi.mocked(prisma.user.update).mockResolvedValue({} as never);
|
||||
|
||||
const res = await callPut(
|
||||
makeReq({ version: 1, widgets: weightOnly, todayCardVisible: "no" }),
|
||||
);
|
||||
expect(res.status).toBe(422);
|
||||
expect(vi.mocked(prisma.user.update)).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps a hidden top card hidden when the client omits the field", async () => {
|
||||
// A save from a client that predates the field must not bring it back.
|
||||
const stored = serializeDashboardLayout({
|
||||
version: 1,
|
||||
widgets: weightOnly,
|
||||
todayCardVisible: false,
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
dashboardWidgetsJson: stored,
|
||||
} as never);
|
||||
vi.mocked(prisma.user.update).mockResolvedValue({} as never);
|
||||
|
||||
const res = await callPut(makeReq({ version: 1, widgets: weightOnly }));
|
||||
expect(res.status).toBe(200);
|
||||
expect(persistedLayout().todayCardVisible).toBe(false);
|
||||
});
|
||||
|
||||
it("stores showing it again as the omitted default", async () => {
|
||||
const stored = serializeDashboardLayout({
|
||||
version: 1,
|
||||
widgets: weightOnly,
|
||||
todayCardVisible: false,
|
||||
});
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
dashboardWidgetsJson: stored,
|
||||
} as never);
|
||||
vi.mocked(prisma.user.update).mockResolvedValue({} as never);
|
||||
|
||||
const res = await callPut(
|
||||
makeReq({ version: 1, widgets: weightOnly, todayCardVisible: true }),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(persistedLayout()).not.toHaveProperty("todayCardVisible");
|
||||
});
|
||||
|
||||
it("GET answers true for a layout that never chose", async () => {
|
||||
vi.mocked(prisma.user.findUnique).mockResolvedValue({
|
||||
dashboardWidgetsJson: serializeDashboardLayout(DEFAULT_DASHBOARD_LAYOUT),
|
||||
} as never);
|
||||
const res = await callGet();
|
||||
const body = (await res.json()) as {
|
||||
data: { todayCardVisible?: boolean };
|
||||
};
|
||||
expect(body.data.todayCardVisible).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("a toggle that cannot do anything is not offered", () => {
|
||||
// A switch that is on over a tile that can never paint, with nothing
|
||||
// saying why, is the defect the HRV fallback exists to remove — so the
|
||||
|
||||
@@ -170,6 +170,10 @@ const layoutSchema = z.object({
|
||||
// Same preserve-when-absent contract as `comparisonBaseline`; the
|
||||
// resolver drops unknown stored values back to "score" on read.
|
||||
hero: z.enum(HERO_PRIMARY_CONTENTS).optional(),
|
||||
// v1.42 — whether the dashboard shows its top card. Same additive,
|
||||
// preserve-when-absent contract as `hero`; the default (shown) is omitted
|
||||
// from storage.
|
||||
todayCardVisible: z.boolean().optional(),
|
||||
// v1.32.16 (issue #581) — the optimistic-concurrency base token used to
|
||||
// live here. v1.32.21 (R5a) moved it to the shared `takeBaseToken` helper,
|
||||
// which strips it BEFORE this parse: the token is transport, not a layout
|
||||
@@ -503,6 +507,8 @@ export const DELETE = apiHandler(async () => {
|
||||
...existingLayout,
|
||||
widgets: DEFAULT_DASHBOARD_LAYOUT.widgets,
|
||||
enabledHeroItemKinds: DEFAULT_DASHBOARD_LAYOUT.enabledHeroItemKinds,
|
||||
// A reset brings a hidden top card back.
|
||||
todayCardVisible: DEFAULT_DASHBOARD_LAYOUT.todayCardVisible,
|
||||
});
|
||||
|
||||
const updated = await tx.user.update({
|
||||
|
||||
@@ -954,7 +954,10 @@ export default function DashboardPageClient({
|
||||
// rehydrates from disk. The error / empty branches stay reachable only
|
||||
// post-mount. DO NOT add `["daily", …]` to that allowlist, or render
|
||||
// the hero from any client-only source, without revisiting this gate.
|
||||
digestQuery.data ? (
|
||||
// v1.42 — the top card can be switched off in the layout settings;
|
||||
// only an explicit `false` hides it, and with it its skeleton and
|
||||
// its error card.
|
||||
layout.todayCardVisible === false ? null : digestQuery.data ? (
|
||||
<TodayHero
|
||||
digest={digestQuery.data}
|
||||
renderFilteredAllClear={renderFilteredHeroAllClear}
|
||||
|
||||
@@ -621,3 +621,44 @@ describe("<DashboardLayoutSection> — hero content", () => {
|
||||
expect(html).toContain("Notification settings are managed separately.");
|
||||
});
|
||||
});
|
||||
|
||||
describe("<DashboardLayoutSection> — the top card switch", () => {
|
||||
function control(html: string): string {
|
||||
const match = html.match(/<button[^>]*data-slot="today-card-switch"[^>]*>/);
|
||||
expect(match).not.toBeNull();
|
||||
return match![0];
|
||||
}
|
||||
|
||||
it("offers one labelled switch, on by default", () => {
|
||||
const html = render(<DashboardLayoutSection id="dashboard-layout" />);
|
||||
expect(html.match(/data-slot="today-card-switch"/g)).toHaveLength(1);
|
||||
expect(control(html)).toContain('data-state="checked"');
|
||||
const id = control(html).match(/id="([^"]+)"/)![1];
|
||||
expect(html).toContain(`for="${id}"`);
|
||||
expect(html).toContain("Show the top card");
|
||||
expect(html).toContain("Using default layout");
|
||||
});
|
||||
|
||||
it("reads a layout that predates the field as shown", () => {
|
||||
const legacy: DashboardLayout = { ...DEFAULT_DASHBOARD_LAYOUT };
|
||||
delete legacy.todayCardVisible;
|
||||
queryState.layout = legacy;
|
||||
const html = render(<DashboardLayoutSection id="dashboard-layout" />);
|
||||
expect(control(html)).toContain('data-state="checked"');
|
||||
});
|
||||
|
||||
it("shows a hidden top card as off, and as a customised layout", () => {
|
||||
queryState.layout = {
|
||||
...DEFAULT_DASHBOARD_LAYOUT,
|
||||
todayCardVisible: false,
|
||||
};
|
||||
const html = render(<DashboardLayoutSection id="dashboard-layout" />);
|
||||
expect(control(html)).toContain('data-state="unchecked"');
|
||||
expect(html).toContain("Custom layout active");
|
||||
});
|
||||
|
||||
it("labels it in German", () => {
|
||||
const html = render(<DashboardLayoutSection id="dashboard-layout" />, "de");
|
||||
expect(html).toContain("Obere Karte anzeigen");
|
||||
});
|
||||
});
|
||||
@@ -207,6 +207,7 @@ export function DashboardLayoutSection({ id }: { id: string }) {
|
||||
const heroDescriptionId = useId();
|
||||
const heroNotificationNoteId = useId();
|
||||
const heroContentSelectId = useId();
|
||||
const todayCardSwitchId = useId();
|
||||
// v1.34 — the "Today highlights" fieldset below is gated on `layout`,
|
||||
// which can be truthy on the very first client render whenever a
|
||||
// returning visitor's browser already has a warm, offline-persisted
|
||||
@@ -404,6 +405,12 @@ export function DashboardLayoutSection({ id }: { id: string }) {
|
||||
});
|
||||
}
|
||||
|
||||
/** v1.42 — the server-persisted on/off switch for the dashboard's top card. */
|
||||
function setTodayCardVisible(visible: boolean) {
|
||||
if (!layout) return;
|
||||
setDraft({ ...layout, todayCardVisible: visible });
|
||||
}
|
||||
|
||||
/**
|
||||
* Hero primary content — the server-persisted `hero` field on the layout
|
||||
* blob. The value set is closed ("score" | "reminders"); anything else a
|
||||
@@ -483,6 +490,20 @@ export function DashboardLayoutSection({ id }: { id: string }) {
|
||||
<p id={heroDescriptionId} className="text-muted-foreground text-xs">
|
||||
{t("dashboard.heroItemsDescription")}
|
||||
</p>
|
||||
{/* v1.42 — the top card itself can be switched off. Shown is the
|
||||
default; the choices below shape the card while it is shown. */}
|
||||
<div className="flex min-h-11 items-center justify-between gap-3">
|
||||
<Label htmlFor={todayCardSwitchId} className="text-sm font-medium">
|
||||
{t("dashboard.todayCardVisible")}
|
||||
</Label>
|
||||
<Switch
|
||||
id={todayCardSwitchId}
|
||||
checked={layout.todayCardVisible !== false}
|
||||
onCheckedChange={setTodayCardVisible}
|
||||
disabled={saveMutation.isPending}
|
||||
data-slot="today-card-switch"
|
||||
/>
|
||||
</div>
|
||||
{/* Hero primary content — a single labelled select beside the
|
||||
highlight toggles. "score" keeps the health-score read;
|
||||
"reminders" promotes the highlight rail into the hero slot. */}
|
||||
@@ -758,6 +779,7 @@ export function DashboardLayoutSection({ id }: { id: string }) {
|
||||
<Button
|
||||
size="sm"
|
||||
className="min-h-11 sm:min-h-9"
|
||||
data-slot="settings-dashboard-layout-save"
|
||||
onClick={() => layout && saveMutation.mutate(layout)}
|
||||
disabled={saveMutation.isPending}
|
||||
>
|
||||
@@ -777,7 +799,8 @@ export function DashboardLayoutSection({ id }: { id: string }) {
|
||||
JSON.stringify(DEFAULT_DASHBOARD_LAYOUT.widgets) &&
|
||||
JSON.stringify(layout.enabledHeroItemKinds ?? PRIORITY_ITEM_KINDS) ===
|
||||
JSON.stringify(DEFAULT_DASHBOARD_LAYOUT.enabledHeroItemKinds) &&
|
||||
(layout.hero ?? "score") === DEFAULT_DASHBOARD_LAYOUT.hero
|
||||
(layout.hero ?? "score") === DEFAULT_DASHBOARD_LAYOUT.hero &&
|
||||
layout.todayCardVisible !== false
|
||||
? t("dashboard.layoutUsingDefaults")
|
||||
: t("dashboard.layoutCustomized")}
|
||||
</p>
|
||||
|
||||
@@ -1061,6 +1061,7 @@ describe("layout field merge disposition", () => {
|
||||
heroRingOrder: ["HEALTH_SCORE", "MED_COMPLIANCE"],
|
||||
enabledHeroItemKinds: [...PRIORITY_ITEM_KINDS],
|
||||
hero: "reminders",
|
||||
todayCardVisible: false,
|
||||
};
|
||||
|
||||
it("assigns a disposition to every top-level layout field", () => {
|
||||
@@ -1087,6 +1088,7 @@ describe("layout field merge disposition", () => {
|
||||
"hero",
|
||||
"heroRingOrder",
|
||||
"selectedScoreRings",
|
||||
"todayCardVisible",
|
||||
"widgets",
|
||||
]);
|
||||
});
|
||||
@@ -1226,3 +1228,55 @@ describe("resolveDashboardLayout() — hero primary content", () => {
|
||||
expect(resolveDashboardLayout(serialized).hero).toBe("reminders");
|
||||
});
|
||||
});
|
||||
|
||||
describe("todayCardVisible — the dashboard top card switch", () => {
|
||||
const base = {
|
||||
version: 1,
|
||||
widgets: [{ id: "weight", visible: true, order: 0 }],
|
||||
};
|
||||
|
||||
it("shows the top card by default", () => {
|
||||
expect(DEFAULT_DASHBOARD_LAYOUT.todayCardVisible).toBe(true);
|
||||
expect(resolveDashboardLayout(base).todayCardVisible).toBe(true);
|
||||
expect(resolveDashboardLayout(null).todayCardVisible).toBe(true);
|
||||
});
|
||||
|
||||
it("hides it only on an explicit false", () => {
|
||||
expect(
|
||||
resolveDashboardLayout({ ...base, todayCardVisible: false })
|
||||
.todayCardVisible,
|
||||
).toBe(false);
|
||||
for (const value of [true, "false", 0, null]) {
|
||||
expect(
|
||||
resolveDashboardLayout({ ...base, todayCardVisible: value })
|
||||
.todayCardVisible,
|
||||
).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("is not driven by the retired heroVisible flag", () => {
|
||||
expect(
|
||||
resolveDashboardLayout({ ...base, heroVisible: false }).todayCardVisible,
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("stores false and omits the default", () => {
|
||||
const hidden = serializeDashboardLayout(
|
||||
resolveDashboardLayout({ ...base, todayCardVisible: false }),
|
||||
);
|
||||
expect(hidden.todayCardVisible).toBe(false);
|
||||
const shown = serializeDashboardLayout(resolveDashboardLayout(base));
|
||||
expect(shown).not.toHaveProperty("todayCardVisible");
|
||||
});
|
||||
|
||||
it("round-trips through serialize and resolve", () => {
|
||||
const stored = serializeDashboardLayout({
|
||||
...resolveDashboardLayout(base),
|
||||
todayCardVisible: false,
|
||||
});
|
||||
expect(
|
||||
resolveDashboardLayout(JSON.parse(JSON.stringify(stored)))
|
||||
.todayCardVisible,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -617,6 +617,15 @@ export interface DashboardLayout {
|
||||
* `"score"`.
|
||||
*/
|
||||
hero?: HeroPrimaryContent;
|
||||
/**
|
||||
* v1.42 — whether the dashboard shows its top card (the Today card above
|
||||
* the tiles) at all. Optional and additive: missing resolves to `true`,
|
||||
* and the serializer omits the default, so an untouched account's blob
|
||||
* stays as it was and clients that predate the field round-trip it
|
||||
* untouched. Deliberately not `heroVisible`: that name belonged to the
|
||||
* retired opt-in hero and old blobs may still carry it.
|
||||
*/
|
||||
todayCardVisible?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -651,6 +660,7 @@ export const LAYOUT_FIELD_MERGE_DISPOSITION = {
|
||||
heroRingOrder: "preserve",
|
||||
enabledHeroItemKinds: "preserve",
|
||||
hero: "preserve",
|
||||
todayCardVisible: "preserve",
|
||||
} as const satisfies Record<keyof DashboardLayout, "replace" | "preserve">;
|
||||
|
||||
/**
|
||||
@@ -725,6 +735,7 @@ export const DEFAULT_DASHBOARD_LAYOUT: DashboardLayout = {
|
||||
heroRingOrder: [...DEFAULT_HERO_RING_ORDER],
|
||||
enabledHeroItemKinds: [...PRIORITY_ITEM_KINDS],
|
||||
hero: DEFAULT_HERO_PRIMARY_CONTENT,
|
||||
todayCardVisible: true,
|
||||
widgets: [
|
||||
{ id: "weight", visible: true, tileVisible: true, order: 0 },
|
||||
{ id: "bp", visible: true, tileVisible: true, order: 1 },
|
||||
@@ -899,6 +910,8 @@ export function resolveDashboardLayout(raw: unknown): DashboardLayout {
|
||||
// default "score" so a stale client cannot swap the hero to a
|
||||
// composition the renderer doesn't know how to draw.
|
||||
hero: coerceHeroPrimaryContent(candidate.hero),
|
||||
// Only an explicit `false` hides the top card; anything else shows it.
|
||||
todayCardVisible: candidate.todayCardVisible !== false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -952,5 +965,7 @@ export function serializeDashboardLayout(
|
||||
// contract as `enabledHeroItemKinds`), so an untouched account's blob
|
||||
// is byte-identical to the pre-field shape.
|
||||
...(hero === DEFAULT_HERO_PRIMARY_CONTENT ? {} : { hero }),
|
||||
// Shown is the default and is omitted the same way.
|
||||
...(layout.todayCardVisible === false ? { todayCardVisible: false } : {}),
|
||||
};
|
||||
}
|
||||
@@ -251,6 +251,19 @@ describe("admitAccountSettings", () => {
|
||||
expect(coachReasoningLevel(parseCoachPrefs(restored))).toBe("high");
|
||||
});
|
||||
|
||||
it("restores a hidden dashboard top card with the rest of the layout", () => {
|
||||
const layout = {
|
||||
version: 1,
|
||||
widgets: [{ id: "weight", visible: true, tileVisible: true, order: 0 }],
|
||||
todayCardVisible: false,
|
||||
};
|
||||
const restored = admitAccountSettings(
|
||||
JSON.parse(JSON.stringify({ dashboardWidgetsJson: layout })),
|
||||
ctx,
|
||||
).data.dashboardWidgetsJson;
|
||||
expect(restored).toEqual(layout);
|
||||
});
|
||||
|
||||
it("writes a database null for a JSON setting the file clears", () => {
|
||||
expect(
|
||||
admitAccountSettings({ dashboardWidgetsJson: null }, ctx).data
|
||||
|
||||
@@ -78,11 +78,17 @@ const dashboardLayoutSchema = z
|
||||
.describe(
|
||||
'Primary hero-card content on the dashboard: "score" (the health-score read, default) or "reminders" (the Today-highlight rail promoted into the hero slot). Missing resolves to "score"; the stored default is omitted from responses.',
|
||||
),
|
||||
todayCardVisible: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.describe(
|
||||
"v1.42 — whether the dashboard shows its top card (the Today card above the tiles). Missing resolves to `true`; only an explicit `false` hides it. The stored default is omitted. A layout reset (DELETE) shows it again. Additive.",
|
||||
),
|
||||
})
|
||||
.meta({
|
||||
id: "DashboardLayoutBody",
|
||||
description:
|
||||
"Per-user dashboard widget layout. `widgets` is the ordered tile and chart list. `comparisonBaseline`, `chartOverlayPrefs`, `selectedScoreRings`, `heroRingOrder`, `enabledHeroItemKinds`, and `hero` are additive dashboard preferences. Every field except `version` is optional on input and preserved when absent from PUT. Missing `enabledHeroItemKinds` resolves to every current kind, while an empty array hides every Today highlight. Widget ids use the closed dashboard catalogue; unknown ids are dropped before validation.",
|
||||
"Per-user dashboard widget layout. `widgets` is the ordered tile and chart list. `comparisonBaseline`, `chartOverlayPrefs`, `selectedScoreRings`, `heroRingOrder`, `enabledHeroItemKinds`, `hero`, and `todayCardVisible` are additive dashboard preferences. Every field except `version` is optional on input and preserved when absent from PUT. Missing `enabledHeroItemKinds` resolves to every current kind, while an empty array hides every Today highlight. Widget ids use the closed dashboard catalogue; unknown ids are dropped before validation.",
|
||||
});
|
||||
|
||||
const dashboardLayoutPutBody = dashboardLayoutSchema
|
||||
|
||||
Reference in new issue
Block a user