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

112 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-03-26 03:07:42 +01:00
import React, { useState } from 'react';
import classNames from 'classnames';
2018-04-12 22:23:26 +02:00
import {
isImageTypeSupported,
isVideoTypeSupported,
} from '../../../util/GoogleChrome';
2019-01-14 22:49:58 +01:00
import { LocalizerType } from '../../../types/Util';
import { MediaItemType } from '../../LightboxGallery';
2021-03-26 03:07:42 +01:00
import { useEncryptedFileFetch } from '../../../hooks/useEncryptedFileFetch';
2018-04-12 22:23:26 +02:00
2021-03-26 03:07:42 +01:00
type Props = {
mediaItem: MediaItemType;
2018-04-15 08:16:39 +02:00
onClick?: () => void;
2019-01-14 22:49:58 +01:00
i18n: LocalizerType;
2021-03-26 03:07:42 +01:00
};
2018-04-12 22:23:26 +02:00
2021-03-26 03:07:42 +01:00
const MediaGridItemContent = (props: Props) => {
const { mediaItem, i18n } = props;
const { attachment, contentType } = mediaItem;
2021-03-26 03:07:42 +01:00
const urlToDecrypt = mediaItem.thumbnailObjectUrl || '';
const [imageBroken, setImageBroken] = useState(false);
2021-03-26 03:07:42 +01:00
const { loading, urlToLoad } = useEncryptedFileFetch(
urlToDecrypt,
contentType
);
// data will be url if loading is finished and '' if not
const srcData = !loading ? urlToLoad : '';
2021-03-26 03:07:42 +01:00
const onImageError = () => {
// tslint:disable-next-line no-console
console.log(
'MediaGridItem: Image failed to load; failing over to placeholder'
);
2021-03-26 03:07:42 +01:00
setImageBroken(true);
};
2018-04-12 22:23:26 +02:00
2021-03-26 03:07:42 +01:00
if (!attachment) {
return null;
}
2021-03-26 03:07:42 +01:00
if (contentType && isImageTypeSupported(contentType)) {
if (imageBroken || !srcData) {
return (
2021-03-26 03:07:42 +01:00
<div
className={classNames(
'module-media-grid-item__icon',
'module-media-grid-item__icon-image'
)}
/>
);
}
return (
2021-03-26 03:07:42 +01:00
<img
alt={i18n('lightboxImageAlt')}
className="module-media-grid-item__image"
src={srcData}
onError={onImageError}
/>
);
2021-03-26 03:07:42 +01:00
} else if (contentType && isVideoTypeSupported(contentType)) {
if (imageBroken || !srcData) {
return (
<div
className={classNames(
'module-media-grid-item__icon',
'module-media-grid-item__icon-video'
)}
/>
);
}
2018-04-12 22:23:26 +02:00
2018-04-15 08:16:39 +02:00
return (
2021-03-26 03:07:42 +01:00
<div className="module-media-grid-item__image-container">
<img
alt={i18n('lightboxImageAlt')}
className="module-media-grid-item__image"
src={srcData}
onError={onImageError}
/>
<div className="module-media-grid-item__circle-overlay">
<div className="module-media-grid-item__play-overlay" />
</div>
2018-04-15 08:16:39 +02:00
</div>
);
2018-04-12 22:23:26 +02:00
}
2021-03-26 03:07:42 +01:00
return (
<div
className={classNames(
'module-media-grid-item__icon',
'module-media-grid-item__icon-generic'
)}
/>
);
};
export const MediaGridItem = (props: Props) => {
return (
<div
className="module-media-grid-item"
role="button"
onClick={props.onClick}
>
<MediaGridItemContent {...props} />
</div>
);
};