about summary refs log tree commit diff
path: root/src/view/com/util/TimeElapsed.tsx
blob: a5d3a53722d5c5281e9d8dfcc315d5f1eee868a6 (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 {useTickEveryMinute} from '#/state/shell'
import {ago} from 'lib/strings/time'

export function TimeElapsed({
  timestamp,
  children,
  timeToString = ago,
}: {
  timestamp: string
  children: ({timeElapsed}: {timeElapsed: string}) => JSX.Element
  timeToString?: (timeElapsed: string) => string
}) {
  const tick = useTickEveryMinute()
  const [timeElapsed, setTimeAgo] = React.useState(() =>
    timeToString(timestamp),
  )

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

  return children({timeElapsed})
}