From 6f3aa056a1fb0e85970d72cebca3443ec0954fbf Mon Sep 17 00:00:00 2001 From: Marcel Date: Mon, 20 Apr 2026 22:46:53 +0200 Subject: [PATCH] fix(relativeTime): guard against Invalid Date producing NaN strings If a row ever receives a malformed uploadedAt (e.g. manual SQL migration, backend regression), the helper now falls back to "vor 0 Minute(n)" rather than rendering "vor NaN Tag(en)" to the user. Addresses Nora's review suggestion. Co-Authored-By: Claude Opus 4.7 (1M context) --- frontend/src/lib/relativeTime.spec.ts | 7 +++++++ frontend/src/lib/relativeTime.ts | 3 +++ 2 files changed, 10 insertions(+) diff --git a/frontend/src/lib/relativeTime.spec.ts b/frontend/src/lib/relativeTime.spec.ts index 9373986e..1855d07d 100644 --- a/frontend/src/lib/relativeTime.spec.ts +++ b/frontend/src/lib/relativeTime.spec.ts @@ -31,4 +31,11 @@ describe('relativeTimeDe', () => { expect(relativeTimeDe(NOW, NOW)).toMatch(/0/); expect(relativeTimeDe(NOW, NOW)).toMatch(/Minute/i); }); + + it('falls back to 0 minutes when the input Date is invalid', () => { + const invalid = new Date('not-a-real-date'); + // Never crash the UI if the backend ever ships a malformed uploadedAt. + expect(relativeTimeDe(invalid, NOW)).toMatch(/0/); + expect(relativeTimeDe(invalid, NOW)).toMatch(/Minute/i); + }); }); diff --git a/frontend/src/lib/relativeTime.ts b/frontend/src/lib/relativeTime.ts index 1c78367a..f2e45a7e 100644 --- a/frontend/src/lib/relativeTime.ts +++ b/frontend/src/lib/relativeTime.ts @@ -2,6 +2,9 @@ import * as m from '$lib/paraglide/messages.js'; export function relativeTimeDe(from: Date, now: Date = new Date()): string { const minutes = Math.round((now.getTime() - from.getTime()) / 60_000); + // Malformed input (e.g. Invalid Date from a broken backend string) must not + // crash the dashboard — fall back to "0 Minuten" rather than render NaN. + if (!Number.isFinite(minutes)) return m.comment_time_minutes({ count: 0 }); if (minutes < 60) return m.comment_time_minutes({ count: minutes }); if (minutes < 1440) return m.comment_time_hours({ count: Math.round(minutes / 60) }); return m.comment_time_days({ count: Math.round(minutes / 1440) });