From 14fe84123f512533532f5959e4a40ebfa007f02d Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 17 Feb 2025 11:00:37 -0600 Subject: [PATCH] Expiry should round up --- .../moderation/LabelsOnMeDialog.tsx | 2 +- .../moderation/ModerationDetailsDialog.tsx | 2 +- src/lib/hooks/useTimeAgo.ts | 27 ++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/components/moderation/LabelsOnMeDialog.tsx b/src/components/moderation/LabelsOnMeDialog.tsx index a46e7ca658..b4e87258ef 100644 --- a/src/components/moderation/LabelsOnMeDialog.tsx +++ b/src/components/moderation/LabelsOnMeDialog.tsx @@ -123,7 +123,7 @@ function Label({ const sourceName = labeler ? sanitizeHandle(labeler.creator.handle, '@') : label.src - const timeDiff = useGetTimeAgo() + const timeDiff = useGetTimeAgo({future: true}) return ( { - const diff = dateDiff(earlier, later) + const diff = dateDiff(earlier, later, future ? 'up' : 'down') return formatDateDiff({diff, i18n, format: options?.format}) }, - [i18n], + [i18n, future], ) } @@ -45,6 +45,7 @@ export function useGetTimeAgo() { export function dateDiff( earlier: number | string | Date, later: number | string | Date, + rounding: 'up' | 'down' = 'down', ): DateDiff { let diff = { value: 0, @@ -65,25 +66,37 @@ export function dateDiff( unit: 'second' as DateDiff['unit'], } } else if (diffSeconds < HOUR) { - const value = Math.floor(diffSeconds / MINUTE) + const value = + rounding === 'up' + ? Math.ceil(diffSeconds / MINUTE) + : Math.floor(diffSeconds / MINUTE) diff = { value, unit: 'minute' as DateDiff['unit'], } } else if (diffSeconds < DAY) { - const value = Math.floor(diffSeconds / HOUR) + const value = + rounding === 'up' + ? Math.ceil(diffSeconds / HOUR) + : Math.floor(diffSeconds / HOUR) diff = { value, unit: 'hour' as DateDiff['unit'], } } else if (diffSeconds < MONTH_30) { - const value = Math.floor(diffSeconds / DAY) + const value = + rounding === 'up' + ? Math.ceil(diffSeconds / DAY) + : Math.floor(diffSeconds / DAY) diff = { value, unit: 'day' as DateDiff['unit'], } } else { - const value = Math.floor(diffSeconds / MONTH_30) + const value = + rounding === 'up' + ? Math.ceil(diffSeconds / MONTH_30) + : Math.floor(diffSeconds / MONTH_30) diff = { value, unit: 'month' as DateDiff['unit'],