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

128 lines
3 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
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';
import type { LocalizerType } from '../../../types/Util';
import type { MediaItemType } from '../../../types/MediaItem';
import * as log from '../../../logging/log';
2018-04-12 20:23:26 +00:00
export type Props = {
mediaItem: MediaItemType;
2018-04-15 06:16:39 +00:00
onClick?: () => void;
2019-01-14 21:49:58 +00:00
i18n: LocalizerType;
};
2018-04-12 20:23:26 +00:00
type State = {
imageBroken: boolean;
};
export class MediaGridItem extends React.Component<Props, State> {
2019-01-14 21:49:58 +00:00
private readonly onImageErrorBound: () => void;
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 {
log.info(
'MediaGridItem: Image failed to load; failing over to placeholder'
);
this.setState({
imageBroken: true,
});
}
2020-09-14 19:51:27 +00:00
public renderContent(): JSX.Element | null {
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}
/>
);
2020-09-14 19:51:27 +00:00
}
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 override render(): JSX.Element {
2020-09-14 19:51:27 +00:00
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
}
}