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

69 lines
2 KiB
React
Raw Normal View History

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