lbry-desktop/ui/component/dateTime/view.jsx

80 lines
1.9 KiB
React
Raw Normal View History

// @flow
import { getTimeAgoStr } from 'util/time';
import moment from 'moment';
import React from 'react';
const DEFAULT_MIN_UPDATE_DELTA_MS = 60 * 1000;
type State = {
lastRenderTime: Date,
};
type Props = {
clock24h?: boolean,
date?: any,
genericSeconds?: boolean,
minUpdateDeltaMs?: number,
2022-01-03 21:05:19 +01:00
showFutureDate?: boolean,
timeAgo?: boolean,
2022-01-03 21:05:19 +01:00
type?: string,
};
class DateTime extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
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 ||
this.props.type !== nextProps.type
) {
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;
if (timeAgo) this.setState({ lastRenderTime: new Date() });
}
render() {
const { clock24h, date, genericSeconds, showFutureDate, timeAgo, type } = this.props;
2019-12-10 20:09:10 +01:00
const clockFormat = clock24h ? 'HH:mm' : 'hh:mm A';
return (
<span className="date_time" title={timeAgo && moment(date).format(`MMMM Do, YYYY ${clockFormat}`)}>
{date
? timeAgo
? getTimeAgoStr(date, showFutureDate, genericSeconds)
: moment(date).format(type === 'date' ? 'MMMM Do, YYYY' : clockFormat)
: '...'}
</span>
);
}
}
export default DateTime;