lbry-desktop/ui/component/fileThumbnail/view.jsx

69 lines
2 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import type { Node } from 'react';
2020-12-15 19:54:35 +01:00
import { getThumbnailCdnUrl } from 'util/thumbnail';
import React from 'react';
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';
2021-04-14 21:02:02 +02:00
import useLazyLoading from 'effects/use-lazy-loading';
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
children?: Node,
allowGifs: boolean,
2020-11-02 17:51:08 +01:00
claim: ?StreamClaim,
doResolveUri: (string) => void,
2020-11-02 17:51:08 +01:00
className?: string,
2018-03-26 23:32:43 +02:00
};
2020-11-02 17:51:08 +01:00
function FileThumbnail(props: Props) {
const { claim, uri, doResolveUri, thumbnail: rawThumbnail, children, allowGifs = false, className } = 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;
const thumbnailRef = React.useRef(null);
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
useLazyLoading(thumbnailRef);
2020-11-02 17:51:08 +01:00
if (!allowGifs && thumbnail && thumbnail.endsWith('gif')) {
return (
2020-11-02 17:51:08 +01:00
<FreezeframeWrapper src={thumbnail} className={classnames('media__thumb', className)}>
{children}
2020-11-02 17:51:08 +01:00
</FreezeframeWrapper>
);
2017-07-14 01:18:28 +02:00
}
2020-11-02 17:51:08 +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 });
}
// @endif
2020-11-02 17:51:08 +01:00
const thumnailUrl = url ? url.replace(/'/g, "\\'") : '';
2020-11-02 17:51:08 +01:00
return (
<div
ref={thumbnailRef}
data-background-image={thumnailUrl}
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;