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';
|
2018-07-09 21:29:13 +00:00
|
|
|
import { Color, Localizer } from '../../types/Util';
|
|
|
|
import { ContactName } from './ContactName';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-04-10 01:31:52 +00:00
|
|
|
interface Props {
|
2018-07-09 21:29:13 +00:00
|
|
|
attachment?: QuotedAttachment;
|
|
|
|
authorPhoneNumber: string;
|
2018-04-12 07:33:52 +00:00
|
|
|
authorProfileName?: string;
|
2018-07-09 21:29:13 +00:00
|
|
|
authorName?: string;
|
|
|
|
authorColor: Color;
|
2018-05-22 19:31:43 +00:00
|
|
|
i18n: Localizer;
|
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-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
export interface QuotedAttachment {
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-12 06:55:32 +00:00
|
|
|
function getObjectUrl(thumbnail: Attachment | undefined): string | null {
|
|
|
|
if (thumbnail && thumbnail.objectUrl) {
|
|
|
|
return thumbnail.objectUrl;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 06:55:32 +00:00
|
|
|
return null;
|
2018-04-10 01:31:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 20:53:49 +00:00
|
|
|
function getTypeLabel({
|
|
|
|
i18n,
|
|
|
|
contentType,
|
|
|
|
isVoiceMessage,
|
|
|
|
}: {
|
|
|
|
i18n: Localizer;
|
|
|
|
contentType: MIME.MIMEType;
|
|
|
|
isVoiceMessage: boolean;
|
|
|
|
}): string | null {
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-22 19:31:43 +00:00
|
|
|
export class Quote extends React.Component<Props> {
|
|
|
|
public renderImage(url: string, i18n: Localizer, 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">
|
|
|
|
<img src={url} alt={i18n('quoteThumbnailAlt')} />
|
|
|
|
{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-07-09 21:29:13 +00:00
|
|
|
const { attachment } = 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" />
|
|
|
|
<div className="module-quote__generic-file__text">{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;
|
|
|
|
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)) {
|
2018-04-12 06:55:32 +00:00
|
|
|
return objectUrl
|
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)) {
|
2018-05-22 19:31:43 +00:00
|
|
|
return objectUrl
|
|
|
|
? 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-07-09 21:29:13 +00:00
|
|
|
const { i18n, text, attachment } = this.props;
|
2018-04-10 01:31:52 +00:00
|
|
|
|
|
|
|
if (text) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return (
|
2018-07-27 17:42:26 +00:00
|
|
|
<div dir="auto" className="module-quote__primary__text">
|
2018-05-22 19:31:43 +00:00
|
|
|
<MessageBody text={text} 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 (
|
|
|
|
<div className="module-quote__primary__type-label">{typeLabel}</div>
|
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-18 23:10:14 +00:00
|
|
|
// We don't want the overall click handler for the quote to fire, so we stop
|
|
|
|
// propagation before handing control to the caller's callback.
|
|
|
|
const onClick = (e: React.MouseEvent<{}>): void => {
|
|
|
|
e.stopPropagation();
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
className="module-quote__close-button"
|
|
|
|
role="button"
|
|
|
|
onClick={onClick}
|
|
|
|
/>
|
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,
|
|
|
|
authorColor,
|
|
|
|
i18n,
|
|
|
|
isFromMe,
|
|
|
|
} = 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',
|
|
|
|
!isFromMe ? `module-quote__primary__author--${authorColor}` : null
|
|
|
|
)}
|
|
|
|
>
|
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}
|
|
|
|
i18n={i18n}
|
|
|
|
/>
|
2018-04-27 21:25:04 +00:00
|
|
|
)}
|
2018-04-30 19:28:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-07-09 21:29:13 +00:00
|
|
|
const {
|
|
|
|
authorColor,
|
|
|
|
isFromMe,
|
|
|
|
isIncoming,
|
|
|
|
onClick,
|
|
|
|
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
|
|
|
|
onClick={onClick}
|
|
|
|
role="button"
|
2018-07-07 00:48:14 +00:00
|
|
|
className={classNames(
|
2018-06-27 20:53:49 +00:00
|
|
|
'module-quote',
|
|
|
|
isIncoming ? 'module-quote--incoming' : 'module-quote--outgoing',
|
2018-07-09 21:29:13 +00:00
|
|
|
!isIncoming && !isFromMe
|
|
|
|
? `module-quote--outgoing-${authorColor}`
|
|
|
|
: null,
|
|
|
|
!isIncoming && isFromMe ? 'module-quote--outgoing-you' : null,
|
2018-06-27 20:53:49 +00:00
|
|
|
!onClick ? 'module-quote--no-click' : null,
|
|
|
|
withContentAbove ? 'module-quote--with-content-above' : null
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<div className="module-quote__primary">
|
2018-04-30 19:28:14 +00:00
|
|
|
{this.renderAuthor()}
|
2018-06-27 20:53:49 +00:00
|
|
|
{this.renderGenericFile()}
|
2018-04-10 01:31:52 +00:00
|
|
|
{this.renderText()}
|
|
|
|
</div>
|
|
|
|
{this.renderIconContainer()}
|
2018-04-18 20:06:33 +00:00
|
|
|
{this.renderClose()}
|
2018-04-10 01:31:52 +00:00
|
|
|
</div>
|
2018-04-03 22:56:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|