about summary refs log tree commit diff
path: root/src/view/com/util/TimeElapsed.tsx
blob: d939b3163d4653ce0cb45e3aca6c3c6a448026a1 (plain) (blame)
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
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))

  const [prevTick, setPrevTick] = React.useState(tick)
  if (prevTick !== tick) {
    setPrevTick(tick)
    setTimeAgo(format(timestamp))
  }

  return children({timeElapsed})
}