You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
7.0 KiB
204 lines
7.0 KiB
8 years ago
|
import React from 'react';
|
||
2 years ago
|
import { injectIntl, defineMessages, InjectedIntl } from 'react-intl';
|
||
8 years ago
|
|
||
7 years ago
|
const messages = defineMessages({
|
||
5 years ago
|
today: { id: 'relative_time.today', defaultMessage: 'today' },
|
||
7 years ago
|
just_now: { id: 'relative_time.just_now', defaultMessage: 'now' },
|
||
3 years ago
|
just_now_full: { id: 'relative_time.full.just_now', defaultMessage: 'just now' },
|
||
7 years ago
|
seconds: { id: 'relative_time.seconds', defaultMessage: '{number}s' },
|
||
3 years ago
|
seconds_full: { id: 'relative_time.full.seconds', defaultMessage: '{number, plural, one {# second} other {# seconds}} ago' },
|
||
7 years ago
|
minutes: { id: 'relative_time.minutes', defaultMessage: '{number}m' },
|
||
3 years ago
|
minutes_full: { id: 'relative_time.full.minutes', defaultMessage: '{number, plural, one {# minute} other {# minutes}} ago' },
|
||
7 years ago
|
hours: { id: 'relative_time.hours', defaultMessage: '{number}h' },
|
||
3 years ago
|
hours_full: { id: 'relative_time.full.hours', defaultMessage: '{number, plural, one {# hour} other {# hours}} ago' },
|
||
7 years ago
|
days: { id: 'relative_time.days', defaultMessage: '{number}d' },
|
||
3 years ago
|
days_full: { id: 'relative_time.full.days', defaultMessage: '{number, plural, one {# day} other {# days}} ago' },
|
||
6 years ago
|
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' },
|
||
7 years ago
|
});
|
||
|
|
||
8 years ago
|
const dateFormatOptions = {
|
||
|
hour12: false,
|
||
|
year: 'numeric',
|
||
|
month: 'short',
|
||
|
day: '2-digit',
|
||
|
hour: '2-digit',
|
||
|
minute: '2-digit',
|
||
2 years ago
|
} as const;
|
||
8 years ago
|
|
||
7 years ago
|
const shortDateFormatOptions = {
|
||
7 years ago
|
month: 'short',
|
||
7 years ago
|
day: 'numeric',
|
||
2 years ago
|
} as const;
|
||
7 years ago
|
|
||
|
const SECOND = 1000;
|
||
|
const MINUTE = 1000 * 60;
|
||
|
const HOUR = 1000 * 60 * 60;
|
||
|
const DAY = 1000 * 60 * 60 * 24;
|
||
|
|
||
|
const MAX_DELAY = 2147483647;
|
||
|
|
||
2 years ago
|
const selectUnits = (delta: number) => {
|
||
7 years ago
|
const absDelta = Math.abs(delta);
|
||
|
|
||
|
if (absDelta < MINUTE) {
|
||
|
return 'second';
|
||
|
} else if (absDelta < HOUR) {
|
||
|
return 'minute';
|
||
|
} else if (absDelta < DAY) {
|
||
|
return 'hour';
|
||
|
}
|
||
|
|
||
|
return 'day';
|
||
|
};
|
||
|
|
||
2 years ago
|
const getUnitDelay = (units: string) => {
|
||
7 years ago
|
switch (units) {
|
||
|
case 'second':
|
||
|
return SECOND;
|
||
|
case 'minute':
|
||
|
return MINUTE;
|
||
|
case 'hour':
|
||
|
return HOUR;
|
||
|
case 'day':
|
||
|
return DAY;
|
||
|
default:
|
||
|
return MAX_DELAY;
|
||
|
}
|
||
|
};
|
||
|
|
||
2 years ago
|
export const timeAgoString = (intl: InjectedIntl, date: Date, now: number, year: number, timeGiven: boolean, short?: boolean) => {
|
||
6 years ago
|
const delta = now - date.getTime();
|
||
|
|
||
|
let relativeTime;
|
||
|
|
||
5 years ago
|
if (delta < DAY && !timeGiven) {
|
||
|
relativeTime = intl.formatMessage(messages.today);
|
||
|
} else if (delta < 10 * SECOND) {
|
||
3 years ago
|
relativeTime = intl.formatMessage(short ? messages.just_now : messages.just_now_full);
|
||
6 years ago
|
} else if (delta < 7 * DAY) {
|
||
|
if (delta < MINUTE) {
|
||
3 years ago
|
relativeTime = intl.formatMessage(short ? messages.seconds : messages.seconds_full, { number: Math.floor(delta / SECOND) });
|
||
6 years ago
|
} else if (delta < HOUR) {
|
||
3 years ago
|
relativeTime = intl.formatMessage(short ? messages.minutes : messages.minutes_full, { number: Math.floor(delta / MINUTE) });
|
||
6 years ago
|
} else if (delta < DAY) {
|
||
3 years ago
|
relativeTime = intl.formatMessage(short ? messages.hours : messages.hours_full, { number: Math.floor(delta / HOUR) });
|
||
6 years ago
|
} else {
|
||
3 years ago
|
relativeTime = intl.formatMessage(short ? messages.days : messages.days_full, { number: Math.floor(delta / DAY) });
|
||
6 years ago
|
}
|
||
|
} else if (date.getFullYear() === year) {
|
||
|
relativeTime = intl.formatDate(date, shortDateFormatOptions);
|
||
|
} else {
|
||
|
relativeTime = intl.formatDate(date, { ...shortDateFormatOptions, year: 'numeric' });
|
||
|
}
|
||
|
|
||
|
return relativeTime;
|
||
|
};
|
||
|
|
||
2 years ago
|
const timeRemainingString = (intl: InjectedIntl, date: Date, now: number, timeGiven = true) => {
|
||
6 years ago
|
const delta = date.getTime() - now;
|
||
|
|
||
|
let relativeTime;
|
||
|
|
||
5 years ago
|
if (delta < DAY && !timeGiven) {
|
||
|
relativeTime = intl.formatMessage(messages.today);
|
||
|
} else if (delta < 10 * SECOND) {
|
||
6 years ago
|
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;
|
||
|
};
|
||
|
|
||
2 years ago
|
type Props = {
|
||
|
intl: InjectedIntl;
|
||
|
timestamp: string;
|
||
|
year: number;
|
||
|
futureDate?: boolean;
|
||
|
short?: boolean;
|
||
|
}
|
||
|
type States = {
|
||
|
now: number;
|
||
|
}
|
||
|
class RelativeTimestamp extends React.Component<Props, States> {
|
||
8 years ago
|
|
||
7 years ago
|
state = {
|
||
|
now: this.props.intl.now(),
|
||
|
};
|
||
|
|
||
7 years ago
|
static defaultProps = {
|
||
|
year: (new Date()).getFullYear(),
|
||
3 years ago
|
short: true,
|
||
7 years ago
|
};
|
||
|
|
||
2 years ago
|
_timer: number | undefined;
|
||
|
|
||
|
shouldComponentUpdate (nextProps: Props, nextState: States) {
|
||
8 years ago
|
// 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 ||
|
||
7 years ago
|
this.props.intl.locale !== nextProps.intl.locale ||
|
||
|
this.state.now !== nextState.now;
|
||
|
}
|
||
|
|
||
2 years ago
|
UNSAFE_componentWillReceiveProps (nextProps: Props) {
|
||
7 years ago
|
if (this.props.timestamp !== nextProps.timestamp) {
|
||
|
this.setState({ now: this.props.intl.now() });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
componentDidMount () {
|
||
|
this._scheduleNextUpdate(this.props, this.state);
|
||
|
}
|
||
|
|
||
2 years ago
|
UNSAFE_componentWillUpdate (nextProps: Props, nextState: States) {
|
||
7 years ago
|
this._scheduleNextUpdate(nextProps, nextState);
|
||
|
}
|
||
|
|
||
7 years ago
|
componentWillUnmount () {
|
||
2 years ago
|
window.clearTimeout(this._timer);
|
||
7 years ago
|
}
|
||
|
|
||
2 years ago
|
_scheduleNextUpdate (props: Props, state: States) {
|
||
|
window.clearTimeout(this._timer);
|
||
7 years ago
|
|
||
|
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);
|
||
|
|
||
2 years ago
|
this._timer = window.setTimeout(() => {
|
||
7 years ago
|
this.setState({ now: this.props.intl.now() });
|
||
|
}, delay);
|
||
8 years ago
|
}
|
||
|
|
||
|
render () {
|
||
3 years ago
|
const { timestamp, intl, year, futureDate, short } = this.props;
|
||
7 years ago
|
|
||
5 years ago
|
const timeGiven = timestamp.includes('T');
|
||
6 years ago
|
const date = new Date(timestamp);
|
||
3 years ago
|
const relativeTime = futureDate ? timeRemainingString(intl, date, this.state.now, timeGiven) : timeAgoString(intl, date, this.state.now, year, timeGiven, short);
|
||
8 years ago
|
|
||
|
return (
|
||
|
<time dateTime={timestamp} title={intl.formatDate(date, dateFormatOptions)}>
|
||
7 years ago
|
{relativeTime}
|
||
8 years ago
|
</time>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|
||
2 years ago
|
|
||
|
export default injectIntl(RelativeTimestamp);
|