From 0a92d455c8277ec9643af9af205894e4537c60ea Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 2 Mar 2025 15:13:56 +0100 Subject: [PATCH] show localized elapsed time --- interface/src/app/settings/Version.tsx | 62 +++++++++++++++++--------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/interface/src/app/settings/Version.tsx b/interface/src/app/settings/Version.tsx index 179068798..b852bcacf 100644 --- a/interface/src/app/settings/Version.tsx +++ b/interface/src/app/settings/Version.tsx @@ -31,7 +31,7 @@ import { FormLoader, SectionContent, useLayoutTitle } from 'components'; import { useI18nContext } from 'i18n/i18n-react'; const Version = () => { - const { LL } = useI18nContext(); + const { LL, locale } = useI18nContext(); const [restarting, setRestarting] = useState(false); const [openInstallDialog, setOpenInstallDialog] = useState(false); const [usingDevVersion, setUsingDevVersion] = useState(false); @@ -92,6 +92,30 @@ const Version = () => { } }, [latestVersion, latestDevVersion]); + const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }); + const DIVISIONS = [ + { amount: 60, name: 'seconds' }, + { amount: 60, name: 'minutes' }, + { amount: 24, name: 'hours' }, + { amount: 7, name: 'days' }, + { amount: 4.34524, name: 'weeks' }, + { amount: 12, name: 'months' }, + { amount: Number.POSITIVE_INFINITY, name: 'years' } + ]; + function formatTimeAgo(date) { + let duration = (date.getTime() - new Date().getTime()) / 1000; + for (let i = 0; i < DIVISIONS.length; i++) { + const division = DIVISIONS[i]; + if (Math.abs(duration) < division.amount) { + return rtf.format( + Math.round(duration), + division.name as Intl.RelativeTimeFormatUnit + ); + } + duration /= division.amount; + } + } + const getBinURL = () => { if (!internetLive) { return ''; @@ -274,7 +298,7 @@ const Version = () => { { }} /> } + slotProps={{ + typography: { + color: 'grey' + } + }} checked={!isDev} label={LL.STABLE()} - sx={{ '& .MuiSvgIcon-root': { fontSize: 18 } }} + sx={{ '& .MuiSvgIcon-root': { fontSize: 16 } }} /> { }} /> } + slotProps={{ + typography: { + color: 'grey' + } + }} checked={isDev} label={LL.DEVELOPMENT()} - sx={{ '& .MuiSvgIcon-root': { fontSize: 18 } }} + sx={{ '& .MuiSvgIcon-root': { fontSize: 16 } }} /> @@ -332,14 +366,7 @@ const Version = () => { {latestVersion.published_at && (  ( - {LL.DAYS_AGO( - Math.floor( - (Date.now() - - new Date(latestVersion.published_at).getTime()) / - (1000 * 60 * 60 * 24) - ) - )} - ) + {formatTimeAgo(new Date(latestVersion.published_at))}) )} {!usingDevVersion && showButtons(false)} @@ -357,14 +384,7 @@ const Version = () => { {latestDevVersion.published_at && (  ( - {LL.DAYS_AGO( - Math.floor( - (Date.now() - - new Date(latestDevVersion.published_at).getTime()) / - (1000 * 60 * 60 * 24) - ) - )} - ) + {formatTimeAgo(new Date(latestDevVersion.published_at))}) )} {showButtons(true)}