Cap chronotype window in the shared assembler

Make computeSleepRhythmFromNights own the chronotype look-back so the dashboard
summary (a year of nights) and GET /api/sleep/rhythm (six weeks) return the
same band, mid-sleep, and social-jetlag values rather than averaging different
samples. Derive the free/work day-type from the local wake-day digits so the
classification holds in far-east timezones, and read the shared sleep-rhythm
DTO type into the client hook instead of a hand-mirrored copy.
This commit is contained in:
Marc-André Bombeck committed 2026-06-14 19:10:44 +02:00
1 parent d1ba01548f
commit 1ce1c786b6
4 files changed
+99 -59

No files matched your search

@@ -5,42 +5,16 @@ import { useQuery } from "@tanstack/react-query";
import { queryKeys } from "@/lib/query-keys";
import { apiGet } from "@/lib/api/api-fetch";
/**
* v1.17.0 — sleep-debt DTO (mirrors the server `SleepDebtDto`). `partial` is
* the calm "still learning" state below the night threshold; `ready` asserts
* the cumulative deficit.
*/
export interface SleepDebtDto {
state: "partial" | "ready";
debtMinutes: number;
needMinutes: number;
nightsCounted: number;
windowNights: number;
nightsUntilReady: number;
}
// v1.17.0 — the wire shape IS the server DTO. Import the type directly (erased
// at build, so no server code reaches the client bundle) rather than keeping a
// second hand-mirrored copy that silently drifts when the server shape changes.
import type {
SleepDebtDto,
ChronotypeDto,
SleepRhythmDto,
} from "@/lib/insights/derived/sleep-rhythm";
/** v1.17.0 — chronotype DTO (mirrors the server `ChronotypeDto`). */
export interface ChronotypeDto {
state: "learning" | "ready";
msfMinutes: number | null;
msfScMinutes: number | null;
band:
| "extreme_early"
| "early"
| "intermediate"
| "late"
| "extreme_late"
| null;
socialJetlagMinutes: number | null;
freeNightsCounted: number;
workNightsCounted: number;
freeNightsUntilReady: number;
}
export interface SleepRhythmDto {
sleepDebt: SleepDebtDto;
chronotype: ChronotypeDto;
}
export type { SleepDebtDto, ChronotypeDto, SleepRhythmDto };
/**
* Read the server-authoritative sleep-rhythm DTO. Gated on `enabled` so a