mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
On /insights/mood the line chart used to paint first and the calendar, whose aggregate is the slower read, pushed it down when it landed (CLS 0.16 at 390 px on a cold load). The page now holds both on final-height skeletons until the chart's read has settled and the calendar's has landed, then shows them in the same frame. The calendar's height is written in container-query units, so its box is the right size from the first paint, in the skeleton and the painted grid alike. A server wrapper prefetches both reads in parallel (time-boxed, skipped under a record switch, off when SSR prefetch is off), so on a warm cache they are in the first HTML. The route and the prefetch share `moodInsightsWire`, so the seeded cell is the route's shape. The "Log a mood entry" and "Take a mental-wellbeing check-in" text links under the heading are gone. Logging is the header plus, which opens the mood form in place; the check-in keeps its own place in the navigation. The plus is one shared component, `SubPageAddButton`, now also behind the measurement pages' capture link and the workouts page, which drew its own. An audit of every Insights page found no other text add-button of this kind; the remaining buttons are empty-state calls to action. A sweep keeps it that way. `e2e/v142-qol.spec.ts` covers all three surfaces of this change set.
80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
import { z } from "zod/v4";
|
|
|
|
import { apiSuccess, returnAllZodIssues } from "@/lib/api-response";
|
|
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { cachedSwr, caches, type ServerCache } from "@/lib/cache/server-cache";
|
|
import {
|
|
fetchMoodAggregates,
|
|
MOOD_STABILITY_WINDOWS,
|
|
moodInsightsWire,
|
|
type MoodAggregates,
|
|
type MoodStabilityWindow,
|
|
} from "@/lib/insights/mood-aggregates";
|
|
import { requireModuleEnabled } from "@/lib/modules/gate";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
/**
|
|
* v1.8.5 — pre-computed mood-insights aggregates for the Mood Insights
|
|
* page (heatmap, distribution, weekday pattern, tag breakdown,
|
|
* cross-metric correlations, summary headline).
|
|
*
|
|
* The page stays a client component for the interactive line chart but
|
|
* reads the heavy aggregates from here: the compute runs server-side
|
|
* once per 60-s cache window, the browser only ever receives pre-shaped
|
|
* data (no raw mood rows). This follows the v1.8.3 anti-freeze posture
|
|
* — a cheap cached read, never a synchronous LLM call.
|
|
*
|
|
* v1.12.1 — stale-while-revalidate. On an expired or freshly-marked-
|
|
* stale bucket (a mood write marks rather than evicts) the prior
|
|
* aggregate is served immediately and a single background recompute
|
|
* warms a fresh one, so an active logger never re-pays the cold compute.
|
|
*/
|
|
/**
|
|
* `?days=N` cuts `stability` to the trailing N days, so a period picker shows
|
|
* the steadiness of the period it shows. Without it the score covers the
|
|
* whole year, as it always has; `stabilityWindowDays` names the window the
|
|
* served score covers either way.
|
|
*/
|
|
const querySchema = z.object({
|
|
days: z
|
|
.enum(MOOD_STABILITY_WINDOWS.map(String) as [string, ...string[]])
|
|
.transform((v) => Number(v) as MoodStabilityWindow)
|
|
.optional(),
|
|
});
|
|
|
|
export const GET = apiHandler(async (request: Request) => {
|
|
const { user } = await requireAuth();
|
|
|
|
const query = querySchema.safeParse(
|
|
Object.fromEntries(new URL(request.url).searchParams),
|
|
);
|
|
if (!query.success) return returnAllZodIssues(query.error, 422);
|
|
const stabilityWindowDays: MoodStabilityWindow = query.data.days ?? 365;
|
|
|
|
// Per-domain gate: the mood-insights aggregate surface only serves the
|
|
// mood module's analysis page, so it must be gated on the mood module
|
|
// (mirrors `/api/insights/mood-status`). Disabled ⇒ 403 module.disabled.
|
|
const gate = await requireModuleEnabled(user.id, "mood");
|
|
if (!gate.enabled) return gate.response;
|
|
|
|
const result = await cachedSwr(
|
|
caches.moodInsights as ServerCache<MoodAggregates>,
|
|
user.id,
|
|
() => fetchMoodAggregates(user.id),
|
|
annotate,
|
|
);
|
|
|
|
annotate({
|
|
action: { name: "mood.insights.read" },
|
|
meta: {
|
|
total_entries: result.summary.totalEntries,
|
|
heatmap_window_days: result.heatmap.windowDays,
|
|
tag_count: result.tags.length,
|
|
},
|
|
});
|
|
|
|
return apiSuccess(moodInsightsWire(result, stabilityWindowDays));
|
|
});
|