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

244 lines
5.9 KiB
TypeScript
Raw Normal View History

import React from 'react';
import classnames from 'classnames';
2018-04-10 01:24:24 +02:00
import * as MIME from '../../../ts/types/MIME';
import * as GoogleChrome from '../../../ts/util/GoogleChrome';
import { Emojify } from './Emojify';
import { MessageBody } from './MessageBody';
interface Props {
attachments: Array<QuotedAttachment>;
2018-04-12 09:33:52 +02:00
authorColor: string;
authorProfileName?: string;
authorTitle: string;
i18n: (key: string, values?: Array<string>) => string;
2018-04-12 21:21:37 +02:00
isFromMe: string;
2018-04-12 09:33:52 +02:00
isIncoming: boolean;
2018-04-12 21:21:37 +02:00
onClick?: () => void;
onClose?: () => void;
2018-04-12 09:33:52 +02:00
text: string;
}
interface QuotedAttachment {
2018-04-24 22:59:45 +02:00
contentType: MIME.MIMEType;
2018-04-12 09:33:52 +02:00
fileName: string;
/* Not included in protobuf */
isVoiceMessage: boolean;
2018-04-12 09:33:52 +02:00
thumbnail?: Attachment;
}
interface Attachment {
2018-04-24 22:59:45 +02: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 23:25:04 +02:00
const iconElement = icon ? (
<div className={classnames('icon', 'with-image', icon)} />
) : null;
2018-04-12 09:33:52 +02:00
return (
<div className="icon-container">
<div className="inner">
<img src={url} />
2018-04-12 09:33:52 +02:00
{iconElement}
</div>
</div>
);
}
public renderIcon(icon: string) {
2018-04-12 21:21:37 +02:00
const { authorColor, isIncoming } = this.props;
const backgroundColor = isIncoming ? 'white' : authorColor;
2018-04-12 21:21:37 +02:00
const iconColor = isIncoming ? authorColor : 'white';
return (
2018-04-12 09:33:52 +02: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 23:25:04 +02:00
return objectUrl ? this.renderImage(objectUrl) : this.renderIcon('image');
}
2018-04-10 01:24:24 +02: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 23:25:04 +02:00
return (
<div className="text">
<MessageBody text={text} />
</div>
2018-04-27 23:25:04 +02:00
);
}
if (!attachments || attachments.length === 0) {
return null;
}
const first = attachments[0];
const { contentType, fileName, isVoiceMessage } = first;
if (GoogleChrome.isVideoTypeSupported(contentType)) {
2018-04-12 09:33:52 +02:00
return <div className="type-label">{i18n('video')}</div>;
}
if (GoogleChrome.isImageTypeSupported(contentType)) {
2018-04-12 09:33:52 +02:00
return <div className="type-label">{i18n('photo')}</div>;
}
2018-04-10 01:24:24 +02:00
if (MIME.isAudio(contentType) && isVoiceMessage) {
2018-04-12 09:33:52 +02:00
return <div className="type-label">{i18n('voiceMessage')}</div>;
}
2018-04-10 01:24:24 +02:00
if (MIME.isAudio(contentType)) {
2018-04-12 09:33:52 +02:00
return <div className="type-label">{i18n('audio')}</div>;
}
2018-04-12 09:33:52 +02:00
return <div className="filename-label">{fileName}</div>;
}
2018-04-12 21:21:37 +02:00
public renderIOSLabel() {
2018-04-27 23:25:04 +02:00
const {
i18n,
isIncoming,
isFromMe,
authorTitle,
authorProfileName,
} = this.props;
2018-04-12 21:21:37 +02: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 21:21:37 +02: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 23:25:04 +02:00
const authorProfileElement = authorProfileName ? (
<span className="profile-name">
~<Emojify text={authorProfileName} />
</span>
2018-04-27 23:25:04 +02:00
) : null;
return (
<div className={classnames(authorColor, 'author')}>
2018-04-27 23:25:04 +02:00
{isFromMe ? (
i18n('you')
) : (
<span>
<Emojify text={authorTitle} /> {authorProfileElement}
2018-04-27 23:25:04 +02:00
</span>
)}
</div>
);
}
public render() {
2018-04-27 23:25:04 +02: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 22:57:14 +02:00
!onClick ? 'no-click' : null
);
2018-04-12 09:33:52 +02:00
return (
<div onClick={onClick} className={classes}>
<div className="primary">
2018-04-12 21:21:37 +02:00
{this.renderIOSLabel()}
{this.renderAuthor()}
{this.renderText()}
</div>
{this.renderIconContainer()}
{this.renderClose()}
</div>
);
}
}