mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
Merge work/nap-separation
This commit is contained in:
commit
1c5191d1e3
12 files changed
+555
-55
No files matched your search
@@ -5,7 +5,10 @@ import { I18nProvider } from "@/lib/i18n/context";
|
||||
import {
|
||||
SleepStageStackedBar,
|
||||
STAGE_ORDER,
|
||||
buildCompositionRows,
|
||||
windowHasNap,
|
||||
type SleepStageBreakdown,
|
||||
type SleepStageNight,
|
||||
} from "../sleep-stage-stacked-bar";
|
||||
|
||||
/**
|
||||
@@ -230,3 +233,61 @@ describe("<SleepStageStackedBar>", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("nap band", () => {
|
||||
/**
|
||||
* Recharts draws nothing under `renderToStaticMarkup`, so the band itself
|
||||
* is pinned through the pure row builder the chart feeds.
|
||||
*/
|
||||
const label = (dayKey: string) => dayKey;
|
||||
|
||||
it("carries the nap minutes on the day that has one", () => {
|
||||
const perNight: SleepStageNight[] = [
|
||||
{ dayKey: "2026-06-03", stages: { CORE: 250, DEEP: 50 } },
|
||||
{
|
||||
dayKey: "2026-06-04",
|
||||
stages: { CORE: 250, DEEP: 50 },
|
||||
napMinutes: 110,
|
||||
napCount: 1,
|
||||
},
|
||||
];
|
||||
const { rows } = buildCompositionRows(perNight, 7, label);
|
||||
expect(rows[1].NAP).toBe(110);
|
||||
expect(rows[1].napCount).toBe(1);
|
||||
// The night's own stages are untouched by the nap sitting beside them.
|
||||
expect(rows[1].CORE).toBe(250);
|
||||
// And the day before, which had no nap, carries nothing.
|
||||
expect(rows[0].NAP).toBe(0);
|
||||
expect(windowHasNap(rows)).toBe(true);
|
||||
});
|
||||
|
||||
it("mounts no band at all on a week without a nap", () => {
|
||||
const perNight: SleepStageNight[] = [
|
||||
{ dayKey: "2026-06-03", stages: { CORE: 250, DEEP: 50 } },
|
||||
{ dayKey: "2026-06-04", stages: { CORE: 260, DEEP: 40 } },
|
||||
];
|
||||
const { rows } = buildCompositionRows(perNight, 7, label);
|
||||
expect(windowHasNap(rows)).toBe(false);
|
||||
});
|
||||
|
||||
it("drops the band when the nap falls outside the visible window", () => {
|
||||
// The nap is on the oldest day; a 2-day window scrolls past it, so the
|
||||
// legend must not keep a nap entry the user cannot see.
|
||||
const perNight: SleepStageNight[] = [
|
||||
{
|
||||
dayKey: "2026-06-02",
|
||||
stages: { CORE: 250 },
|
||||
napMinutes: 110,
|
||||
napCount: 1,
|
||||
},
|
||||
{ dayKey: "2026-06-03", stages: { CORE: 250 } },
|
||||
{ dayKey: "2026-06-04", stages: { CORE: 260 } },
|
||||
];
|
||||
expect(windowHasNap(buildCompositionRows(perNight, 3, label).rows)).toBe(
|
||||
true,
|
||||
);
|
||||
expect(windowHasNap(buildCompositionRows(perNight, 2, label).rows)).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -44,7 +44,16 @@ import type { Locale } from "@/lib/i18n/config";
|
||||
export interface SleepStageNight {
|
||||
/** Berlin-tz day key (YYYY-MM-DD). */
|
||||
dayKey: string;
|
||||
/** Per-stage minutes of the MAIN night. Naps are not in here. */
|
||||
stages: Record<string, number>;
|
||||
/**
|
||||
* Time asleep across this day's naps, in minutes. Absent on a day with no
|
||||
* nap, which is most days — the chart draws nothing for it and the legend
|
||||
* does not carry a nap entry.
|
||||
*/
|
||||
napMinutes?: number;
|
||||
/** How many naps that is. Absent alongside `napMinutes`. */
|
||||
napCount?: number;
|
||||
}
|
||||
|
||||
export interface SleepStageBreakdown {
|
||||
@@ -87,6 +96,16 @@ type WindowSize = (typeof WINDOW_DAYS)[number];
|
||||
*/
|
||||
export const STAGE_ORDER = ["DEEP", "REM", "CORE", "ASLEEP", "AWAKE"] as const;
|
||||
|
||||
/**
|
||||
* The nap band. Not a sleep stage — it is the day's daytime sleep, kept out
|
||||
* of the night's stage buckets and drawn as its own block on top of the
|
||||
* column so the night below it reads as the night.
|
||||
*
|
||||
* The band is only rendered when the visible window actually holds a nap. On
|
||||
* a week without one there is no bar, no legend entry, and no tooltip row.
|
||||
*/
|
||||
const NAP_KEY = "NAP";
|
||||
|
||||
/**
|
||||
* Dracula stage palette. Exported so the last-night hypnogram
|
||||
* (`sleep-hypnogram.tsx`) reuses the exact same tokens — per the
|
||||
@@ -99,6 +118,7 @@ export const STAGE_COLORS: Record<string, string> = {
|
||||
ASLEEP: "var(--success)", // dracula-green — legacy iOS 15- unspecified
|
||||
AWAKE: "var(--dracula-yellow)", // dracula-yellow — wake bouts
|
||||
IN_BED: "var(--chart-inbed)", // muted blue-grey — pre-asleep
|
||||
[NAP_KEY]: "var(--dracula-orange)", // daytime sleep, not a night stage
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -139,6 +159,48 @@ function formatDayTick(
|
||||
});
|
||||
}
|
||||
|
||||
/** One Recharts row: the day's label plus a numeric key per drawn band. */
|
||||
export type CompositionRow = Record<string, number | string>;
|
||||
|
||||
/**
|
||||
* Turn the trailing window of nights into the chart's rows.
|
||||
*
|
||||
* A day without a nap gets a plain `NAP: 0`, which draws as nothing. Pulled
|
||||
* out of the component because Recharts renders no markup under SSR, so this
|
||||
* is the only place the nap decision can actually be pinned by a test.
|
||||
*/
|
||||
export function buildCompositionRows(
|
||||
perNight: readonly SleepStageNight[],
|
||||
windowDays: number,
|
||||
formatLabel: (dayKey: string) => string,
|
||||
): { rows: CompositionRow[] } {
|
||||
const trailing = perNight.slice(-windowDays);
|
||||
const rows = trailing.map((night) => {
|
||||
const row: CompositionRow = {
|
||||
dayKey: night.dayKey,
|
||||
label: formatLabel(night.dayKey),
|
||||
};
|
||||
for (const stage of STAGE_ORDER) {
|
||||
row[stage] = night.stages[stage] ?? 0;
|
||||
}
|
||||
row[NAP_KEY] = night.napMinutes ?? 0;
|
||||
row.napCount = night.napCount ?? 0;
|
||||
return row;
|
||||
});
|
||||
return { rows };
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the visible window holds a nap at all. When it does not, the nap
|
||||
* band is never mounted — so there is no bar, no legend entry, and no tooltip
|
||||
* row saying a nap did not happen.
|
||||
*/
|
||||
export function windowHasNap(rows: readonly CompositionRow[]): boolean {
|
||||
return rows.some(
|
||||
(row) => typeof row[NAP_KEY] === "number" && (row[NAP_KEY] as number) > 0,
|
||||
);
|
||||
}
|
||||
|
||||
export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
const { t, locale } = useTranslations();
|
||||
|
||||
@@ -155,6 +217,7 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
ASLEEP: t("insights.sleep.stages.asleep"),
|
||||
AWAKE: t("insights.sleep.stages.awake"),
|
||||
IN_BED: t("insights.sleep.stages.inBed"),
|
||||
[NAP_KEY]: t("insights.sleep.stages.nap"),
|
||||
};
|
||||
|
||||
// v1.4.25 W3f — per-night dataset. Slice the trailing N nights and
|
||||
@@ -176,19 +239,14 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
}
|
||||
return [fallbackRow];
|
||||
}
|
||||
const trailing = perNight.slice(-windowDays);
|
||||
return trailing.map((night) => {
|
||||
const row: Record<string, number | string> = {
|
||||
dayKey: night.dayKey,
|
||||
label: formatDayTick(night.dayKey, windowDays, locale),
|
||||
};
|
||||
for (const stage of STAGE_ORDER) {
|
||||
row[stage] = night.stages[stage] ?? 0;
|
||||
}
|
||||
return row;
|
||||
});
|
||||
return buildCompositionRows(perNight, windowDays, (dayKey) =>
|
||||
formatDayTick(dayKey, windowDays, locale),
|
||||
).rows;
|
||||
}, [breakdown, windowDays, locale, t]);
|
||||
|
||||
// Mount the nap band only when the VISIBLE window holds one.
|
||||
const hasNap = useMemo(() => windowHasNap(data), [data]);
|
||||
|
||||
// Empty-state guard — no perNight rows AND no aggregate.
|
||||
const hasData =
|
||||
data.length > 0 &&
|
||||
@@ -287,10 +345,19 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
content={({ active, payload, label }) => {
|
||||
if (!active || !payload || payload.length === 0)
|
||||
return null;
|
||||
const row = payload[0]?.payload as
|
||||
Record<string, number | string> | undefined;
|
||||
const napCount =
|
||||
typeof row?.napCount === "number" ? row.napCount : 0;
|
||||
// The footer total is the NIGHT. The nap is listed above
|
||||
// it as its own line and stays out of the sum, so the
|
||||
// per-stage percentages keep meaning "of this night".
|
||||
const totalNight = payload.reduce(
|
||||
(sum, entry) =>
|
||||
sum +
|
||||
(typeof entry.value === "number" ? entry.value : 0),
|
||||
entry.dataKey === NAP_KEY
|
||||
? sum
|
||||
: sum +
|
||||
(typeof entry.value === "number" ? entry.value : 0),
|
||||
0,
|
||||
);
|
||||
return (
|
||||
@@ -303,6 +370,14 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
const minutes =
|
||||
typeof entry.value === "number" ? entry.value : 0;
|
||||
if (minutes === 0) return null;
|
||||
const isNap = stage === NAP_KEY;
|
||||
const label = isNap
|
||||
? napCount > 1
|
||||
? t("insights.sleep.stages.napCount", {
|
||||
count: String(napCount),
|
||||
})
|
||||
: stageLabels[NAP_KEY]
|
||||
: (stageLabels[stage] ?? stage);
|
||||
const pct =
|
||||
totalNight > 0
|
||||
? Math.round((minutes / totalNight) * 100)
|
||||
@@ -318,10 +393,14 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
className="inline-block h-2 w-2 rounded-sm"
|
||||
style={{ background: STAGE_COLORS[stage] }}
|
||||
/>
|
||||
{stageLabels[stage] ?? stage}
|
||||
{label}
|
||||
</span>
|
||||
<span className="text-muted-foreground">
|
||||
{formatDurationMinutes(minutes, t)} · {pct}%
|
||||
{/* The nap is not part of the night, so it
|
||||
gets no share-of-night percentage. */}
|
||||
{isNap
|
||||
? formatDurationMinutes(minutes, t)
|
||||
: `${formatDurationMinutes(minutes, t)} · ${pct}%`}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
@@ -351,6 +430,17 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
|
||||
<Cell fill={STAGE_COLORS[stage]} />
|
||||
</Bar>
|
||||
))}
|
||||
{hasNap && (
|
||||
<Bar
|
||||
key={NAP_KEY}
|
||||
dataKey={NAP_KEY}
|
||||
stackId="stages"
|
||||
fill={STAGE_COLORS[NAP_KEY]}
|
||||
isAnimationActive={false}
|
||||
>
|
||||
<Cell fill={STAGE_COLORS[NAP_KEY]} />
|
||||
</Bar>
|
||||
)}
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user