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

285 lines
7 KiB
TypeScript
Raw Normal View History

// tslint:disable:react-this-binding-issue
import React from 'react';
import classnames from 'classnames';
2018-04-09 23:24:24 +00:00
import * as MIME from '../../../ts/types/MIME';
import * as GoogleChrome from '../../../ts/util/GoogleChrome';
import { Emojify } from './Emojify';
import { MessageBody } from './MessageBody';
import { Localizer } from '../../types/Util';
interface Props {
attachments: Array<QuotedAttachment>;
color: string;
2018-04-12 07:33:52 +00:00
authorProfileName?: string;
authorTitle: string;
i18n: Localizer;
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;
}
export interface QuotedAttachment {
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.attachments && quote.attachments.length > 0) {
return true;
}
return false;
}
function getObjectUrl(thumbnail: Attachment | undefined): string | null {
if (thumbnail && thumbnail.objectUrl) {
return thumbnail.objectUrl;
}
return null;
}
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;
}
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 ? (
<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')} />
{iconElement}
</div>
);
}
public renderIcon(icon: string) {
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>
);
}
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;
}
return (
<div className="module-quote__generic-file">
<div className="module-quote__generic-file__icon" />
<div className="module-quote__generic-file__text">{fileName}</div>
</div>
);
}
public renderIconContainer() {
const { attachments, i18n } = this.props;
if (!attachments || attachments.length === 0) {
return null;
}
const first = attachments[0];
const { contentType, thumbnail } = first;
const objectUrl = getObjectUrl(thumbnail);
if (GoogleChrome.isVideoTypeSupported(contentType)) {
return objectUrl
? this.renderImage(objectUrl, i18n, 'play')
: this.renderIcon('movie');
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
return objectUrl
? 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;
}
public renderText() {
const { i18n, text, attachments } = this.props;
if (text) {
2018-04-27 21:25:04 +00:00
return (
<div className="module-quote__primary__text">
<MessageBody text={text} i18n={i18n} />
</div>
2018-04-27 21:25:04 +00:00
);
}
if (!attachments || attachments.length === 0) {
return null;
}
const first = attachments[0];
const { contentType, isVoiceMessage } = first;
const typeLabel = getTypeLabel({ i18n, contentType, isVoiceMessage });
if (typeLabel) {
return (
<div className="module-quote__primary__type-label">{typeLabel}</div>
);
}
return null;
2018-04-12 19:21:37 +00:00
}
public renderClose() {
const { onClose } = this.props;
if (!onClose) {
return null;
}
// 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();
};
// We need the container to give us the flexibility to implement the iOS design.
return (
<div className="module-quote__close-container">
<div
className="module-quote__close-button"
role="button"
onClick={onClick}
/>
</div>
);
}
public renderAuthor() {
const { authorProfileName, authorTitle, i18n, isFromMe } = this.props;
2018-04-27 21:25:04 +00:00
const authorProfileElement = authorProfileName ? (
<span className="module-quote__primary__profile-name">
~<Emojify text={authorProfileName} i18n={i18n} />
</span>
2018-04-27 21:25:04 +00:00
) : null;
return (
<div className="module-quote__primary__author">
2018-04-27 21:25:04 +00:00
{isFromMe ? (
i18n('you')
) : (
<span>
<Emojify text={authorTitle} i18n={i18n} /> {authorProfileElement}
2018-04-27 21:25:04 +00:00
</span>
)}
</div>
);
}
public render() {
const { color, isIncoming, onClick, withContentAbove } = this.props;
if (!validateQuote(this.props)) {
return null;
}
return (
<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">
{this.renderAuthor()}
{this.renderGenericFile()}
{this.renderText()}
</div>
{this.renderIconContainer()}
{this.renderClose()}
</div>
);
}
}