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

146 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-04-13 20:25:52 +00:00
/**
* @prettier
*/
2018-04-11 15:57:31 +00:00
import React from 'react';
2018-04-13 00:56:05 +00:00
import moment from 'moment';
2018-04-15 01:11:40 +00:00
import { AttachmentSection } from './AttachmentSection';
import { AttachmentType } from './types/AttachmentType';
2018-04-13 00:56:05 +00:00
import { groupMessagesByDate } from './groupMessagesByDate';
import { ItemClickEvent } from './types/ItemClickEvent';
import { Message } from './types/Message';
2018-04-12 20:23:26 +00:00
2018-04-11 15:57:31 +00:00
interface Props {
2018-04-13 00:56:05 +00:00
documents: Array<Message>;
2018-04-12 20:23:26 +00:00
i18n: (key: string, values?: Array<string>) => string;
2018-04-13 00:56:05 +00:00
media: Array<Message>;
2018-04-15 06:16:39 +00:00
onItemClick?: (event: ItemClickEvent) => void;
2018-04-12 20:23:26 +00:00
}
interface State {
selectedTab: AttachmentType;
2018-04-11 15:57:31 +00:00
}
2018-04-13 00:56:05 +00:00
const MONTH_FORMAT = 'MMMM YYYY';
2018-04-24 16:41:41 +00:00
const COLOR_GRAY = '#f3f3f3';
2018-04-12 20:23:26 +00:00
const tabStyle = {
width: '100%',
2018-04-24 16:41:41 +00:00
backgroundColor: COLOR_GRAY,
2018-04-12 20:23:26 +00:00
padding: 20,
textAlign: 'center',
};
const styles = {
tabContainer: {
cursor: 'pointer',
display: 'flex',
width: '100%',
},
tab: {
default: tabStyle,
active: {
...tabStyle,
borderBottom: '2px solid #08f',
},
},
attachmentsContainer: {
padding: 20,
},
};
interface TabSelectEvent {
type: AttachmentType;
}
const Tab = ({
isSelected,
label,
onSelect,
type,
}: {
2018-04-13 20:25:52 +00:00
isSelected: boolean;
label: string;
onSelect?: (event: TabSelectEvent) => void;
type: AttachmentType;
2018-04-12 20:23:26 +00:00
}) => {
2018-04-13 20:25:52 +00:00
const handleClick = onSelect ? () => onSelect({ type }) : undefined;
2018-04-12 20:23:26 +00:00
return (
<div
style={isSelected ? styles.tab.active : styles.tab.default}
onClick={handleClick}
>
{label}
</div>
);
};
export class MediaGallery extends React.Component<Props, State> {
public state: State = {
selectedTab: 'media',
};
2018-04-11 15:57:31 +00:00
public render() {
2018-04-12 20:23:26 +00:00
const { selectedTab } = this.state;
2018-04-11 15:57:31 +00:00
return (
2018-04-12 20:23:26 +00:00
<div>
<div style={styles.tabContainer}>
<Tab
label="Media"
type="media"
isSelected={selectedTab === 'media'}
onSelect={this.handleTabSelect}
/>
<Tab
label="Documents"
type="documents"
isSelected={selectedTab === 'documents'}
onSelect={this.handleTabSelect}
/>
</div>
2018-04-13 20:25:52 +00:00
<div style={styles.attachmentsContainer}>{this.renderSections()}</div>
2018-04-12 20:23:26 +00:00
</div>
2018-04-11 15:57:31 +00:00
);
}
2018-04-12 20:23:26 +00:00
private handleTabSelect = (event: TabSelectEvent): void => {
2018-04-13 20:25:52 +00:00
this.setState({ selectedTab: event.type });
};
2018-04-13 00:56:05 +00:00
private renderSections() {
2018-04-15 06:16:39 +00:00
const { i18n, media, documents, onItemClick } = this.props;
2018-04-13 00:56:05 +00:00
const { selectedTab } = this.state;
const messages = selectedTab === 'media' ? media : documents;
const type = selectedTab;
if (!messages || messages.length === 0) {
return null;
}
const now = Date.now();
2018-04-13 20:27:06 +00:00
const sections = groupMessagesByDate(now, messages);
return sections.map(section => {
const first = section.messages[0];
const date = moment(first.received_at);
const header =
section.type === 'yearMonth'
? date.format(MONTH_FORMAT)
: i18n(section.type);
2018-04-13 00:56:05 +00:00
return (
2018-04-15 01:11:40 +00:00
<AttachmentSection
2018-04-13 00:56:05 +00:00
key={header}
header={header}
i18n={i18n}
type={type}
2018-04-13 20:27:06 +00:00
messages={section.messages}
2018-04-15 06:16:39 +00:00
onItemClick={onItemClick}
2018-04-13 00:56:05 +00:00
/>
);
});
2018-04-12 20:23:26 +00:00
}
2018-04-11 15:57:31 +00:00
}