2018-07-09 21:29:13 +00:00
|
|
|
import React from 'react';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import { formatRelativeTime } from '../../util/formatRelativeTime';
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
import { LocalizerType } from '../../types/Util';
|
2018-07-09 21:29:13 +00:00
|
|
|
|
2020-08-26 21:08:22 +00:00
|
|
|
export interface Props {
|
2019-01-14 21:49:58 +00:00
|
|
|
timestamp?: number;
|
|
|
|
extended?: boolean;
|
2018-07-09 21:29:13 +00:00
|
|
|
module?: string;
|
2018-07-18 03:25:55 +00:00
|
|
|
withImageNoCaption?: boolean;
|
2019-05-16 22:32:11 +00:00
|
|
|
withSticker?: boolean;
|
2019-06-26 19:33:13 +00:00
|
|
|
withTapToViewExpired?: boolean;
|
2019-10-04 18:06:17 +00:00
|
|
|
withUnread?: boolean;
|
2018-07-18 03:25:55 +00:00
|
|
|
direction?: 'incoming' | 'outgoing';
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2018-07-09 21:29:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const UPDATE_FREQUENCY = 60 * 1000;
|
|
|
|
|
|
|
|
export class Timestamp extends React.Component<Props> {
|
|
|
|
private interval: any;
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.interval = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
const update = () => {
|
|
|
|
this.setState({
|
|
|
|
lastUpdated: Date.now(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
this.interval = setInterval(update, UPDATE_FREQUENCY);
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const {
|
|
|
|
direction,
|
|
|
|
i18n,
|
|
|
|
module,
|
|
|
|
timestamp,
|
|
|
|
withImageNoCaption,
|
2019-05-16 22:32:11 +00:00
|
|
|
withSticker,
|
2019-06-26 19:33:13 +00:00
|
|
|
withTapToViewExpired,
|
2019-10-04 18:06:17 +00:00
|
|
|
withUnread,
|
2018-07-18 03:25:55 +00:00
|
|
|
extended,
|
2018-07-09 21:29:13 +00:00
|
|
|
} = this.props;
|
|
|
|
const moduleName = module || 'module-timestamp';
|
|
|
|
|
2018-07-18 15:14:34 +00:00
|
|
|
if (timestamp === null || timestamp === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
className={classNames(
|
|
|
|
moduleName,
|
2018-07-18 03:25:55 +00:00
|
|
|
direction ? `${moduleName}--${direction}` : null,
|
2019-06-26 19:33:13 +00:00
|
|
|
withTapToViewExpired && direction
|
|
|
|
? `${moduleName}--${direction}-with-tap-to-view-expired`
|
|
|
|
: null,
|
2019-05-16 22:32:11 +00:00
|
|
|
withImageNoCaption ? `${moduleName}--with-image-no-caption` : null,
|
2019-10-04 18:06:17 +00:00
|
|
|
withSticker ? `${moduleName}--with-sticker` : null,
|
|
|
|
withUnread ? `${moduleName}--with-unread` : null
|
2018-07-09 21:29:13 +00:00
|
|
|
)}
|
|
|
|
title={moment(timestamp).format('llll')}
|
|
|
|
>
|
2018-07-18 03:25:55 +00:00
|
|
|
{formatRelativeTime(timestamp, { i18n, extended })}
|
2018-07-09 21:29:13 +00:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|