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

237 lines
5.7 KiB
TypeScript
Raw Normal View History

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';
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 {
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;
}
export class Quote extends React.Component<Props, {}> {
public renderImage(url: string, icon?: string) {
2018-04-27 21:25:04 +00:00
const iconElement = icon ? (
<div className={classnames('icon', 'with-image', icon)} />
) : null;
2018-04-12 07:33:52 +00:00
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 (GoogleChrome.isVideoTypeSupported(contentType)) {
return objectUrl
? this.renderImage(objectUrl, 'play')
: this.renderIcon('movie');
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
2018-04-27 21:25:04 +00:00
return objectUrl ? this.renderImage(objectUrl) : this.renderIcon('image');
}
2018-04-09 23:24:24 +00:00
if (MIME.isAudio(contentType)) {
return this.renderIcon('microphone');
}
return this.renderIcon('file');
}
public renderText() {
const { i18n, text, attachments } = this.props;
if (text) {
2018-04-27 21:25:04 +00:00
return (
<div className="text" dangerouslySetInnerHTML={{ __html: text }} />
);
}
if (!attachments || attachments.length === 0) {
return null;
}
const first = attachments[0];
const { contentType, fileName, isVoiceMessage } = first;
if (GoogleChrome.isVideoTypeSupported(contentType)) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('video')}</div>;
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('photo')}</div>;
}
2018-04-09 23:24:24 +00:00
if (MIME.isAudio(contentType) && isVoiceMessage) {
2018-04-12 07:33:52 +00:00
return <div className="type-label">{i18n('voiceMessage')}</div>;
}
2018-04-09 23:24:24 +00:00
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() {
2018-04-27 21:25:04 +00:00
const {
i18n,
isIncoming,
isFromMe,
authorTitle,
authorProfileName,
} = this.props;
2018-04-12 19:21:37 +00:00
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 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="close-container">
<div className="close-button" onClick={onClick} />
</div>
);
}
public renderAuthor() {
const {
authorColor,
authorProfileName,
authorTitle,
i18n,
isFromMe,
} = this.props;
2018-04-27 21:25:04 +00:00
const authorProfileElement = authorProfileName ? (
<span className="profile-name">~{authorProfileName}</span>
) : null;
return (
<div className={classnames(authorColor, 'author')}>
2018-04-27 21:25:04 +00:00
{isFromMe ? (
i18n('you')
) : (
<span>
{authorTitle} {authorProfileElement}
</span>
)}
</div>
);
}
public render() {
2018-04-27 21:25:04 +00:00
const { authorColor, onClick, isFromMe } = this.props;
if (!validateQuote(this.props)) {
return null;
}
const classes = classnames(
authorColor,
'quoted-message',
isFromMe ? 'from-me' : null,
2018-04-24 20:57:14 +00:00
!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()}
{this.renderAuthor()}
{this.renderText()}
</div>
{this.renderIconContainer()}
{this.renderClose()}
</div>
);
}
}