signal-desktop/ts/components/conversation/media-gallery/AttachmentSection.tsx

82 lines
2.4 KiB
TypeScript
Raw Normal View History

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';
import { ItemClickEvent } from './types/ItemClickEvent';
2018-04-14 21:11:40 -04:00
import { MediaGridItem } from './MediaGridItem';
import { MediaItemType } from '../../LightboxGallery';
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
export type Props = {
2019-01-14 13:49:58 -08:00
i18n: LocalizerType;
2018-04-12 16:23:26 -04:00
header?: string;
type: 'media' | 'documents';
mediaItems: Array<MediaItemType>;
2018-04-15 02:16:39 -04:00
onItemClick?: (event: ItemClickEvent) => void;
};
2018-04-12 16:23:26 -04: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() {
const { i18n, mediaItems, type } = this.props;
2018-04-12 20:56:05 -04: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
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
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
key={`${message.id}-${index}`}
fileName={attachment.fileName}
fileSize={attachment.size}
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) => () => {
const { onItemClick, type } = this.props;
const { message, attachment } = mediaItem;
2018-04-15 02:16:39 -04:00
if (!onItemClick) {
return;
}
onItemClick({ type, message, attachment });
2018-04-15 02:16:39 -04:00
};
2018-04-12 16:23:26 -04:00
}