feat(insights): sleep-stages chart renders per-night stacked bars (7/14/30d toggle)

Marc directive 2026-05-14 — the /insights/schlaf chart now shows one
bar per night with DEEP / REM / CORE / AWAKE / IN_BED / ASLEEP stacked,
matching Apple Health's sleep tab. The 30-day average-composition
read is preserved as a fallback when the API doesn't yet ship
perNight (legacy clients during rollout).

Plumbing:
- src/app/api/analytics/route.ts: computeSleepStageBreakdown() also
  builds a per-Berlin-tz-day perNight array, sorted ascending. Empty
  days drop out — the chart treats them as gaps.
- src/components/insights/sleep-stage-stacked-bar.tsx: rewrites
  the visualisation. Default window = 7d; 14d + 30d toggles in the
  card header (same min-h-11 touch targets as the dashboard range
  tabs). Y-axis renders hours, tooltips show per-stage minutes +
  percent + nightly total.
- Mobile-first: window-toggle pills reflow under the title on <sm
  screens, sit inline on sm+. Bars use Recharts vertical layout
  (per the directive — no horizontal reflow on mobile, just tighter
  XAxis labels via preserveStartEnd).

i18n: no new keys — reuses insights.sleep.stages.* + the existing
compositionTitle / compositionSubtitle / compositionAriaLabel slots.

