feat(dashboard): top tiles selectable in layout settings

Until v1.4.14 the layout schema in `lib/dashboard-layout.ts` had a
single `visible` flag per widget that controlled BOTH the upper-row
strip tile AND the lower-row chart for the same metric. Marc wanted
them as independent toggles — to keep the chart visible for tracking
without the tile cluttering the strip, or vice versa.

Schema upgrade (back-compat):

- New optional `tileVisible: boolean` on `DashboardWidgetConfig`. When
  omitted on a saved layout, `resolveDashboardLayout()` mirrors it
  from `visible` so legacy users see no behavioural change until they
  flip the new switch. `serializeDashboardLayout()` always writes the
  field explicitly so a re-read keeps the user's choice.
- Zod input schema (`/api/dashboard/widgets`) marks the field
  optional so older clients (web v1.4.14 cache, native iOS) can keep
  PUTting their existing payload shape.
- Default layout sets `tileVisible: true` for the seven default-on
  widgets and `tileVisible: false` for the six default-off ones.

Settings UI:

- The single-column toggle list grows a SECOND switch column. Header
  row labels them "Tile" / "Kachel" and "Chart" / "Chart". Each widget
  now paints two switches (`data-slot="widget-tile-switch"` and
  `widget-chart-switch`) with discrete `aria-label`s
  `<metric> — Tile` / `<metric> — Chart` so a screen-reader user can
  tell them apart.

Dashboard render (`src/app/page.tsx`):

- `isWidgetVisible(id)` split into `isTileVisible(id)` (new flag
  with mirror fallback) and `isChartVisible(id)` (legacy flag). Each
  metric now has a `showXxxTile` AND `showXxxChart` gate. Glucose
  has no separate chart slot today, so its single flag still maps
  to the tile flag.

Tests:

- `resolveDashboardLayout()`: mirrors tileVisible from visible for
  legacy layouts, respects an explicit value, auto-appends new
  widgets with both flags `false`, defaults gracefully on null
  input.
- `serializeDashboardLayout()`: persists tileVisible explicitly,
  derives from visible when missing, normalises order to dense
  0-based.
- `<DashboardLayoutSection>` SSR smoke: column headers paint EN +
  DE, both switch slots present, exactly one tile-switch + one
  chart-switch per widget.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-09 20:37:22 +02:00
1 parent 4e2386e2d1
commit 8ccdfac280
8 files changed
+356 -51

No files matched your search

