mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-12 06:06:10 +00:00
Six MEDs grouped per the W5 reconcile brief:
- Sec-MED-1: `hl_onboarding` cookie now `SameSite=Strict` (no
cross-site flow depends on it; sibling `healthlog_session` stays
`Lax` for the Withings OAuth callback).
- Sec-MED-2: `/onboarding` PUBLIC_PATHS match tightened from loose
`startsWith("/onboarding")` to exact path + subroute guard
(`pathname === "/onboarding" || pathname.startsWith("/onboarding/")`).
A future `/onboarding-export` route can no longer inherit the
silent-pass-through. Same guard mirrored on the redirect short-
circuit. Two new proxy tests pin the contract.
- Code-MED-3: lift `berlinDayKey()` from
`src/app/api/analytics/route.ts` to a shared
`src/lib/analytics/berlin-day.ts` and route the targets
sparkline-bucketing through it. The targets route used to bucket
by UTC `YYYY-MM-DD` so a 23:30-Berlin reading on Tuesday landed
in Wednesday's bucket. Four new unit tests pin DST + UTC-midnight
edge cases.
- Code-MED (streaming race): on slow connections SSE `done` fires
before the invalidate-refetch resolves, producing a 200-500ms
duplicate-bubble render. Add a 150ms grace window on the
message-thread that suppresses the persisted twin while the
streaming bubble is still in flight.
- Code-MED-2: drop dead i18n keys `insights.coach.settings` and
`insights.coach.settingsTooltip` (W3 removed the cog; the keys
had zero call sites and would carry confusing legacy if v1.4.23's
prompt-tuning panel picks a different key shape).
- Code-LOW-5: centralise `["user", "dashboardWidgets"]` query key
in `queryKeys.dashboardWidgets()` and migrate the three call
sites (dashboard, insights, settings/dashboard-layout-section).
Same query-key-collision discipline as `queryKeys.analytics()`.
Closes Sec-MED-1, Sec-MED-2, Code-MED-2, Code-MED-3, Code-MED-3
(streaming race), Code-LOW-5.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
142 lines
5.2 KiB
TypeScript
142 lines
5.2 KiB
TypeScript
/**
|
|
* Centralized TanStack Query key factory.
|
|
*
|
|
* Every useQuery/invalidateQueries call should go through this factory so that
|
|
* mutations invalidate the right consumers. Hard-coded string arrays drifted in
|
|
* the past (e.g. ["measurements"] didn't invalidate ["analytics"] on the
|
|
* dashboard), so treat this file as the single source of truth.
|
|
*/
|
|
|
|
import type { QueryClient, QueryKey } from "@tanstack/react-query";
|
|
|
|
export const queryKeys = {
|
|
auth: () => ["auth"] as const,
|
|
authRegistrationStatus: () => ["auth", "registration-status"] as const,
|
|
|
|
measurements: () => ["measurements"] as const,
|
|
moodEntries: () => ["mood-entries"] as const,
|
|
|
|
analytics: () => ["analytics"] as const,
|
|
moodAnalytics: () => ["mood-analytics"] as const,
|
|
|
|
insightsRoot: () => ["insights"] as const,
|
|
insightsComprehensive: () => ["insights", "comprehensive"] as const,
|
|
insightsTargets: () => ["insights", "targets"] as const,
|
|
/**
|
|
* v1.4.16 phase D reconcile (CRITICAL C1 + C2) — shared cache key for
|
|
* the rich `/api/insights/generate` advisor payload. Both `/insights`
|
|
* (full `<InsightAdvisorCard>`) and `/` (`<InsightsCardPreview>`) read
|
|
* this key so a regenerate on one surface refreshes the other without
|
|
* a second LLM round-trip.
|
|
*/
|
|
insightsAdvisor: () => ["insights", "advisor"] as const,
|
|
insightsGeneralStatus: (locale: string) =>
|
|
["insights", "general-status", locale] as const,
|
|
insightsBpStatus: (locale: string) =>
|
|
["insights", "blood-pressure-status", locale] as const,
|
|
insightsWeightStatus: (locale: string) =>
|
|
["insights", "weight-status", locale] as const,
|
|
insightsPulseStatus: (locale: string) =>
|
|
["insights", "pulse-status", locale] as const,
|
|
insightsBmiStatus: (locale: string) =>
|
|
["insights", "bmi-status", locale] as const,
|
|
insightsMoodStatus: (locale: string) =>
|
|
["insights", "mood-status", locale] as const,
|
|
insightsMedicationComplianceStatus: (locale: string) =>
|
|
["insights", "medication-compliance-status", locale] as const,
|
|
|
|
medications: () => ["medications"] as const,
|
|
medicationDetail: (id: string) => ["medications", id] as const,
|
|
medicationComplianceChart: (medicationId: string) =>
|
|
["compliance-chart-inline", medicationId] as const,
|
|
medicationPhaseConfig: (medicationId: string) =>
|
|
["phase-config", medicationId] as const,
|
|
medicationIntakeSummary: () => ["medications", "intake-summary"] as const,
|
|
|
|
gamificationAchievements: () => ["gamification", "achievements"] as const,
|
|
|
|
passkeys: () => ["passkeys"] as const,
|
|
|
|
notificationsPreferences: () => ["notifications", "preferences"] as const,
|
|
|
|
settingsGlobalServices: () => ["settings", "global-services"] as const,
|
|
settingsNtfy: () => ["settings", "ntfy"] as const,
|
|
|
|
tokens: () => ["tokens"] as const,
|
|
telegram: () => ["telegram"] as const,
|
|
telegramSettings: () => ["telegram", "settings"] as const,
|
|
withings: () => ["withings"] as const,
|
|
|
|
adminSettings: () => ["admin", "settings"] as const,
|
|
adminStatus: () => ["admin", "status"] as const,
|
|
adminUsers: () => ["admin", "users"] as const,
|
|
adminTokens: () => ["admin", "tokens"] as const,
|
|
adminAuditLog: (filter: unknown) => ["admin", "audit-log", filter] as const,
|
|
|
|
bugreportStatus: () => ["bugreport", "status"] as const,
|
|
|
|
/**
|
|
* v1.4.22 W5 reconcile (Code-LOW-5) — `["user", "dashboardWidgets"]`
|
|
* was duplicated as a literal at three call sites (dashboard,
|
|
* insights, settings/dashboard-layout). One typo turns into a
|
|
* silent cache miss + extra fetch; the centralised key defends
|
|
* against the same query-key-collision class as `analytics()`.
|
|
*/
|
|
dashboardWidgets: () => ["user", "dashboardWidgets"] as const,
|
|
};
|
|
|
|
/**
|
|
* Keys that should be invalidated when a measurement is created, updated or
|
|
* deleted. Kept here so dashboards, insights, and targets always stay in sync.
|
|
*/
|
|
export const measurementDependentKeys = [
|
|
queryKeys.measurements(),
|
|
queryKeys.analytics(),
|
|
queryKeys.insightsRoot(),
|
|
queryKeys.insightsTargets(),
|
|
queryKeys.gamificationAchievements(),
|
|
];
|
|
|
|
/**
|
|
* Keys that should be invalidated when a mood entry is created, updated or
|
|
* deleted.
|
|
*/
|
|
export const moodDependentKeys = [
|
|
queryKeys.moodEntries(),
|
|
queryKeys.moodAnalytics(),
|
|
queryKeys.insightsRoot(),
|
|
queryKeys.insightsTargets(),
|
|
queryKeys.gamificationAchievements(),
|
|
];
|
|
|
|
/**
|
|
* Keys invalidated when medications change (CRUD or intake).
|
|
*/
|
|
export const medicationDependentKeys = [
|
|
queryKeys.medications(),
|
|
queryKeys.analytics(),
|
|
queryKeys.insightsRoot(),
|
|
queryKeys.insightsTargets(),
|
|
queryKeys.medicationIntakeSummary(),
|
|
queryKeys.gamificationAchievements(),
|
|
];
|
|
|
|
/**
|
|
* Invalidate every key in the bundle in parallel. Use this from mutation
|
|
* `onSuccess` handlers so the call site stays a one-liner instead of repeating
|
|
* `Promise.all(keys.map(...))` everywhere.
|
|
*
|
|
* Uses `allSettled` so one transient network failure doesn't abort subsequent
|
|
* invalidations (cache would otherwise be left half-stale) and so the `void
|
|
* invalidateKeys(...)` fire-and-forget pattern in delete handlers never
|
|
* surfaces an unhandled rejection.
|
|
*/
|
|
export function invalidateKeys(
|
|
queryClient: QueryClient,
|
|
keys: readonly QueryKey[],
|
|
): Promise<PromiseSettledResult<unknown>[]> {
|
|
return Promise.allSettled(
|
|
keys.map((queryKey) => queryClient.invalidateQueries({ queryKey })),
|
|
);
|
|
}
|