feat(insights): give ECG a routed home and index-surface presence

ECG recordings had no pill in the tab strip, no entry in the sub-page
manager, and no routed page — the only inbound pointer was a cross-link
card on the spot-pulse page. Hiding the overview ECG teaser left no route
back to a user's recordings.

- Add a routed `/insights/ecg` sub-page that reuses the existing ECG
  section (heading suppressed so the page shell owns the title); the
  overview teaser stays.
- Add an ECG entry to the Heart group of the tab strip, gated on a
  dedicated `hasEcgRecordings` signal since ECG carries no measurement
  type. The Heart group surfaces for an ECG-only device too. No ECG pill
  appears without recordings.
- Mount the ECG cross-link on the resting-pulse and HRV pages, matching
  its placement on the pulse page.
- List ECG in the sub-page manager under Heart, its eye toggling the ECG
  overview section so hiding it is reversible.
- Point the metric catalog's ECG row at the new page.
- Add `navEcg`, the sub-page description, and the empty-state copy across
  all locales.
This commit is contained in:
Marc-André Bombeck committed 2026-07-18 09:24:28 +02:00
1 parent e8f4bb3dba
commit adf80da989
19 files changed
+713 -32

No files matched your search

@@ -0,0 +1,100 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
/**
* v1.30 (UX/IA audit H1) — `<EcgCrossLink>` now mounts on the resting-pulse
* and HRV sub-pages (previously only on `/insights/pulse`), threaded through
* `<HealthKitMetricPage afterAssessment>`. The pointer self-gates on
* recordings, so the shared `insightsEcgList` cache is pre-seeded with one
* recording to exercise the mounted path.
*/
vi.mock("next/dynamic", () => ({
default: () => {
const Stub = () => <div data-slot="healthkit-chart-stub" />;
Stub.displayName = "HealthChartStub";
return Stub;
},
}));
vi.mock("@/hooks/use-auth", () => ({
useAuth: () => ({
user: { timezone: "UTC", dateOfBirth: null, gender: null },
isAuthenticated: true,
}),
}));
vi.mock("@/hooks/use-insights-layout-prefs", () => ({
useInsightsLayoutPrefs: () => ({ layout: null, compareBaseline: false }),
}));
const analyticsMock = vi.fn();
vi.mock("@/hooks/use-insights-analytics", () => ({
useInsightsAnalytics: () => analyticsMock(),
}));
const metricStatusMock = vi.fn();
vi.mock("@/hooks/use-insight-status", async (importOriginal) => {
const actual =
await importOriginal<typeof import("@/hooks/use-insight-status")>();
return {
...actual,
useInsightMetricStatus: (...args: unknown[]) => metricStatusMock(...args),
};
});
import RestingPulsePage from "@/app/insights/resting-pulse/page";
import HrvPage from "@/app/insights/hrv/page";
function render(node: React.ReactNode) {
const client = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity } },
});
// Seed the ECG list the cross-link reads so its data-availability gate opens.
client.setQueryData(queryKeys.insightsEcgList(), {
recordings: [{ id: "a", classification: null }],
hasRecordings: true,
});
return renderToStaticMarkup(
<QueryClientProvider client={client}>
<I18nProvider initialLocale="en">{node}</I18nProvider>
</QueryClientProvider>,
);
}
beforeEach(() => {
analyticsMock.mockReset();
metricStatusMock.mockReset();
// Data-bearing branch for both metric pages.
analyticsMock.mockReturnValue({
data: {
summaries: {
RESTING_HEART_RATE: { count: 5 },
HEART_RATE_VARIABILITY: { count: 5 },
},
},
isEmpty: false,
isLoading: false,
error: null,
refetch: vi.fn(),
});
metricStatusMock.mockReturnValue({ data: undefined, isLoading: false });
});
describe("EcgCrossLink mount on Heart sub-pages (H1)", () => {
it("mounts the ECG cross-link on the resting-pulse page", () => {
const html = render(<RestingPulsePage />);
expect(html).toContain('data-slot="ecg-cross-link"');
expect(html).toContain('href="/insights#ecg"');
});
it("mounts the ECG cross-link on the HRV page", () => {
const html = render(<HrvPage />);
expect(html).toContain('data-slot="ecg-cross-link"');
expect(html).toContain('href="/insights#ecg"');
});
});
@@ -0,0 +1,88 @@
import { describe, it, expect, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { I18nProvider } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
/**
* v1.30 (UX/IA audit H1) — `/insights/ecg` routed sub-page.
*
* The page reuses `<EcgSection>` (verbatim, `hideHeading` set so the shell
* owns the heading) and self-gates to an empty state when the account has no
* recordings. `useAuth` is mocked; the shared `insightsEcgList` cache is
* pre-seeded so both the page probe and the section read the same payload.
*/
vi.mock("@/hooks/use-auth", () => ({
useAuth: () => ({ isAuthenticated: true, user: null }),
}));
import InsightsEcgPage from "../page";
interface EcgRecord {
id: string;
recordedAt: string;
durationSeconds: number | null;
samplingFrequency: number;
sampleCount: number;
averageHeartRate: number | null;
lead: string | null;
classification: "IRREGULAR" | "NOT_DETECTED" | "INCONCLUSIVE" | null;
source: string;
hasWaveform: boolean;
}
function seededRecording(): EcgRecord {
return {
id: "rec-1",
recordedAt: "2026-07-10T08:00:00.000Z",
durationSeconds: 30,
samplingFrequency: 300,
sampleCount: 9000,
averageHeartRate: 62,
lead: null,
classification: null,
source: "withings",
hasWaveform: true,
};
}
function render(data: { recordings: EcgRecord[]; hasRecordings: boolean }) {
const client = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity } },
});
client.setQueryData(queryKeys.insightsEcgList(), data);
return renderToStaticMarkup(
<QueryClientProvider client={client}>
<I18nProvider initialLocale="en">
<InsightsEcgPage />
</I18nProvider>
</QueryClientProvider>,
);
}
describe("/insights/ecg page (H1)", () => {
it("renders the EcgSection with its disclaimer when recordings exist", () => {
const html = render({
recordings: [seededRecording()],
hasRecordings: true,
});
// The reused section + its load-bearing non-diagnostic disclaimer render.
expect(html).toContain('data-slot="ecg-section"');
expect(html).toContain('data-slot="ecg-card"');
expect(html).toContain('data-slot="ecg-disclaimer"');
// The shell owns the page heading (`<h1>`); the section's own
// `<SectionHeading>` (`<h2>`) is suppressed via `hideHeading`.
expect(html).toContain('id="insights-subpage-title"');
expect(html).not.toContain("<h2");
});
it("shows the empty state (no section) when the account has no recordings", () => {
const html = render({ recordings: [], hasRecordings: false });
expect(html).not.toContain('data-slot="ecg-section"');
expect(html).toContain("No ECG recordings yet");
// The empty-state CTA points at the integrations surface.
expect(html).toContain('href="/settings/integrations"');
});
});
+82
View File
@@ -0,0 +1,82 @@
"use client";
import Link from "next/link";
import { Activity } from "lucide-react";
import { useQuery } from "@tanstack/react-query";
import { useAuth } from "@/hooks/use-auth";
import { useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { apiGet } from "@/lib/api/api-fetch";
import { Button } from "@/components/ui/button";
import { SubPageShell } from "@/components/insights/sub-page-shell";
import { MetricEmptyState } from "@/components/insights/metric-empty-state";
import { EcgSection } from "@/components/insights/ecg-section";
/**
* v1.30 — `/insights/ecg` (UX/IA audit finding H1).
*
* Routed home for the ECG recording surface. Before this page the only
* inbound pointer to a user's ECGs was a cross-link card on the spot-pulse
* page; hiding the overview ECG teaser in "Anpassen" left no route back at
* all. This page gives ECG its own address, reached from the Heart-group
* pill in the tab strip (gated on `hasRecordings`) and the metric catalog.
*
* The surface itself is the existing `<EcgSection>`, reused verbatim — its
* non-diagnostic disclaimer + waveform logic are load-bearing and untouched.
* We only suppress its internal `<SectionHeading>` (via `hideHeading`) because
* `<SubPageShell>` already renders the page heading, mirroring how every other
* routed sub-page lets the shell own the `<h1>`.
*
* The overview `<EcgSection>` teaser on `/insights` stays as-is.
*/
export default function InsightsEcgPage() {
const { isAuthenticated } = useAuth();
const { t } = useTranslations();
// Reuse the SAME query cell `<EcgSection>` reads (`insightsEcgList`) so the
// page and the section share one cache entry. The page only needs the
// `hasRecordings` flag to choose between the empty state and the section.
const { data, isLoading } = useQuery({
queryKey: queryKeys.insightsEcgList(),
queryFn: () => apiGet<{ hasRecordings: boolean }>("/api/insights/ecg"),
enabled: isAuthenticated,
});
// Data-availability empty state — a direct URL hit (or deleted recordings)
// lands here without a section to show. Mirrors the sub-page convention:
// a calm one-line scaffold + a connect-a-device CTA, never a blank surface.
// While the probe is in flight we render nothing extra (the section self-
// gates too), so first paint stays quiet rather than flashing an empty card.
if (!isLoading && data && !data.hasRecordings) {
return (
<SubPageShell
title={t("insights.ecg.sectionTitle")}
description={t("insights.subPage.ecgDescription")}
>
<MetricEmptyState
icon={<Activity className="size-6" />}
title={t("insights.ecg.emptyTitle")}
description={t("insights.ecg.emptyDescription")}
cta={
<Button size="sm" asChild>
<Link href="/settings/integrations">
{t("insights.ecg.emptyCta")}
</Link>
</Button>
}
coachPrefill={null}
/>
</SubPageShell>
);
}
return (
<SubPageShell
title={t("insights.ecg.sectionTitle")}
description={t("insights.subPage.ecgDescription")}
>
<EcgSection enabled={isAuthenticated} hideHeading />
</SubPageShell>
);
}
+4
View File
@@ -3,6 +3,7 @@
import { Activity } from "lucide-react";
import { HealthKitMetricPage } from "@/components/insights/healthkit-metric-page";
import { EcgCrossLink } from "@/components/insights/ecg-cross-link";
/**
* v1.4.32 — `/insights/hrv`.
@@ -33,6 +34,9 @@ export default function InsightsHrvPage() {
emptyStateIcon={<Activity className="size-6" />}
emptyStateCtaType={null}
coachPrefill="I haven't logged any HRV data yet — what does heart-rate variability tell me, and how do I capture it?"
// S10 / H1 — device-attributed pointer into the ECG viewer, in the
// HRV / heart context. Self-gates to nothing without recordings.
afterAssessment={<EcgCrossLink />}
/>
);
}
+4
View File
@@ -3,6 +3,7 @@
import { Heart } from "lucide-react";
import { HealthKitMetricPage } from "@/components/insights/healthkit-metric-page";
import { EcgCrossLink } from "@/components/insights/ecg-cross-link";
import { useTranslations } from "@/lib/i18n/context";
/**
@@ -30,6 +31,9 @@ export default function InsightsRestingHrPage() {
emptyStateIcon={<Heart className="size-6" />}
emptyStateCtaType={null}
coachPrefill={t("insights.restingHr.coachPrefill")}
// S10 / H1 — device-attributed pointer into the ECG viewer, in the
// resting-HR context. Self-gates to nothing without recordings.
afterAssessment={<EcgCrossLink />}
/>
);
}