bsky-app/src/view/com/util/TimeElapsed.tsx
Eric Bailey 983d85384b
Force callers of getTimeAgo to pass in the value for "now" (#4560)
* Remove icky hook for now

* Force callers of getTimeAgo to pass in the 'now' value

* Update usage in Newskie dialog
2024-06-18 21:50:07 +03:00

29 lines
724 B
TypeScript

import React from '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?: (timeElapsed: string) => string
}) {
const ago = useGetTimeAgo()
const format = timeToString ?? ago
const tick = useTickEveryMinute()
const [timeElapsed, setTimeAgo] = React.useState(() =>
format(timestamp, tick),
)
const [prevTick, setPrevTick] = React.useState(tick)
if (prevTick !== tick) {
setPrevTick(tick)
setTimeAgo(format(timestamp, tick))
}
return children({timeElapsed})
}