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