2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2018-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
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';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { LocalizerType } from '../../../types/Util';
|
2018-11-14 18:47:19 +00:00
|
|
|
import { MediaItemType } from '../../LightboxGallery';
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2018-11-14 18:47:19 +00:00
|
|
|
mediaItem: MediaItemType;
|
2018-04-15 06:16:39 +00:00
|
|
|
onClick?: () => void;
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type State = {
|
2018-07-20 23:37:57 +00:00
|
|
|
imageBroken: boolean;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2018-07-20 23:37:57 +00:00
|
|
|
|
|
|
|
export class MediaGridItem extends React.Component<Props, State> {
|
2019-01-14 21:49:58 +00:00
|
|
|
private readonly onImageErrorBound: () => void;
|
2018-07-20 23:37:57 +00:00
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
imageBroken: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onImageErrorBound = this.onImageError.bind(this);
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public onImageError(): void {
|
|
|
|
window.log.info(
|
2018-07-21 19:00:08 +00:00
|
|
|
'MediaGridItem: Image failed to load; failing over to placeholder'
|
|
|
|
);
|
2018-07-20 23:37:57 +00:00
|
|
|
this.setState({
|
|
|
|
imageBroken: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public renderContent(): JSX.Element | null {
|
2018-11-14 18:47:19 +00:00
|
|
|
const { mediaItem, i18n } = this.props;
|
2018-07-20 23:37:57 +00:00
|
|
|
const { imageBroken } = this.state;
|
2018-11-14 18:47:19 +00:00
|
|
|
const { attachment, contentType } = mediaItem;
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2018-11-14 18:47:19 +00:00
|
|
|
if (!attachment) {
|
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
|
|
|
if (contentType && isImageTypeSupported(contentType)) {
|
2018-11-14 18:47:19 +00:00
|
|
|
if (imageBroken || !mediaItem.thumbnailObjectUrl) {
|
2018-07-20 23:37:57 +00:00
|
|
|
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"
|
2018-11-14 18:47:19 +00:00
|
|
|
src={mediaItem.thumbnailObjectUrl}
|
2018-07-20 23:37:57 +00:00
|
|
|
onError={this.onImageErrorBound}
|
2018-07-18 23:00:51 +00:00
|
|
|
/>
|
|
|
|
);
|
2020-09-14 19:51:27 +00:00
|
|
|
}
|
|
|
|
if (contentType && isVideoTypeSupported(contentType)) {
|
2018-11-14 18:47:19 +00:00
|
|
|
if (imageBroken || !mediaItem.thumbnailObjectUrl) {
|
2018-07-20 23:37:57 +00:00
|
|
|
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"
|
2018-11-14 18:47:19 +00:00
|
|
|
src={mediaItem.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
|
|
|
}
|
|
|
|
|
2020-09-14 19:51:27 +00:00
|
|
|
public render(): JSX.Element {
|
|
|
|
const { onClick } = this.props;
|
|
|
|
|
2018-04-15 06:16:39 +00:00
|
|
|
return (
|
2020-09-14 19:51:27 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="module-media-grid-item"
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
2018-04-15 06:16:39 +00:00
|
|
|
{this.renderContent()}
|
2019-11-07 21:36:16 +00:00
|
|
|
</button>
|
2018-04-15 06:16:39 +00:00
|
|
|
);
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
}
|