2018-12-02 02:48:53 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
2021-04-22 10:03:58 +02:00
|
|
|
import { isImageTypeSupported, isVideoTypeSupported } from '../../util/GoogleChrome';
|
2018-12-02 02:48:53 +01:00
|
|
|
import { Image } from './Image';
|
|
|
|
import { StagedGenericAttachment } from './StagedGenericAttachment';
|
2019-01-15 18:33:23 +01:00
|
|
|
import { StagedPlaceholderAttachment } from './StagedPlaceholderAttachment';
|
2019-01-14 22:49:58 +01:00
|
|
|
import {
|
|
|
|
areAllAttachmentsVisual,
|
|
|
|
AttachmentType,
|
|
|
|
getUrl,
|
|
|
|
isVideoAttachment,
|
|
|
|
} from '../../types/Attachment';
|
2018-12-02 02:48:53 +01:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
attachments: Array<AttachmentType>;
|
|
|
|
// onError: () => void;
|
|
|
|
onClickAttachment: (attachment: AttachmentType) => void;
|
|
|
|
onCloseAttachment: (attachment: AttachmentType) => void;
|
2019-01-15 18:33:23 +01:00
|
|
|
onAddAttachment: () => void;
|
2018-12-02 02:48:53 +01:00
|
|
|
onClose: () => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IMAGE_WIDTH = 120;
|
|
|
|
const IMAGE_HEIGHT = 120;
|
|
|
|
|
|
|
|
export class AttachmentList extends React.Component<Props> {
|
|
|
|
// tslint:disable-next-line max-func-body-length */
|
|
|
|
public render() {
|
|
|
|
const {
|
|
|
|
attachments,
|
2019-01-15 18:33:23 +01:00
|
|
|
onAddAttachment,
|
2018-12-02 02:48:53 +01:00
|
|
|
onClickAttachment,
|
|
|
|
onCloseAttachment,
|
|
|
|
onClose,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (!attachments.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-15 18:33:23 +01:00
|
|
|
const allVisualAttachments = areAllAttachmentsVisual(attachments);
|
|
|
|
|
2018-12-02 02:48:53 +01:00
|
|
|
return (
|
|
|
|
<div className="module-attachments">
|
|
|
|
{attachments.length > 1 ? (
|
|
|
|
<div className="module-attachments__header">
|
2021-04-22 10:03:58 +02:00
|
|
|
<div role="button" onClick={onClose} className="module-attachments__close-button" />
|
2018-12-02 02:48:53 +01:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<div className="module-attachments__rail">
|
|
|
|
{(attachments || []).map((attachment, index) => {
|
|
|
|
const { contentType } = attachment;
|
2021-04-22 10:03:58 +02:00
|
|
|
if (isImageTypeSupported(contentType) || isVideoTypeSupported(contentType)) {
|
|
|
|
const imageKey = getUrl(attachment) || attachment.fileName || index;
|
|
|
|
const clickCallback = attachments.length > 1 ? onClickAttachment : undefined;
|
2019-01-14 22:49:58 +01:00
|
|
|
|
2018-12-02 02:48:53 +01:00
|
|
|
return (
|
|
|
|
<Image
|
2019-01-14 22:49:58 +01:00
|
|
|
key={imageKey}
|
2021-04-22 10:03:58 +02:00
|
|
|
alt={window.i18n('stagedImageAttachment', [attachment.fileName])}
|
2020-10-20 04:20:09 +02:00
|
|
|
i18n={window.i18n}
|
2018-12-02 02:48:53 +01:00
|
|
|
attachment={attachment}
|
|
|
|
softCorners={true}
|
|
|
|
playIconOverlay={isVideoAttachment(attachment)}
|
|
|
|
height={IMAGE_HEIGHT}
|
|
|
|
width={IMAGE_WIDTH}
|
|
|
|
url={getUrl(attachment)}
|
|
|
|
closeButton={true}
|
2019-01-14 22:49:58 +01:00
|
|
|
onClick={clickCallback}
|
2018-12-02 02:48:53 +01:00
|
|
|
onClickClose={onCloseAttachment}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-22 10:03:58 +02:00
|
|
|
const genericKey = getUrl(attachment) || attachment.fileName || index;
|
2019-01-14 22:49:58 +01:00
|
|
|
|
2018-12-02 02:48:53 +01:00
|
|
|
return (
|
|
|
|
<StagedGenericAttachment
|
2019-01-14 22:49:58 +01:00
|
|
|
key={genericKey}
|
2018-12-02 02:48:53 +01:00
|
|
|
attachment={attachment}
|
2020-10-20 04:20:09 +02:00
|
|
|
i18n={window.i18n}
|
2018-12-02 02:48:53 +01:00
|
|
|
onClose={onCloseAttachment}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
2021-04-22 10:03:58 +02:00
|
|
|
{allVisualAttachments ? <StagedPlaceholderAttachment onClick={onAddAttachment} /> : null}
|
2018-12-02 02:48:53 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|