signal-desktop/ts/components/conversation/Timestamp.tsx

89 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
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';
export interface Props {
2019-01-14 21:49:58 +00:00
timestamp?: number;
extended?: boolean;
module?: string;
withImageNoCaption?: boolean;
withSticker?: boolean;
2019-06-26 19:33:13 +00:00
withTapToViewExpired?: boolean;
2019-10-04 18:06:17 +00:00
withUnread?: boolean;
direction?: 'incoming' | 'outgoing';
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
}
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;
constructor(props: Props) {
super(props);
this.interval = null;
}
2020-09-14 19:51:27 +00:00
public componentDidMount(): void {
const update = () => {
this.setState({
2020-09-14 19:51:27 +00:00
// Used to trigger renders
// eslint-disable-next-line react/no-unused-state
lastUpdated: Date.now(),
});
};
this.interval = setInterval(update, UPDATE_FREQUENCY);
}
2020-09-14 19:51:27 +00:00
public componentWillUnmount(): void {
if (this.interval) {
clearInterval(this.interval);
}
}
2020-09-14 19:51:27 +00:00
public render(): JSX.Element | null {
const {
direction,
i18n,
module,
timestamp,
withImageNoCaption,
withSticker,
2019-06-26 19:33:13 +00:00
withTapToViewExpired,
2019-10-04 18:06:17 +00:00
withUnread,
extended,
} = this.props;
const moduleName = module || 'module-timestamp';
if (timestamp === null || timestamp === undefined) {
return null;
}
return (
<span
className={classNames(
moduleName,
direction ? `${moduleName}--${direction}` : null,
2019-06-26 19:33:13 +00:00
withTapToViewExpired && direction
? `${moduleName}--${direction}-with-tap-to-view-expired`
: null,
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
)}
title={moment(timestamp).format('llll')}
>
{formatRelativeTime(timestamp, { i18n, extended })}
</span>
);
}
}