fix: null-safe date formatting — formatDate/formatRelative/daysUntil now handle null/undefined

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.
This commit is contained in:
Z User committed 2026-10-29 18:09:40 +00:00
1 parent e06916de22
commit 52effad4a5
1 file changed
+12 -4
+12 -4
View File
@@ -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)
}