mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-09 04:50:59 +00:00
refactor(mood): keep the trend windows in one place
The dead-code sweep found the window type exported and unread. It was unread because the surface that offers the choice carried its own copy of the same three numbers: the chart could not import the calculator's, since that module reaches the database client two hops down and a client component pulling it in would ship Prisma to the browser. Both now come from the dimension table, which is pure by construction and which the chart already imported. One declaration of 7 / 30 / 90, read by the calculator that averages over them and by the control that offers them, so the two cannot drift.
This commit is contained in:
1 parent
3dd79ec734
commit
5e1f5aa9ea
4 files changed
+26
-13
No files matched your search
@@ -12,7 +12,11 @@ import {
|
||||
} from "recharts";
|
||||
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
import { MOOD_DIMENSIONS } from "@/lib/mood/dimensions";
|
||||
import {
|
||||
MOOD_DIMENSIONS,
|
||||
MOOD_DIMENSION_WINDOWS,
|
||||
type MoodDimensionWindow,
|
||||
} from "@/lib/mood/dimensions";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
/**
|
||||
@@ -50,9 +54,6 @@ export interface MoodDimensionSummaryData {
|
||||
series: MoodDimensionPointData[];
|
||||
}
|
||||
|
||||
const WINDOWS = [7, 30, 90] as const;
|
||||
type Window = (typeof WINDOWS)[number];
|
||||
|
||||
/** One colour per dimension, from the same semantic set the other charts use. */
|
||||
const COLOR_BY_KEY: Record<string, string> = {
|
||||
a1: "var(--chart-1)",
|
||||
@@ -64,7 +65,7 @@ const COLOR_BY_KEY: Record<string, string> = {
|
||||
|
||||
function meanForWindow(
|
||||
summary: MoodDimensionSummaryData,
|
||||
window: Window,
|
||||
window: MoodDimensionWindow,
|
||||
): number | null {
|
||||
if (window === 7) return summary.avg7;
|
||||
if (window === 30) return summary.avg30;
|
||||
@@ -77,7 +78,7 @@ export function MoodDimensionTrends({
|
||||
dimensions: MoodDimensionSummaryData[];
|
||||
}) {
|
||||
const { t } = useTranslations();
|
||||
const [window, setWindow] = useState<Window>(30);
|
||||
const [window, setWindow] = useState<MoodDimensionWindow>(30);
|
||||
|
||||
const present = dimensions.filter((d) => d.present);
|
||||
const missing = dimensions.filter((d) => !d.present);
|
||||
@@ -115,7 +116,7 @@ export function MoodDimensionTrends({
|
||||
return (
|
||||
<div className="space-y-3" data-slot="mood-dimension-trends">
|
||||
<div className="flex flex-wrap items-center gap-1.5">
|
||||
{WINDOWS.map((option) => (
|
||||
{MOOD_DIMENSION_WINDOWS.map((option) => (
|
||||
<button
|
||||
key={option}
|
||||
type="button"
|
||||
|
||||
@@ -41,7 +41,6 @@ import {
|
||||
} from "@/lib/insights/mood-dimension-series";
|
||||
export {
|
||||
computeMoodDimensionSeries,
|
||||
MOOD_DIMENSION_WINDOWS,
|
||||
type MoodDimensionPoint,
|
||||
type MoodDimensionSummary,
|
||||
} from "@/lib/insights/mood-dimension-series";
|
||||
|
||||
@@ -14,14 +14,14 @@
|
||||
* is indistinguishable from a given one — and five is the answer somebody
|
||||
* gives on a genuinely average day.
|
||||
*/
|
||||
import { MOOD_DIMENSIONS, type MoodDimensionKey } from "@/lib/mood/dimensions";
|
||||
import {
|
||||
MOOD_DIMENSIONS,
|
||||
MOOD_DIMENSION_WINDOWS,
|
||||
type MoodDimensionKey,
|
||||
} from "@/lib/mood/dimensions";
|
||||
import { dayKeyAgeInDays } from "@/lib/insights/measurement-freshness";
|
||||
import { round } from "@/lib/insights/status-shared";
|
||||
|
||||
/** Windows the trend reports, in days. Concept §10.1. */
|
||||
export const MOOD_DIMENSION_WINDOWS = [7, 30, 90] as const;
|
||||
export type MoodDimensionWindow = (typeof MOOD_DIMENSION_WINDOWS)[number];
|
||||
|
||||
/** One day's mean for one dimension. */
|
||||
export interface MoodDimensionPoint {
|
||||
/** `YYYY-MM-DD`, the row's own local day key. */
|
||||
|
||||
@@ -107,6 +107,19 @@ export const MOOD_DIMENSIONS: readonly MoodDimension[] = [
|
||||
},
|
||||
] as const;
|
||||
|
||||
/**
|
||||
* Windows the per-dimension trend reports, in days. Concept §10.1.
|
||||
*
|
||||
* Here rather than beside the calculator that averages over them, because the
|
||||
* chart offering the choice needs the same three numbers and cannot import
|
||||
* that module: it reaches Prisma two hops down, and a client component that
|
||||
* pulled it in would drag the database client into the browser bundle. This
|
||||
* module is pure by construction, which is what makes it the one place both
|
||||
* ends can agree on.
|
||||
*/
|
||||
export const MOOD_DIMENSION_WINDOWS = [7, 30, 90] as const;
|
||||
export type MoodDimensionWindow = (typeof MOOD_DIMENSION_WINDOWS)[number];
|
||||
|
||||
/** Lowest value any dimension accepts. */
|
||||
export const MOOD_DIMENSION_MIN = 0;
|
||||
/** Highest value any dimension accepts. */
|
||||
|
||||
Reference in new issue
Block a user