feat(dashboard): rename 7-day-average to 7-day-trend with delta indicator per metric

The dashboard tiles labelled the 7-day average as "7T:" / "7d:" with
the mean value beside it. Marc found that label unclear when the
goal of the tile is to convey *direction* over the past week, not a
mid-point average:

  Before:  7T:    80,5     30T:    79,8
  After:   7T-Trend:  80,5 (+1.2)   30T:    79,8

The avg7 line now flips its label to `charts.trend7dShort` ("7d trend"
/ "7T-Trend") when a `trend7Delta` prop is supplied, and renders a
signed delta indicator next to the average. Color of the delta
follows the same `directionSentiment` rules the headline arrow
already uses:

- up-bad metrics (BP, weight, body fat): +delta paints orange,
  -delta paints green.
- up-good metrics (mood, sleep, steps): +delta paints green,
  -delta paints orange.
- neutral metrics (pulse): both signs stay muted.

Tiny absolute deltas (< 0.05) read as "no movement" and stay muted
regardless of sentiment, so a flat metric never paints a colour.

Delta source is the existing 7-day linear-regression slope from
`DataSummary.slope7` (already computed by `summarize()` in
`lib/analytics/trends.ts`). Projection: `slope.slope * 7 days =
end-to-end delta of the regression line` — extracted into the pure
helper `summaryToTrend7Delta()` in `lib/analytics/trend-delta.ts` so
the rule stays unit-testable.

Wired to weight, BP-sys, BP-dia, pulse, body fat, mood, sleep, steps.
Glucose tiles keep the legacy avg label until v1.5 (per-context
slopes need a wider summary contract).

Tests:

- Unit `summaryToTrend7Delta()`: null when summary missing /
  slope7 null, projects rate × 7 for positive and negative slopes,
  zero for stable slope.
- Component `<TrendCard>`: trend label ("7d trend" / "7T-Trend")
  shows in EN + DE, legacy label preserved when no delta supplied
  (back-compat), delta renders with `+`/`−` sign and one decimal,
  ±0 for near-zero, +delta on up-bad → orange, +delta on up-good →
  green, neutral metric → muted regardless of sign.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Marc-André BombeckandClaude Opus 4.7 committed 2026-05-09 20:31:37 +02:00
1 parent 39c83fb89a
commit 4e2386e2d1
7 files changed
+303 -1

No files matched your search

@@ -0,0 +1,39 @@
import { describe, it, expect } from "vitest";
import { summaryToTrend7Delta } from "../trend-delta";
const STABLE = { slope: 0, direction: "stable" as const, confidence: 0.1 };
const RISING_HALF_PER_DAY = {
slope: 0.5,
direction: "up" as const,
confidence: 0.8,
};
const FALLING_TENTH_PER_DAY = {
slope: -0.1,
direction: "down" as const,
confidence: 0.7,
};
describe("summaryToTrend7Delta()", () => {
it("returns null when the summary is missing", () => {
expect(summaryToTrend7Delta(null)).toBeNull();
expect(summaryToTrend7Delta(undefined)).toBeNull();
});
it("returns null when slope7 is not computable (insufficient data)", () => {
expect(summaryToTrend7Delta({ slope7: null })).toBeNull();
});
it("projects slope (units per day) over a 7-day window", () => {
expect(summaryToTrend7Delta({ slope7: RISING_HALF_PER_DAY })).toBe(3.5);
// Negative slope produces a negative delta — the tile shows it
// with the unicode minus sign in the formatter.
expect(summaryToTrend7Delta({ slope7: FALLING_TENTH_PER_DAY })).toBeCloseTo(
-0.7,
3,
);
});
it("returns 0 for a stable slope (the tile then paints ±0)", () => {
expect(summaryToTrend7Delta({ slope7: STABLE })).toBe(0);
});
});
+35
View File
@@ -0,0 +1,35 @@
/**
* Compute a 7-day trend delta from a metric summary.
*
* Backstory — v1.4.15 Fix 4 ("7-Tage-Schnitt" → "7-Tage-Trend"):
* the dashboard tiles labelled the 7-day average as "7T:" / "7d:"
* with the mean value beside it. Marc found that label unclear
* (sounded like an "average") when the goal of the tile is to
* convey *direction* over the past week. We re-use the existing
* 7-day linear-regression slope (already in `DataSummary.slope7`)
* and project a stable end-to-end delta as
*
* delta = slope.slope * 7 // units per day → unit delta over 7d
*
* The trend-card then paints the delta with metric-aware sentiment
* colour (up-good vs up-bad). Returning `null` when slope7 is not
* available keeps the tile rendering the legacy avg label for back-
* compat with call sites that haven't been migrated.
*
* Pure & deterministic — pinned by unit test.
*/
import type { TrendSlope } from "@/lib/analytics/trends";
export interface TrendCapableSummary {
slope7: TrendSlope | null;
}
export function summaryToTrend7Delta(
summary: TrendCapableSummary | undefined | null,
): number | null {
const slope7 = summary?.slope7;
if (!slope7) return null;
// slope.slope is units per day (see lib/analytics/trends.ts) → 7 days
// is the projected end-to-end delta of the regression line.
return slope7.slope * 7;
}