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

93 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-04-13 20:25:52 +00:00
/**
* @prettier
*/
2018-04-12 20:23:26 +00:00
import React from 'react';
2018-04-15 01:11:40 +00:00
import { DocumentListItem } from './DocumentListItem';
2018-04-15 06:16:39 +00:00
import { ItemClickEvent } from './events/ItemClickEvent';
2018-04-15 01:11:40 +00:00
import { MediaGridItem } from './MediaGridItem';
2018-04-12 20:23:26 +00:00
import { Message } from './propTypes/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: 'media' | 'documents';
messages: Array<Message>;
2018-04-15 06:16:39 +00:00
onItemClick?: (event: ItemClickEvent) => void;
2018-04-12 20:23:26 +00:00
}
2018-04-15 01:11:40 +00:00
export class AttachmentSection extends React.Component<Props, {}> {
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
2018-04-13 20:25:52 +00:00
return messages.map(message => {
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
key={message.received_at}
message={message}
onClick={onClick}
/>
);
2018-04-13 00:56:05 +00:00
case 'documents':
return (
2018-04-15 01:11:40 +00:00
<DocumentListItem
key={message.received_at}
2018-04-13 00:56:05 +00:00
i18n={i18n}
fileSize={firstAttachment.size}
fileName={firstAttachment.fileName}
timestamp={message.received_at}
2018-04-15 06:16:39 +00:00
onClick={onClick}
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 } = this.props;
if (!onItemClick) {
return;
}
onItemClick({ message });
};
2018-04-12 20:23:26 +00:00
public render() {
const { header } = this.props;
return (
<div style={styles.container}>
2018-04-13 20:47:35 +00:00
<h2 style={styles.header}>{header}</h2>
2018-04-13 20:25:52 +00:00
<div style={styles.itemContainer}>{this.renderItems()}</div>
2018-04-12 20:23:26 +00:00
</div>
);
}
}