fix(dashboard): ensure we only pass dates to formatdate

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-03-13 21:17:20 +01:00
parent 08b07e42fb
commit 1784a48bfc
2 changed files with 30 additions and 18 deletions

View File

@@ -72,7 +72,15 @@ export const useXAxisProps = (
scale: 'utc', scale: 'utc',
domain: ['dataMin', 'dataMax'] as AxisDomain, domain: ['dataMin', 'dataMax'] as AxisDomain,
tickFormatter: 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, type: 'number' as const,
tickLine: false, tickLine: false,
minTickGap: 20, minTickGap: 20,

View File

@@ -1,26 +1,30 @@
import type { IInterval } from '@openpanel/validation'; import type { IInterval } from '@openpanel/validation';
export function formatDateInterval(interval: IInterval, date: Date): string { export function formatDateInterval(interval: IInterval, date: Date): string {
if (interval === 'hour' || interval === 'minute') { try {
return new Intl.DateTimeFormat('en-GB', { if (interval === 'hour' || interval === 'minute') {
hour: '2-digit', return new Intl.DateTimeFormat('en-GB', {
minute: '2-digit', hour: '2-digit',
}).format(date); minute: '2-digit',
} }).format(date);
}
if (interval === 'month') { if (interval === 'month') {
return new Intl.DateTimeFormat('en-GB', { month: 'short' }).format(date); return new Intl.DateTimeFormat('en-GB', { month: 'short' }).format(date);
} }
if (interval === 'day') { if (interval === 'day') {
return new Intl.DateTimeFormat('en-GB', { return new Intl.DateTimeFormat('en-GB', {
weekday: 'short', weekday: 'short',
day: '2-digit', day: '2-digit',
month: '2-digit', month: '2-digit',
}).format(date); }).format(date);
} }
return date.toISOString(); return date.toISOString();
} catch (e) {
return '';
}
} }
export function useFormatDateInterval(interval: IInterval) { export function useFormatDateInterval(interval: IInterval) {