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