2020-10-30 15:34:04 -05:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-04-12 16:23:26 -04:00
|
|
|
import React from 'react';
|
|
|
|
|
2018-04-14 21:11:40 -04:00
|
|
|
import { DocumentListItem } from './DocumentListItem';
|
2018-04-25 16:42:08 -04:00
|
|
|
import { ItemClickEvent } from './types/ItemClickEvent';
|
2018-04-14 21:11:40 -04:00
|
|
|
import { MediaGridItem } from './MediaGridItem';
|
2018-11-14 10:47:19 -08:00
|
|
|
import { MediaItemType } from '../../LightboxGallery';
|
2018-04-13 17:54:53 -04:00
|
|
|
import { missingCaseError } from '../../../util/missingCaseError';
|
2019-01-14 13:49:58 -08:00
|
|
|
import { LocalizerType } from '../../../types/Util';
|
2021-03-04 16:44:57 -05:00
|
|
|
import { getMessageTimestamp } from '../../../util/getMessageTimestamp';
|
2018-04-12 16:23:26 -04:00
|
|
|
|
2021-01-14 12:07:05 -06:00
|
|
|
export type Props = {
|
2019-01-14 13:49:58 -08:00
|
|
|
i18n: LocalizerType;
|
2018-04-12 16:23:26 -04:00
|
|
|
header?: string;
|
2018-11-14 10:47:19 -08:00
|
|
|
type: 'media' | 'documents';
|
|
|
|
mediaItems: Array<MediaItemType>;
|
2018-04-15 02:16:39 -04:00
|
|
|
onItemClick?: (event: ItemClickEvent) => void;
|
2021-01-14 12:07:05 -06:00
|
|
|
};
|
2018-04-12 16:23:26 -04:00
|
|
|
|
2018-05-22 12:31:43 -07:00
|
|
|
export class AttachmentSection extends React.Component<Props> {
|
2020-09-14 12:51:27 -07:00
|
|
|
public render(): JSX.Element {
|
2018-04-15 02:53:19 -04:00
|
|
|
const { header } = this.props;
|
|
|
|
|
|
|
|
return (
|
2018-07-17 17:15:34 -07:00
|
|
|
<div className="module-attachment-section">
|
|
|
|
<h2 className="module-attachment-section__header">{header}</h2>
|
|
|
|
<div className="module-attachment-section__items">
|
|
|
|
{this.renderItems()}
|
|
|
|
</div>
|
2018-04-15 02:53:19 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-15 02:16:39 -04:00
|
|
|
private renderItems() {
|
2018-11-14 10:47:19 -08:00
|
|
|
const { i18n, mediaItems, type } = this.props;
|
2018-04-12 20:56:05 -04:00
|
|
|
|
2018-11-14 10:47:19 -08:00
|
|
|
return mediaItems.map((mediaItem, position, array) => {
|
|
|
|
const shouldShowSeparator = position < array.length - 1;
|
|
|
|
const { message, index, attachment } = mediaItem;
|
2018-04-12 20:56:05 -04:00
|
|
|
|
2018-11-14 10:47:19 -08:00
|
|
|
const onClick = this.createClickHandler(mediaItem);
|
2018-04-12 20:56:05 -04:00
|
|
|
switch (type) {
|
|
|
|
case 'media':
|
2018-04-15 02:16:39 -04:00
|
|
|
return (
|
|
|
|
<MediaGridItem
|
2018-11-14 10:47:19 -08:00
|
|
|
key={`${message.id}-${index}`}
|
|
|
|
mediaItem={mediaItem}
|
2018-04-15 02:16:39 -04:00
|
|
|
onClick={onClick}
|
2018-07-17 17:15:34 -07:00
|
|
|
i18n={i18n}
|
2018-04-15 02:16:39 -04:00
|
|
|
/>
|
|
|
|
);
|
2018-04-12 20:56:05 -04:00
|
|
|
case 'documents':
|
|
|
|
return (
|
2018-04-14 21:11:40 -04:00
|
|
|
<DocumentListItem
|
2018-11-14 10:47:19 -08:00
|
|
|
key={`${message.id}-${index}`}
|
|
|
|
fileName={attachment.fileName}
|
|
|
|
fileSize={attachment.size}
|
2018-05-08 15:56:10 -04:00
|
|
|
shouldShowSeparator={shouldShowSeparator}
|
2018-04-15 02:16:39 -04:00
|
|
|
onClick={onClick}
|
2021-03-04 16:44:57 -05:00
|
|
|
timestamp={getMessageTimestamp(message)}
|
2018-04-12 20:56:05 -04:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return missingCaseError(type);
|
|
|
|
}
|
|
|
|
});
|
2018-04-12 16:23:26 -04:00
|
|
|
}
|
|
|
|
|
2019-01-14 13:49:58 -08:00
|
|
|
private readonly createClickHandler = (mediaItem: MediaItemType) => () => {
|
2018-04-25 16:42:08 -04:00
|
|
|
const { onItemClick, type } = this.props;
|
2018-11-14 10:47:19 -08:00
|
|
|
const { message, attachment } = mediaItem;
|
|
|
|
|
2018-04-15 02:16:39 -04:00
|
|
|
if (!onItemClick) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:47:19 -08:00
|
|
|
onItemClick({ type, message, attachment });
|
2018-04-15 02:16:39 -04:00
|
|
|
};
|
2018-04-12 16:23:26 -04:00
|
|
|
}
|