fix(insights): shade the SpO2 chart from the own target when one is set

The oxygen saturation page drew a fixed clinical band, so a person with
a lower target of their own saw their normal range shaded as a warning.
The page now reads the resolved range from /api/user/thresholds and
draws it; without a target the clinical bands stay as they were.

Refs #1261
This commit is contained in:
Marc-André Bombeck committed 2026-10-22 10:23:15 +02:00
1 parent 90106d88c8
commit ff49bbb5fc
5 files changed
+237 -8

No files matched your search

+58
View File
@@ -186,3 +186,61 @@ export function getBodyFatTargetRange(gender: string | null | undefined): {
// Gender-neutral fallback: midpoint of male/female fitness+acceptable.
return { min: 17, max: 27 };
}
/**
* Chart bands for oxygen saturation (%).
*
* Without a personal target the chart keeps its clinical shading: below 90 %
* the hypoxia floor, 90 to 95 % the watch zone, 95 to 100 % normal. With one
* (a COPD or high-altitude user living at 88 to 92 %, say) the green band is
* the person's own and the wings come from the resolved range, so the zone
* agrees with the `/targets` card. Saturation cannot exceed 100 %, so nothing
* is shaded above it; the floor stays at 80 % unless the person's own wing
* reaches lower.
*/
export function buildOxygenSaturationBands(
ownRange: TrafficRange | null,
): ValueBand[] {
if (!ownRange) {
return [
{ min: 80, max: 90, color: "var(--destructive)", opacity: 0.18 },
{ min: 90, max: 95, color: "var(--warning)", opacity: 0.18 },
{ min: 95, max: 100, color: "var(--success)", opacity: 0.18 },
];
}
const floor = Math.min(80, ownRange.orangeMin);
const ceiling = 100;
const bands: ValueBand[] = [
{
min: floor,
max: ownRange.orangeMin,
color: "var(--destructive)",
opacity: 0.18,
},
{
min: ownRange.orangeMin,
max: ownRange.greenMin,
color: "var(--warning)",
opacity: 0.18,
},
{
min: ownRange.greenMin,
max: Math.min(ownRange.greenMax, ceiling),
color: "var(--success)",
opacity: 0.18,
},
{
min: Math.min(ownRange.greenMax, ceiling),
max: Math.min(ownRange.orangeMax, ceiling),
color: "var(--warning)",
opacity: 0.18,
},
{
min: Math.min(ownRange.orangeMax, ceiling),
max: ceiling,
color: "var(--destructive)",
opacity: 0.18,
},
];
return bands.filter((band) => band.max > band.min);
}
@@ -0,0 +1,30 @@
import { describe, expect, it } from "vitest";
import { selectOwnTargetRange } from "../use-own-target-range";
const range = { greenMin: 88, greenMax: 92, orangeMin: 87.4, orangeMax: 92.6 };
describe("selectOwnTargetRange", () => {
it("returns the resolved range when the person set the target", () => {
expect(
selectOwnTargetRange(
{ effective: { OXYGEN_SATURATION: { range, isOverride: true } } },
"OXYGEN_SATURATION",
),
).toEqual(range);
});
it("returns null for a default range, so the chart keeps its own band", () => {
expect(
selectOwnTargetRange(
{ effective: { OXYGEN_SATURATION: { range, isOverride: false } } },
"OXYGEN_SATURATION",
),
).toBeNull();
});
it("returns null before the read lands or for an absent metric", () => {
expect(selectOwnTargetRange(null, "OXYGEN_SATURATION")).toBeNull();
expect(selectOwnTargetRange({ effective: {} }, "PULSE")).toBeNull();
});
});
+48
View File
@@ -0,0 +1,48 @@
"use client";
import { useQuery } from "@tanstack/react-query";
import { apiGet } from "@/lib/api/api-fetch";
import type { ThresholdMetric } from "@/lib/analytics/effective-range";
import type { TrafficRange } from "@/lib/analytics/value-bands";
import { useAuth } from "@/hooks/use-auth";
import { queryKeys } from "@/lib/query-keys";
/** The slice of `GET /api/user/thresholds` a chart band needs. */
interface ThresholdsSlice {
effective: Partial<
Record<string, { range: TrafficRange | null; isOverride: boolean }>
>;
}
/**
* The user's own target band for one metric, as the server resolved it, or
* `null` when they have not set one (the chart then keeps its default band).
*
* `/api/user/thresholds` resolves every override through `getEffectiveRange`,
* orange wings included, so the page draws the resolved range rather than
* re-deriving one. Shares the `userThresholds()` cache entry with the target
* editor, which invalidates it on save, so editing a target repaints the
* chart without a reload.
*/
export function useOwnTargetRange(
metric: ThresholdMetric,
): TrafficRange | null {
const { isAuthenticated } = useAuth();
const { data } = useQuery({
queryKey: queryKeys.userThresholds(),
queryFn: () => apiGet<ThresholdsSlice>("/api/user/thresholds"),
enabled: isAuthenticated,
});
return selectOwnTargetRange(data ?? null, metric);
}
/** Pure projection, exported for the unit test. */
export function selectOwnTargetRange(
data: ThresholdsSlice | null,
metric: ThresholdMetric,
): TrafficRange | null {
const entry = data?.effective?.[metric];
if (!entry?.isOverride) return null;
return entry.range ?? null;
}