2022-06-10 17:02:04 +02:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { FormattedRelativeTime } from 'react-intl'
|
|
|
|
import { AppState } from 'react-native'
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
time: string | number
|
|
|
|
}
|
|
|
|
|
2022-06-14 23:43:11 +02:00
|
|
|
const RelativeTime: React.FC<Props> = ({ time }) => {
|
2022-06-10 17:02:04 +02:00
|
|
|
const [now, setNow] = useState(new Date().getTime())
|
|
|
|
useEffect(() => {
|
2022-12-03 21:56:52 +01:00
|
|
|
const appStateListener = AppState.addEventListener('change', () => {
|
2022-06-10 17:02:04 +02:00
|
|
|
setNow(new Date().getTime())
|
|
|
|
})
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
appStateListener.remove()
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FormattedRelativeTime
|
2022-06-14 23:43:11 +02:00
|
|
|
value={(new Date(time).getTime() - now) / 1000}
|
2022-06-10 17:02:04 +02:00
|
|
|
updateIntervalInSeconds={1}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RelativeTime
|