From 2eebf681c2c6cad994321dc55b081b39c00b00b6 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Sun, 18 Oct 2015 23:08:50 -0400 Subject: [PATCH] Drop forceUpdate from TimeSince; use state/props changes instead --- .../sentry/app/components/timeSince.jsx | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/sentry/static/sentry/app/components/timeSince.jsx b/src/sentry/static/sentry/app/components/timeSince.jsx index 521c3376d870a5..e5cc5e5e26a4ee 100644 --- a/src/sentry/static/sentry/app/components/timeSince.jsx +++ b/src/sentry/static/sentry/app/components/timeSince.jsx @@ -7,6 +7,15 @@ var TimeSince = React.createClass({ PureRenderMixin ], + statics: { + getDateObj(date) { + if (typeof date === "string" || typeof date === "number") { + date = new Date(date); + } + return date; + } + }, + propTypes: { date: React.PropTypes.any.isRequired, suffix: React.PropTypes.string @@ -18,35 +27,46 @@ var TimeSince = React.createClass({ }; }, + getInitialState() { + return { + relative: this.getRelativeDate() + }; + }, + componentDidMount() { - var delay = 2600; + this.setRelativeDateTicker(); + }, - this.ticker = setInterval(this.ensureValidity, delay); + setRelativeDateTicker() { + const ONE_MINUTE_IN_MS = 3600; + + this.ticker = setTimeout(() => { + this.setState({ + relative: this.getRelativeDate() + }); + this.setRelativeDateTicker(); + }, ONE_MINUTE_IN_MS); + }, + + getRelativeDate() { + let date = TimeSince.getDateObj(this.props.date); + return moment(date).fromNow(true); }, componentWillUnmount() { if (this.ticker) { - clearInterval(this.ticker); + clearTimeout(this.ticker); this.ticker = null; } }, - ensureValidity() { - // TODO(dcramer): this should ensure we actually *need* to update the value - this.forceUpdate(); - }, - render() { - var date = this.props.date; - - if (typeof date === "string" || typeof date === "number") { - date = new Date(date); - } + let date = TimeSince.getDateObj(this.props.date); return ( + title={date.toString()}>{this.state.relative} {this.props.suffix || ''} ); } });