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

93 lines
2.3 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-04-13 00:56:05 +00:00
2018-04-12 20:23:26 +00:00
const styles = {
container: {
width: '100%',
},
2018-04-13 20:47:35 +00:00
header: {
fontSize: 14,
fontWeight: 'normal',
lineHeight: '28px',
} as React.CSSProperties,
2018-04-12 20:23:26 +00:00
itemContainer: {
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'flex-start',
alignItems: 'flex-start',
} as React.CSSProperties,
};
interface Props {
i18n: (value: string) => string;
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 (
<div style={styles.container}>
<h2 style={styles.header}>{header}</h2>
<div style={styles.itemContainer}>{this.renderItems()}</div>
</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-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}
i18n={i18n}
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
}