mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
The per-metric target editor receives a glucose range already projected into the user's display unit (mmol/L) along with `unit="mmol/L"`, but it seeded, validated and saved that value verbatim. HealthLog stores glucose canonically in mg/dL, so a user on the mmol/L preference either had a valid target (e.g. 3.9–5.5 mmol/L) rejected by the 40–400 mg/dL bounds, or — when an override already existed — saw a mg/dL number rendered as if it were mmol/L and wrote it straight back. Add `mmolToMgdl` / `toCanonicalMgdl` (the inverse of `convertGlucose`) and teach the edit sheet to treat the display unit at its boundary: seed the persisted mg/dL override into the display unit, validate against bounds expressed in the display unit, and convert the typed value back to canonical mg/dL before the PUT. A no-op for every non-glucose metric and for users on the mg/dL preference. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> (cherry picked from commit 0cc0361058a85f32d7135e5f2fd425c98f62d885)
513 lines
19 KiB
TypeScript
513 lines
19 KiB
TypeScript
"use client";
|
||
|
||
import { useEffect, useId, useRef, useState } from "react";
|
||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||
import { toast } from "sonner";
|
||
import { RotateCcw } from "lucide-react";
|
||
|
||
import { ResponsiveSheet } from "@/components/ui/responsive-sheet";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Input } from "@/components/ui/input";
|
||
import { Label } from "@/components/ui/label";
|
||
import { useTranslations } from "@/lib/i18n/context";
|
||
import { queryKeys } from "@/lib/query-keys";
|
||
import {
|
||
METRIC_BOUNDS,
|
||
type ThresholdMetric,
|
||
type EffectiveRange,
|
||
} from "@/lib/analytics/effective-range";
|
||
import { convertGlucose, toCanonicalMgdl } from "@/lib/glucose";
|
||
|
||
/**
|
||
* Per-metric target editor, mounted inline on the Insights metric
|
||
* reference panel (`<MetricTargetSummary>`). The user adjusts the
|
||
* min/max for the underlying threshold metric, hits Save, and every
|
||
* surface that consumes `getEffectiveRange()` (dashboard bands,
|
||
* doctor-report, the Insights panels themselves) updates on the next
|
||
* query refetch.
|
||
*
|
||
* Server contract: PUT /api/user/thresholds with a partial map
|
||
* `{ [metric]: { min, max } }`. The endpoint validates the values
|
||
* against `METRIC_BOUNDS` and persists them under
|
||
* `User.thresholdsJson`. The cached query `["user", "thresholds"]` is
|
||
* invalidated on success so the same dialog re-opens with the new
|
||
* values.
|
||
*
|
||
* Visual contract: a small Dialog (not a Sheet drawer) so the
|
||
* interaction reads as "I'm editing one specific metric, not a global
|
||
* settings page". Mobile-first: full-width buttons under sm:, inline
|
||
* row from sm: up.
|
||
*
|
||
* Mapping target-card type → ThresholdMetric:
|
||
* BLOOD_PRESSURE_IN_TARGET / BMI / MOOD_SCORE / MOOD_STABILITY /
|
||
* MEDICATION_COMPLIANCE / BLOOD_GLUCOSE_RANDOM
|
||
* → no editable threshold (the metric is a derived score). The
|
||
* cog still renders (consistency rule) but disables the Save
|
||
* button + shows an explanatory caption that this card derives
|
||
* from other metrics.
|
||
* BLOOD_PRESSURE
|
||
* → edits BOTH BLOOD_PRESSURE_SYS and BLOOD_PRESSURE_DIA in a
|
||
* single Save round-trip; the parent page receives the
|
||
* diastolic range from the targets API.
|
||
* Everything else maps 1:1 to the same enum literal.
|
||
*/
|
||
|
||
const TARGET_TYPE_TO_METRIC: Record<string, ThresholdMetric | null> = {
|
||
WEIGHT: "WEIGHT",
|
||
PULSE: "PULSE",
|
||
SLEEP_DURATION: "SLEEP_DURATION",
|
||
BODY_FAT: "BODY_FAT",
|
||
ACTIVITY_STEPS: "ACTIVITY_STEPS",
|
||
BLOOD_GLUCOSE_FASTING: "BLOOD_GLUCOSE_FASTING",
|
||
BLOOD_GLUCOSE_POSTPRANDIAL: "BLOOD_GLUCOSE_POSTPRANDIAL",
|
||
BLOOD_GLUCOSE_RANDOM: "BLOOD_GLUCOSE_RANDOM",
|
||
BLOOD_GLUCOSE_BEDTIME: "BLOOD_GLUCOSE_BEDTIME",
|
||
// BLOOD_PRESSURE handled specially with both sys + dia.
|
||
// BMI / MOOD_SCORE / MOOD_STABILITY / MEDICATION_COMPLIANCE /
|
||
// BLOOD_PRESSURE_IN_TARGET — derived, no editable threshold.
|
||
};
|
||
|
||
interface CurrentRange {
|
||
min: number;
|
||
max: number;
|
||
}
|
||
|
||
interface ThresholdsApiResponse {
|
||
effective: Record<ThresholdMetric, EffectiveRange>;
|
||
overrides: Partial<Record<ThresholdMetric, CurrentRange>>;
|
||
}
|
||
|
||
export interface TargetEditSheetProps {
|
||
targetType: string;
|
||
targetLabel: string;
|
||
unit: string;
|
||
/**
|
||
* Current sys/dia ranges for BP. Threaded from the page; the dialog
|
||
* uses them to seed BOTH inputs when target.type === BLOOD_PRESSURE.
|
||
*/
|
||
initialRange: CurrentRange | null;
|
||
initialDiastolicRange?: CurrentRange | null;
|
||
open: boolean;
|
||
onOpenChange: (open: boolean) => void;
|
||
}
|
||
|
||
export function TargetEditSheet(props: TargetEditSheetProps) {
|
||
// Only mount the body (and its TanStack Query hooks) when the dialog
|
||
// is open. This keeps the parent reference panel test-friendly:
|
||
// callers can render the panel without standing up a
|
||
// QueryClientProvider, and the hooks only execute once the user
|
||
// actually opens the editor.
|
||
//
|
||
// v1.4.27 R4 RC2 — migrated raw <Dialog> → <ResponsiveSheet> so the
|
||
// bottom-sheet branch sticky-pins reset + cancel + save above the
|
||
// soft keyboard on phones. The lazy-mount toggle stays at this layer
|
||
// so closed renders never instantiate the TanStack Query hooks.
|
||
if (!props.open) return null;
|
||
return <TargetEditSheetBody {...props} />;
|
||
}
|
||
|
||
function TargetEditSheetBody({
|
||
targetType,
|
||
targetLabel,
|
||
unit,
|
||
initialRange,
|
||
initialDiastolicRange,
|
||
onOpenChange,
|
||
}: TargetEditSheetProps) {
|
||
const { t } = useTranslations();
|
||
const queryClient = useQueryClient();
|
||
|
||
const isBp = targetType === "BLOOD_PRESSURE";
|
||
const metric = TARGET_TYPE_TO_METRIC[targetType] ?? null;
|
||
const isDerivedMetric = !isBp && metric == null;
|
||
|
||
// Glucose is the one metric whose display unit can differ from the
|
||
// canonical storage unit: HealthLog stores mg/dL, but a user on the
|
||
// mmol/L preference sees — and types — mmol/L. The parent already
|
||
// hands us `unit="mmol/L"` and a `initialRange` pre-converted to that
|
||
// unit. We must therefore (a) seed the mg/dL persisted override into
|
||
// the display unit, (b) validate the typed value against bounds
|
||
// expressed in the display unit, and (c) convert the typed value back
|
||
// to canonical mg/dL before the PUT — otherwise a `5.5 mmol/L` target
|
||
// is rejected by the 40–400 mg/dL bounds or stored verbatim as 5.5.
|
||
const isGlucoseMmol =
|
||
metric != null &&
|
||
metric.startsWith("BLOOD_GLUCOSE") &&
|
||
unit === "mmol/L";
|
||
const toDisplay = (mgdl: number) =>
|
||
isGlucoseMmol ? convertGlucose(mgdl, "mmol/L") : mgdl;
|
||
const toCanonical = (displayValue: number) =>
|
||
isGlucoseMmol ? toCanonicalMgdl(displayValue, "mmol/L") : displayValue;
|
||
|
||
// Lazy-load the thresholds payload so the dialog also catches any
|
||
// already-persisted override even when the seeded `initialRange`
|
||
// came from the targets API (which can paint a different in-band
|
||
// tone vs. the persisted override). The body only mounts when the
|
||
// dialog is open, so we don't need a separate `enabled` flag.
|
||
const { data: thresholdsData } = useQuery({
|
||
queryKey: ["user", "thresholds"],
|
||
queryFn: async () => {
|
||
const res = await fetch("/api/user/thresholds");
|
||
if (!res.ok) throw new Error("failed");
|
||
const json = await res.json();
|
||
return json.data as ThresholdsApiResponse;
|
||
},
|
||
});
|
||
|
||
// Seed input strings from override → initialRange → default bound.
|
||
// We use strings so the user can type a partial number (e.g. "12") without
|
||
// the input snapping back to a parsed value.
|
||
const seedRange = (
|
||
m: ThresholdMetric | null,
|
||
fallback: CurrentRange | null,
|
||
) => {
|
||
// The persisted override is canonical (mg/dL for glucose); the
|
||
// parent-provided `fallback` (initialRange) is already in the
|
||
// display unit. Convert the override into the display unit so the
|
||
// seeded inputs always read in the unit the label announces.
|
||
if (m && thresholdsData?.overrides?.[m]) {
|
||
const override = thresholdsData.overrides[m]!;
|
||
return { min: toDisplay(override.min), max: toDisplay(override.max) };
|
||
}
|
||
if (fallback) return fallback;
|
||
if (m) {
|
||
const bounds = METRIC_BOUNDS[m];
|
||
return { min: toDisplay(bounds.min), max: toDisplay(bounds.max) };
|
||
}
|
||
return { min: 0, max: 100 };
|
||
};
|
||
|
||
const primary = seedRange(isBp ? "BLOOD_PRESSURE_SYS" : metric, initialRange);
|
||
const secondary = isBp
|
||
? seedRange("BLOOD_PRESSURE_DIA", initialDiastolicRange ?? null)
|
||
: null;
|
||
|
||
// The body only mounts when open=true (see the wrapper above), so
|
||
// the initial useState seed is already correct — no setState-inside-
|
||
// useEffect dance needed. When `thresholdsData` arrives async, we
|
||
// use it as the *displayed* fallback whenever the user hasn't typed
|
||
// anything yet, via the `??` chain below.
|
||
const [minStr, setMinStr] = useState<string | null>(null);
|
||
const [maxStr, setMaxStr] = useState<string | null>(null);
|
||
const [diaMinStr, setDiaMinStr] = useState<string | null>(null);
|
||
const [diaMaxStr, setDiaMaxStr] = useState<string | null>(null);
|
||
|
||
// Display values: user-edited string (if any) wins, otherwise we
|
||
// surface the freshest seeded value derived from the
|
||
// `thresholdsData` query + the parent-provided `initialRange`.
|
||
const displayMin = minStr ?? String(primary.min);
|
||
const displayMax = maxStr ?? String(primary.max);
|
||
const displayDiaMin = diaMinStr ?? (secondary ? String(secondary.min) : "");
|
||
const displayDiaMax = diaMaxStr ?? (secondary ? String(secondary.max) : "");
|
||
|
||
// Focus management — the first input gets focus when the sheet
|
||
// opens, matching the Dashboard chart cog's open-on-focus pattern.
|
||
// v1.4.27 R4 RC2 — under the ResponsiveSheet primitive we no longer
|
||
// hook Radix's `onOpenAutoFocus`; a mount effect lands focus on the
|
||
// primary input as soon as the portal paints.
|
||
const firstInputRef = useRef<HTMLInputElement | null>(null);
|
||
const dialogContentId = useId();
|
||
useEffect(() => {
|
||
// Defer one frame so Radix has finished placing the portal before
|
||
// we steal focus — otherwise the focus jumps to the close-X on
|
||
// some browsers.
|
||
const handle = window.requestAnimationFrame(() => {
|
||
if (firstInputRef.current) {
|
||
firstInputRef.current.focus();
|
||
firstInputRef.current.select();
|
||
}
|
||
});
|
||
return () => window.cancelAnimationFrame(handle);
|
||
}, []);
|
||
|
||
const updateMutation = useMutation({
|
||
mutationFn: async (
|
||
payload: Partial<Record<ThresholdMetric, CurrentRange>>,
|
||
) => {
|
||
const res = await fetch("/api/user/thresholds", {
|
||
method: "PUT",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify(payload),
|
||
});
|
||
if (!res.ok) {
|
||
const err = await res.json().catch(() => null);
|
||
throw new Error(err?.error ?? "save failed");
|
||
}
|
||
},
|
||
onSuccess: () => {
|
||
queryClient.invalidateQueries({ queryKey: ["user", "thresholds"] });
|
||
queryClient.invalidateQueries({ queryKey: queryKeys.insightsTargets() });
|
||
queryClient.invalidateQueries({ queryKey: queryKeys.analytics() });
|
||
queryClient.invalidateQueries({ queryKey: ["insights"] });
|
||
toast.success(t("targets.edit.saveSuccess"));
|
||
onOpenChange(false);
|
||
},
|
||
onError: (err) =>
|
||
toast.error(
|
||
err instanceof Error ? err.message : t("targets.edit.saveError"),
|
||
),
|
||
});
|
||
|
||
const resetMutation = useMutation({
|
||
mutationFn: async () => {
|
||
if (isBp) {
|
||
// Reset BOTH sys + dia.
|
||
for (const m of ["BLOOD_PRESSURE_SYS", "BLOOD_PRESSURE_DIA"] as const) {
|
||
const res = await fetch(`/api/user/thresholds?metric=${m}`, {
|
||
method: "DELETE",
|
||
});
|
||
if (!res.ok) throw new Error("reset failed");
|
||
}
|
||
return;
|
||
}
|
||
if (!metric) return;
|
||
const res = await fetch(`/api/user/thresholds?metric=${metric}`, {
|
||
method: "DELETE",
|
||
});
|
||
if (!res.ok) throw new Error("reset failed");
|
||
},
|
||
onSuccess: () => {
|
||
queryClient.invalidateQueries({ queryKey: ["user", "thresholds"] });
|
||
queryClient.invalidateQueries({ queryKey: queryKeys.insightsTargets() });
|
||
queryClient.invalidateQueries({ queryKey: queryKeys.analytics() });
|
||
queryClient.invalidateQueries({ queryKey: ["insights"] });
|
||
toast.success(t("targets.edit.resetSuccess"));
|
||
onOpenChange(false);
|
||
},
|
||
onError: () => toast.error(t("targets.edit.saveError")),
|
||
});
|
||
|
||
const busy = updateMutation.isPending || resetMutation.isPending;
|
||
|
||
const minNum = parseFloat(displayMin);
|
||
const maxNum = parseFloat(displayMax);
|
||
const diaMinNum = parseFloat(displayDiaMin);
|
||
const diaMaxNum = parseFloat(displayDiaMax);
|
||
|
||
const canonicalPrimaryBounds = isBp
|
||
? METRIC_BOUNDS.BLOOD_PRESSURE_SYS
|
||
: metric
|
||
? METRIC_BOUNDS[metric]
|
||
: null;
|
||
const secondaryBounds = isBp ? METRIC_BOUNDS.BLOOD_PRESSURE_DIA : null;
|
||
|
||
// Bounds the user is validated/clamped against — expressed in the
|
||
// display unit so a mmol/L glucose entry is checked against the
|
||
// mmol/L-projected 40–400 mg/dL window, not the raw mg/dL numbers.
|
||
const primaryBounds = canonicalPrimaryBounds
|
||
? {
|
||
min: toDisplay(canonicalPrimaryBounds.min),
|
||
max: toDisplay(canonicalPrimaryBounds.max),
|
||
unit,
|
||
}
|
||
: null;
|
||
|
||
const primaryValid =
|
||
primaryBounds != null &&
|
||
Number.isFinite(minNum) &&
|
||
Number.isFinite(maxNum) &&
|
||
minNum >= primaryBounds.min &&
|
||
maxNum <= primaryBounds.max &&
|
||
minNum < maxNum;
|
||
|
||
const secondaryValid =
|
||
!isBp ||
|
||
(secondaryBounds != null &&
|
||
Number.isFinite(diaMinNum) &&
|
||
Number.isFinite(diaMaxNum) &&
|
||
diaMinNum >= secondaryBounds.min &&
|
||
diaMaxNum <= secondaryBounds.max &&
|
||
diaMinNum < diaMaxNum);
|
||
|
||
const canSave = !isDerivedMetric && primaryValid && secondaryValid && !busy;
|
||
|
||
const handleSave = () => {
|
||
if (!canSave) return;
|
||
if (isBp) {
|
||
updateMutation.mutate({
|
||
BLOOD_PRESSURE_SYS: { min: minNum, max: maxNum },
|
||
BLOOD_PRESSURE_DIA: { min: diaMinNum, max: diaMaxNum },
|
||
});
|
||
return;
|
||
}
|
||
if (!metric) return;
|
||
// Convert the typed display-unit value back to canonical mg/dL for
|
||
// glucose on the mmol/L preference; a no-op for every other metric.
|
||
updateMutation.mutate({
|
||
[metric]: { min: toCanonical(minNum), max: toCanonical(maxNum) },
|
||
});
|
||
};
|
||
|
||
const hasOverride = isBp
|
||
? Boolean(
|
||
thresholdsData?.overrides?.BLOOD_PRESSURE_SYS ||
|
||
thresholdsData?.overrides?.BLOOD_PRESSURE_DIA,
|
||
)
|
||
: metric
|
||
? Boolean(thresholdsData?.overrides?.[metric])
|
||
: false;
|
||
|
||
// v1.4.27 R4 RC2 — focus the first numeric input on mount instead of
|
||
// relying on Radix's <Dialog>-specific onOpenAutoFocus. The body
|
||
// only mounts once `open=true` (lazy mount above), so the effect
|
||
// runs exactly when the surface comes into view.
|
||
// Note: ResponsiveSheet does not currently expose onOpenAutoFocus —
|
||
// we use a tiny mount effect to steal focus.
|
||
return (
|
||
<ResponsiveSheet
|
||
open
|
||
onOpenChange={onOpenChange}
|
||
title={t("targets.edit.title", { metric: targetLabel })}
|
||
description={
|
||
isDerivedMetric
|
||
? t("targets.edit.derivedHint")
|
||
: t("targets.edit.description", { unit })
|
||
}
|
||
className="sm:max-w-md"
|
||
footer={
|
||
<div className="flex w-full flex-col-reverse gap-2 sm:flex-row sm:items-center sm:justify-between sm:gap-3">
|
||
{hasOverride ? (
|
||
<Button
|
||
variant="ghost"
|
||
size="sm"
|
||
onClick={() => resetMutation.mutate()}
|
||
disabled={busy}
|
||
data-slot="target-edit-reset"
|
||
>
|
||
<RotateCcw className="mr-2 h-3.5 w-3.5" />
|
||
{t("targets.edit.resetToDefault")}
|
||
</Button>
|
||
) : (
|
||
<span aria-hidden="true" />
|
||
)}
|
||
<div className="flex flex-col-reverse gap-2 sm:flex-row sm:gap-2">
|
||
<Button
|
||
variant="outline"
|
||
size="sm"
|
||
onClick={() => onOpenChange(false)}
|
||
disabled={busy}
|
||
>
|
||
{t("common.cancel")}
|
||
</Button>
|
||
<Button
|
||
size="sm"
|
||
onClick={handleSave}
|
||
disabled={!canSave}
|
||
data-slot="target-edit-save"
|
||
>
|
||
{t("common.save")}
|
||
</Button>
|
||
</div>
|
||
</div>
|
||
}
|
||
>
|
||
<div
|
||
data-slot="target-edit-sheet"
|
||
data-target-type={targetType}
|
||
aria-describedby={dialogContentId}
|
||
>
|
||
<p id={dialogContentId} className="sr-only">
|
||
{isDerivedMetric
|
||
? t("targets.edit.derivedHint")
|
||
: t("targets.edit.description", { unit })}
|
||
</p>
|
||
|
||
{!isDerivedMetric && primaryBounds && (
|
||
<div className="space-y-4">
|
||
<div
|
||
className="grid grid-cols-1 gap-3 sm:grid-cols-2"
|
||
data-slot="target-edit-primary-row"
|
||
>
|
||
<div className="space-y-1">
|
||
<Label htmlFor="target-edit-min" className="text-xs">
|
||
{isBp
|
||
? t("targets.edit.systolicMin")
|
||
: t("targets.edit.minLabel")}
|
||
</Label>
|
||
<Input
|
||
id="target-edit-min"
|
||
ref={firstInputRef}
|
||
type="number"
|
||
step={targetType === "ACTIVITY_STEPS" ? 100 : 0.1}
|
||
min={primaryBounds.min}
|
||
max={primaryBounds.max}
|
||
value={displayMin}
|
||
onChange={(e) => setMinStr(e.target.value)}
|
||
disabled={busy}
|
||
data-slot="target-edit-min-input"
|
||
/>
|
||
</div>
|
||
<div className="space-y-1">
|
||
<Label htmlFor="target-edit-max" className="text-xs">
|
||
{isBp
|
||
? t("targets.edit.systolicMax")
|
||
: t("targets.edit.maxLabel")}
|
||
</Label>
|
||
<Input
|
||
id="target-edit-max"
|
||
type="number"
|
||
step={targetType === "ACTIVITY_STEPS" ? 100 : 0.1}
|
||
min={primaryBounds.min}
|
||
max={primaryBounds.max}
|
||
value={displayMax}
|
||
onChange={(e) => setMaxStr(e.target.value)}
|
||
disabled={busy}
|
||
data-slot="target-edit-max-input"
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
{isBp && secondaryBounds && (
|
||
<div
|
||
className="grid grid-cols-1 gap-3 sm:grid-cols-2"
|
||
data-slot="target-edit-secondary-row"
|
||
>
|
||
<div className="space-y-1">
|
||
<Label htmlFor="target-edit-dia-min" className="text-xs">
|
||
{t("targets.edit.diastolicMin")}
|
||
</Label>
|
||
<Input
|
||
id="target-edit-dia-min"
|
||
type="number"
|
||
step={0.1}
|
||
min={secondaryBounds.min}
|
||
max={secondaryBounds.max}
|
||
value={displayDiaMin}
|
||
onChange={(e) => setDiaMinStr(e.target.value)}
|
||
disabled={busy}
|
||
data-slot="target-edit-dia-min-input"
|
||
/>
|
||
</div>
|
||
<div className="space-y-1">
|
||
<Label htmlFor="target-edit-dia-max" className="text-xs">
|
||
{t("targets.edit.diastolicMax")}
|
||
</Label>
|
||
<Input
|
||
id="target-edit-dia-max"
|
||
type="number"
|
||
step={0.1}
|
||
min={secondaryBounds.min}
|
||
max={secondaryBounds.max}
|
||
value={displayDiaMax}
|
||
onChange={(e) => setDiaMaxStr(e.target.value)}
|
||
disabled={busy}
|
||
data-slot="target-edit-dia-max-input"
|
||
/>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
<p className="text-muted-foreground text-xs">
|
||
{t("targets.edit.boundsHint", {
|
||
min: String(primaryBounds.min),
|
||
max: String(primaryBounds.max),
|
||
unit: primaryBounds.unit,
|
||
})}
|
||
</p>
|
||
</div>
|
||
)}
|
||
</div>
|
||
</ResponsiveSheet>
|
||
);
|
||
}
|