2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
import React, { useCallback, useState } from 'react';
|
2018-07-20 23:37:57 +00:00
|
|
|
import classNames from 'classnames';
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2018-07-18 23:00:51 +00:00
|
|
|
import {
|
|
|
|
isImageTypeSupported,
|
|
|
|
isVideoTypeSupported,
|
|
|
|
} from '../../../util/GoogleChrome';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../../../types/Util';
|
|
|
|
import type { MediaItemType } from '../../../types/MediaItem';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../../../logging/log';
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
export type Props = {
|
2023-01-13 20:07:26 +00:00
|
|
|
mediaItem: ReadonlyDeep<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
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
function MediaGridItemContent(props: Props) {
|
|
|
|
const { mediaItem, i18n } = props;
|
|
|
|
const { attachment, contentType } = mediaItem;
|
2018-07-20 23:37:57 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
const [imageBroken, setImageBroken] = useState(false);
|
2018-07-20 23:37:57 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
const handleImageError = useCallback(() => {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2018-07-21 19:00:08 +00:00
|
|
|
'MediaGridItem: Image failed to load; failing over to placeholder'
|
|
|
|
);
|
2023-04-12 23:17:56 +00:00
|
|
|
setImageBroken(true);
|
|
|
|
}, []);
|
2018-04-12 20:23:26 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
if (!attachment) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-20 23:37:57 +00:00
|
|
|
|
2023-04-12 23:17:56 +00:00
|
|
|
if (contentType && isImageTypeSupported(contentType)) {
|
|
|
|
if (imageBroken || !mediaItem.thumbnailObjectUrl) {
|
2018-07-18 23:00:51 +00:00
|
|
|
return (
|
2023-04-12 23:17:56 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-media-grid-item__icon',
|
|
|
|
'module-media-grid-item__icon-image'
|
|
|
|
)}
|
2018-07-18 23:00:51 +00:00
|
|
|
/>
|
|
|
|
);
|
2020-09-14 19:51:27 +00:00
|
|
|
}
|
2018-07-18 23:00:51 +00:00
|
|
|
|
2018-07-20 23:37:57 +00:00
|
|
|
return (
|
2023-04-12 23:17:56 +00:00
|
|
|
<img
|
|
|
|
alt={i18n('icu:lightboxImageAlt')}
|
|
|
|
className="module-media-grid-item__image"
|
|
|
|
src={mediaItem.thumbnailObjectUrl}
|
|
|
|
onError={handleImageError}
|
2018-07-20 23:37:57 +00:00
|
|
|
/>
|
|
|
|
);
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 23:17:56 +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'
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2020-09-14 19:51:27 +00:00
|
|
|
|
2018-04-15 06:16:39 +00:00
|
|
|
return (
|
2023-04-12 23:17:56 +00:00
|
|
|
<div className="module-media-grid-item__image-container">
|
|
|
|
<img
|
|
|
|
alt={i18n('icu:lightboxImageAlt')}
|
|
|
|
className="module-media-grid-item__image"
|
|
|
|
src={mediaItem.thumbnailObjectUrl}
|
|
|
|
onError={handleImageError}
|
|
|
|
/>
|
|
|
|
<div className="module-media-grid-item__circle-overlay">
|
|
|
|
<div className="module-media-grid-item__play-overlay" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-04-15 06:16:39 +00:00
|
|
|
);
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|
2023-04-12 23:17:56 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'module-media-grid-item__icon',
|
|
|
|
'module-media-grid-item__icon-generic'
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function MediaGridItem(props: Props): JSX.Element {
|
|
|
|
const { onClick } = props;
|
|
|
|
return (
|
|
|
|
<button type="button" className="module-media-grid-item" onClick={onClick}>
|
|
|
|
<MediaGridItemContent {...props} />
|
|
|
|
</button>
|
|
|
|
);
|
2018-04-12 20:23:26 +00:00
|
|
|
}
|