@@ -0,0 +1,75 @@
import { describe, it, expect, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { DEFAULT_DASHBOARD_LAYOUT } from "@/lib/dashboard-layout";
/**
* v1.4.15 Fix 5 — independent strip-tile + chart toggles.
*
* The settings section grew a SECOND switch column for the upper-row
* tile, distinct from the existing chart switch. SSR smoke tests are
* sufficient here; full state-mutation testing would require a DOM
* runtime which the rest of the settings suite doesn't pull in.
*/
vi.mock("@tanstack/react-query", () => ({
useQuery: () => ({ data: DEFAULT_DASHBOARD_LAYOUT, isLoading: false }),
useQueryClient: () => ({ setQueryData: vi.fn() }),
useMutation: () => ({
mutate: vi.fn(),
mutateAsync: vi.fn(),
isPending: false,
}),
}));
vi.mock("sonner", () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
},
}));
import { I18nProvider } from "@/lib/i18n/context";
import { DashboardLayoutSection } from "../dashboard-layout-section";
function render(node: React.ReactElement, locale: "en" | "de" = "en") {
return renderToStaticMarkup(
<I18nProvider initialLocale={locale}>{node}</I18nProvider>,
);
}
describe("<DashboardLayoutSection> — tile + chart split", () => {
it("renders both Tile and Chart column headers in English", () => {
const html = render(<DashboardLayoutSection id="dashboard-layout" />);
// Column headers — the v1.4.15 split.
expect(html).toContain("Tile");
expect(html).toContain("Chart");
});
it("renders both column headers in German", () => {
const html = render(
<DashboardLayoutSection id="dashboard-layout" />,
"de",
);
expect(html).toContain("Kachel");
expect(html).toContain("Chart");
});
it("paints both switch slots per widget", () => {
const html = render(<DashboardLayoutSection id="dashboard-layout" />);
// The two new data-slots distinguish tile vs chart switches in
// visual-verify and other consumers.
expect(html).toContain('data-slot="widget-tile-switch"');
expect(html).toContain('data-slot="widget-chart-switch"');
});
it("each widget paints exactly one tile-switch and one chart-switch", () => {
const html = render(<DashboardLayoutSection id="dashboard-layout" />);
const tileSwitches = html.match(/data-slot="widget-tile-switch"/g) ?? [];
const chartSwitches = html.match(/data-slot="widget-chart-switch"/g) ?? [];
// Default layout has 13 widgets — both switch counts must match
// exactly so the split control covers every widget.
expect(tileSwitches).toHaveLength(DEFAULT_DASHBOARD_LAYOUT.widgets.length);
expect(chartSwitches).toHaveLength(DEFAULT_DASHBOARD_LAYOUT.widgets.length);
});
});
@@ -97,6 +97,23 @@ export function DashboardLayoutSection({ id }: { id: string }) {
});
}
/**
* v1.4.15 Fix 5 — independent toggle for the *strip tile* (the upper
* row of trend cards). Until v1.4.14 a single switch controlled both
* the tile AND the chart for the same metric, which Marc found too
* coarse: he wanted a chart visible without the tile (for metrics he
* tracks without wanting the at-a-glance number) or vice versa.
*/
function toggleTile(widgetId: DashboardWidgetId, tileVisible: boolean) {
if (!layout) return;
setDraft({
...layout,
widgets: layout.widgets.map((w) =>
w.id === widgetId ? { ...w, tileVisible } : w,
),
});
}
function move(widgetId: DashboardWidgetId, delta: -1 | 1) {
if (!layout) return;
const sorted = [...layout.widgets].sort((a, b) => a.order - b.order);
@@ -146,10 +163,26 @@ export function DashboardLayoutSection({ id }: { id: string }) {
</div>
) : (
<div className="space-y-2">
{/* v1.4.15 Fix 5 — table-style header naming the two
switches. The "tile" column controls the strip tile in
the upper row; the "chart" column controls the line
chart in the lower row. Marc wanted independent control
of the two surfaces (memory feedback_dashboard_top_tiles
_selectable.md). */}
<div className="text-muted-foreground flex items-center gap-3 px-3 pb-1 text-[10px] font-medium tracking-wide uppercase">
<span className="w-5" aria-hidden="true" />
<span className="flex-1" />
<span className="w-12 text-center">{t("dashboard.layoutTileColumn")}</span>
<span className="w-12 text-center">{t("dashboard.layoutChartColumn")}</span>
</div>
{[...layout.widgets]
.sort((a, b) => a.order - b.order)
.map((widget, index, arr) => {
const labelKey = WIDGET_LABEL_KEYS[widget.id] ?? widget.id;
const tileChecked =
typeof widget.tileVisible === "boolean"
? widget.tileVisible
: widget.visible;
return (
<div
key={widget.id}
@@ -182,12 +215,24 @@ export function DashboardLayoutSection({ id }: { id: string }) {
</Button>
</div>
<span className="flex-1 text-sm">{t(labelKey)}</span>
<Switch
checked={widget.visible}
onCheckedChange={(v) => toggle(widget.id, v)}
aria-label={t(labelKey)}
disabled={saveMutation.isPending}
/>
<div className="w-12 flex justify-center">
<Switch
checked={tileChecked}
onCheckedChange={(v) => toggleTile(widget.id, v)}
aria-label={`${t(labelKey)} — ${t("dashboard.layoutTileColumn")}`}
disabled={saveMutation.isPending}
data-slot="widget-tile-switch"
/>
</div>
<div className="w-12 flex justify-center">
<Switch
checked={widget.visible}
onCheckedChange={(v) => toggle(widget.id, v)}
aria-label={`${t(labelKey)} — ${t("dashboard.layoutChartColumn")}`}
disabled={saveMutation.isPending}
data-slot="widget-chart-switch"
/>
</div>
</div>
);
})}