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

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2018-04-12 20:23:26 +00:00
import React from 'react';
import { AttachmentType } from './types/AttachmentType';
2018-04-15 01:11:40 +00:00
import { DocumentListItem } from './DocumentListItem';
import { ItemClickEvent } from './types/ItemClickEvent';
2018-04-15 01:11:40 +00:00
import { MediaGridItem } from './MediaGridItem';
import { Message } from './types/Message';
import { missingCaseError } from '../../../util/missingCaseError';
2018-07-18 00:15:34 +00:00
import { Localizer } from '../../../types/Util';
2018-04-12 20:23:26 +00:00
interface Props {
2018-07-18 00:15:34 +00:00
i18n: Localizer;
2018-04-12 20:23:26 +00:00
header?: string;
type: AttachmentType;
2018-04-12 20:23:26 +00:00
messages: Array<Message>;
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> {
2018-04-15 06:53:19 +00:00
public render() {
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-04-12 20:23:26 +00:00
const { i18n, messages, type } = this.props;
2018-04-13 00:56:05 +00:00
return messages.map((message, index, array) => {
const shouldShowSeparator = index < array.length - 1;
2018-04-13 00:56:05 +00:00
const { attachments } = message;
const firstAttachment = attachments[0];
2018-04-15 06:16:39 +00:00
const onClick = this.createClickHandler(message);
2018-04-13 00:56:05 +00:00
switch (type) {
case 'media':
2018-04-15 06:16:39 +00:00
return (
<MediaGridItem
2018-04-24 16:23:03 +00:00
key={message.id}
2018-04-15 06:16:39 +00:00
message={message}
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-04-24 16:23:03 +00:00
key={message.id}
2018-04-13 00:56:05 +00:00
fileName={firstAttachment.fileName}
fileSize={firstAttachment.size}
shouldShowSeparator={shouldShowSeparator}
2018-04-15 06:16:39 +00:00
onClick={onClick}
timestamp={message.received_at}
2018-04-13 00:56:05 +00:00
/>
);
default:
return missingCaseError(type);
}
});
2018-04-12 20:23:26 +00:00
}
2018-04-15 06:16:39 +00:00
private createClickHandler = (message: Message) => () => {
const { onItemClick, type } = this.props;
2018-04-15 06:16:39 +00:00
if (!onItemClick) {
return;
}
onItemClick({ type, message });
2018-04-15 06:16:39 +00:00
};
2018-04-12 20:23:26 +00:00
}