forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseTranslatedTimeDeltaFormatter.ts
42 lines (37 loc) · 1.21 KB
/
useTranslatedTimeDeltaFormatter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { useTranslation } from 'react-i18next'
import { Formatter } from 'react-timeago'
export interface UseTranslatedTimeDeltaFormatterOptions {
// Whether or not to show words around the time delta. If false, the time is
// just rendered as the value (e.g. 1 day). If true, the time would be
// rendered as "1 day ago", "1 day left", or "in 1 day".
words: boolean
// If date is in the future, use either "left" suffix or "in" prefix.
futureMode?: 'left' | 'in'
}
export const useTranslatedTimeDeltaFormatter = ({
words,
futureMode = 'left',
}: UseTranslatedTimeDeltaFormatterOptions): Formatter => {
const { t } = useTranslation()
const timeDeltaFormatter: Formatter = (value, unit, suffix) => {
const lessThanOneMinute = unit === 'second' && value < 60
if (lessThanOneMinute) {
value = 1
unit = 'minute'
}
return t(
words
? suffix === 'ago'
? 'format.timeAgo'
: futureMode === 'left'
? 'format.timeLeft'
: 'format.inTime'
: 'format.time',
{
value: lessThanOneMinute ? '< 1' : value,
unit: t(`unit.${unit}s`, { count: value }).toLocaleLowerCase(),
}
)
}
return timeDeltaFormatter
}