8651f31ebb
* refactor: consistent localized formatting * refactor: localized date time * refactor: localize relative time with strings * chore: fix typo from copy-paste * Clean up useTimeAgo * Remove old ago * Const * Reuse * Prettier --------- Co-authored-by: Mary <git@mary.my.id>
34 lines
903 B
TypeScript
34 lines
903 B
TypeScript
import React from 'react'
|
|
import {I18n} from '@lingui/core'
|
|
import {useLingui} from '@lingui/react'
|
|
|
|
import {useGetTimeAgo} from '#/lib/hooks/useTimeAgo'
|
|
import {useTickEveryMinute} from '#/state/shell'
|
|
|
|
export function TimeElapsed({
|
|
timestamp,
|
|
children,
|
|
timeToString,
|
|
}: {
|
|
timestamp: string
|
|
children: ({timeElapsed}: {timeElapsed: string}) => JSX.Element
|
|
timeToString?: (i18n: I18n, timeElapsed: string) => string
|
|
}) {
|
|
const {i18n} = useLingui()
|
|
const ago = useGetTimeAgo()
|
|
const tick = useTickEveryMinute()
|
|
const [timeElapsed, setTimeAgo] = React.useState(() =>
|
|
timeToString ? timeToString(i18n, timestamp) : ago(timestamp, tick),
|
|
)
|
|
|
|
const [prevTick, setPrevTick] = React.useState(tick)
|
|
if (prevTick !== tick) {
|
|
setPrevTick(tick)
|
|
setTimeAgo(
|
|
timeToString ? timeToString(i18n, timestamp) : ago(timestamp, tick),
|
|
)
|
|
}
|
|
|
|
return children({timeElapsed})
|
|
}
|