2023-05-23 08:52:27 +00:00
|
|
|
import { Component } from 'react';
|
2023-05-10 10:59:29 +00:00
|
|
|
|
2023-05-31 21:43:39 +00:00
|
|
|
import type { IntlShape } from 'react-intl';
|
2023-05-10 10:59:29 +00:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl';
|
2017-01-04 14:43:28 +00:00
|
|
|
|
2017-10-03 23:23:52 +00:00
|
|
|
const messages = defineMessages({
|
2020-01-26 22:13:48 +00:00
|
|
|
today: { id: 'relative_time.today', defaultMessage: 'today' },
|
2017-10-03 23:23:52 +00:00
|
|
|
just_now: { id: 'relative_time.just_now', defaultMessage: 'now' },
|
2023-05-09 17:02:12 +00:00
|
|
|
just_now_full: {
|
|
|
|
id: 'relative_time.full.just_now',
|
|
|
|
defaultMessage: 'just now',
|
|
|
|
},
|
2017-10-03 23:23:52 +00:00
|
|
|
seconds: { id: 'relative_time.seconds', defaultMessage: '{number}s' },
|
2023-05-09 17:02:12 +00:00
|
|
|
seconds_full: {
|
|
|
|
id: 'relative_time.full.seconds',
|
|
|
|
defaultMessage: '{number, plural, one {# second} other {# seconds}} ago',
|
|
|
|
},
|
2017-10-03 23:23:52 +00:00
|
|
|
minutes: { id: 'relative_time.minutes', defaultMessage: '{number}m' },
|
2023-05-09 17:02:12 +00:00
|
|
|
minutes_full: {
|
|
|
|
id: 'relative_time.full.minutes',
|
|
|
|
defaultMessage: '{number, plural, one {# minute} other {# minutes}} ago',
|
|
|
|
},
|
2017-10-03 23:23:52 +00:00
|
|
|
hours: { id: 'relative_time.hours', defaultMessage: '{number}h' },
|
2023-05-09 17:02:12 +00:00
|
|
|
hours_full: {
|
|
|
|
id: 'relative_time.full.hours',
|
|
|
|
defaultMessage: '{number, plural, one {# hour} other {# hours}} ago',
|
|
|
|
},
|
2017-10-03 23:23:52 +00:00
|
|
|
days: { id: 'relative_time.days', defaultMessage: '{number}d' },
|
2023-05-09 17:02:12 +00:00
|
|
|
days_full: {
|
|
|
|
id: 'relative_time.full.days',
|
|
|
|
defaultMessage: '{number, plural, one {# day} other {# days}} 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',
|
|
|
|
},
|
2017-10-03 23:23:52 +00:00
|
|
|
});
|
|
|
|
|
2017-05-26 01:25:41 +00:00
|
|
|
const dateFormatOptions = {
|
|
|
|
hour12: false,
|
|
|
|
year: 'numeric',
|
|
|
|
month: 'short',
|
|
|
|
day: '2-digit',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
2023-04-30 22:51:31 +00:00
|
|
|
} as const;
|
2016-08-24 19:08:00 +00:00
|
|
|
|
2017-10-03 23:23:52 +00:00
|
|
|
const shortDateFormatOptions = {
|
2018-04-22 22:43:53 +00:00
|
|
|
month: 'short',
|
2017-10-03 23:23:52 +00:00
|
|
|
day: 'numeric',
|
2023-04-30 22:51:31 +00:00
|
|
|
} as const;
|
2017-10-03 23:23:52 +00:00
|
|
|
|
|
|
|
const SECOND = 1000;
|
|
|
|
const MINUTE = 1000 * 60;
|
2023-05-09 17:02:12 +00:00
|
|
|
const HOUR = 1000 * 60 * 60;
|
|
|
|
const DAY = 1000 * 60 * 60 * 24;
|
2017-10-03 23:23:52 +00:00
|
|
|
|
|
|
|
const MAX_DELAY = 2147483647;
|
|
|
|
|
2023-04-30 22:51:31 +00:00
|
|
|
const selectUnits = (delta: number) => {
|
2017-10-03 23:23:52 +00:00
|
|
|
const absDelta = Math.abs(delta);
|
|
|
|
|
|
|
|
if (absDelta < MINUTE) {
|
|
|
|
return 'second';
|
|
|
|
} else if (absDelta < HOUR) {
|
|
|
|
return 'minute';
|
|
|
|
} else if (absDelta < DAY) {
|
|
|
|
return 'hour';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'day';
|
|
|
|
};
|
|
|
|
|
2023-04-30 22:51:31 +00:00
|
|
|
const getUnitDelay = (units: string) => {
|
2017-10-03 23:23:52 +00:00
|
|
|
switch (units) {
|
2023-05-09 17:02:12 +00:00
|
|
|
case 'second':
|
|
|
|
return SECOND;
|
|
|
|
case 'minute':
|
|
|
|
return MINUTE;
|
|
|
|
case 'hour':
|
|
|
|
return HOUR;
|
|
|
|
case 'day':
|
|
|
|
return DAY;
|
|
|
|
default:
|
|
|
|
return MAX_DELAY;
|
2017-10-03 23:23:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
export const timeAgoString = (
|
2023-05-31 21:43:39 +00:00
|
|
|
intl: IntlShape,
|
2023-05-09 17:02:12 +00:00
|
|
|
date: Date,
|
|
|
|
now: number,
|
|
|
|
year: number,
|
|
|
|
timeGiven: boolean,
|
2023-07-13 09:26:45 +00:00
|
|
|
short?: boolean,
|
2023-05-09 17:02:12 +00:00
|
|
|
) => {
|
2018-07-28 17:25:33 +00:00
|
|
|
const delta = now - date.getTime();
|
|
|
|
|
|
|
|
let relativeTime;
|
|
|
|
|
2020-01-26 22:13:48 +00:00
|
|
|
if (delta < DAY && !timeGiven) {
|
|
|
|
relativeTime = intl.formatMessage(messages.today);
|
|
|
|
} else if (delta < 10 * SECOND) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(
|
2023-07-13 09:26:45 +00:00
|
|
|
short ? messages.just_now : messages.just_now_full,
|
2023-05-09 17:02:12 +00:00
|
|
|
);
|
2018-07-28 17:25:33 +00:00
|
|
|
} else if (delta < 7 * DAY) {
|
|
|
|
if (delta < MINUTE) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(
|
|
|
|
short ? messages.seconds : messages.seconds_full,
|
2023-07-13 09:26:45 +00:00
|
|
|
{ number: Math.floor(delta / SECOND) },
|
2023-05-09 17:02:12 +00:00
|
|
|
);
|
2018-07-28 17:25:33 +00:00
|
|
|
} else if (delta < HOUR) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(
|
|
|
|
short ? messages.minutes : messages.minutes_full,
|
2023-07-13 09:26:45 +00:00
|
|
|
{ number: Math.floor(delta / MINUTE) },
|
2023-05-09 17:02:12 +00:00
|
|
|
);
|
2018-07-28 17:25:33 +00:00
|
|
|
} else if (delta < DAY) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(
|
|
|
|
short ? messages.hours : messages.hours_full,
|
2023-07-13 09:26:45 +00:00
|
|
|
{ number: Math.floor(delta / HOUR) },
|
2023-05-09 17:02:12 +00:00
|
|
|
);
|
2018-07-28 17:25:33 +00:00
|
|
|
} else {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(
|
|
|
|
short ? messages.days : messages.days_full,
|
2023-07-13 09:26:45 +00:00
|
|
|
{ number: Math.floor(delta / DAY) },
|
2023-05-09 17:02:12 +00:00
|
|
|
);
|
2018-07-28 17:25:33 +00:00
|
|
|
}
|
|
|
|
} else if (date.getFullYear() === year) {
|
|
|
|
relativeTime = intl.formatDate(date, shortDateFormatOptions);
|
|
|
|
} else {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatDate(date, {
|
|
|
|
...shortDateFormatOptions,
|
|
|
|
year: 'numeric',
|
|
|
|
});
|
2018-07-28 17:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return relativeTime;
|
|
|
|
};
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
const timeRemainingString = (
|
2023-05-31 21:43:39 +00:00
|
|
|
intl: IntlShape,
|
2023-05-09 17:02:12 +00:00
|
|
|
date: Date,
|
|
|
|
now: number,
|
2023-07-13 09:26:45 +00:00
|
|
|
timeGiven = true,
|
2023-05-09 17:02:12 +00:00
|
|
|
) => {
|
2019-04-03 16:16:55 +00:00
|
|
|
const delta = date.getTime() - now;
|
|
|
|
|
|
|
|
let relativeTime;
|
|
|
|
|
2020-01-26 22:13:48 +00:00
|
|
|
if (delta < DAY && !timeGiven) {
|
|
|
|
relativeTime = intl.formatMessage(messages.today);
|
|
|
|
} else if (delta < 10 * SECOND) {
|
2019-04-03 16:16:55 +00:00
|
|
|
relativeTime = intl.formatMessage(messages.moments_remaining);
|
|
|
|
} else if (delta < MINUTE) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(messages.seconds_remaining, {
|
|
|
|
number: Math.floor(delta / SECOND),
|
|
|
|
});
|
2019-04-03 16:16:55 +00:00
|
|
|
} else if (delta < HOUR) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(messages.minutes_remaining, {
|
|
|
|
number: Math.floor(delta / MINUTE),
|
|
|
|
});
|
2019-04-03 16:16:55 +00:00
|
|
|
} else if (delta < DAY) {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(messages.hours_remaining, {
|
|
|
|
number: Math.floor(delta / HOUR),
|
|
|
|
});
|
2019-04-03 16:16:55 +00:00
|
|
|
} else {
|
2023-05-09 17:02:12 +00:00
|
|
|
relativeTime = intl.formatMessage(messages.days_remaining, {
|
|
|
|
number: Math.floor(delta / DAY),
|
|
|
|
});
|
2019-04-03 16:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return relativeTime;
|
|
|
|
};
|
|
|
|
|
2023-05-10 10:59:29 +00:00
|
|
|
interface Props {
|
2023-05-31 21:43:39 +00:00
|
|
|
intl: IntlShape;
|
2023-04-30 22:51:31 +00:00
|
|
|
timestamp: string;
|
|
|
|
year: number;
|
|
|
|
futureDate?: boolean;
|
|
|
|
short?: boolean;
|
2023-05-10 10:59:29 +00:00
|
|
|
}
|
|
|
|
interface States {
|
2023-04-30 22:51:31 +00:00
|
|
|
now: number;
|
2023-05-10 10:59:29 +00:00
|
|
|
}
|
2023-05-23 08:52:27 +00:00
|
|
|
class RelativeTimestamp extends Component<Props, States> {
|
2017-10-03 23:23:52 +00:00
|
|
|
state = {
|
2023-05-31 21:43:39 +00:00
|
|
|
now: Date.now(),
|
2017-10-03 23:23:52 +00:00
|
|
|
};
|
|
|
|
|
2018-04-22 22:43:53 +00:00
|
|
|
static defaultProps = {
|
2023-05-09 17:02:12 +00:00
|
|
|
year: new Date().getFullYear(),
|
2022-02-09 00:17:07 +00:00
|
|
|
short: true,
|
2018-04-22 22:43:53 +00:00
|
|
|
};
|
|
|
|
|
2023-04-30 22:51:31 +00:00
|
|
|
_timer: number | undefined;
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
shouldComponentUpdate(nextProps: Props, nextState: States) {
|
2017-05-26 01:25:41 +00: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.
|
2023-05-09 17:02:12 +00:00
|
|
|
return (
|
|
|
|
this.props.timestamp !== nextProps.timestamp ||
|
2017-10-03 23:23:52 +00:00
|
|
|
this.props.intl.locale !== nextProps.intl.locale ||
|
2023-05-09 17:02:12 +00:00
|
|
|
this.state.now !== nextState.now
|
|
|
|
);
|
2017-10-03 23:23:52 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2017-10-03 23:23:52 +00:00
|
|
|
if (this.props.timestamp !== nextProps.timestamp) {
|
2023-05-31 21:43:39 +00:00
|
|
|
this.setState({ now: Date.now() });
|
2017-10-03 23:23:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
componentDidMount() {
|
2017-10-03 23:23:52 +00:00
|
|
|
this._scheduleNextUpdate(this.props, this.state);
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
UNSAFE_componentWillUpdate(nextProps: Props, nextState: States) {
|
2017-10-03 23:23:52 +00:00
|
|
|
this._scheduleNextUpdate(nextProps, nextState);
|
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
componentWillUnmount() {
|
2023-04-30 22:51:31 +00:00
|
|
|
window.clearTimeout(this._timer);
|
2017-10-16 01:03:36 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
_scheduleNextUpdate(props: Props, state: States) {
|
2023-04-30 22:51:31 +00:00
|
|
|
window.clearTimeout(this._timer);
|
2017-10-03 23:23:52 +00:00
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
const { timestamp } = props;
|
|
|
|
const delta = new Date(timestamp).getTime() - state.now;
|
|
|
|
const unitDelay = getUnitDelay(selectUnits(delta));
|
|
|
|
const unitRemainder = Math.abs(delta % unitDelay);
|
2017-10-03 23:23:52 +00:00
|
|
|
const updateInterval = 1000 * 10;
|
2023-05-09 17:02:12 +00:00
|
|
|
const delay =
|
|
|
|
delta < 0
|
|
|
|
? Math.max(updateInterval, unitDelay - unitRemainder)
|
|
|
|
: Math.max(updateInterval, unitRemainder);
|
2017-10-03 23:23:52 +00:00
|
|
|
|
2023-04-30 22:51:31 +00:00
|
|
|
this._timer = window.setTimeout(() => {
|
2023-05-31 21:43:39 +00:00
|
|
|
this.setState({ now: Date.now() });
|
2017-10-03 23:23:52 +00:00
|
|
|
}, delay);
|
2017-05-26 01:25:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
render() {
|
2022-02-09 00:17:07 +00:00
|
|
|
const { timestamp, intl, year, futureDate, short } = this.props;
|
2017-10-03 23:23:52 +00:00
|
|
|
|
2023-05-09 17:02:12 +00:00
|
|
|
const timeGiven = timestamp.includes('T');
|
|
|
|
const date = new Date(timestamp);
|
|
|
|
const relativeTime = futureDate
|
|
|
|
? timeRemainingString(intl, date, this.state.now, timeGiven)
|
|
|
|
: timeAgoString(intl, date, this.state.now, year, timeGiven, short);
|
2017-05-26 01:25:41 +00:00
|
|
|
|
|
|
|
return (
|
2023-05-09 17:02:12 +00:00
|
|
|
<time
|
|
|
|
dateTime={timestamp}
|
|
|
|
title={intl.formatDate(date, dateFormatOptions)}
|
|
|
|
>
|
2017-10-03 23:23:52 +00:00
|
|
|
{relativeTime}
|
2017-05-26 01:25:41 +00:00
|
|
|
</time>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 02:17:53 +00:00
|
|
|
|
2023-05-09 01:11:56 +00:00
|
|
|
const RelativeTimestampWithIntl = injectIntl(RelativeTimestamp);
|
|
|
|
|
|
|
|
export { RelativeTimestampWithIntl as RelativeTimestamp };
|