2017-05-16 13:34:00 +02:00
|
|
|
|
|
|
|
import moment from 'moment'
|
|
|
|
|
|
|
|
import React from 'react'
|
|
|
|
|
|
|
|
export default class RelativeTime extends React.Component {
|
|
|
|
|
2018-10-03 16:07:40 +02:00
|
|
|
// Local state updates, to trigger a rerender
|
|
|
|
// every second for time updates.
|
|
|
|
componentDidMount() {
|
|
|
|
this.timer = setInterval(() => {
|
|
|
|
this.setState({
|
|
|
|
now: Date.now()
|
|
|
|
})
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop timer
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearInterval(this.timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper: Assert time is an instance of moment
|
|
|
|
getTime() {
|
2018-09-25 22:32:01 +02:00
|
|
|
if (!this.props.value) {
|
2018-10-03 16:07:40 +02:00
|
|
|
return false;
|
2018-09-25 22:32:01 +02:00
|
|
|
}
|
2017-05-16 13:34:00 +02:00
|
|
|
|
2018-10-01 15:14:53 +02:00
|
|
|
let time = false;
|
|
|
|
if (this.props.value instanceof moment) {
|
|
|
|
time = this.props.value;
|
|
|
|
} else {
|
|
|
|
time = moment.utc(this.props.value);
|
|
|
|
}
|
2018-10-03 16:07:40 +02:00
|
|
|
return time
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Time can be capped, if we are handling a past
|
|
|
|
// or future event:
|
|
|
|
capTime(time) {
|
|
|
|
const now = moment.utc();
|
|
|
|
if (this.props.pastEvent && time.isAfter(now)) {
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.futureEvent && time.isBefore(now)) {
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let time = this.getTime();
|
|
|
|
if (!time) {
|
|
|
|
return null; // Well, nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
time = this.capTime(time);
|
2018-10-01 15:14:53 +02:00
|
|
|
|
2018-10-03 15:30:41 +02:00
|
|
|
// A few seconds ago / in a few seconds can be replaced
|
|
|
|
// with 'just now'.
|
|
|
|
// fuzzyNow can be set as a threshold of seconds
|
|
|
|
if (this.props.fuzzyNow) {
|
|
|
|
const now = moment.utc();
|
|
|
|
if (Math.abs(now - time) / 1000.0 < this.props.fuzzyNow) {
|
|
|
|
return (
|
|
|
|
<span>just now</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-16 13:34:00 +02:00
|
|
|
return (
|
|
|
|
<span>{time.fromNow(this.props.suffix)}</span>
|
2018-09-25 22:32:01 +02:00
|
|
|
);
|
2017-05-16 13:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|