2018-03-26 14:32:43 -07:00
|
|
|
// @flow
|
2020-01-29 11:38:46 -05:00
|
|
|
import type { Node } from 'react';
|
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) {
|
|
|
|
const { claim, uri, doResolveUri, thumbnail: rawThumbnail, children, allowGifs = false, className } = props;
|
|
|
|
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(() => {
|
|
|
|
if (!hasResolvedClaim) {
|
|
|
|
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-11-02 15:43:52 -05:00
|
|
|
const url = passedThumbnail || (uri ? thumbnailFromClaim : Placeholder);
|
2020-11-02 11:51:08 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{ backgroundImage: `url('${url ? url.replace(/'/g, "\\'") : ''}')` }}
|
|
|
|
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;
|