// Shared formatting helpers (dates, success badges). Locale-aware via Paraglide. import { getLocale } from '@web/paraglide/runtime'; import { m } from '@web/paraglide/messages'; export function fmtDate(iso: string | null | undefined): string { if (!iso) return '—'; const d = new Date(iso.replace(' ', 'T')); return d.toLocaleDateString(getLocale(), { day: '2-digit', month: '2-digit', year: 'numeric' }); } // The date (no time) of a Unix-seconds timestamp, for day headings. export function fmtDay(unixSeconds: number | null | undefined): string { if (!unixSeconds) return '—'; return new Date(unixSeconds * 1000).toLocaleDateString(getLocale(), { weekday: 'long', day: '2-digit', month: 'long', year: 'numeric', }); } // A coarse, locale-aware "3 minutes ago" for the same Unix-seconds timestamp. export function fmtRelative(unixSeconds: number | null | undefined): string { if (!unixSeconds) return '—'; const rtf = new Intl.RelativeTimeFormat(getLocale(), { numeric: 'auto' }); const diffSec = unixSeconds - Date.now() / 1000; const abs = Math.abs(diffSec); const steps: [Intl.RelativeTimeFormatUnit, number][] = [ ['year', 31_536_000], ['month', 2_592_000], ['day', 86_400], ['hour', 3600], ['minute', 60], ]; for (const [unit, secs] of steps) { if (abs >= secs) return rtf.format(Math.round(diffSec / secs), unit); } return rtf.format(Math.round(diffSec), 'second'); } export function daysUntil(iso: string | null | undefined): number | null { if (!iso) return null; const target = new Date(iso); const now = new Date(); return Math.ceil((target.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)); } export interface SuccessBadge { cls: 'green' | 'orange' | 'red' | 'grey'; label: string; } export function successBadge(rate: number | null | undefined): SuccessBadge { if (rate === null || rate === undefined) return { cls: 'grey', label: m.card_not_tested_yet() }; if (rate >= 75) return { cls: 'green', label: m.fmt_success_rate({ rate }) }; if (rate >= 50) return { cls: 'orange', label: m.fmt_success_rate({ rate }) }; return { cls: 'red', label: m.fmt_success_rate({ rate }) }; }