mirror of
https://github.com/bitbaum/vitareba.git
synced 2026-12-05 07:28:27 +01:00
- lib/domain/checkin.ts: add streakMessage() — milestone thresholds (30/14/7/3/2 days) are domain decisions, not UI copy; page now imports instead of defining - lib/utils/format.ts: add relativeDate() — was duplicated in admin/patients page; formatting utilities belong here alongside other date helpers - lib/domain/signals.ts: add sparkLevel() — wellness categorisation using SPARKLINE thresholds belongs in signals domain next to wellnessAvg() - admin/patients page: remove all three local function defs, clean unused imports - admin/reports page: replace magic -7 with SIGNAL_CHECKIN_WINDOW_DAYS No behaviour change — same logic, now in testable, single-homed modules. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
// Date formatting utilities — SSOT for locale + format decisions
|
|
|
|
export const HOUR_MS = 60 * 60 * 1000;
|
|
export const DAY_MS = 24 * HOUR_MS;
|
|
|
|
const LOCALE = "en-GB";
|
|
|
|
/** "2 Apr 2025" */
|
|
export function formatDateShort(date: Date | string): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return d.toLocaleDateString(LOCALE, { day: "numeric", month: "short", year: "numeric" });
|
|
}
|
|
|
|
/** "2 April 2025" */
|
|
export function formatDateLong(date: Date | string): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return d.toLocaleDateString(LOCALE, { day: "numeric", month: "long", year: "numeric" });
|
|
}
|
|
|
|
/** "02/04/2025" (numeric DD/MM/YYYY) */
|
|
export function formatDateNumeric(date: Date | string): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return d.toLocaleDateString(LOCALE);
|
|
}
|
|
|
|
/** "2 Apr, 10:30" — for message timestamps */
|
|
export function formatDateTime(date: Date | string): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return d.toLocaleString(LOCALE, { day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" });
|
|
}
|
|
|
|
/** "2 Apr" — for chart axis labels where year is omitted for compactness */
|
|
export function formatDateMonthDay(date: Date | string): string {
|
|
const d = typeof date === "string" ? new Date(date) : date;
|
|
return d.toLocaleDateString(LOCALE, { day: "numeric", month: "short" });
|
|
}
|
|
|
|
/** ISO date string "YYYY-MM-DD" — for DB date column comparisons */
|
|
export function formatDateISO(date: Date): string {
|
|
return date.toISOString().slice(0, 10);
|
|
}
|
|
|
|
/**
|
|
* Human-readable relative date label for admin lists: "Today", "Yesterday", "Nd ago".
|
|
* Accepts an ISO date string (YYYY-MM-DD) and a reference Date for testability.
|
|
*/
|
|
export function relativeDate(dateStr: string, now: Date): string {
|
|
const d = new Date(dateStr + "T00:00:00");
|
|
const days = Math.floor((now.getTime() - d.getTime()) / DAY_MS);
|
|
if (days === 0) return "Today";
|
|
if (days === 1) return "Yesterday";
|
|
return `${days}d ago`;
|
|
}
|
|
|
|
/**
|
|
* Returns the patient's display name: their name if set, otherwise the
|
|
* local part of their email address, otherwise the fallback string.
|
|
* Used wherever a human-readable name is needed for emails and alerts.
|
|
*/
|
|
export function displayName(
|
|
name: string | null | undefined,
|
|
email: string | null | undefined,
|
|
fallback = "there"
|
|
): string {
|
|
return name ?? email?.split("@")[0] ?? fallback;
|
|
}
|