forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
45 lines (41 loc) · 1.31 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Component } from 'react';
import { isDate, formatDistanceStrict, parseISO } from 'date-fns';
import { date } from '../../utils/prop-types';
import isDateWithin from '../../utils/isDateWithin';
/**
* Display a human-readable relative string between a date and now.
* Optionally also show a relative distance between that date and an
* additional offset date.
*/
export default class DateDistance extends Component {
static defaultProps = {
offset: null,
};
static propTypes = {
/**
* The origin date for which to render a relative string from now.
*/
from: date.isRequired,
/**
* An optional date for which to also show a relative string between `from`
* and `offset`.
*/
offset: date,
};
render() {
const now = new Date();
const { from, offset } = this.props;
const fromParsed = isDate(from) ? from : parseISO(from);
const unit = isDateWithin(fromParsed, new Date(), 44, 120)
? 'minute'
: undefined;
const fromNow = formatDistanceStrict(fromParsed, now, {
addSuffix: true,
unit,
});
const offsetParsed = isDate(offset) ? offset : parseISO(offset);
const offsetNow =
offset && formatDistanceStrict(fromParsed, offsetParsed, { unit });
return offsetNow ? `${fromNow} (${offsetNow} later)` : fromNow;
}
}