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-06-27 20:53:49 +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-19 00:03:24 +00:00
|
|
|
import { Emojify } from './Emojify';
|
2018-05-14 20:52:10 +00:00
|
|
|
import { MessageBody } from './MessageBody';
|
2018-05-22 19:31:43 +00:00
|
|
|
import { Localizer } from '../../types/Util';
|
2018-05-14 20:52:10 +00:00
|
|
|
|
2018-04-10 01:31:52 +00:00
|
|
|
interface Props {
|
2018-04-12 06:55:32 +00:00
|
|
|
attachments: Array<QuotedAttachment>;
|
2018-06-27 20:53:49 +00:00
|
|
|
color: string;
|
2018-04-12 07:33:52 +00:00
|
|
|
authorProfileName?: string;
|
|
|
|
authorTitle: string;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quote.attachments && quote.attachments.length > 0) {
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
className={classnames(
|
|
|
|
'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() {
|
|
|
|
const { attachments } = this.props;
|
|
|
|
|
|
|
|
if (!attachments || !attachments.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const first = attachments[0];
|
|
|
|
const { fileName, contentType } = first;
|
|
|
|
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-05-22 19:31:43 +00:00
|
|
|
const { attachments, i18n } = this.props;
|
2018-04-10 01:31:52 +00:00
|
|
|
if (!attachments || attachments.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const first = attachments[0];
|
2018-04-12 06:55:32 +00:00
|
|
|
const { contentType, thumbnail } = first;
|
|
|
|
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() {
|
|
|
|
const { i18n, text, attachments } = this.props;
|
|
|
|
|
|
|
|
if (text) {
|
2018-04-27 21:25:04 +00:00
|
|
|
return (
|
2018-06-27 20:53:49 +00:00
|
|
|
<div 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
|
|
|
}
|
|
|
|
|
|
|
|
if (!attachments || attachments.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const first = attachments[0];
|
2018-06-27 20:53:49 +00:00
|
|
|
const { contentType, isVoiceMessage } = first;
|
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-06-27 20:53:49 +00:00
|
|
|
const { authorProfileName, authorTitle, i18n, isFromMe } = this.props;
|
2018-04-30 19:28:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
const authorProfileElement = authorProfileName ? (
|
2018-06-27 20:53:49 +00:00
|
|
|
<span className="module-quote__primary__profile-name">
|
2018-05-22 19:31:43 +00:00
|
|
|
~<Emojify text={authorProfileName} i18n={i18n} />
|
2018-05-19 00:03:24 +00:00
|
|
|
</span>
|
2018-04-27 21:25:04 +00:00
|
|
|
) : null;
|
2018-04-30 19:28:14 +00:00
|
|
|
|
|
|
|
return (
|
2018-06-27 20:53:49 +00:00
|
|
|
<div className="module-quote__primary__author">
|
2018-04-27 21:25:04 +00:00
|
|
|
{isFromMe ? (
|
|
|
|
i18n('you')
|
|
|
|
) : (
|
|
|
|
<span>
|
2018-05-22 19:31:43 +00:00
|
|
|
<Emojify text={authorTitle} i18n={i18n} /> {authorProfileElement}
|
2018-04-27 21:25:04 +00:00
|
|
|
</span>
|
|
|
|
)}
|
2018-04-30 19:28:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-06-27 20:53:49 +00:00
|
|
|
const { color, 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"
|
|
|
|
className={classnames(
|
|
|
|
'module-quote',
|
|
|
|
isIncoming ? 'module-quote--incoming' : 'module-quote--outgoing',
|
|
|
|
!isIncoming ? `module-quote--outgoing-${color}` : null,
|
|
|
|
!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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|