Fix relativeDateFormat for short duractions

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Jonathan
2025-06-11 17:40:27 +02:00
committed by Sacha Al Himdani
parent 8a4c64c42f
commit b841e86964

View File

@@ -48,7 +48,8 @@ export function TranslatorProvider({
); );
} }
const MINUTES = 60_000; const SECONDS = 1000;
const MINUTES = SECONDS * 60;
const HOURS = MINUTES * 60; const HOURS = MINUTES * 60;
const DAYS = HOURS * 24; const DAYS = HOURS * 24;
const WEEKS = DAYS * 7; const WEEKS = DAYS * 7;
@@ -62,7 +63,7 @@ const relativeFormat = [
{ limit: DAYS, unit: "days" }, { limit: DAYS, unit: "days" },
{ limit: HOURS, unit: "hours" }, { limit: HOURS, unit: "hours" },
{ limit: MINUTES, unit: "minutes" }, { limit: MINUTES, unit: "minutes" },
{ limit: 0, unit: "seconds" }, { limit: SECONDS, unit: "seconds" },
] as const; ] as const;
export function useTranslate() { export function useTranslate() {
@@ -86,17 +87,18 @@ export function useTranslate() {
}; };
const relativeDateFormat = ( const relativeDateFormat = (
data: Date | string | null | undefined, date: Date | string | null | undefined,
options: Intl.RelativeTimeFormatOptions = { options: Intl.RelativeTimeFormatOptions = {
style: "long", style: "long",
} }
) => { ) => {
if (!data) { if (!date) {
return ""; return "";
} }
const distanceInSeconds = const distanceInSeconds =
(data instanceof Date ? data.getTime() : parseDate(data).getTime()) - (date instanceof Date ? date.getTime() : parseDate(date).getTime()) -
Date.now(); Date.now();
const formatter = new Intl.RelativeTimeFormat(lang, options); const formatter = new Intl.RelativeTimeFormat(lang, options);
for (const { limit, unit } of relativeFormat) { for (const { limit, unit } of relativeFormat) {
if (Math.abs(distanceInSeconds) > limit) { if (Math.abs(distanceInSeconds) > limit) {