tooot/src/components/RelativeTime.tsx

30 lines
668 B
TypeScript
Raw Normal View History

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(() => {
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