forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
89 lines (74 loc) · 1.97 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Component } from 'react';
import { isDate, intervalToDuration, parseISO } from 'date-fns';
import { date } from '../../utils/prop-types';
/**
* Display a time string (00h 00m 00s) relative string between a date and now.
* Optionally also show a relative distance between that date and an
* additional offset date.
*/
export default class Duration 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,
};
state = {
updates: 0,
};
componentDidMount() {
if (!this.props.offset) {
// make dynamic to show that something is still running
this.interval = setInterval(() => this.tick(), 1000);
}
}
componentDidUpdate() {
if (this.props.offset && this.interval) {
clearInterval(this.interval);
}
}
componentWillUnmount() {
if (this.interval) {
clearInterval(this.interval);
}
}
tick() {
this.setState(state => ({
updates: state.updates + 1,
}));
}
render() {
const { from, offset } = this.props;
const start = isDate(from) ? from : parseISO(from);
let end;
if (offset) {
end = isDate(offset) ? offset : parseISO(offset);
} else {
end = new Date();
}
const interval = intervalToDuration({ start, end });
const pad = num => String(num).padStart(2, '0');
const parts = [pad(interval.minutes), 'm ', pad(interval.seconds), 's '];
if (interval.hours > 0) {
parts.unshift(interval.hours, 'h ');
}
if (interval.days > 0) {
parts.unshift(interval.days, 'd ');
}
if (interval.months > 0) {
parts.unshift(interval.months, 'm ');
}
if (interval.years > 0) {
parts.unshift(interval.years, 'y ');
}
return parts.join('');
}
}