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

211 lines
5.1 KiB
TypeScript
Raw Normal View History

import React from 'react';
import classnames from 'classnames';
// @ts-ignore
import Mime from '../../../js/modules/types/mime';
interface Props {
attachments: Array<QuotedAttachment>;
2018-04-12 07:33:52 +00:00
authorColor: string;
authorProfileName?: string;
authorTitle: string;
i18n: (key: string, values?: Array<string>) => string;
2018-04-12 19:21:37 +00:00
isFromMe: string;
2018-04-12 07:33:52 +00:00
isIncoming: boolean;
2018-04-12 19:21:37 +00:00
onClick?: () => void;
onClose?: () => void;
2018-04-12 07:33:52 +00:00
text: string;
}
interface QuotedAttachment {
contentType: string;
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 {
contentType: string;
/* 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;
}
export class Quote extends React.Component<Props, {}> {
public renderImage(url: string, icon?: string) {
2018-04-12 07:33:52 +00:00
const iconElement = icon
? <div className={classnames('icon', 'with-image', icon)} />
2018-04-12 07:33:52 +00:00
: null;
return (
<div className="icon-container">
<div className="inner">
<img src={url} />
2018-04-12 07:33:52 +00:00
{iconElement}
</div>
</div>
);
}
public renderIcon(icon: string) {
2018-04-12 19:21:37 +00:00
const { authorColor, isIncoming } = this.props;
const backgroundColor = isIncoming ? 'white' : authorColor;
2018-04-12 19:21:37 +00:00
const iconColor = isIncoming ? authorColor : 'white';
return (
2018-04-12 07:33:52 +00:00
<div className="icon-container">
<div className={classnames('circle-background', backgroundColor)} />
<div className={classnames('icon', icon, iconColor)} />
</div>
);
}
public renderIconContainer() {
const { attachments } = this.props;
if (!attachments || attachments.length === 0) {
return null;
}
const first = attachments[0];
const { contentType, thumbnail } = first;
const objectUrl = getObjectUrl(thumbnail);
if (Mime.isVideo(contentType)) {
return objectUrl
? this.renderImage(objectUrl, 'play')
: this.renderIcon('play');
}
if (Mime.isImage(contentType)) {
return objectUrl
? this.renderImage(objectUrl)
: this.renderIcon('image');
}
if (Mime.isAudio(contentType)) {
return this.renderIcon('microphone');
}
return this.renderIcon('file');
}
public renderText() {
const { i18n, text, attachments } = this.props;
if (text) {
2018-04-12 07:33:52 +00:00
return <div className="text">{text}</div>;
}
if (!attachments || attachments.length === 0) {
return null;
}
const first = attachments[0];
const { contentType, fileName, isVoiceMessage } = first;
if (Mime.isVideo(contentType)) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('video')}</div>;
}
if (Mime.isImage(contentType)) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('photo')}</div>;
}
if (Mime.isAudio(contentType) && isVoiceMessage) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('voiceMessage')}</div>;
}
if (Mime.isAudio(contentType)) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('audio')}</div>;
}
2018-04-12 07:33:52 +00:00
return <div className="filename-label">{fileName}</div>;
}
2018-04-12 19:21:37 +00:00
public renderIOSLabel() {
const { i18n, isIncoming, isFromMe, authorTitle, authorProfileName } = this.props;
const profileString = authorProfileName ? ` ~${authorProfileName}` : '';
const authorName = `${authorTitle}${profileString}`;
const label = isFromMe
? isIncoming
? i18n('replyingToYou')
: i18n('replyingToYourself')
: i18n('replyingTo', [authorName]);
return <div className="ios-label">{label}</div>;
2018-04-12 19:21:37 +00:00
}
public renderClose() {
const { onClose } = this.props;
if (!onClose) {
return null;
}
// We need the container to give us the flexibility to implement the iOS design.
// We put the onClick on both because the Android theme juse uses close-container
return (
<div className="close-container" onClick={onClose}>
<div className="close-button" onClick={onClose}></div>
</div>
);
}
public render() {
const {
authorTitle,
authorProfileName,
authorColor,
2018-04-12 19:21:37 +00:00
onClick,
isFromMe,
} = this.props;
if (!validateQuote(this.props)) {
return null;
}
2018-04-12 07:33:52 +00:00
const authorProfileElement = authorProfileName
? <span className="profile-name">~{authorProfileName}</span>
: null;
const classes = classnames(
authorColor,
'quoted-message',
isFromMe ? 'from-me' : null,
!onClick ? 'no-click' : null,
);
2018-04-12 07:33:52 +00:00
return (
<div onClick={onClick} className={classes}>
<div className="primary">
2018-04-12 19:21:37 +00:00
{this.renderIOSLabel()}
<div className={classnames(authorColor, 'author')}>
2018-04-12 07:33:52 +00:00
{authorTitle}{' '}{authorProfileElement}
</div>
{this.renderText()}
</div>
{this.renderIconContainer()}
{this.renderClose()}
</div>
);
}
}