2018-04-12 20:23:26 +00:00
|
|
|
import React from 'react';
|
2018-07-20 23:37:57 +00:00
|
|
|
import classNames from 'classnames';
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2018-07-18 23:00:51 +00:00
|
|
|
import {
|
|
|
|
isImageTypeSupported,
|
|
|
|
isVideoTypeSupported,
|
|
|
|
} from '../../../util/GoogleChrome';
|
2018-04-25 20:42:08 +00:00
|
|
|
import { Message } from './types/Message';
|
2018-07-18 00:15:34 +00:00
|
|
|
import { Localizer } from '../../../types/Util';
|
2018-04-12 20:23:26 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
message: Message;
|
2018-04-15 06:16:39 +00:00
|
|
|
onClick?: () => void;
|
2018-07-18 00:15:34 +00:00
|
|
|
i18n: Localizer;
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 23:37:57 +00:00
|
|
|
interface State {
|
|
|
|
imageBroken: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MediaGridItem extends React.Component<Props, State> {
|
|
|
|
private onImageErrorBound: () => void;
|
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
imageBroken: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onImageErrorBound = this.onImageError.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public onImageError() {
|
2018-07-21 19:00:08 +00:00
|
|
|
// tslint:disable-next-line no-console
|
|
|
|
console.log(
|
|
|
|
'MediaGridItem: Image failed to load; failing over to placeholder'
|
|
|
|
);
|
2018-07-20 23:37:57 +00:00
|
|
|
this.setState({
|
|
|
|
imageBroken: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-12 20:23:26 +00:00
|
|
|
public renderContent() {
|
2018-07-18 00:15:34 +00:00
|
|
|
const { message, i18n } = this.props;
|
2018-07-20 23:37:57 +00:00
|
|
|
const { imageBroken } = this.state;
|
2018-07-18 23:00:51 +00:00
|
|
|
const { attachments } = message;
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2018-07-18 23:00:51 +00:00
|
|
|
if (!attachments || !attachments.length) {
|
2018-04-15 06:53:34 +00:00
|
|
|
return null;
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 23:00:51 +00:00
|
|
|
const first = attachments[0];
|
|
|
|
const { contentType } = first;
|
|
|
|
|
|
|
|
if (contentType && isImageTypeSupported(contentType)) {
|
2018-07-20 23:37:57 +00:00
|
|
|
if (imageBroken || !message.thumbnailObjectUrl) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-media-grid-item__icon',
|
|
|
|
'module-media-grid-item__icon-image'
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-18 23:00:51 +00:00
|
|
|
return (
|
|
|
|
<img
|
|
|
|
alt={i18n('lightboxImageAlt')}
|
|
|
|
className="module-media-grid-item__image"
|
|
|
|
src={message.thumbnailObjectUrl}
|
2018-07-20 23:37:57 +00:00
|
|
|
onError={this.onImageErrorBound}
|
2018-07-18 23:00:51 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (contentType && isVideoTypeSupported(contentType)) {
|
2018-07-20 23:37:57 +00:00
|
|
|
if (imageBroken || !message.thumbnailObjectUrl) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-media-grid-item__icon',
|
|
|
|
'module-media-grid-item__icon-video'
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-18 23:00:51 +00:00
|
|
|
return (
|
|
|
|
<div className="module-media-grid-item__image-container">
|
|
|
|
<img
|
|
|
|
alt={i18n('lightboxImageAlt')}
|
|
|
|
className="module-media-grid-item__image"
|
|
|
|
src={message.thumbnailObjectUrl}
|
2018-07-20 23:37:57 +00:00
|
|
|
onError={this.onImageErrorBound}
|
2018-07-18 23:00:51 +00:00
|
|
|
/>
|
|
|
|
<div className="module-media-grid-item__circle-overlay">
|
|
|
|
<div className="module-media-grid-item__play-overlay" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-07-20 23:37:57 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-media-grid-item__icon',
|
|
|
|
'module-media-grid-item__icon-generic'
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-04-15 06:16:39 +00:00
|
|
|
return (
|
2018-07-18 00:15:34 +00:00
|
|
|
<div
|
|
|
|
className="module-media-grid-item"
|
|
|
|
role="button"
|
|
|
|
onClick={this.props.onClick}
|
|
|
|
>
|
2018-04-15 06:16:39 +00:00
|
|
|
{this.renderContent()}
|
|
|
|
</div>
|
|
|
|
);
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
}
|