2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2020-01-29 17:38:46 +01:00
|
|
|
import type { Node } from 'react';
|
2020-12-15 19:54:35 +01:00
|
|
|
import { getThumbnailCdnUrl } from 'util/thumbnail';
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2021-02-18 08:13:54 +01:00
|
|
|
import { THUMBNAIL_FALLBACK } from 'config';
|
2020-05-28 19:07:04 +02:00
|
|
|
import FreezeframeWrapper from './FreezeframeWrapper';
|
2019-03-05 05:46:57 +01:00
|
|
|
import Placeholder from './placeholder.png';
|
2020-11-02 17:51:08 +01:00
|
|
|
import classnames from 'classnames';
|
2017-07-14 01:18:28 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
2020-11-02 17:51:08 +01:00
|
|
|
uri: string,
|
2018-03-26 23:32:43 +02:00
|
|
|
thumbnail: ?string, // externally sourced image
|
2020-01-29 17:38:46 +01:00
|
|
|
children?: Node,
|
2020-05-29 19:26:06 +02:00
|
|
|
allowGifs: boolean,
|
2020-11-02 17:51:08 +01:00
|
|
|
claim: ?StreamClaim,
|
|
|
|
doResolveUri: string => void,
|
|
|
|
className?: string,
|
2021-02-18 08:13:54 +01:00
|
|
|
fallbackThumbnail?: string,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2018-01-09 02:15:44 +01:00
|
|
|
|
2020-11-02 17:51:08 +01:00
|
|
|
function FileThumbnail(props: Props) {
|
2021-02-18 08:13:54 +01:00
|
|
|
const { claim, uri, doResolveUri, thumbnail: rawThumbnail, children, allowGifs = false, className, fallbackThumbnail = THUMBNAIL_FALLBACK } = props;
|
2020-11-02 17:51:08 +01:00
|
|
|
const passedThumbnail = rawThumbnail && rawThumbnail.trim().replace(/^http:\/\//i, 'https://');
|
|
|
|
const thumbnailFromClaim =
|
|
|
|
uri && claim && claim.value && claim.value.thumbnail ? claim.value.thumbnail.url : undefined;
|
|
|
|
const thumbnail = passedThumbnail || thumbnailFromClaim;
|
|
|
|
const hasResolvedClaim = claim !== undefined;
|
2019-10-06 07:47:08 +02:00
|
|
|
|
2020-11-02 17:51:08 +01:00
|
|
|
React.useEffect(() => {
|
2020-12-24 17:58:19 +01:00
|
|
|
if (!hasResolvedClaim && uri) {
|
2020-11-02 17:51:08 +01:00
|
|
|
doResolveUri(uri);
|
2020-05-28 19:07:04 +02:00
|
|
|
}
|
2020-11-02 17:51:08 +01:00
|
|
|
}, [hasResolvedClaim, uri, doResolveUri]);
|
2020-05-28 19:07:04 +02:00
|
|
|
|
2020-11-02 17:51:08 +01:00
|
|
|
if (!allowGifs && thumbnail && thumbnail.endsWith('gif')) {
|
2020-01-29 17:38:46 +01:00
|
|
|
return (
|
2020-11-02 17:51:08 +01:00
|
|
|
<FreezeframeWrapper src={thumbnail} className={classnames('media__thumb', className)}>
|
2020-01-29 17:38:46 +01:00
|
|
|
{children}
|
2020-11-02 17:51:08 +01:00
|
|
|
</FreezeframeWrapper>
|
2020-01-29 17:38:46 +01:00
|
|
|
);
|
2017-07-14 01:18:28 +02:00
|
|
|
}
|
2020-11-02 17:51:08 +01:00
|
|
|
|
2020-12-08 21:10:11 +01:00
|
|
|
let url = thumbnail || (hasResolvedClaim ? Placeholder : '');
|
|
|
|
// @if TARGET='web'
|
|
|
|
// Pass image urls through a compression proxy
|
2020-12-15 19:54:35 +01:00
|
|
|
if (thumbnail) {
|
|
|
|
url = getThumbnailCdnUrl({ thumbnail });
|
2020-12-08 21:10:11 +01:00
|
|
|
}
|
|
|
|
// @endif
|
2020-11-02 17:51:08 +01:00
|
|
|
|
2020-08-12 21:43:41 +02:00
|
|
|
const cleanUrl = url ? url.replace(/'/g, "\\'") : '';
|
|
|
|
|
2020-11-02 17:51:08 +01:00
|
|
|
return (
|
|
|
|
<div
|
2021-02-18 08:13:54 +01:00
|
|
|
style={{ backgroundImage: `url('${cleanUrl}'), url('${fallbackThumbnail}')` }}
|
2020-11-02 17:51:08 +01:00
|
|
|
className={classnames('media__thumb', className, {
|
|
|
|
'media__thumb--resolving': !hasResolvedClaim,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
2017-07-14 01:18:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 19:07:04 +02:00
|
|
|
export default FileThumbnail;
|