mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 23:00:30 +00:00
72 lines
1.4 KiB
JavaScript
72 lines
1.4 KiB
JavaScript
// @flow
|
|
import * as React from 'react';
|
|
import autobind from 'autobind-decorator';
|
|
import moment from 'moment';
|
|
|
|
type Props = {
|
|
timestamp: number | Date | string,
|
|
intervalSeconds?: number,
|
|
className?: string,
|
|
capitalize?: boolean,
|
|
};
|
|
|
|
type State = {
|
|
text: string,
|
|
};
|
|
|
|
@autobind
|
|
class TimeFromNow extends React.PureComponent<Props, State> {
|
|
_interval: any;
|
|
|
|
constructor(props: any) {
|
|
super(props);
|
|
this._interval = null;
|
|
this.state = {
|
|
text: '',
|
|
};
|
|
}
|
|
|
|
_update() {
|
|
const { timestamp, capitalize } = this.props;
|
|
|
|
let text = moment(timestamp).fromNow();
|
|
|
|
// Shorten default case
|
|
if (text === 'a few seconds ago') {
|
|
text = 'just now';
|
|
}
|
|
|
|
// Capitalize if needed
|
|
if (capitalize) {
|
|
text = text.replace(/\w\S*/g, function(txt) {
|
|
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
|
|
});
|
|
}
|
|
|
|
this.setState({ text });
|
|
}
|
|
|
|
componentDidMount() {
|
|
const intervalSeconds = this.props.intervalSeconds || 5;
|
|
this._interval = setInterval(this._update, intervalSeconds * 1000);
|
|
this._update();
|
|
}
|
|
|
|
componentWillUnmount() {
|
|
clearInterval(this._interval);
|
|
}
|
|
|
|
render() {
|
|
const { className, timestamp } = this.props;
|
|
const { text } = this.state;
|
|
|
|
return (
|
|
<span title={moment(timestamp).toString()} className={className}>
|
|
{text}
|
|
</span>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default TimeFromNow;
|