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

656 lines
16 KiB
TypeScript
Raw Normal View History

2022-03-04 21:14:52 +00:00
// Copyright 2018-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
import React, { useRef, useState, useEffect } from 'react';
import { noop } from 'lodash';
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';
2022-03-04 21:14:52 +00:00
import type { AttachmentType, ThumbnailType } from '../../types/Attachment';
2022-11-10 04:59:36 +00:00
import type { HydratedBodyRangesType, LocalizerType } from '../../types/Util';
import type {
ConversationColorType,
CustomColorType,
} from '../../types/Colors';
import { ContactName } from './ContactName';
import { Emojify } from './Emojify';
import { TextAttachment } from '../TextAttachment';
2020-09-18 21:43:57 +00:00
import { getTextWithMentions } from '../../util/getTextWithMentions';
2022-03-16 17:30:14 +00:00
import { getClassNamesFor } from '../../util/getClassNamesFor';
2021-05-28 16:15:17 +00:00
import { getCustomColorStyle } from '../../util/getCustomColorStyle';
2022-11-30 21:47:54 +00:00
import type { AnyPaymentEvent } from '../../types/Payment';
import { PaymentEventKind } from '../../types/Payment';
import { getPaymentEventNotificationText } from '../../messages/helpers';
export type Props = {
2020-07-24 01:35:32 +00:00
authorTitle: string;
2021-05-28 16:15:17 +00:00
conversationColor: ConversationColorType;
2022-11-30 21:47:54 +00:00
conversationTitle: string;
2021-05-28 16:15:17 +00:00
customColor?: CustomColorType;
2022-11-10 04:59:36 +00:00
bodyRanges?: HydratedBodyRangesType;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
isFromMe: boolean;
isIncoming?: boolean;
2022-06-15 17:53:08 +00:00
isCompose?: boolean;
isStoryReply?: boolean;
2022-03-16 17:30:14 +00:00
moduleClassName?: string;
2018-04-12 19:21:37 +00:00
onClick?: () => void;
onClose?: () => void;
2018-04-12 07:33:52 +00:00
text: string;
rawAttachment?: QuotedAttachmentType;
2022-11-30 21:47:54 +00:00
payment?: AnyPaymentEvent;
2022-05-11 20:59:58 +00:00
isGiftBadge: boolean;
isViewOnce: boolean;
reactionEmoji?: string;
referencedMessageNotFound: boolean;
doubleCheckMissingQuoteReference?: () => unknown;
};
type State = {
imageBroken: boolean;
};
2022-03-04 21:14:52 +00:00
export type QuotedAttachmentType = Pick<
AttachmentType,
2022-04-15 00:08:46 +00:00
'contentType' | 'fileName' | 'isVoiceMessage' | 'thumbnail' | 'textAttachment'
2022-03-04 21:14:52 +00:00
>;
function validateQuote(quote: Props): boolean {
if (
quote.isStoryReply &&
(quote.referencedMessageNotFound || quote.reactionEmoji)
) {
return true;
}
2022-05-11 20:59:58 +00:00
if (quote.isGiftBadge) {
return true;
}
if (quote.text) {
return true;
}
if (quote.rawAttachment) {
return true;
}
2022-11-30 21:47:54 +00:00
if (quote.payment?.kind === PaymentEventKind.Notification) {
return true;
}
return false;
}
// Long message attachments should not be shown.
function getAttachment(
rawAttachment: undefined | QuotedAttachmentType
): undefined | QuotedAttachmentType {
return rawAttachment && !MIME.isLongMessage(rawAttachment.contentType)
? rawAttachment
: undefined;
}
2022-03-04 21:14:52 +00:00
function getUrl(thumbnail?: ThumbnailType): string | undefined {
if (!thumbnail) {
return;
}
2022-03-04 21:14:52 +00:00
return thumbnail.objectUrl || thumbnail.url;
}
function getTypeLabel({
i18n,
isViewOnce = false,
contentType,
isVoiceMessage,
}: {
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
isViewOnce?: boolean;
contentType: MIME.MIMEType;
2022-03-04 21:14:52 +00:00
isVoiceMessage?: boolean;
2019-01-14 21:49:58 +00:00
}): string | undefined {
if (GoogleChrome.isVideoTypeSupported(contentType)) {
if (isViewOnce) {
return i18n('message--getDescription--disappearing-video');
}
return i18n('video');
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
if (isViewOnce) {
return i18n('message--getDescription--disappearing-photo');
}
return i18n('photo');
}
if (isViewOnce) {
return i18n('message--getDescription--disappearing-media');
}
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> {
2022-03-16 17:30:14 +00:00
private getClassName: (modifier?: string) => string;
2020-09-14 19:51:27 +00:00
constructor(props: Props) {
super(props);
this.state = {
imageBroken: false,
};
2022-03-16 17:30:14 +00:00
this.getClassName = getClassNamesFor('module-quote', props.moduleClassName);
2020-09-14 19:51:27 +00:00
}
2019-11-07 21:36:16 +00:00
override componentDidMount(): void {
2021-11-11 22:43:05 +00:00
const { doubleCheckMissingQuoteReference, referencedMessageNotFound } =
this.props;
if (referencedMessageNotFound) {
doubleCheckMissingQuoteReference?.();
}
}
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
};
2022-05-11 20:59:58 +00:00
public renderImage(
url: string,
icon: string | undefined,
isGiftBadge?: boolean
): JSX.Element {
const { isIncoming } = this.props;
2018-04-27 21:25:04 +00:00
const iconElement = icon ? (
2022-03-16 17:30:14 +00:00
<div className={this.getClassName('__icon-container__inner')}>
<div
className={this.getClassName('__icon-container__circle-background')}
>
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__icon-container__icon'),
this.getClassName(`__icon-container__icon--${icon}`)
)}
/>
</div>
</div>
2018-04-27 21:25:04 +00:00
) : null;
2018-04-12 07:33:52 +00:00
return (
2022-03-16 17:30:14 +00:00
<ThumbnailImage
2022-05-11 20:59:58 +00:00
className={classNames(
this.getClassName('__icon-container'),
isIncoming === false &&
isGiftBadge &&
this.getClassName('__icon-container__outgoing-gift-badge')
)}
2022-03-16 17:30:14 +00:00
src={url}
onError={this.handleImageError}
>
{iconElement}
</ThumbnailImage>
);
}
2020-09-14 19:51:27 +00:00
public renderIcon(icon: string): JSX.Element {
return (
2022-03-16 17:30:14 +00:00
<div className={this.getClassName('__icon-container')}>
<div className={this.getClassName('__icon-container__inner')}>
<div
className={this.getClassName('__icon-container__circle-background')}
>
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__icon-container__icon'),
this.getClassName(`__icon-container__icon--${icon}`)
)}
/>
</div>
</div>
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderGenericFile(): JSX.Element | null {
const { rawAttachment, isIncoming } = this.props;
const attachment = getAttachment(rawAttachment);
if (!attachment) {
2020-09-14 19:51:27 +00:00
return null;
}
2022-04-15 00:08:46 +00:00
const { fileName, contentType, textAttachment } = attachment;
const isGenericFile =
!GoogleChrome.isVideoTypeSupported(contentType) &&
!GoogleChrome.isImageTypeSupported(contentType) &&
2022-04-15 00:08:46 +00:00
!textAttachment &&
!MIME.isAudio(contentType);
if (!isGenericFile) {
return null;
}
return (
2022-03-16 17:30:14 +00:00
<div className={this.getClassName('__generic-file')}>
<div className={this.getClassName('__generic-file__icon')} />
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__generic-file__text'),
isIncoming
? this.getClassName('__generic-file__text--incoming')
: null
)}
>
{fileName}
</div>
</div>
);
}
2022-11-30 21:47:54 +00:00
public renderPayment(): JSX.Element | null {
const { payment, authorTitle, conversationTitle, isFromMe, i18n } =
this.props;
if (payment == null) {
return null;
}
return (
<>
<Emojify text="💳" />
{getPaymentEventNotificationText(
payment,
authorTitle,
conversationTitle,
isFromMe,
i18n
)}
</>
);
}
2020-09-14 19:51:27 +00:00
public renderIconContainer(): JSX.Element | null {
2022-05-11 20:59:58 +00:00
const { isGiftBadge, isViewOnce, i18n, rawAttachment } = this.props;
const { imageBroken } = this.state;
const attachment = getAttachment(rawAttachment);
2022-05-11 20:59:58 +00:00
if (isGiftBadge) {
return this.renderImage('images/gift-thumbnail.svg', undefined, true);
}
if (!attachment) {
return null;
}
2022-04-15 00:08:46 +00:00
const { contentType, textAttachment, thumbnail } = attachment;
2022-03-04 21:14:52 +00:00
const url = getUrl(thumbnail);
if (isViewOnce) {
return this.renderIcon('view-once');
}
2022-04-15 00:08:46 +00:00
if (textAttachment) {
return (
<div className={this.getClassName('__icon-container')}>
<TextAttachment
i18n={i18n}
isThumbnail
textAttachment={textAttachment}
/>
</div>
);
2022-04-15 00:08:46 +00:00
}
if (GoogleChrome.isVideoTypeSupported(contentType)) {
2022-03-04 21:14:52 +00:00
return url && !imageBroken
? this.renderImage(url, 'play')
: this.renderIcon('movie');
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
2022-03-04 21:14:52 +00:00
return url && !imageBroken
2022-05-11 20:59:58 +00:00
? this.renderImage(url, undefined)
: 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 {
2022-05-11 20:59:58 +00:00
const {
bodyRanges,
isGiftBadge,
i18n,
text,
rawAttachment,
isIncoming,
isViewOnce,
} = this.props;
2022-05-16 19:54:38 +00:00
if (text && !isGiftBadge) {
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(
2022-03-16 17:30:14 +00:00
this.getClassName('__primary__text'),
isIncoming ? this.getClassName('__primary__text--incoming') : null
)}
>
2021-09-16 22:45:32 +00:00
<MessageBody
disableLinks
disableJumbomoji
text={quoteText}
i18n={i18n}
/>
</div>
2018-04-27 21:25:04 +00:00
);
}
const attachment = getAttachment(rawAttachment);
2022-05-11 20:59:58 +00:00
let typeLabel;
if (isGiftBadge) {
typeLabel = i18n('quote--giftBadge');
} else if (attachment) {
const { contentType, isVoiceMessage } = attachment;
typeLabel = getTypeLabel({
i18n,
isViewOnce,
contentType,
isVoiceMessage,
});
} else {
return null;
}
if (typeLabel) {
return (
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__primary__type-label'),
isIncoming
? this.getClassName('__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 (
2022-03-16 17:30:14 +00:00
<div className={this.getClassName('__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"
2022-03-16 17:30:14 +00:00
className={this.getClassName('__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 { authorTitle, i18n, isFromMe, isIncoming, isStoryReply } =
this.props;
const title = isFromMe ? i18n('you') : <ContactName title={authorTitle} />;
const author = isStoryReply ? (
<>
{title} &middot; {i18n('Quote__story')}
</>
) : (
title
);
return (
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__primary__author'),
isIncoming ? this.getClassName('__primary__author--incoming') : null
)}
>
{author}
</div>
);
}
2020-09-14 19:51:27 +00:00
public renderReferenceWarning(): JSX.Element | null {
2021-05-28 16:15:17 +00:00
const {
conversationColor,
customColor,
i18n,
isIncoming,
isStoryReply,
2021-05-28 16:15:17 +00:00
referencedMessageNotFound,
} = this.props;
if (!referencedMessageNotFound || isStoryReply) {
return null;
}
return (
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__reference-warning'),
2021-05-28 16:15:17 +00:00
isIncoming
2022-03-16 17:30:14 +00:00
? this.getClassName(`--incoming-${conversationColor}`)
: this.getClassName(`--outgoing-${conversationColor}`)
)}
2021-05-28 16:15:17 +00:00
style={{ ...getCustomColorStyle(customColor, true) }}
>
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__reference-warning__icon'),
isIncoming
2022-03-16 17:30:14 +00:00
? this.getClassName('__reference-warning__icon--incoming')
: null
)}
/>
<div
className={classNames(
2022-03-16 17:30:14 +00:00
this.getClassName('__reference-warning__text'),
isIncoming
2022-03-16 17:30:14 +00:00
? this.getClassName('__reference-warning__text--incoming')
: null
)}
>
{i18n('originalMessageNotFound')}
</div>
</div>
);
}
public override render(): JSX.Element | null {
const {
2021-05-28 16:15:17 +00:00
conversationColor,
customColor,
2022-06-15 17:53:08 +00:00
isCompose,
isIncoming,
onClick,
rawAttachment,
reactionEmoji,
referencedMessageNotFound,
} = this.props;
if (!validateQuote(this.props)) {
return null;
}
2022-07-06 16:34:01 +00:00
let colorClassName: string;
2022-06-15 17:53:08 +00:00
let directionClassName: string;
if (isCompose) {
directionClassName = this.getClassName('--compose');
2022-07-06 16:34:01 +00:00
colorClassName = this.getClassName(`--compose-${conversationColor}`);
2022-06-15 17:53:08 +00:00
} else if (isIncoming) {
directionClassName = this.getClassName('--incoming');
2022-07-06 16:34:01 +00:00
colorClassName = this.getClassName(`--incoming-${conversationColor}`);
2022-06-15 17:53:08 +00:00
} else {
directionClassName = this.getClassName('--outgoing');
2022-07-06 16:34:01 +00:00
colorClassName = this.getClassName(`--outgoing-${conversationColor}`);
2022-06-15 17:53:08 +00:00
}
return (
2022-03-16 17:30:14 +00:00
<div className={this.getClassName('__container')}>
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(
2022-03-16 17:30:14 +00:00
this.getClassName(''),
2022-06-15 17:53:08 +00:00
directionClassName,
colorClassName,
2022-03-16 17:30:14 +00:00
!onClick && this.getClassName('--no-click'),
referencedMessageNotFound &&
2022-05-11 20:59:58 +00:00
this.getClassName('--with-reference-warning')
)}
2021-05-28 16:15:17 +00:00
style={{ ...getCustomColorStyle(customColor, true) }}
>
2022-03-16 17:30:14 +00:00
<div className={this.getClassName('__primary')}>
{this.renderAuthor()}
{this.renderGenericFile()}
2022-11-30 21:47:54 +00:00
{this.renderPayment()}
{this.renderText()}
</div>
{reactionEmoji && (
<div
className={
rawAttachment
? this.getClassName('__reaction-emoji')
: this.getClassName('__reaction-emoji--story-unavailable')
}
>
<Emojify text={reactionEmoji} />
</div>
)}
{this.renderIconContainer()}
{this.renderClose()}
2019-11-07 21:36:16 +00:00
</button>
{this.renderReferenceWarning()}
</div>
);
}
}
function ThumbnailImage({
2022-03-16 17:30:14 +00:00
className,
src,
onError,
children,
}: Readonly<{
2022-03-16 17:30:14 +00:00
className: string;
src: string;
onError: () => void;
children: ReactNode;
}>): JSX.Element {
const imageRef = useRef(new Image());
const [loadedSrc, setLoadedSrc] = useState<null | string>(null);
useEffect(() => {
const image = new Image();
image.onload = () => {
setLoadedSrc(src);
};
image.src = src;
imageRef.current = image;
return () => {
image.onload = noop;
};
}, [src]);
useEffect(() => {
setLoadedSrc(null);
}, [src]);
useEffect(() => {
const image = imageRef.current;
image.onerror = onError;
return () => {
image.onerror = noop;
};
}, [onError]);
return (
<div
2022-03-16 17:30:14 +00:00
className={className}
style={
2021-02-19 17:50:32 +00:00
loadedSrc ? { backgroundImage: `url('${encodeURI(loadedSrc)}')` } : {}
}
>
{children}
</div>
);
}