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

146 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-04-11 15:57:31 +00:00
import React from 'react';
2018-07-18 00:15:34 +00:00
import classNames from 'classnames';
2018-04-11 15:57:31 +00:00
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-26 23:06:48 +00:00
import { EmptyState } from './EmptyState';
2018-04-13 00:56:05 +00:00
import { groupMessagesByDate } from './groupMessagesByDate';
import { ItemClickEvent } from './types/ItemClickEvent';
import { Message } from './types/Message';
2018-04-26 23:06:48 +00:00
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
2018-04-11 15:57:31 +00:00
interface Props {
2018-04-13 00:56:05 +00:00
documents: Array<Message>;
2018-07-18 00:15:34 +00:00
i18n: Localizer;
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-12 20:23:26 +00:00
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
}) => {
const handleClick = onSelect
? () => {
onSelect({ type });
}
: undefined;
2018-04-12 20:23:26 +00:00
return (
<div
2018-07-18 00:15:34 +00:00
className={classNames(
'module-media-gallery__tab',
isSelected ? 'module-media-gallery__tab--active' : null
)}
2018-04-12 20:23:26 +00:00
onClick={handleClick}
role="tab"
2018-04-12 20:23:26 +00:00
>
{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-07-18 00:15:34 +00:00
<div className="module-media-gallery">
<div className="module-media-gallery__tab-container">
2018-04-12 20:23:26 +00:00
<Tab
label="Media"
type="media"
isSelected={selectedTab === 'media'}
onSelect={this.handleTabSelect}
/>
<Tab
label="Documents"
type="documents"
isSelected={selectedTab === 'documents'}
onSelect={this.handleTabSelect}
/>
</div>
2018-07-18 00:15:34 +00:00
<div className="module-media-gallery__content">
{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) {
2018-04-26 23:06:48 +00:00
const label = (() => {
switch (type) {
case 'media':
return i18n('mediaEmptyState');
case 'documents':
return i18n('documentsEmptyState');
default:
throw missingCaseError(type);
}
})();
2018-04-26 23:06:48 +00:00
return <EmptyState data-test="EmptyState" label={label} />;
2018-04-13 00:56:05 +00:00
}
const now = Date.now();
2018-04-26 23:06:13 +00:00
const sections = groupMessagesByDate(now, messages).map(section => {
2018-04-13 20:27:06 +00:00
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-26 23:06:13 +00:00
2018-07-18 00:15:34 +00:00
return <div className="module-media-gallery__sections">{sections}</div>;
2018-04-12 20:23:26 +00:00
}
2018-04-11 15:57:31 +00:00
}