2017-05-03 02:04:16 +02:00
|
|
|
import React from 'react';
|
2017-10-04 01:23:52 +02:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl';
|
2017-04-21 20:05:35 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2017-01-04 15:43:28 +01:00
|
|
|
|
2017-10-04 01:23:52 +02:00
|
|
|
const messages = defineMessages({
|
2020-01-26 23:13:48 +01:00
|
|
|
today: { id: 'relative_time.today', defaultMessage: 'today' },
|
2017-10-04 01:23:52 +02:00
|
|
|
just_now: { id: 'relative_time.just_now', defaultMessage: 'now' },
|
|
|
|
seconds: { id: 'relative_time.seconds', defaultMessage: '{number}s' },
|
|
|
|
minutes: { id: 'relative_time.minutes', defaultMessage: '{number}m' },
|
|
|
|
hours: { id: 'relative_time.hours', defaultMessage: '{number}h' },
|
|
|
|
days: { id: 'relative_time.days', defaultMessage: '{number}d' },
|
2019-04-03 18:16:55 +02:00
|
|
|
moments_remaining: { id: 'time_remaining.moments', defaultMessage: 'Moments remaining' },
|
|
|
|
seconds_remaining: { id: 'time_remaining.seconds', defaultMessage: '{number, plural, one {# second} other {# seconds}} left' },
|
|
|
|
minutes_remaining: { id: 'time_remaining.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}} left' },
|
|
|
|
hours_remaining: { id: 'time_remaining.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}} left' },
|
|
|
|
days_remaining: { id: 'time_remaining.days', defaultMessage: '{number, plural, one {# day} other {# days}} left' },
|
2017-10-04 01:23:52 +02:00
|
|
|
});
|
|
|
|
|
2017-05-26 03:25:41 +02:00
|
|
|
const dateFormatOptions = {
|
|
|
|
hour12: false,
|
|
|
|
year: 'numeric',
|
|
|
|
month: 'short',
|
|
|
|
day: '2-digit',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
2016-11-16 17:20:52 +01:00
|
|
|
};
|
2016-08-24 21:08:00 +02:00
|
|
|
|
2017-10-04 01:23:52 +02:00
|
|
|
const shortDateFormatOptions = {
|
2018-05-02 13:45:40 +02:00
|
|
|
month: 'short',
|
2017-10-04 01:23:52 +02:00
|
|
|
day: 'numeric',
|
|
|
|
};
|
|
|
|
|
|
|
|
const SECOND = 1000;
|
|
|
|
const MINUTE = 1000 * 60;
|
|
|
|
const HOUR = 1000 * 60 * 60;
|
|
|
|
const DAY = 1000 * 60 * 60 * 24;
|
|
|
|
|
|
|
|
const MAX_DELAY = 2147483647;
|
|
|
|
|
|
|
|
const selectUnits = delta => {
|
|
|
|
const absDelta = Math.abs(delta);
|
|
|
|
|
|
|
|
if (absDelta < MINUTE) {
|
|
|
|
return 'second';
|
|
|
|
} else if (absDelta < HOUR) {
|
|
|
|
return 'minute';
|
|
|
|
} else if (absDelta < DAY) {
|
|
|
|
return 'hour';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'day';
|
|
|
|
};
|
|
|
|
|
|
|
|
const getUnitDelay = units => {
|
|
|
|
switch (units) {
|
|
|
|
case 'second':
|
|
|
|
return SECOND;
|
|
|
|
case 'minute':
|
|
|
|
return MINUTE;
|
|
|
|
case 'hour':
|
|
|
|
return HOUR;
|
|
|
|
case 'day':
|
|
|
|
return DAY;
|
|
|
|
default:
|
|
|
|
return MAX_DELAY;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-26 23:13:48 +01:00
|
|
|
export const timeAgoString = (intl, date, now, year, timeGiven = true) => {
|
2018-07-31 22:54:18 +02:00
|
|
|
const delta = now - date.getTime();
|
|
|
|
|
|
|
|
let relativeTime;
|
|
|
|
|
2020-01-26 23:13:48 +01:00
|
|
|
if (delta < DAY && !timeGiven) {
|
|
|
|
relativeTime = intl.formatMessage(messages.today);
|
|
|
|
} else if (delta < 10 * SECOND) {
|
2018-07-31 22:54:18 +02:00
|
|
|
relativeTime = intl.formatMessage(messages.just_now);
|
|
|
|
} else if (delta < 7 * DAY) {
|
|
|
|
if (delta < MINUTE) {
|
|
|
|
relativeTime = intl.formatMessage(messages.seconds, { number: Math.floor(delta / SECOND) });
|
|
|
|
} else if (delta < HOUR) {
|
|
|
|
relativeTime = intl.formatMessage(messages.minutes, { number: Math.floor(delta / MINUTE) });
|
|
|
|
} else if (delta < DAY) {
|
|
|
|
relativeTime = intl.formatMessage(messages.hours, { number: Math.floor(delta / HOUR) });
|
|
|
|
} else {
|
|
|
|
relativeTime = intl.formatMessage(messages.days, { number: Math.floor(delta / DAY) });
|
|
|
|
}
|
|
|
|
} else if (date.getFullYear() === year) {
|
|
|
|
relativeTime = intl.formatDate(date, shortDateFormatOptions);
|
|
|
|
} else {
|
|
|
|
relativeTime = intl.formatDate(date, { ...shortDateFormatOptions, year: 'numeric' });
|
|
|
|
}
|
|
|
|
|
|
|
|
return relativeTime;
|
|
|
|
};
|
|
|
|
|
2020-01-26 23:13:48 +01:00
|
|
|
const timeRemainingString = (intl, date, now, timeGiven = true) => {
|
2019-04-03 18:16:55 +02:00
|
|
|
const delta = date.getTime() - now;
|
|
|
|
|
|
|
|
let relativeTime;
|
|
|
|
|
2020-01-26 23:13:48 +01:00
|
|
|
if (delta < DAY && !timeGiven) {
|
|
|
|
relativeTime = intl.formatMessage(messages.today);
|
|
|
|
} else if (delta < 10 * SECOND) {
|
2019-04-03 18:16:55 +02:00
|
|
|
relativeTime = intl.formatMessage(messages.moments_remaining);
|
|
|
|
} else if (delta < MINUTE) {
|
|
|
|
relativeTime = intl.formatMessage(messages.seconds_remaining, { number: Math.floor(delta / SECOND) });
|
|
|
|
} else if (delta < HOUR) {
|
|
|
|
relativeTime = intl.formatMessage(messages.minutes_remaining, { number: Math.floor(delta / MINUTE) });
|
|
|
|
} else if (delta < DAY) {
|
|
|
|
relativeTime = intl.formatMessage(messages.hours_remaining, { number: Math.floor(delta / HOUR) });
|
|
|
|
} else {
|
|
|
|
relativeTime = intl.formatMessage(messages.days_remaining, { number: Math.floor(delta / DAY) });
|
|
|
|
}
|
|
|
|
|
|
|
|
return relativeTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default @injectIntl
|
|
|
|
class RelativeTimestamp extends React.Component {
|
2017-05-26 03:25:41 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
timestamp: PropTypes.string.isRequired,
|
2018-05-02 13:45:40 +02:00
|
|
|
year: PropTypes.number.isRequired,
|
2019-04-03 18:16:55 +02:00
|
|
|
futureDate: PropTypes.bool,
|
2017-05-26 03:25:41 +02:00
|
|
|
};
|
|
|
|
|
2017-10-04 01:23:52 +02:00
|
|
|
state = {
|
|
|
|
now: this.props.intl.now(),
|
|
|
|
};
|
|
|
|
|
2018-05-02 13:45:40 +02:00
|
|
|
static defaultProps = {
|
|
|
|
year: (new Date()).getFullYear(),
|
|
|
|
};
|
|
|
|
|
2017-10-04 01:23:52 +02:00
|
|
|
shouldComponentUpdate (nextProps, nextState) {
|
2017-05-26 03:25:41 +02:00
|
|
|
// As of right now the locale doesn't change without a new page load,
|
|
|
|
// but we might as well check in case that ever changes.
|
|
|
|
return this.props.timestamp !== nextProps.timestamp ||
|
2017-10-04 01:23:52 +02:00
|
|
|
this.props.intl.locale !== nextProps.intl.locale ||
|
|
|
|
this.state.now !== nextState.now;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (this.props.timestamp !== nextProps.timestamp) {
|
|
|
|
this.setState({ now: this.props.intl.now() });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this._scheduleNextUpdate(this.props, this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUpdate (nextProps, nextState) {
|
|
|
|
this._scheduleNextUpdate(nextProps, nextState);
|
|
|
|
}
|
|
|
|
|
2017-10-16 03:03:36 +02:00
|
|
|
componentWillUnmount () {
|
|
|
|
clearTimeout(this._timer);
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:23:52 +02:00
|
|
|
_scheduleNextUpdate (props, state) {
|
|
|
|
clearTimeout(this._timer);
|
|
|
|
|
|
|
|
const { timestamp } = props;
|
|
|
|
const delta = (new Date(timestamp)).getTime() - state.now;
|
|
|
|
const unitDelay = getUnitDelay(selectUnits(delta));
|
|
|
|
const unitRemainder = Math.abs(delta % unitDelay);
|
|
|
|
const updateInterval = 1000 * 10;
|
|
|
|
const delay = delta < 0 ? Math.max(updateInterval, unitDelay - unitRemainder) : Math.max(updateInterval, unitRemainder);
|
|
|
|
|
|
|
|
this._timer = setTimeout(() => {
|
|
|
|
this.setState({ now: this.props.intl.now() });
|
|
|
|
}, delay);
|
2017-05-26 03:25:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2019-04-03 18:16:55 +02:00
|
|
|
const { timestamp, intl, year, futureDate } = this.props;
|
2017-10-04 01:23:52 +02:00
|
|
|
|
2020-01-26 23:13:48 +01:00
|
|
|
const timeGiven = timestamp.includes('T');
|
2018-07-31 22:54:18 +02:00
|
|
|
const date = new Date(timestamp);
|
2020-01-26 23:13:48 +01:00
|
|
|
const relativeTime = futureDate ? timeRemainingString(intl, date, this.state.now, timeGiven) : timeAgoString(intl, date, this.state.now, year, timeGiven);
|
2017-05-26 03:25:41 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<time dateTime={timestamp} title={intl.formatDate(date, dateFormatOptions)}>
|
2017-10-04 01:23:52 +02:00
|
|
|
{relativeTime}
|
2017-05-26 03:25:41 +02:00
|
|
|
</time>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|