From 52effad4a5f597e74a8f7a78a33a498d6483f46d Mon Sep 17 00:00:00 2001 From: Z User Date: Thu, 29 Oct 2026 18:09:40 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20null-safe=20date=20formatting=20?= =?UTF-8?q?=E2=80=94=20formatDate/formatRelative/daysUntil=20now=20handle?= =?UTF-8?q?=20null/undefined?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause: instance detail crashed with 'Cannot read properties of undefined (reading toLocaleDateString)' because license.cancelledAt was null but formatDate() didn't handle null. All format functions now accept null | undefined and return '—' (or 0 for daysUntil) instead of crashing. --- src/lib/format.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/lib/format.ts b/src/lib/format.ts index a53cd69..7395096 100755 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -20,18 +20,24 @@ export function formatEuroCompact(cents: number): string { return formatEuro(cents) } -export function formatDate(d: Date | string, opts?: Intl.DateTimeFormatOptions): string { +export function formatDate(d: Date | string | null | undefined, opts?: Intl.DateTimeFormatOptions): string { + if (!d) return '—' const date = typeof d === 'string' ? new Date(d) : d + if (isNaN(date.getTime())) return '—' return date.toLocaleDateString('de-DE', opts ?? { day: '2-digit', month: '2-digit', year: 'numeric' }) } -export function formatDateTime(d: Date | string): string { +export function formatDateTime(d: Date | string | null | undefined): string { + if (!d) return '—' const date = typeof d === 'string' ? new Date(d) : d + if (isNaN(date.getTime())) return '—' return date.toLocaleString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit' }) } -export function formatRelative(d: Date | string): string { +export function formatRelative(d: Date | string | null | undefined): string { + if (!d) return '—' const date = typeof d === 'string' ? new Date(d) : d + if (isNaN(date.getTime())) return '—' const diff = date.getTime() - Date.now() const days = Math.round(diff / 86400000) const rtf = new Intl.RelativeTimeFormat('de-DE', { numeric: 'auto' }) @@ -42,8 +48,10 @@ export function formatRelative(d: Date | string): string { return rtf.format(mins, 'minute') } -export function daysUntil(d: Date | string): number { +export function daysUntil(d: Date | string | null | undefined): number { + if (!d) return 0 const date = typeof d === 'string' ? new Date(d) : d + if (isNaN(date.getTime())) return 0 return Math.round((date.getTime() - Date.now()) / 86400000) }