-
Notifications
You must be signed in to change notification settings - Fork 569
/
TimeAgo.jsx
72 lines (64 loc) · 1.97 KB
/
TimeAgo.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
import React from "react";
import {FormattedRelative} from "react-intl";
import {ChainStore} from "bitsharesjs";
import PropTypes from "prop-types";
import {Tooltip} from "bitshares-ui-style-guide";
class TimeAgo extends React.Component {
static propTypes = {
time: PropTypes.any.isRequired,
chain_time: PropTypes.bool,
component: PropTypes.element,
className: PropTypes.string
};
static defaultProps = {
chain_time: true
};
shouldComponentUpdate(nextProps) {
return nextProps.time !== this.props.time;
}
render() {
let {time, chain_time} = this.props;
var offset_mills = chain_time
? ChainStore.getEstimatedChainTimeOffset()
: 0;
if (!time) {
return null;
}
if (
typeof time === "string" &&
time.indexOf("+") === -1 &&
!/Z$/.test(time)
) {
time += "Z";
}
let timePassed = Math.round(
(new Date().getTime() - new Date(time).getTime() + offset_mills) /
1000
);
let interval;
if (timePassed < 60) {
// 60s
interval = 500; // 0.5s
} else if (timePassed < 60 * 60) {
// 1 hour
interval = 60 * 500; // 30 seconds
} else {
interval = 60 * 60 * 500; // 30 minutes
}
return (
<Tooltip placement="bottom" title={new Date(time).toString()}>
<span
className={"tooltip inline-block " + this.props.className}
ref={"timeago_ttip_" + time}
>
<FormattedRelative
updateInterval={interval}
value={new Date(time).getTime() + offset_mills * 0.75}
initialNow={Date.now()}
/>
</span>
</Tooltip>
);
}
}
export default TimeAgo;