Files
HealthLog/src/lib/format.ts
T
Marc-André Bombeck 267868c013 chore(cleanup): retire orphan exports and the stale legacy insights prompt module
Drop fourteen exported helpers and constants with zero call sites:
countMessages, buildGeneratePrompts, _resetSafetyContractsCacheForTests,
forEachLocaleAndRule, getPulseRange, trendLinePoints,
CHART_MINI_HEIGHT_PX, formatDateWithWeekday,
resolveGeneralStatusLocale, isSubPageSlug, recordHeartbeat,
EVENT_TYPE_LABELS, cleanupExpiredRateLimits, and sleepStageEnum.

Also retire the v1.4.20 legacy insights prompt scaffold —
getInsightsSystemPrompt, InsightsOutput, and the two dead
INSIGHTS_SYSTEM_PROMPT_EN/DE constants — plus the matching vi.mock
key in the generate route test that no longer maps to a live export.
Drop the empty __testables block in glp1-snapshot.ts, the legacy
helper reference in the insight-generator JSDoc, and the two stale
v1.4.16 InsightsPageHero comments in hero-strip + its test file.

Consume the MedicationComplianceChart compareBaseline prop via an
explicit void so the destructure documents the intentional accept-and-
defer pattern instead of looking like a missed wire-up.

Every symbol verified zero-caller via grep across src/, scripts/, and
__tests__/ before drop.
2026-05-15 21:16:35 +02:00

64 lines
2.2 KiB
TypeScript

/**
* Legacy locale-aware date/time helpers.
*
* These originally hard-coded `de-DE`. They now defer to `makeFormatters`
* using a best-effort read of `healthlog-locale` from cookie → localStorage,
* falling back to `en`. New code should prefer `useFormatters()` from
* `@/lib/i18n/context` (client) or `makeFormatters(locale)` from
* `@/lib/format-locale` (server).
*
* SSR caveat: on the server `activeLocale()` always returns `en` because
* `document` is undefined. All current call sites render their formatted
* strings only after a `useQuery` fetch — i.e. post-hydration — so there is
* no hydration-mismatch path today. If you add a new caller that renders
* pre-fetch data (e.g. a static prop), migrate it to `useFormatters()` to
* stay consistent with the server-rendered locale.
*/
import { makeFormatters, DISPLAY_TIMEZONE } from "./format-locale";
import type { Locale } from "./i18n/config";
export { DISPLAY_TIMEZONE };
function activeLocale(): Locale {
if (typeof document === "undefined") return "en";
// Prefer the cookie (what SSR used) over localStorage to stay in sync
// across hydration. Falls back to localStorage for backwards compat.
const cookieMatch = document.cookie.match(
/(?:^|;\s*)healthlog-locale=([^;]+)/,
);
const fromCookie = cookieMatch?.[1];
if (fromCookie === "de" || fromCookie === "en") return fromCookie;
const fromStorage = window.localStorage?.getItem("healthlog-locale");
return fromStorage === "de" ? "de" : "en";
}
function formatters() {
return makeFormatters(activeLocale());
}
/** Locale-aware "19.02.2026, 14:30" or "02/19/2026, 2:30 PM". */
export function formatDateTime(date: Date | string): string {
return formatters().dateTime(date);
}
/** Locale-aware "19.02.2026" or "02/19/2026". */
export function formatDate(date: Date | string): string {
return formatters().date(date);
}
/** Locale-aware short date without year unless `includeYear`. */
export function formatDateShort(
date: Date | string,
includeYear = false,
): string {
const f = formatters();
return includeYear ? f.date(date) : f.dateShort(date);
}
/** Locale-aware "14:30". */
export function formatTime(date: Date | string): string {
return formatters().time(date);
}