2021-01-12 00:59:46 +00:00
|
|
|
// Copyright 2018-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-02 01:48:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { Image } from './Image';
|
|
|
|
import { StagedGenericAttachment } from './StagedGenericAttachment';
|
2019-01-15 17:33:23 +00:00
|
|
|
import { StagedPlaceholderAttachment } from './StagedPlaceholderAttachment';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { LocalizerType } from '../../types/Util';
|
|
|
|
import {
|
|
|
|
areAllAttachmentsVisual,
|
|
|
|
AttachmentType,
|
|
|
|
getUrl,
|
2021-01-12 00:59:46 +00:00
|
|
|
isImageAttachment,
|
2019-01-14 21:49:58 +00:00
|
|
|
isVideoAttachment,
|
|
|
|
} from '../../types/Attachment';
|
2018-12-02 01:48:53 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2018-12-02 01:48:53 +00:00
|
|
|
attachments: Array<AttachmentType>;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2021-04-27 22:35:35 +00:00
|
|
|
onAddAttachment?: () => void;
|
|
|
|
onClickAttachment?: (attachment: AttachmentType) => void;
|
|
|
|
onClose?: () => void;
|
2018-12-02 01:48:53 +00:00
|
|
|
onCloseAttachment: (attachment: AttachmentType) => void;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-12-02 01:48:53 +00:00
|
|
|
|
|
|
|
const IMAGE_WIDTH = 120;
|
|
|
|
const IMAGE_HEIGHT = 120;
|
|
|
|
|
2021-01-12 00:59:46 +00:00
|
|
|
// This is a 1x1 black square.
|
|
|
|
const BLANK_VIDEO_THUMBNAIL =
|
|
|
|
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVR42mNiAAAABgADm78GJQAAAABJRU5ErkJggg==';
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
export const AttachmentList = ({
|
|
|
|
attachments,
|
|
|
|
i18n,
|
|
|
|
onAddAttachment,
|
|
|
|
onClickAttachment,
|
|
|
|
onCloseAttachment,
|
|
|
|
onClose,
|
|
|
|
}: Props): JSX.Element | null => {
|
|
|
|
if (!attachments.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
const allVisualAttachments = areAllAttachmentsVisual(attachments);
|
2018-12-02 01:48:53 +00:00
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
return (
|
|
|
|
<div className="module-attachments">
|
2021-04-27 22:35:35 +00:00
|
|
|
{onClose && attachments.length > 1 ? (
|
2020-09-14 19:51:27 +00:00
|
|
|
<div className="module-attachments__header">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={onClose}
|
|
|
|
className="module-attachments__close-button"
|
|
|
|
aria-label={i18n('close')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
<div className="module-attachments__rail">
|
|
|
|
{(attachments || []).map((attachment, index) => {
|
2021-01-12 00:59:46 +00:00
|
|
|
const url = getUrl(attachment);
|
|
|
|
|
|
|
|
const key = url || attachment.fileName || index;
|
|
|
|
|
|
|
|
const isImage = isImageAttachment(attachment);
|
|
|
|
const isVideo = isVideoAttachment(attachment);
|
|
|
|
|
|
|
|
if (isImage || isVideo) {
|
2020-09-14 19:51:27 +00:00
|
|
|
const clickCallback =
|
|
|
|
attachments.length > 1 ? onClickAttachment : undefined;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2021-01-12 00:59:46 +00:00
|
|
|
const imageUrl =
|
|
|
|
isVideo && !attachment.screenshot ? BLANK_VIDEO_THUMBNAIL : url;
|
|
|
|
|
2018-12-02 01:48:53 +00:00
|
|
|
return (
|
2020-09-14 19:51:27 +00:00
|
|
|
<Image
|
2021-01-12 00:59:46 +00:00
|
|
|
key={key}
|
2020-09-14 19:51:27 +00:00
|
|
|
alt={i18n('stagedImageAttachment', [
|
2021-06-17 17:15:10 +00:00
|
|
|
attachment.fileName || url || index.toString(),
|
2020-09-14 19:51:27 +00:00
|
|
|
])}
|
2018-12-02 01:48:53 +00:00
|
|
|
i18n={i18n}
|
2020-09-14 19:51:27 +00:00
|
|
|
attachment={attachment}
|
|
|
|
softCorners
|
2021-01-12 00:59:46 +00:00
|
|
|
playIconOverlay={isVideo}
|
2020-09-14 19:51:27 +00:00
|
|
|
height={IMAGE_HEIGHT}
|
|
|
|
width={IMAGE_WIDTH}
|
2021-01-12 00:59:46 +00:00
|
|
|
url={imageUrl}
|
2020-09-14 19:51:27 +00:00
|
|
|
closeButton
|
|
|
|
onClick={clickCallback}
|
|
|
|
onClickClose={onCloseAttachment}
|
|
|
|
onError={() => {
|
|
|
|
onCloseAttachment(attachment);
|
|
|
|
}}
|
2018-12-02 01:48:53 +00:00
|
|
|
/>
|
|
|
|
);
|
2020-09-14 19:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StagedGenericAttachment
|
2021-01-12 00:59:46 +00:00
|
|
|
key={key}
|
2020-09-14 19:51:27 +00:00
|
|
|
attachment={attachment}
|
2019-11-07 21:36:16 +00:00
|
|
|
i18n={i18n}
|
2020-09-14 19:51:27 +00:00
|
|
|
onClose={onCloseAttachment}
|
2019-11-07 21:36:16 +00:00
|
|
|
/>
|
2020-09-14 19:51:27 +00:00
|
|
|
);
|
|
|
|
})}
|
2021-04-27 22:35:35 +00:00
|
|
|
{allVisualAttachments && onAddAttachment ? (
|
2020-09-14 19:51:27 +00:00
|
|
|
<StagedPlaceholderAttachment onClick={onAddAttachment} i18n={i18n} />
|
|
|
|
) : null}
|
2018-12-02 01:48:53 +00:00
|
|
|
</div>
|
2020-09-14 19:51:27 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|