This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/shared/components/Time.js
Taylor Lapeyre d3773dc943 fix: Update all Time components each second ()
* first pass at updating all Time components each second

* fix a couple date variable typos

* use class style state management instead of hooks

* address feedback

* lint

Co-authored-by: Tom Moor <tom.moor@gmail.com>
2020-03-23 22:31:17 -07:00

56 lines
1.2 KiB
JavaScript

// @flow
import * as React from 'react';
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,
};
class Time extends React.Component<Props> {
removeEachMinuteCallback: () => void;
componentDidMount() {
this.removeEachMinuteCallback = eachMinute(() => {
this.forceUpdate();
});
}
componentWillUnmount() {
this.removeEachMinuteCallback();
}
render() {
return (
<Tooltip
tooltip={format(this.props.dateTime, 'MMMM Do, YYYY h:mm a')}
placement="bottom"
>
<time dateTime={this.props.dateTime}>
{this.props.children || distanceInWordsToNow(this.props.dateTime)}
</time>
</Tooltip>
);
}
}
export default Time;