signal-desktop/ts/components/LightboxGallery.tsx

102 lines
2.3 KiB
TypeScript
Raw Normal View History

/**
* @prettier
*/
import React from 'react';
import * as MIME from '../types/MIME';
import { Lightbox } from './Lightbox';
2018-04-26 20:48:08 +00:00
import { Message } from './conversation/media-gallery/types/Message';
import { Localizer } from '../types/Util';
interface Item {
2018-04-26 20:48:08 +00:00
objectURL?: string;
contentType: MIME.MIMEType | undefined;
}
interface Props {
close: () => void;
i18n: Localizer;
2018-04-26 20:48:08 +00:00
messages: Array<Message>;
onSave?: ({ message }: { message: Message }) => void;
selectedIndex: number;
}
interface State {
selectedIndex: number;
}
2018-04-26 20:48:08 +00:00
const messageToItem = (message: Message): Item => ({
objectURL: message.objectURL,
2018-04-26 20:48:08 +00:00
contentType: message.attachments[0].contentType,
});
export class LightboxGallery extends React.Component<Props, State> {
public static defaultProps: Partial<Props> = {
selectedIndex: 0,
};
constructor(props: Props) {
super(props);
this.state = {
selectedIndex: this.props.selectedIndex,
};
}
public render() {
const { close, messages, onSave, i18n } = this.props;
const { selectedIndex } = this.state;
2018-04-26 20:48:08 +00:00
const selectedMessage: Message = messages[selectedIndex];
const selectedItem = messageToItem(selectedMessage);
const firstIndex = 0;
const onPrevious =
selectedIndex > firstIndex ? this.handlePrevious : undefined;
2018-04-26 20:48:08 +00:00
const lastIndex = messages.length - 1;
const onNext = selectedIndex < lastIndex ? this.handleNext : undefined;
const objectURL = selectedItem.objectURL || 'images/alert-outline.svg';
2018-04-26 20:48:08 +00:00
return (
<Lightbox
close={close}
onPrevious={onPrevious}
onNext={onNext}
2018-04-26 20:48:08 +00:00
onSave={onSave ? this.handleSave : undefined}
objectURL={objectURL}
contentType={selectedItem.contentType}
i18n={i18n}
/>
);
}
private handlePrevious = () => {
this.setState(prevState => ({
selectedIndex: Math.max(prevState.selectedIndex - 1, 0),
}));
};
private handleNext = () => {
this.setState((prevState, props) => ({
selectedIndex: Math.min(
prevState.selectedIndex + 1,
2018-04-26 20:48:08 +00:00
props.messages.length - 1
),
}));
};
2018-04-26 20:48:08 +00:00
private handleSave = () => {
const { messages, onSave } = this.props;
if (!onSave) {
return;
}
const { selectedIndex } = this.state;
const message = messages[selectedIndex];
onSave({ message });
};
}