diff --git a/shared/components/Time.js b/shared/components/Time.js index 2400a6c4..aa9a2fa3 100644 --- a/shared/components/Time.js +++ b/shared/components/Time.js @@ -4,18 +4,52 @@ import Tooltip from 'components/Tooltip'; import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'; import format from 'date-fns/format'; +let callbacks = []; + +// This is a shared timer that fires every minute, used for +// updating all Time components across the page all at once. +setInterval(() => { + callbacks.forEach(cb => cb()); +}, 1000 * 60); + +function eachMinute(fn) { + callbacks.push(fn); + + return () => { + callbacks = callbacks.filter(cb => cb !== fn); + }; +} + type Props = { dateTime: string, children?: React.Node, }; -function Time({ dateTime, children }: Props) { - const date = new Date(dateTime); - return ( - - - - ); +class Time extends React.Component { + removeEachMinuteCallback: () => void; + + componentDidMount() { + this.removeEachMinuteCallback = eachMinute(() => { + this.forceUpdate(); + }); + } + + componentWillUnmount() { + this.removeEachMinuteCallback(); + } + + render() { + return ( + + + + ); + } } export default Time;