2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-02-24 21:56:07 +00:00
|
|
|
import { injectIntl, defineMessages } from 'react-intl'
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const dateFormatOptions = {
|
|
|
|
hour12: false,
|
|
|
|
year: 'numeric',
|
|
|
|
month: 'short',
|
|
|
|
day: '2-digit',
|
|
|
|
hour: '2-digit',
|
|
|
|
minute: '2-digit',
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const shortDateFormatOptions = {
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric',
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
const SECOND = 1000
|
|
|
|
const MINUTE = 1000 * 60
|
|
|
|
const HOUR = 1000 * 60 * 60
|
|
|
|
const DAY = 1000 * 60 * 60 * 24
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
const MAX_DELAY = 2147483647
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const selectUnits = delta => {
|
2020-02-24 21:56:07 +00:00
|
|
|
const absDelta = Math.abs(delta)
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
if (absDelta < MINUTE) {
|
2020-02-24 21:56:07 +00:00
|
|
|
return 'second'
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (absDelta < HOUR) {
|
2020-02-24 21:56:07 +00:00
|
|
|
return 'minute'
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (absDelta < DAY) {
|
2020-02-24 21:56:07 +00:00
|
|
|
return 'hour'
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
return 'day'
|
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const getUnitDelay = units => {
|
|
|
|
switch (units) {
|
2020-04-08 02:06:59 +01:00
|
|
|
case 'second':
|
|
|
|
return SECOND
|
|
|
|
case 'minute':
|
|
|
|
return MINUTE
|
|
|
|
case 'hour':
|
|
|
|
return HOUR
|
|
|
|
case 'day':
|
|
|
|
return DAY
|
|
|
|
default:
|
|
|
|
return MAX_DELAY
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
export const timeAgoString = (intl, date, now, year) => {
|
2020-02-24 21:56:07 +00:00
|
|
|
const delta = now - date.getTime()
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
if (delta < 10 * SECOND) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.just_now)
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (delta < 7 * DAY) {
|
|
|
|
if (delta < MINUTE) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.seconds, { number: Math.floor(delta / SECOND) })
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (delta < HOUR) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.minutes, { number: Math.floor(delta / MINUTE) })
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (delta < DAY) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.hours, { number: Math.floor(delta / HOUR) })
|
2019-08-07 06:02:36 +01:00
|
|
|
} else {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.days, { number: Math.floor(delta / DAY) })
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
} else if (date.getFullYear() === year) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatDate(date, shortDateFormatOptions)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatDate(date, { ...shortDateFormatOptions, year: 'numeric' })
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
const timeRemainingString = (intl, date, now) => {
|
2020-02-24 21:56:07 +00:00
|
|
|
const delta = date.getTime() - now
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
if (delta < 10 * SECOND) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.moments_remaining)
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (delta < MINUTE) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.seconds_remaining, { number: Math.floor(delta / SECOND) })
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (delta < HOUR) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.minutes_remaining, { number: Math.floor(delta / MINUTE) })
|
2019-08-07 06:02:36 +01:00
|
|
|
} else if (delta < DAY) {
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.hours_remaining, { number: Math.floor(delta / HOUR) })
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:06:59 +01:00
|
|
|
return intl.formatMessage(messages.days_remaining, { number: Math.floor(delta / DAY) })
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-04-08 02:06:59 +01:00
|
|
|
class RelativeTimestamp extends React.Component {
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
now: this.props.intl.now(),
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
year: (new Date()).getFullYear(),
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2019-08-07 06:02:36 +01: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 ||
|
|
|
|
this.props.intl.locale !== nextProps.intl.locale ||
|
2020-02-24 21:56:07 +00:00
|
|
|
this.state.now !== nextState.now
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2019-08-07 06:02:36 +01:00
|
|
|
if (this.props.timestamp !== nextProps.timestamp) {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.setState({ now: this.props.intl.now() })
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this._scheduleNextUpdate(this.props, this.state)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
componentWillUpdate(nextProps, nextState) {
|
|
|
|
this._scheduleNextUpdate(nextProps, nextState)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this._timer)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
_scheduleNextUpdate(props, state) {
|
|
|
|
clearTimeout(this._timer)
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
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)
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
this._timer = setTimeout(() => {
|
2020-02-24 21:56:07 +00:00
|
|
|
this.setState({ now: this.props.intl.now() })
|
|
|
|
}, delay)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
render() {
|
|
|
|
const { timestamp, intl, year, futureDate } = this.props
|
2019-08-07 06:02:36 +01:00
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
const date = new Date(timestamp)
|
|
|
|
const relativeTime = futureDate ? timeRemainingString(intl, date, this.state.now) : timeAgoString(intl, date, this.state.now, year)
|
2019-08-07 06:02:36 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<time dateTime={timestamp} title={intl.formatDate(date, dateFormatOptions)}>
|
|
|
|
{relativeTime}
|
|
|
|
</time>
|
2020-02-24 21:56:07 +00:00
|
|
|
)
|
2019-08-07 06:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-08-18 01:57:35 +01:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
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' },
|
|
|
|
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' },
|
|
|
|
})
|
|
|
|
|
|
|
|
RelativeTimestamp.propTypes = {
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
timestamp: PropTypes.string.isRequired,
|
|
|
|
year: PropTypes.number.isRequired,
|
|
|
|
futureDate: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(RelativeTimestamp)
|