2018-05-22 19:31:43 +00:00
|
|
|
// tslint:disable:react-this-binding-issue
|
|
|
|
|
2018-04-03 22:56:12 +00:00
|
|
|
import React from 'react';
|
2018-07-07 00:48:14 +00:00
|
|
|
import classNames from 'classnames';
|
2018-04-03 22:56:12 +00:00
|
|
|
|
2018-04-09 23:24:24 +00:00
|
|
|
import * as MIME from '../../../ts/types/MIME';
|
2018-04-24 21:16:26 +00:00
|
|
|
import * as GoogleChrome from '../../../ts/util/GoogleChrome';
|
2018-04-03 22:56:12 +00:00
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
import { MessageBody } from './MessageBody';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { ColorType, LocalizerType } from '../../types/Util';
|
2018-07-09 21:29:13 +00:00
|
|
|
import { ContactName } from './ContactName';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-04-10 01:31:52 +00:00
|
|
|
interface Props {
|
2018-11-14 18:47:19 +00:00
|
|
|
attachment?: QuotedAttachmentType;
|
2018-07-09 21:29:13 +00:00
|
|
|
authorPhoneNumber: string;
|
2018-04-12 07:33:52 +00:00
|
|
|
authorProfileName?: string;
|
2018-07-09 21:29:13 +00:00
|
|
|
authorName?: string;
|
2019-01-14 21:49:58 +00:00
|
|
|
authorColor?: ColorType;
|
|
|
|
i18n: LocalizerType;
|
2018-06-27 20:53:49 +00:00
|
|
|
isFromMe: boolean;
|
2018-04-12 07:33:52 +00:00
|
|
|
isIncoming: boolean;
|
2018-06-27 20:53:49 +00:00
|
|
|
withContentAbove: boolean;
|
2018-04-12 19:21:37 +00:00
|
|
|
onClick?: () => void;
|
2018-04-18 20:06:33 +00:00
|
|
|
onClose?: () => void;
|
2018-04-12 07:33:52 +00:00
|
|
|
text: string;
|
2018-08-15 19:31:29 +00:00
|
|
|
referencedMessageNotFound: boolean;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
interface State {
|
|
|
|
imageBroken: boolean;
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
export interface QuotedAttachmentType {
|
2018-04-24 20:59:45 +00:00
|
|
|
contentType: MIME.MIMEType;
|
2018-04-12 07:33:52 +00:00
|
|
|
fileName: string;
|
2018-05-22 19:31:43 +00:00
|
|
|
/** Not included in protobuf */
|
2018-04-10 01:31:52 +00:00
|
|
|
isVoiceMessage: boolean;
|
2018-04-12 07:33:52 +00:00
|
|
|
thumbnail?: Attachment;
|
2018-04-12 06:55:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Attachment {
|
2018-04-24 20:59:45 +00:00
|
|
|
contentType: MIME.MIMEType;
|
2018-05-22 19:31:43 +00:00
|
|
|
/** Not included in protobuf, and is loaded asynchronously */
|
2018-04-12 06:55:32 +00:00
|
|
|
objectUrl?: string;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function validateQuote(quote: Props): boolean {
|
|
|
|
if (quote.text) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
if (quote.attachment) {
|
2018-04-10 01:31:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
function getObjectUrl(thumbnail: Attachment | undefined): string | undefined {
|
2018-04-12 06:55:32 +00:00
|
|
|
if (thumbnail && thumbnail.objectUrl) {
|
|
|
|
return thumbnail.objectUrl;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
function getTypeLabel({
|
|
|
|
i18n,
|
|
|
|
contentType,
|
|
|
|
isVoiceMessage,
|
|
|
|
}: {
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2018-06-27 20:53:49 +00:00
|
|
|
contentType: MIME.MIMEType;
|
|
|
|
isVoiceMessage: boolean;
|
2019-01-14 21:49:58 +00:00
|
|
|
}): string | undefined {
|
2018-06-27 20:53:49 +00:00
|
|
|
if (GoogleChrome.isVideoTypeSupported(contentType)) {
|
|
|
|
return i18n('video');
|
|
|
|
}
|
|
|
|
if (GoogleChrome.isImageTypeSupported(contentType)) {
|
|
|
|
return i18n('photo');
|
|
|
|
}
|
|
|
|
if (MIME.isAudio(contentType) && isVoiceMessage) {
|
|
|
|
return i18n('voiceMessage');
|
|
|
|
}
|
|
|
|
if (MIME.isAudio(contentType)) {
|
|
|
|
return i18n('audio');
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return;
|
2018-06-27 20:53:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:15:07 +00:00
|
|
|
export class Quote extends React.Component<Props, State> {
|
2019-11-07 21:36:16 +00:00
|
|
|
public state = {
|
|
|
|
imageBroken: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
public handleKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
|
|
|
|
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.
|
2019-11-21 19:16:06 +00:00
|
|
|
if (onClick && (event.key === 'Enter' || event.key === ' ')) {
|
2019-11-07 21:36:16 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
};
|
2019-01-30 20:15:07 +00:00
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public handleImageError = () => {
|
2019-01-30 20:15:07 +00:00
|
|
|
// tslint:disable-next-line no-console
|
|
|
|
console.log('Message: Image failed to load; failing over to placeholder');
|
|
|
|
this.setState({
|
|
|
|
imageBroken: true,
|
|
|
|
});
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
2019-01-30 20:15:07 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
public renderImage(url: string, i18n: LocalizerType, icon?: string) {
|
2018-04-27 21:25:04 +00:00
|
|
|
const iconElement = icon ? (
|
2018-06-27 20:53:49 +00:00
|
|
|
<div className="module-quote__icon-container__inner">
|
|
|
|
<div className="module-quote__icon-container__circle-background">
|
|
|
|
<div
|
2018-07-07 00:48:14 +00:00
|
|
|
className={classNames(
|
2018-06-27 20:53:49 +00:00
|
|
|
'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
|
|
|
|
2018-04-12 06:55:32 +00:00
|
|
|
return (
|
2018-06-27 20:53:49 +00:00
|
|
|
<div className="module-quote__icon-container">
|
2019-01-30 20:15:07 +00:00
|
|
|
<img
|
|
|
|
src={url}
|
|
|
|
alt={i18n('quoteThumbnailAlt')}
|
2019-11-07 21:36:16 +00:00
|
|
|
onError={this.handleImageError}
|
2019-01-30 20:15:07 +00:00
|
|
|
/>
|
2018-06-27 20:53:49 +00:00
|
|
|
{iconElement}
|
2018-04-12 06:55:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-04-10 01:31:52 +00:00
|
|
|
|
2018-04-12 06:55:32 +00:00
|
|
|
public renderIcon(icon: string) {
|
2018-06-27 20:53:49 +00:00
|
|
|
return (
|
|
|
|
<div className="module-quote__icon-container">
|
|
|
|
<div className="module-quote__icon-container__inner">
|
|
|
|
<div className="module-quote__icon-container__circle-background">
|
|
|
|
<div
|
2018-07-07 00:48:14 +00:00
|
|
|
className={classNames(
|
2018-06-27 20:53:49 +00:00
|
|
|
'module-quote__icon-container__icon',
|
|
|
|
`module-quote__icon-container__icon--${icon}`
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-04-12 06:55:32 +00:00
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
public renderGenericFile() {
|
2018-08-14 00:06:50 +00:00
|
|
|
const { attachment, isIncoming } = this.props;
|
2018-06-27 20:53:49 +00:00
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
if (!attachment) {
|
2018-06-27 20:53:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
const { fileName, contentType } = attachment;
|
2018-06-27 20:53:49 +00:00
|
|
|
const isGenericFile =
|
|
|
|
!GoogleChrome.isVideoTypeSupported(contentType) &&
|
|
|
|
!GoogleChrome.isImageTypeSupported(contentType) &&
|
|
|
|
!MIME.isAudio(contentType);
|
|
|
|
|
|
|
|
if (!isGenericFile) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-12 06:55:32 +00:00
|
|
|
|
|
|
|
return (
|
2018-06-27 20:53:49 +00:00
|
|
|
<div className="module-quote__generic-file">
|
|
|
|
<div className="module-quote__generic-file__icon" />
|
2018-08-14 00:06:50 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-quote__generic-file__text',
|
|
|
|
isIncoming ? 'module-quote__generic-file__text--incoming' : null
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{fileName}
|
|
|
|
</div>
|
2018-04-12 06:55:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public renderIconContainer() {
|
2018-07-09 21:29:13 +00:00
|
|
|
const { attachment, i18n } = this.props;
|
2019-01-30 20:15:07 +00:00
|
|
|
const { imageBroken } = this.state;
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
if (!attachment) {
|
2018-04-10 01:31:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
const { contentType, thumbnail } = attachment;
|
2018-04-12 06:55:32 +00:00
|
|
|
const objectUrl = getObjectUrl(thumbnail);
|
|
|
|
|
2018-04-24 21:16:26 +00:00
|
|
|
if (GoogleChrome.isVideoTypeSupported(contentType)) {
|
2019-01-30 20:15:07 +00:00
|
|
|
return objectUrl && !imageBroken
|
2018-05-22 19:31:43 +00:00
|
|
|
? this.renderImage(objectUrl, i18n, 'play')
|
2018-04-19 01:20:36 +00:00
|
|
|
: this.renderIcon('movie');
|
2018-04-12 06:55:32 +00:00
|
|
|
}
|
2018-04-24 21:16:26 +00:00
|
|
|
if (GoogleChrome.isImageTypeSupported(contentType)) {
|
2019-01-30 20:15:07 +00:00
|
|
|
return objectUrl && !imageBroken
|
2018-05-22 19:31:43 +00:00
|
|
|
? this.renderImage(objectUrl, i18n)
|
|
|
|
: this.renderIcon('image');
|
2018-04-12 06:55:32 +00:00
|
|
|
}
|
2018-04-09 23:24:24 +00:00
|
|
|
if (MIME.isAudio(contentType)) {
|
2018-04-12 06:55:32 +00:00
|
|
|
return this.renderIcon('microphone');
|
|
|
|
}
|
2018-04-10 01:31:52 +00:00
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
return null;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public renderText() {
|
2018-08-14 00:06:50 +00:00
|
|
|
const { i18n, text, attachment, isIncoming } = this.props;
|
2018-04-10 01:31:52 +00:00
|
|
|
|
|
|
|
if (text) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return (
|
2018-08-14 00:06:50 +00:00
|
|
|
<div
|
|
|
|
dir="auto"
|
|
|
|
className={classNames(
|
|
|
|
'module-quote__primary__text',
|
|
|
|
isIncoming ? 'module-quote__primary__text--incoming' : null
|
|
|
|
)}
|
|
|
|
>
|
2019-01-22 19:58:50 +00:00
|
|
|
<MessageBody text={text} disableLinks={true} i18n={i18n} />
|
2018-05-14 20:52:10 +00:00
|
|
|
</div>
|
2018-04-27 21:25:04 +00:00
|
|
|
);
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
if (!attachment) {
|
2018-04-10 01:31:52 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:29:13 +00:00
|
|
|
const { contentType, isVoiceMessage } = attachment;
|
2018-04-10 01:31:52 +00:00
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
const typeLabel = getTypeLabel({ i18n, contentType, isVoiceMessage });
|
|
|
|
if (typeLabel) {
|
|
|
|
return (
|
2018-08-14 00:06:50 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-quote__primary__type-label',
|
|
|
|
isIncoming ? 'module-quote__primary__type-label--incoming' : null
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{typeLabel}
|
|
|
|
</div>
|
2018-06-27 20:53:49 +00:00
|
|
|
);
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
return null;
|
2018-04-12 19:21:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 20:06:33 +00:00
|
|
|
public renderClose() {
|
|
|
|
const { onClose } = this.props;
|
|
|
|
|
|
|
|
if (!onClose) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-21 19:16:06 +00:00
|
|
|
const clickHandler = (e: React.MouseEvent): void => {
|
2018-04-18 23:10:14 +00:00
|
|
|
e.stopPropagation();
|
2019-11-07 21:36:16 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2018-04-18 23:10:14 +00:00
|
|
|
onClose();
|
|
|
|
};
|
2019-11-21 19:16:06 +00:00
|
|
|
const keyDownHandler = (e: React.KeyboardEvent): void => {
|
|
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
onClose();
|
|
|
|
}
|
|
|
|
};
|
2018-04-18 23:10:14 +00:00
|
|
|
|
2018-04-18 20:06:33 +00:00
|
|
|
// We need the container to give us the flexibility to implement the iOS design.
|
|
|
|
return (
|
2018-06-27 20:53:49 +00:00
|
|
|
<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
|
2018-06-27 20:53:49 +00:00
|
|
|
role="button"
|
2019-11-07 21:36:16 +00:00
|
|
|
className="module-quote__close-button"
|
2019-11-21 19:16:06 +00:00
|
|
|
onKeyDown={keyDownHandler}
|
|
|
|
onClick={clickHandler}
|
2018-06-27 20:53:49 +00:00
|
|
|
/>
|
2018-04-18 20:06:33 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:28:14 +00:00
|
|
|
public renderAuthor() {
|
2018-07-09 21:29:13 +00:00
|
|
|
const {
|
|
|
|
authorProfileName,
|
|
|
|
authorPhoneNumber,
|
|
|
|
authorName,
|
|
|
|
i18n,
|
|
|
|
isFromMe,
|
2018-08-14 00:06:50 +00:00
|
|
|
isIncoming,
|
2018-07-09 21:29:13 +00:00
|
|
|
} = this.props;
|
2018-04-30 19:28:14 +00:00
|
|
|
|
|
|
|
return (
|
2018-07-09 21:29:13 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-quote__primary__author',
|
2018-08-14 00:06:50 +00:00
|
|
|
isIncoming ? 'module-quote__primary__author--incoming' : null
|
2018-07-09 21:29:13 +00:00
|
|
|
)}
|
|
|
|
>
|
2018-04-27 21:25:04 +00:00
|
|
|
{isFromMe ? (
|
|
|
|
i18n('you')
|
|
|
|
) : (
|
2018-07-09 21:29:13 +00:00
|
|
|
<ContactName
|
|
|
|
phoneNumber={authorPhoneNumber}
|
|
|
|
name={authorName}
|
|
|
|
profileName={authorProfileName}
|
|
|
|
/>
|
2018-04-27 21:25:04 +00:00
|
|
|
)}
|
2018-04-30 19:28:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-15 19:31:29 +00:00
|
|
|
public renderReferenceWarning() {
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-30 19:28:14 +00:00
|
|
|
public render() {
|
2018-07-09 21:29:13 +00:00
|
|
|
const {
|
2018-10-09 22:56:14 +00:00
|
|
|
authorColor,
|
2018-07-09 21:29:13 +00:00
|
|
|
isIncoming,
|
|
|
|
onClick,
|
2018-08-15 19:31:29 +00:00
|
|
|
referencedMessageNotFound,
|
2018-07-09 21:29:13 +00:00
|
|
|
withContentAbove,
|
|
|
|
} = this.props;
|
2018-04-10 01:31:52 +00:00
|
|
|
|
|
|
|
if (!validateQuote(this.props)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-03 22:56:12 +00:00
|
|
|
return (
|
2018-06-27 20:53:49 +00:00
|
|
|
<div
|
2018-07-07 00:48:14 +00:00
|
|
|
className={classNames(
|
2018-08-15 19:31:29 +00:00
|
|
|
'module-quote-container',
|
|
|
|
withContentAbove ? 'module-quote-container--with-content-above' : null
|
2018-06-27 20:53:49 +00:00
|
|
|
)}
|
|
|
|
>
|
2019-11-07 21:36:16 +00:00
|
|
|
<button
|
2018-08-15 19:31:29 +00:00
|
|
|
onClick={onClick}
|
2019-11-07 21:36:16 +00:00
|
|
|
onKeyDown={this.handleKeyDown}
|
2018-08-15 19:31:29 +00:00
|
|
|
className={classNames(
|
|
|
|
'module-quote',
|
|
|
|
isIncoming ? 'module-quote--incoming' : 'module-quote--outgoing',
|
2018-09-26 00:26:06 +00:00
|
|
|
isIncoming
|
2018-10-09 22:56:14 +00:00
|
|
|
? `module-quote--incoming-${authorColor}`
|
|
|
|
: `module-quote--outgoing-${authorColor}`,
|
2018-08-15 19:31:29 +00:00
|
|
|
!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>
|
2018-08-15 19:31:29 +00:00
|
|
|
{this.renderReferenceWarning()}
|
2018-04-10 01:31:52 +00:00
|
|
|
</div>
|
2018-04-03 22:56:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|