From 1784a48bfcb6c8f9f7eb2286300b14ddc376fbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Thu, 13 Mar 2025 21:17:20 +0100 Subject: [PATCH] fix(dashboard): ensure we only pass dates to formatdate --- .../components/report-chart/common/axis.tsx | 10 ++++- .../src/hooks/useFormatDateInterval.ts | 38 ++++++++++--------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/apps/dashboard/src/components/report-chart/common/axis.tsx b/apps/dashboard/src/components/report-chart/common/axis.tsx index ce0c8c3b..fbb597b9 100644 --- a/apps/dashboard/src/components/report-chart/common/axis.tsx +++ b/apps/dashboard/src/components/report-chart/common/axis.tsx @@ -72,7 +72,15 @@ export const useXAxisProps = ( scale: 'utc', domain: ['dataMin', 'dataMax'] as AxisDomain, tickFormatter: - interval === 'auto' ? undefined : (m: string) => formatDate(new Date(m)), + interval === 'auto' + ? undefined + : (m: string) => { + if (['dataMin', 'dataMax'].includes(m)) { + return m; + } + + return formatDate(new Date(m)); + }, type: 'number' as const, tickLine: false, minTickGap: 20, diff --git a/apps/dashboard/src/hooks/useFormatDateInterval.ts b/apps/dashboard/src/hooks/useFormatDateInterval.ts index e5702211..1e303054 100644 --- a/apps/dashboard/src/hooks/useFormatDateInterval.ts +++ b/apps/dashboard/src/hooks/useFormatDateInterval.ts @@ -1,26 +1,30 @@ import type { IInterval } from '@openpanel/validation'; export function formatDateInterval(interval: IInterval, date: Date): string { - if (interval === 'hour' || interval === 'minute') { - return new Intl.DateTimeFormat('en-GB', { - hour: '2-digit', - minute: '2-digit', - }).format(date); - } + try { + if (interval === 'hour' || interval === 'minute') { + return new Intl.DateTimeFormat('en-GB', { + hour: '2-digit', + minute: '2-digit', + }).format(date); + } - if (interval === 'month') { - return new Intl.DateTimeFormat('en-GB', { month: 'short' }).format(date); - } + if (interval === 'month') { + return new Intl.DateTimeFormat('en-GB', { month: 'short' }).format(date); + } - if (interval === 'day') { - return new Intl.DateTimeFormat('en-GB', { - weekday: 'short', - day: '2-digit', - month: '2-digit', - }).format(date); - } + if (interval === 'day') { + return new Intl.DateTimeFormat('en-GB', { + weekday: 'short', + day: '2-digit', + month: '2-digit', + }).format(date); + } - return date.toISOString(); + return date.toISOString(); + } catch (e) { + return ''; + } } export function useFormatDateInterval(interval: IInterval) {