Merge remote-tracking branch 'origin/work/parity-coherence' into release/v1.30.34

This commit is contained in:
Marc-André Bombeck committed 2026-07-19 12:46:04 +02:00
commit 4c65949bac
21 files changed
+601 -61

No files matched your search

@@ -22,8 +22,11 @@ function hoursMinutes(totalMinutes: number): {
/**
* v1.17.0 — sleep-debt headline.
*
* The cumulative shortfall between sleep need and sleep obtained over the
* rolling window. Under the night threshold it shows the calm `partial`
* The outstanding balance between sleep need and sleep obtained over the
* rolling window — a night below need adds to it, a night above need pays it
* down (v1.19.0 replaced the summed shortfall with this running balance; the
* copy follows the arithmetic). Under the night threshold it shows the calm
* `partial`
* state ("still learning — N more nights") and never asserts a total off thin
* data. Mobile-first: a single readable figure + one line of grounded context.
*/
@@ -267,6 +267,21 @@ describe("settings sections — SSR smoke", () => {
expect(html).not.toContain("settings.sections.thresholds.");
});
it("<ThresholdsSection> carries the glucose reference band control", () => {
// The declared diabetes opt-in (`PATCH /api/auth/me/diabetes`) selects the
// tighter glucose band and grounds the Coach, but shipped with no web
// control at all — a web-only account could never reach it. It belongs on
// Targets: same question ("which range judges my reading") as the editor
// below it, and the copy must stay honest about being a reference band
// rather than a diagnosis.
const html = renderFramed("thresholds", <ThresholdsSection />);
expect(html).toContain("Glucose reference band");
expect(html).toContain("Use the diabetes goal band");
expect(html).toContain("not a diagnosis");
expect(html).toContain('data-slot="glucose-reference-disclaimer"');
expect(html).not.toContain("settings.glucoseReference.");
});
it("<ThresholdsSection> resolves the German title", () => {
const html = renderFramed("thresholds", <ThresholdsSection />, "de");
// v1.8.7.1 — German title is "Zielwerte" (Targets only).
@@ -0,0 +1,110 @@
"use client";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Droplet, Info } from "lucide-react";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
import { SettingsCard } from "@/components/settings/settings-card";
import { SettingsCardHeader } from "@/components/settings/_card-header";
import { useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { apiGet, apiPatch } from "@/lib/api/api-fetch";
/**
* Glucose reference band — the web control for the declared diabetes opt-in
* (`GET/PATCH /api/auth/me/diabetes`).
*
* WHY IT LIVES ON TARGETS
* -----------------------
* The flag answers exactly the question this page answers — "which range is my
* reading judged against" — and the page already owns the four glucose target
* rows. Putting it anywhere else would split one concept across two surfaces.
* The card sits ABOVE the per-metric editor because the two controls are
* ordered: this one picks the default band, a per-metric override below then
* beats it (`src/lib/targets/glucose-targets.ts`).
*
* WHAT IT DOES NOT DO
* -------------------
* The flag is a display preference, never a diagnosis, and is never inferred
* from a reading. It selects the band the glucose surfaces and the Coach's
* reference grounding read; the app takes no clinical action on it. The copy
* says so at the point of the switch rather than in a help page.
*
* Note: `GET /api/user/thresholds` (the editor below) reports the general
* bands regardless of this flag — the flag resolves in the targets pipeline
* (`src/lib/targets/build-response.ts`), which is why the mutation invalidates
* `insightsTargets()` rather than `userThresholds()`.
*/
interface DiabetesPrefShape {
hasDiabetes: boolean;
}
export function GlucoseReferenceCard() {
const { t } = useTranslations();
const queryClient = useQueryClient();
const { data } = useQuery({
queryKey: queryKeys.diabetesPref(),
queryFn: async () => apiGet<DiabetesPrefShape>("/api/auth/me/diabetes"),
});
const toggle = useMutation({
mutationFn: async (hasDiabetes: boolean) =>
apiPatch<DiabetesPrefShape>("/api/auth/me/diabetes", { hasDiabetes }),
onSuccess: (next) => {
queryClient.setQueryData(queryKeys.diabetesPref(), next);
void queryClient.invalidateQueries({
queryKey: queryKeys.insightsTargets(),
});
},
});
const enabled = data?.hasDiabetes ?? false;
return (
<SettingsCard>
<SettingsCardHeader
icon={Droplet}
title={t("settings.glucoseReference.title")}
description={t("settings.glucoseReference.description")}
/>
<div className="mt-4 space-y-4 pl-7">
<div className="flex items-start justify-between gap-4">
<Label htmlFor="glucose-reference-diabetes" className="text-sm">
{t("settings.glucoseReference.enable")}
</Label>
<Switch
id="glucose-reference-diabetes"
checked={enabled}
disabled={toggle.isPending || data == null}
onCheckedChange={(v) => toggle.mutate(v)}
className="mt-0.5 shrink-0"
/>
</div>
<p className="text-sm">{t("settings.glucoseReference.explainer")}</p>
<div
data-slot="glucose-reference-disclaimer"
className="border-border bg-muted/40 flex items-start gap-2.5 rounded-lg border-l-2 p-3"
>
<Info
className="text-muted-foreground mt-0.5 h-4 w-4 shrink-0"
aria-hidden="true"
/>
<p className="text-foreground/80 text-xs leading-relaxed">
{t("settings.glucoseReference.disclaimer")}
</p>
</div>
{toggle.isError ? (
<p className="text-destructive text-sm" role="alert">
{t("settings.glucoseReference.error")}
</p>
) : null}
</div>
</SettingsCard>
);
}
+11 -2
View File
@@ -1,5 +1,6 @@
"use client";
import { GlucoseReferenceCard } from "@/components/settings/glucose-reference-card";
import { ThresholdsEditorSection } from "@/components/settings/thresholds-editor-section";
/**
@@ -31,6 +32,14 @@ import { ThresholdsEditorSection } from "@/components/settings/thresholds-editor
* the names stop clashing.
*/
export function ThresholdsSection() {
// Per-metric threshold ranges; the heading is supplied by the route frame.
return <ThresholdsEditorSection id="thresholds" />;
// The glucose reference band sits ABOVE the per-metric editor: it picks the
// DEFAULT band a glucose reading is judged against, and a per-metric
// override in the editor below then beats it. Same question, one page —
// splitting the two across surfaces would hide the precedence.
return (
<div className="space-y-4">
<GlucoseReferenceCard />
<ThresholdsEditorSection id="thresholds" />
</div>
);
}