mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {I18n} from '@lingui/core'
3import {useLingui} from '@lingui/react'
4
5import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
6import {useTickEveryMinute} from '#/state/shell'
7
8export function TimeElapsed({
9 timestamp,
10 children,
11 timeToString,
12}: {
13 timestamp: string
14 children: ({timeElapsed}: {timeElapsed: string}) => JSX.Element
15 timeToString?: (i18n: I18n, timeElapsed: string) => string
16}) {
17 const {i18n} = useLingui()
18 const ago = useGetTimeAgo()
19 const tick = useTickEveryMinute()
20 const [timeElapsed, setTimeAgo] = React.useState(() =>
21 timeToString ? timeToString(i18n, timestamp) : ago(timestamp, tick),
22 )
23
24 const [prevTick, setPrevTick] = React.useState(tick)
25 if (prevTick !== tick) {
26 setPrevTick(tick)
27 setTimeAgo(
28 timeToString ? timeToString(i18n, timestamp) : ago(timestamp, tick),
29 )
30 }
31
32 return children({timeElapsed})
33}