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

122 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-04-12 20:23:26 +00:00
import React from 'react';
import classNames from 'classnames';
2018-04-12 20:23:26 +00:00
import {
isImageTypeSupported,
isVideoTypeSupported,
} from '../../../util/GoogleChrome';
2018-07-18 00:15:34 +00:00
import { Localizer } from '../../../types/Util';
import { MediaItemType } from '../../LightboxGallery';
2018-04-12 20:23:26 +00:00
interface Props {
mediaItem: MediaItemType;
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
}
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() {
// tslint:disable-next-line no-console
console.log(
'MediaGridItem: Image failed to load; failing over to placeholder'
);
this.setState({
imageBroken: true,
});
}
2018-04-12 20:23:26 +00:00
public renderContent() {
const { mediaItem, i18n } = this.props;
const { imageBroken } = this.state;
const { attachment, contentType } = mediaItem;
2018-04-12 20:23:26 +00:00
if (!attachment) {
2018-04-15 06:53:34 +00:00
return null;
2018-04-12 20:23:26 +00:00
}
if (contentType && isImageTypeSupported(contentType)) {
if (imageBroken || !mediaItem.thumbnailObjectUrl) {
return (
<div
className={classNames(
'module-media-grid-item__icon',
'module-media-grid-item__icon-image'
)}
/>
);
}
return (
<img
alt={i18n('lightboxImageAlt')}
className="module-media-grid-item__image"
src={mediaItem.thumbnailObjectUrl}
onError={this.onImageErrorBound}
/>
);
} else if (contentType && isVideoTypeSupported(contentType)) {
if (imageBroken || !mediaItem.thumbnailObjectUrl) {
return (
<div
className={classNames(
'module-media-grid-item__icon',
'module-media-grid-item__icon-video'
)}
/>
);
}
return (
<div className="module-media-grid-item__image-container">
<img
alt={i18n('lightboxImageAlt')}
className="module-media-grid-item__image"
src={mediaItem.thumbnailObjectUrl}
onError={this.onImageErrorBound}
/>
<div className="module-media-grid-item__circle-overlay">
<div className="module-media-grid-item__play-overlay" />
</div>
</div>
);
}
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
}
}