diff --git a/src/app/(main)/reports/insights/InsightsTable.tsx b/src/app/(main)/reports/insights/InsightsTable.tsx index 692d7824b..095b4195c 100644 --- a/src/app/(main)/reports/insights/InsightsTable.tsx +++ b/src/app/(main)/reports/insights/InsightsTable.tsx @@ -4,12 +4,14 @@ import { useFormat, useMessages } from 'components/hooks'; import { ReportContext } from '../[reportId]/Report'; import EmptyPlaceholder from 'components/common/EmptyPlaceholder'; import { formatShortTime } from 'lib/format'; +import { useIntl } from 'react-intl'; export function InsightsTable() { const [fields, setFields] = useState([]); const { report } = useContext(ReportContext); const { formatMessage, labels } = useMessages(); const { formatValue } = useFormat(); + const intl = useIntl(); useEffect( () => { @@ -65,7 +67,7 @@ export function InsightsTable() { > {row => { const n = row?.totaltime / row?.visits; - return `${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`; + return `${+n < 0 ? '-' : ''}${formatShortTime(intl, Math.abs(~~n))}`; }} diff --git a/src/app/(main)/websites/[websiteId]/WebsiteMetricsBar.tsx b/src/app/(main)/websites/[websiteId]/WebsiteMetricsBar.tsx index c3cad3bc1..3eb6d0285 100644 --- a/src/app/(main)/websites/[websiteId]/WebsiteMetricsBar.tsx +++ b/src/app/(main)/websites/[websiteId]/WebsiteMetricsBar.tsx @@ -72,8 +72,7 @@ export function WebsiteMetricsBar({ value: totaltime.value / visits.value, prev: totaltime.prev / visits.prev, change: totaltime.value / visits.value - totaltime.prev / visits.prev, - formatValue: (n: number) => - `${+n < 0 ? '-' : ''}${formatShortTime(Math.abs(~~n), ['m', 's'], ' ')}`, + formatValue: (n: number) => `${+n < 0 ? '-' : ''}${formatShortTime(intl, Math.abs(~~n))}`, }, ] : []; diff --git a/src/lib/format.ts b/src/lib/format.ts index 836040628..b2480756d 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -1,4 +1,4 @@ -import type { FormatNumberOptions } from 'react-intl'; +import type { FormatNumberOptions, IntlShape } from 'react-intl'; export function parseTime(val: number) { const days = ~~(val / 86400); @@ -25,15 +25,15 @@ export function formatTime(val: number) { return `${h}${m}:${s}`; } -export function formatShortTime(val: number, formats = ['m', 's'], space = '') { +export function formatShortTime(intl: IntlShape, val: number, formats = ['m', 's']) { const { days, hours, minutes, seconds, ms } = parseTime(val); let t = ''; - if (days > 0 && formats.indexOf('d') !== -1) t += `${days}d${space}`; - if (hours > 0 && formats.indexOf('h') !== -1) t += `${hours}h${space}`; - if (minutes > 0 && formats.indexOf('m') !== -1) t += `${minutes}m${space}`; - if (seconds > 0 && formats.indexOf('s') !== -1) t += `${seconds}s${space}`; - if (ms > 0 && formats.indexOf('ms') !== -1) t += `${ms}ms`; + if (days > 0 && formats.indexOf('d') !== -1) t += `${intl.formatNumber(days)}d `; + if (hours > 0 && formats.indexOf('h') !== -1) t += `${intl.formatNumber(hours)}h `; + if (minutes > 0 && formats.indexOf('m') !== -1) t += `${intl.formatNumber(minutes)}m `; + if (seconds > 0 && formats.indexOf('s') !== -1) t += `${intl.formatNumber(seconds)}s `; + if (ms > 0 && formats.indexOf('ms') !== -1) t += `${intl.formatNumber(ms)}ms`; if (!t) { return `0${formats[formats.length - 1]}`;