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

82 lines
2.4 KiB
TypeScript
Raw Normal View History

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