Tests: 4 new cases under sleep-stage-stacked-bar.test.tsx cover
- 7d default + three toggle buttons rendering
- empty-state (perNight=[] AND stages={} → 'sleep-stage-empty' slot)
- legacy fallback (no perNight → aggregate row still renders)
- sparse perNight (gaps don't crash the chart)
Existing 4 tests (title + aria + de locale + unknown stage key)
keep passing after perNight prop addition.
This commit is contained in:
Marc-André Bombeck committed 2026-05-14 14:08:56 +02:00
1 parent c50931b4e7
commit 7846d68dd5
6 files changed
+515 -129

No files matched your search

+112
View File
@@ -69,6 +69,38 @@ describe("MEASURE_TYPE_MAP", () => {
it("maps Withings meastype 123 (VO2 max) → VO2_MAX", () => {
expect(MEASURE_TYPE_MAP[123]).toEqual({ type: "VO2_MAX" });
});
// ── v1.4.25 W5d Withings full coverage ──
it("maps Withings meastype 5 (fat free mass) → FAT_FREE_MASS", () => {
expect(MEASURE_TYPE_MAP[5]).toEqual({ type: "FAT_FREE_MASS" });
});
it("maps Withings meastype 8 (fat mass) → FAT_MASS", () => {
expect(MEASURE_TYPE_MAP[8]).toEqual({ type: "FAT_MASS" });
});
it("maps Withings meastype 76 (muscle mass) → MUSCLE_MASS", () => {
expect(MEASURE_TYPE_MAP[76]).toEqual({ type: "MUSCLE_MASS" });
});
it("maps Withings meastype 73 (skin temperature) → SKIN_TEMPERATURE (distinct from BODY_TEMPERATURE)", () => {
expect(MEASURE_TYPE_MAP[73]).toEqual({ type: "SKIN_TEMPERATURE" });
// Sanity guard — sharing the BODY_TEMPERATURE bucket would corrupt
// the rollup (surface temps 30–34 °C, core ~37 °C).
expect(MEASURE_TYPE_MAP[73].type).not.toBe("BODY_TEMPERATURE");
});
it("maps Withings meastype 91 (pulse wave velocity) → PULSE_WAVE_VELOCITY", () => {
expect(MEASURE_TYPE_MAP[91]).toEqual({ type: "PULSE_WAVE_VELOCITY" });
});
it("maps Withings meastype 155 (vascular age) → VASCULAR_AGE", () => {
expect(MEASURE_TYPE_MAP[155]).toEqual({ type: "VASCULAR_AGE" });
});
it("maps Withings meastype 170 (visceral fat) → VISCERAL_FAT", () => {
expect(MEASURE_TYPE_MAP[170]).toEqual({ type: "VISCERAL_FAT" });
});
});
describe("WITHINGS_NOTIFY_APPLIS", () => {
@@ -166,3 +198,83 @@ describe("fetchMeasurements — temperature (meastype 12)", () => {
expect(out[0].value).toBe(36.83);
});
});
// ── v1.4.25 W5d Withings full coverage ──
describe("fetchMeasurements — body composition expansion", () => {
it("decodes a Body+ fat-free mass reading into FAT_FREE_MASS kg", async () => {
// 65.4 kg of lean body mass. value=65400, unit=-3.
installFetchMock(fakeGetmeasPayload([{ type: 5, value: 65400, unit: -3 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "FAT_FREE_MASS", value: 65.4 });
});
it("decodes a Body+ fat-mass reading into FAT_MASS kg", async () => {
// 14.2 kg of fat. value=14200, unit=-3.
installFetchMock(fakeGetmeasPayload([{ type: 8, value: 14200, unit: -3 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "FAT_MASS", value: 14.2 });
});
it("decodes a Body+ muscle-mass reading into MUSCLE_MASS kg", async () => {
// 58.7 kg of muscle. value=58700, unit=-3.
installFetchMock(fakeGetmeasPayload([{ type: 76, value: 58700, unit: -3 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "MUSCLE_MASS", value: 58.7 });
});
it("decodes a Body Comp visceral-fat rating into VISCERAL_FAT", async () => {
// Withings reports the 1–12 rating directly. value=7, unit=0.
installFetchMock(fakeGetmeasPayload([{ type: 170, value: 7, unit: 0 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "VISCERAL_FAT", value: 7 });
});
});
describe("fetchMeasurements — ScanWatch skin temperature (meastype 73)", () => {
it("decodes a ScanWatch skin-temp reading into SKIN_TEMPERATURE °C", async () => {
// 32.5 °C surface temp. value=3250, unit=-2.
installFetchMock(fakeGetmeasPayload([{ type: 73, value: 3250, unit: -2 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "SKIN_TEMPERATURE", value: 32.5 });
});
it("co-exists with core BODY_TEMPERATURE (meastype 71) in the same payload", async () => {
// A user with a Thermo + ScanWatch could have both readings on the
// same day. The rollup MUST keep them in distinct enums so the
// dashboard's "body temperature" chart doesn't paint a 32 °C dip.
installFetchMock(
fakeGetmeasPayload([
{ type: 73, value: 3210, unit: -2 },
{ type: 71, value: 3705, unit: -2 },
]),
);
const out = await fetchMeasurements("token");
expect(out).toHaveLength(2);
const types = new Set(out.map((r) => r.type));
expect(types.has("SKIN_TEMPERATURE")).toBe(true);
expect(types.has("BODY_TEMPERATURE")).toBe(true);
});
});
describe("fetchMeasurements — pulse-wave velocity + vascular age", () => {
it("decodes a Body Cardio PWV reading into PULSE_WAVE_VELOCITY m/s", async () => {
// 7.2 m/s arterial pulse wave velocity. value=72, unit=-1.
installFetchMock(fakeGetmeasPayload([{ type: 91, value: 72, unit: -1 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "PULSE_WAVE_VELOCITY", value: 7.2 });
});
it("decodes a Body Scan vascular-age reading into VASCULAR_AGE years", async () => {
// 42 years biological vascular age. value=42, unit=0.
installFetchMock(fakeGetmeasPayload([{ type: 155, value: 42, unit: 0 }]));
const out = await fetchMeasurements("token");
expect(out).toHaveLength(1);
expect(out[0]).toMatchObject({ type: "VASCULAR_AGE", value: 42 });
});
});
+17
View File
@@ -145,6 +145,23 @@ export const MEASURE_TYPE_MAP: Record<number, { type: string; factor?: number }>
// v1.4.25 — VO2 max from the ScanWatch family. Withings reports
// mL/(kg·min) directly, matching the canonical DB unit.
123: { type: "VO2_MAX" },
// ── v1.4.25 W5d — Withings full coverage ──
// Body composition expansion (Body+, Body Cardio, Body Comp, Body
// Scan ship these values on every measurement). All canonical kg.
5: { type: "FAT_FREE_MASS" }, // Fat-free mass
8: { type: "FAT_MASS" }, // Fat mass (kg form of BODY_FAT)
76: { type: "MUSCLE_MASS" }, // Muscle mass
// Skin temperature (ScanWatch dermal sensor). Distinct from
// BODY_TEMPERATURE — surface temps ~32 °C, core ~37 °C. Same °C
// unit; the SKIN_TEMPERATURE enum value keeps the rollup honest.
73: { type: "SKIN_TEMPERATURE" },
// Pulse-wave velocity m/s — Body Cardio / Body Scan exclusive.
91: { type: "PULSE_WAVE_VELOCITY" },
// Vascular age in years — Body Scan composite of PWV + age.
155: { type: "VASCULAR_AGE" },
// Visceral fat rating (Withings 1–12 scale). Stored under the
// canonical VISCERAL_FAT enum with `rating` as the unit string.
170: { type: "VISCERAL_FAT" },
};
export interface WithingsMeasure {
+14 -13
View File
@@ -14,7 +14,9 @@ Audit + scoring of the full surface lives in
| meastype | Withings name | DB enum | DB unit | Source devices |
|---|---|---|---|---|
| 1 | Weight | `WEIGHT` | kg | every Body / Body+ / Body Cardio / Body Comp / Body Scan |
| 5 | Fat Free Mass | `FAT_FREE_MASS` | kg | Body+ / Body Cardio / Body Comp / Body Scan |
| 6 | Fat Ratio | `BODY_FAT` | % | Body+ family |
| 8 | Fat Mass Weight | `FAT_MASS` | kg | Body+ / Body Cardio / Body Comp / Body Scan |
| 9 | Diastolic BP | `BLOOD_PRESSURE_DIA` | mmHg | BPM Connect / Core / Vision |
| 10 | Systolic BP | `BLOOD_PRESSURE_SYS` | mmHg | BPM Connect / Core / Vision |
| 11 | Heart Pulse | `PULSE` | bpm | BPM cuffs + scales (standing HR) |
@@ -22,9 +24,14 @@ Audit + scoring of the full surface lives in
| 35 | SpO2 (alt code) | `OXYGEN_SATURATION` | % | older firmware reports 35 instead of 54 |
| 54 | SpO2 | `OXYGEN_SATURATION` | % | ScanWatch / BPM Vision |
| 71 | Body Temperature (core) | `BODY_TEMPERATURE` | celsius | current-gen Thermo |
| 73 | Skin Temperature | `SKIN_TEMPERATURE` | celsius | ScanWatch dermal sensor. Distinct from `BODY_TEMPERATURE` — surface temps run ~32 °C, core ~37 °C; sharing the bucket would corrupt analytics. |
| 76 | Muscle Mass | `MUSCLE_MASS` | kg | Body+ / Body Cardio / Body Comp / Body Scan |
| 77 | Hydration / Water Mass | `TOTAL_BODY_WATER` | kg | Body Comp / Body Scan |
| 88 | Bone Mass | `BONE_MASS` | kg | Body Comp / Body Scan |
| 91 | Pulse Wave Velocity | `PULSE_WAVE_VELOCITY` | m/s | Body Cardio / Body Scan exclusive |
| 123 | VO2 max | `VO2_MAX` | mL/(kg·min) | ScanWatch family |
| 155 | Vascular Age | `VASCULAR_AGE` | years | Body Scan; composite of PWV + chronological age |
| 170 | Visceral Fat | `VISCERAL_FAT` | rating | Body Comp / Body Scan; Withings' 1–12 scale (not a percent) |
### Unit handling
@@ -49,25 +56,19 @@ routines in v1.4.26.
## Deferred — v1.4.26
| meastype | Withings name | Why deferred |
|---|---|---|
| 5 | Fat Free Mass | needs new `FAT_FREE_MASS` enum |
| 8 | Fat Mass Weight | needs new `FAT_MASS` enum |
| 73 | Skin Temperature | distinct physiology from core; needs `SKIN_TEMPERATURE` enum |
| 76 | Muscle Mass | needs new `MUSCLE_MASS` enum |
| 91 | Pulse Wave Velocity | Body Cardio / Body Scan exclusive; new enum |
| 155 | Vascular Age | new enum, composite-derived years |
| 170 | Visceral Fat | new enum, rating |
Plus the new sync routines:
The body-composition expansion (meastypes 5, 8, 73, 76, 91, 155, 170)
landed in **v1.4.25 W5d** — see migration `0049_withings_full_metrics`.
Deferred items now:
- **Sleep** (`POST /v2/sleep?action=getsummary`) → `SLEEP_DURATION`,
`HEART_RATE_VARIABILITY` (sdnn_1), per-stage rows via existing
`SleepStage` enum.
- **Activity** (`POST /v2/measure?action=getactivity`) → `ACTIVITY_STEPS`,
`WALKING_RUNNING_DISTANCE`, `FLIGHTS_CLIMBED`, `ACTIVE_ENERGY_BURNED`.
**Requires OAuth scope upgrade** to `users.activity` — every existing
Withings connection has to reconnect once.
**Requires OAuth scope upgrade** to `user.activity` — every existing
Withings connection has to reconnect once. v1.4.25 W5d ships the
scope-upgrade plumbing + reconnect banner; the activity sync routine
itself lands in v1.4.26.
## Deferred — v1.5 (with iOS app)