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