2018-10-10 07:22:06 +02:00
|
|
|
// @flow
|
2022-01-03 13:41:00 +01:00
|
|
|
import { getTimeAgoStr } from 'util/time';
|
2018-10-10 07:22:06 +02:00
|
|
|
import moment from 'moment';
|
2022-01-03 13:41:00 +01:00
|
|
|
import React from 'react';
|
2017-09-03 18:35:01 +02:00
|
|
|
|
2021-05-25 00:29:58 +02:00
|
|
|
const DEFAULT_MIN_UPDATE_DELTA_MS = 60 * 1000;
|
|
|
|
|
2022-01-03 13:41:00 +01:00
|
|
|
type State = {
|
|
|
|
lastRenderTime: Date,
|
|
|
|
};
|
|
|
|
|
2018-10-10 07:22:06 +02:00
|
|
|
type Props = {
|
2021-05-25 00:29:58 +02:00
|
|
|
clock24h?: boolean,
|
2022-01-03 13:41:00 +01:00
|
|
|
date?: any,
|
2022-01-04 21:33:55 +01:00
|
|
|
genericSeconds?: boolean,
|
2021-05-25 00:29:58 +02:00
|
|
|
minUpdateDeltaMs?: number,
|
2022-01-03 21:05:19 +01:00
|
|
|
showFutureDate?: boolean,
|
2022-01-03 13:41:00 +01:00
|
|
|
timeAgo?: boolean,
|
2022-01-03 21:05:19 +01:00
|
|
|
type?: string,
|
2018-10-10 07:22:06 +02:00
|
|
|
};
|
|
|
|
|
2021-05-25 00:29:58 +02:00
|
|
|
class DateTime extends React.Component<Props, State> {
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
2022-01-03 13:41:00 +01:00
|
|
|
|
2021-05-25 00:29:58 +02:00
|
|
|
this.state = {
|
|
|
|
lastRenderTime: new Date(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps: Props): boolean {
|
|
|
|
if (
|
|
|
|
moment(this.props.date).diff(moment(nextProps.date)) !== 0 ||
|
|
|
|
this.props.clock24h !== nextProps.clock24h ||
|
|
|
|
this.props.timeAgo !== nextProps.timeAgo ||
|
|
|
|
this.props.minUpdateDeltaMs !== nextProps.minUpdateDeltaMs ||
|
2022-01-03 13:41:00 +01:00
|
|
|
this.props.type !== nextProps.type
|
2021-05-25 00:29:58 +02:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.timeAgo && nextProps.timeAgo) {
|
|
|
|
const minUpdateDeltaMs = this.props.minUpdateDeltaMs || DEFAULT_MIN_UPDATE_DELTA_MS;
|
|
|
|
const prev = moment(this.state.lastRenderTime);
|
|
|
|
const curr = moment(new Date());
|
|
|
|
const deltaMs = curr.diff(prev);
|
|
|
|
|
|
|
|
if (deltaMs > minUpdateDeltaMs) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
const { timeAgo } = this.props;
|
2022-01-03 13:41:00 +01:00
|
|
|
|
|
|
|
if (timeAgo) this.setState({ lastRenderTime: new Date() });
|
2021-05-25 00:29:58 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 18:35:01 +02:00
|
|
|
render() {
|
2022-01-04 21:33:55 +01:00
|
|
|
const { clock24h, date, genericSeconds, showFutureDate, timeAgo, type } = this.props;
|
2019-12-10 20:09:10 +01:00
|
|
|
|
2022-01-03 13:41:00 +01:00
|
|
|
const clockFormat = clock24h ? 'HH:mm' : 'hh:mm A';
|
2018-10-10 07:22:06 +02:00
|
|
|
|
2017-09-18 02:52:57 +02:00
|
|
|
return (
|
2022-01-04 21:33:55 +01:00
|
|
|
<span className="date_time" title={timeAgo && moment(date).format(`MMMM Do, YYYY ${clockFormat}`)}>
|
2022-01-03 13:41:00 +01:00
|
|
|
{date
|
|
|
|
? timeAgo
|
2022-01-04 21:33:55 +01:00
|
|
|
? getTimeAgoStr(date, showFutureDate, genericSeconds)
|
2022-01-03 13:41:00 +01:00
|
|
|
: moment(date).format(type === 'date' ? 'MMMM Do, YYYY' : clockFormat)
|
|
|
|
: '...'}
|
2017-09-18 02:52:57 +02:00
|
|
|
</span>
|
|
|
|
);
|
2017-09-03 18:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DateTime;
|