fix(insights): give a metric's numeric domain one home

Three defects on one card of the step sub-page, all of them a surface
answering a question that belongs to the metric.

"Your usual range is -5,595.6-13,387.6 steps", under a chart whose own
y-axis refuses to draw a negative step. Same app, same question, two
answers: the axis clamped and the sentence beneath it did not. Both now
read the same fact. `VALUE_RANGES` already declares every metric's
plausibility floor for ingest; `value-domain.ts` reads it and answers
"can this metric be negative" for the padded y-domain and for the median
+/- k.MAD band alike. The band's lower edge floors at zero for a
non-negative metric; `spread` stays the unclamped dispersion so a
deviation-in-sigma calculation is unaffected. `buildBaselineBand` now
takes the metric it is describing as a required argument, so a new caller
cannot quietly reintroduce a negative edge. Two types carried no
plausibility range at all, which let any number through ingest and read
as "unknown" on the negativity question: walking steadiness gets the same
0-100 percent band as its two gait siblings, the loud-listening event a
floor of 1 with a generous ceiling.

"13,387.6 steps" also said steps to a reader of German. Five sub-page
units are English words, not symbols - steps, flights, falls, years, and
a "count" that was a poor label in any language. They now travel as i18n
keys through a new `unitKey` on the metric-page scaffold and resolve in
all six locales. The thirteen real symbols (bpm, mmHg, %, m/s, kg/m2,
...) stay literals; translating a symbol would be the opposite mistake.

And the Min read "104.0 steps". Precision was a per-page prop defaulting
to one decimal, so every page had to remember that its metric is a count
- and the step page did not. It follows the metric now: a discrete metric
(tallies, category events, closed integer instrument scores, ordinal
levels) resolves zero decimals wherever it is rendered, and a page only
passes the prop to override that.
This commit is contained in:
Marc-André Bombeck committed 2026-07-27 23:01:33 +02:00
1 parent 60c30e1e3a
commit f5c676bbeb
24 files changed
+284 -39

No files matched your search

+1 -1
View File
@@ -1337,7 +1337,7 @@ export function HealthChart({
.filter((value): value is number => typeof value === "number")
.filter((value) => Number.isFinite(value));
return computePaddedYDomain(values);
return computePaddedYDomain(values, types);
}, [
chartDataWithCompare,
effectiveCompareBaseline,
@@ -8,6 +8,7 @@ import { useUnitDisplay } from "@/hooks/use-unit-display";
import { useMounted } from "@/hooks/use-mounted";
import { useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
import { metricFractionDigits } from "@/lib/measurements/value-domain";
import { apiGet } from "@/lib/api/api-fetch";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { TileHeader } from "@/components/insights/tile-header";
@@ -38,9 +39,12 @@ export interface CoachReadStripProps {
/** Unit suffix rendered next to the band edges + today's value. */
unit: string;
/**
* Decimal precision for the formatted numbers. Defaults to 1 — enough for
* weight (78.4) / HRV (41.5); integer metrics drop the trailing zero via
* `Intl.NumberFormat`. Pages pass 0 for integer-only metrics (BP, steps).
* Decimal precision for the formatted numbers. Optional: when omitted the
* precision follows the METRIC (`metricFractionDigits`) — a discrete metric
* resolves 0, everything else the shared default of 1. It used to default
* to a flat 1 and rely on each page remembering to pass 0, which is how a
* step page came to print "104.0 steps". Pass it only to override the
* metric's own answer.
*/
fractionDigits?: number;
/**
@@ -55,9 +59,10 @@ export interface CoachReadStripProps {
export function CoachReadStrip({
metricType,
unit,
fractionDigits = 1,
fractionDigits,
valueScale = 1,
}: CoachReadStripProps) {
const digits = fractionDigits ?? metricFractionDigits(metricType);
const { isAuthenticated } = useAuth();
const { t, locale } = useTranslations();
const mounted = useMounted();
@@ -89,7 +94,7 @@ export function CoachReadStrip({
const fmt = (value: number): string =>
new Intl.NumberFormat(locale, {
minimumFractionDigits: 0,
maximumFractionDigits: fractionDigits,
maximumFractionDigits: digits,
}).format(
transformed
? unitDisplay.toDisplay(metricType, value)
@@ -14,6 +14,7 @@ import { useTranslations } from "@/lib/i18n/context";
import type { InsightMetric } from "@/lib/insights/metric-availability";
import type { MetricStatusMetricId } from "@/lib/insights/metric-status-registry";
import type { ChartOverlayKey } from "@/lib/dashboard-layout";
import { metricFractionDigits } from "@/lib/measurements/value-domain";
import { Button } from "@/components/ui/button";
import { QueryErrorRow } from "@/components/ui/query-error-row";
import { Card, CardContent } from "@/components/ui/card";
@@ -98,6 +99,17 @@ export interface HealthKitMetricPageProps {
* transformed types omit these props; everything else still passes `unit`.
*/
unit?: string;
/**
* i18n key for a unit that is a WORD rather than a symbol — `steps`,
* `flights`, `falls`, `years`, `events`. Resolved through `t()` so it
* renders in the reader's language; a German user was shown "13.387,6
* steps". Symbols (`bpm`, `mmHg`, `%`, `m/s`, `kg/m²`, …) are language-
* neutral and stay literals in `unit` — translating a symbol would be the
* opposite mistake. Wins over `unit` / `yAxisUnit` for both the chart and
* the axis label; a type with a registered display transform still
* overrides everything.
*/
unitKey?: string;
/** Optional y-axis label shown above the unit. */
yAxisUnit?: string;
/** Optional value bands (Apple-Health-style target zone shading). */
@@ -206,6 +218,7 @@ export function HealthKitMetricPage({
i18nPrefix,
color,
unit,
unitKey,
yAxisUnit,
valueBands,
emptyStateCtaType,
@@ -264,13 +277,24 @@ export function HealthKitMetricPage({
const transform = unitDisplay.isTransformed(effectiveType)
? unitDisplay.transformFor(effectiveType)
: null;
const resolvedUnit = transform ? transform.displayUnit : unit;
const resolvedYAxisUnit = transform ? transform.displayUnit : yAxisUnit;
// A word-shaped unit arrives as an i18n key and is resolved here, once, for
// the chart line, the axis label, the stat strip and the coach-read strip.
const localisedUnit = unitKey ? t(unitKey) : undefined;
const resolvedUnit = transform
? transform.displayUnit
: (localisedUnit ?? unit);
const resolvedYAxisUnit = transform
? transform.displayUnit
: (localisedUnit ?? yAxisUnit);
const resolvedScale = transform ? transform.factor : (valueScale ?? 1);
const resolvedOffset = transform ? (transform.offset ?? 0) : 0;
// Precision follows the METRIC, not the page. A discrete metric renders no
// fractional part even when the page says nothing — the steps page said
// nothing and its Min read "104.0 steps". An explicit `statFractionDigits`
// still wins for the metrics that genuinely need one (mmol/L, skin temp).
const resolvedFractionDigits = transform
? transform.decimals
: statFractionDigits;
: (statFractionDigits ?? metricFractionDigits(effectiveType));
// Value bands are absolute bounds → affine-convert (factor + offset) so the
// shaded zone tracks the converted line. Metric users get the identity.
const resolvedBands =