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

444 lines
11 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';
2020-09-14 19:51:27 +00:00
import * as MIME from '../../types/MIME';
import * as GoogleChrome from '../../util/GoogleChrome';
import { MessageBody } from './MessageBody';
2020-09-16 22:42:48 +00:00
import { BodyRangesType, LocalizerType } from '../../types/Util';
import { ColorType } from '../../types/Colors';
import { ContactName } from './ContactName';
2020-09-18 21:43:57 +00:00
import { getTextWithMentions } from '../../util/getTextWithMentions';
2020-08-27 17:02:25 +00:00
export interface Props {
attachment?: QuotedAttachmentType;
2020-07-24 01:35:32 +00:00
authorTitle: string;
authorPhoneNumber?: string;
2018-04-12 07:33:52 +00:00
authorProfileName?: string;
authorName?: string;
2019-01-14 21:49:58 +00:00
authorColor?: ColorType;
2020-09-16 22:42:48 +00:00
bodyRanges?: BodyRangesType;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
isFromMe: boolean;
2018-04-12 07:33:52 +00:00
isIncoming: boolean;
withContentAbove: boolean;
2018-04-12 19:21:37 +00:00
onClick?: () => void;
onClose?: () => void;
2018-04-12 07:33:52 +00:00
text: string;
referencedMessageNotFound: boolean;
}
interface State {
imageBroken: boolean;
}
export interface QuotedAttachmentType {
2018-04-24 20:59:45 +00:00
contentType: MIME.MIMEType;
2018-04-12 07:33:52 +00:00
fileName: string;
/** Not included in protobuf */
isVoiceMessage: boolean;
2018-04-12 07:33:52 +00:00
thumbnail?: Attachment;
}
interface Attachment {
2018-04-24 20:59:45 +00:00
contentType: MIME.MIMEType;
/** Not included in protobuf, and is loaded asynchronously */
objectUrl?: string;
}
function validateQuote(quote: Props): boolean {
if (quote.text) {
return true;
}
if (quote.attachment) {
return true;
}
return false;
}
2019-01-14 21:49:58 +00:00
function getObjectUrl(thumbnail: Attachment | undefined): string | undefined {
if (thumbnail && thumbnail.objectUrl) {
return thumbnail.objectUrl;
}
2020-09-14 19:51:27 +00:00
return undefined;
}
function getTypeLabel({
i18n,
contentType,
isVoiceMessage,
}: {
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
contentType: MIME.MIMEType;
isVoiceMessage: boolean;
2019-01-14 21:49:58 +00:00
}): string | undefined {
if (GoogleChrome.isVideoTypeSupported(contentType)) {
return i18n('video');
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
return i18n('photo');
}
if (MIME.isAudio(contentType) && isVoiceMessage) {
return i18n('voiceMessage');
}
2020-09-14 19:51:27 +00:00
return MIME.isAudio(contentType) ? i18n('audio') : undefined;
}
export class Quote extends React.Component<Props, State> {
2020-09-14 19:51:27 +00:00
constructor(props: Props) {
super(props);
this.state = {
imageBroken: false,
};
}
2019-11-07 21:36:16 +00:00
2020-09-14 19:51:27 +00:00
public handleKeyDown = (
event: React.KeyboardEvent<HTMLButtonElement>
): void => {
2019-11-07 21:36:16 +00:00
const { onClick } = this.props;
// This is important to ensure that using this quote to navigate to the referenced
// message doesn't also trigger its parent message's keydown.
if (onClick && (event.key === 'Enter' || event.key === ' ')) {
2019-11-07 21:36:16 +00:00
event.preventDefault();
event.stopPropagation();
onClick();
}
};
2020-09-14 19:51:27 +00:00
public handleClick = (event: React.MouseEvent<HTMLButtonElement>): void => {
const { onClick } = this.props;
if (onClick) {
event.preventDefault();
event.stopPropagation();
onClick();
}
};
2020-09-14 19:51:27 +00:00
public handleImageError = (): void => {
window.console.info(
'Message: Image failed to load; failing over to placeholder'
);
this.setState({
imageBroken: true,
});
2019-11-07 21:36:16 +00:00
};
2020-09-14 19:51:27 +00:00
public renderImage(
url: string,
i18n: LocalizerType,
icon?: string
): JSX.Element {
2018-04-27 21:25:04 +00:00
const iconElement = icon ? (
<div className="module-quote__icon-container__inner">
<div className="module-quote__icon-container__circle-background">
<div
className={classNames(
'module-quote__icon-container__icon',
`module-quote__icon-container__icon--${icon}`
)}
/>
</div>
</div>
2018-04-27 21:25:04 +00:00
) : null;
2018-04-12 07:33:52 +00:00
return (
<div className="module-quote__icon-container">
<img
src={url}
alt={i18n('quoteThumbnailAlt')}
2019-11-07 21:36:16 +00:00
onError={this.handleImageError}
/>
{iconElement}
</div>
);
}
2020-09-14 19:51:27 +00:00
// eslint-disable-next-line class-methods-use-this
public renderIcon(icon: string): JSX.Element {
return (
<div className="module-quote__icon-container">
<div className="module-quote__icon-container__inner">
<div className="module-quote__icon-container__circle-background">
<div
className={classNames(
'module-quote__icon-container__icon',
`module-quote__icon-container__icon--${icon}`
)}
/>
</div>
</div>
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderGenericFile(): JSX.Element | null {
const { attachment, isIncoming } = this.props;
if (!attachment) {
2020-09-14 19:51:27 +00:00
return null;
}
const { fileName, contentType } = attachment;
const isGenericFile =
!GoogleChrome.isVideoTypeSupported(contentType) &&
!GoogleChrome.isImageTypeSupported(contentType) &&
!MIME.isAudio(contentType);
if (!isGenericFile) {
return null;
}
return (
<div className="module-quote__generic-file">
<div className="module-quote__generic-file__icon" />
<div
className={classNames(
'module-quote__generic-file__text',
isIncoming ? 'module-quote__generic-file__text--incoming' : null
)}
>
{fileName}
</div>
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderIconContainer(): JSX.Element | null {
const { attachment, i18n } = this.props;
const { imageBroken } = this.state;
if (!attachment) {
return null;
}
const { contentType, thumbnail } = attachment;
const objectUrl = getObjectUrl(thumbnail);
if (GoogleChrome.isVideoTypeSupported(contentType)) {
return objectUrl && !imageBroken
? this.renderImage(objectUrl, i18n, 'play')
: this.renderIcon('movie');
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
return objectUrl && !imageBroken
? this.renderImage(objectUrl, i18n)
: this.renderIcon('image');
}
2018-04-09 23:24:24 +00:00
if (MIME.isAudio(contentType)) {
return this.renderIcon('microphone');
}
return null;
}
2020-09-16 22:42:48 +00:00
public renderText(): JSX.Element | null {
2020-09-18 21:43:57 +00:00
const { bodyRanges, i18n, text, attachment, isIncoming } = this.props;
if (text) {
2020-09-18 21:43:57 +00:00
const quoteText = bodyRanges
? getTextWithMentions(bodyRanges, text)
: text;
2018-04-27 21:25:04 +00:00
return (
<div
dir="auto"
className={classNames(
'module-quote__primary__text',
isIncoming ? 'module-quote__primary__text--incoming' : null
)}
>
2020-09-18 21:43:57 +00:00
<MessageBody disableLinks text={quoteText} i18n={i18n} />
</div>
2018-04-27 21:25:04 +00:00
);
}
if (!attachment) {
return null;
}
const { contentType, isVoiceMessage } = attachment;
const typeLabel = getTypeLabel({ i18n, contentType, isVoiceMessage });
if (typeLabel) {
return (
<div
className={classNames(
'module-quote__primary__type-label',
isIncoming ? 'module-quote__primary__type-label--incoming' : null
)}
>
{typeLabel}
</div>
);
}
return null;
2018-04-12 19:21:37 +00:00
}
2020-09-14 19:51:27 +00:00
public renderClose(): JSX.Element | null {
const { i18n, onClose } = this.props;
if (!onClose) {
return null;
}
const clickHandler = (e: React.MouseEvent): void => {
e.stopPropagation();
2019-11-07 21:36:16 +00:00
e.preventDefault();
onClose();
};
const keyDownHandler = (e: React.KeyboardEvent): void => {
if (e.key === 'Enter' || e.key === ' ') {
e.stopPropagation();
e.preventDefault();
onClose();
}
};
// We need the container to give us the flexibility to implement the iOS design.
return (
<div className="module-quote__close-container">
<div
2019-11-07 21:36:16 +00:00
tabIndex={0}
// We can't be a button because the overall quote is a button; can't nest them
role="button"
2019-11-07 21:36:16 +00:00
className="module-quote__close-button"
2020-09-14 19:51:27 +00:00
aria-label={i18n('close')}
onKeyDown={keyDownHandler}
onClick={clickHandler}
/>
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderAuthor(): JSX.Element {
const {
authorProfileName,
authorPhoneNumber,
2020-07-24 01:35:32 +00:00
authorTitle,
authorName,
i18n,
isFromMe,
isIncoming,
} = this.props;
return (
<div
className={classNames(
'module-quote__primary__author',
isIncoming ? 'module-quote__primary__author--incoming' : null
)}
>
2018-04-27 21:25:04 +00:00
{isFromMe ? (
i18n('you')
) : (
<ContactName
phoneNumber={authorPhoneNumber}
name={authorName}
profileName={authorProfileName}
2020-07-24 01:35:32 +00:00
title={authorTitle}
i18n={i18n}
/>
2018-04-27 21:25:04 +00:00
)}
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderReferenceWarning(): JSX.Element | null {
const { i18n, isIncoming, referencedMessageNotFound } = this.props;
if (!referencedMessageNotFound) {
return null;
}
return (
<div
className={classNames(
'module-quote__reference-warning',
isIncoming ? 'module-quote__reference-warning--incoming' : null
)}
>
<div
className={classNames(
'module-quote__reference-warning__icon',
isIncoming
? 'module-quote__reference-warning__icon--incoming'
: null
)}
/>
<div
className={classNames(
'module-quote__reference-warning__text',
isIncoming
? 'module-quote__reference-warning__text--incoming'
: null
)}
>
{i18n('originalMessageNotFound')}
</div>
</div>
);
}
2020-09-14 19:51:27 +00:00
public render(): JSX.Element | null {
const {
authorColor,
isIncoming,
onClick,
referencedMessageNotFound,
withContentAbove,
} = this.props;
if (!validateQuote(this.props)) {
return null;
}
return (
<div
className={classNames(
'module-quote-container',
withContentAbove ? 'module-quote-container--with-content-above' : null
)}
>
2019-11-07 21:36:16 +00:00
<button
2020-09-14 19:51:27 +00:00
type="button"
onClick={this.handleClick}
2019-11-07 21:36:16 +00:00
onKeyDown={this.handleKeyDown}
className={classNames(
'module-quote',
isIncoming ? 'module-quote--incoming' : 'module-quote--outgoing',
isIncoming
? `module-quote--incoming-${authorColor}`
: `module-quote--outgoing-${authorColor}`,
!onClick ? 'module-quote--no-click' : null,
withContentAbove ? 'module-quote--with-content-above' : null,
referencedMessageNotFound
? 'module-quote--with-reference-warning'
: null
)}
>
<div className="module-quote__primary">
{this.renderAuthor()}
{this.renderGenericFile()}
{this.renderText()}
</div>
{this.renderIconContainer()}
{this.renderClose()}
2019-11-07 21:36:16 +00:00
</button>
{this.renderReferenceWarning()}
</div>
);
}
}