2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
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
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type 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;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
const UPDATE_FREQUENCY = 60 * 1000;
|
|
|
|
|
|
|
|
export class Timestamp extends React.Component<Props> {
|
2020-09-14 19:51:27 +00:00
|
|
|
private interval: NodeJS.Timeout | null;
|
2018-07-09 21:29:13 +00:00
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.interval = null;
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public componentDidMount(): void {
|
2018-07-09 21:29:13 +00:00
|
|
|
const update = () => {
|
|
|
|
this.setState({
|
2020-09-14 19:51:27 +00:00
|
|
|
// Used to trigger renders
|
|
|
|
// eslint-disable-next-line react/no-unused-state
|
2018-07-09 21:29:13 +00:00
|
|
|
lastUpdated: Date.now(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
this.interval = setInterval(update, UPDATE_FREQUENCY);
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public componentWillUnmount(): void {
|
2018-07-09 21:29:13 +00:00
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public render(): JSX.Element | null {
|
2018-07-09 21:29:13 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|