2019-05-07 04:35:04 +02:00
|
|
|
// @flow
|
2020-06-30 07:51:15 +02:00
|
|
|
import React from 'react';
|
2019-05-11 21:06:22 +02:00
|
|
|
import { parseURI } from 'lbry-redux';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Gerbil from './gerbil.png';
|
2021-06-01 13:12:08 +02:00
|
|
|
import Transparent from './transparent_1x1.png';
|
2020-05-28 19:07:04 +02:00
|
|
|
import FreezeframeWrapper from 'component/fileThumbnail/FreezeframeWrapper';
|
2021-02-16 22:09:20 +01:00
|
|
|
import ChannelStakedIndicator from 'component/channelStakedIndicator';
|
2021-05-17 21:51:21 +02:00
|
|
|
import { getThumbnailCdnUrl } from 'util/thumbnail';
|
|
|
|
import useLazyLoading from 'effects/use-lazy-loading';
|
2019-05-07 04:35:04 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
thumbnail: ?string,
|
2019-10-23 09:03:45 +02:00
|
|
|
uri: ?string,
|
2019-06-11 20:10:58 +02:00
|
|
|
className?: string,
|
2019-06-28 19:00:29 +02:00
|
|
|
thumbnailPreview: ?string,
|
2019-08-02 02:56:25 +02:00
|
|
|
obscure?: boolean,
|
2019-12-04 19:07:40 +01:00
|
|
|
small?: boolean,
|
2020-05-28 19:07:04 +02:00
|
|
|
allowGifs?: boolean,
|
2020-07-23 16:22:57 +02:00
|
|
|
claim: ?ChannelClaim,
|
2021-02-16 22:09:20 +01:00
|
|
|
doResolveUri: (string) => void,
|
2020-08-24 20:44:41 +02:00
|
|
|
isResolving: boolean,
|
2020-11-19 18:17:47 +01:00
|
|
|
showDelayedMessage?: boolean,
|
2021-02-16 22:09:20 +01:00
|
|
|
hideStakedIndicator?: boolean,
|
2021-04-23 21:59:48 +02:00
|
|
|
xsmall?: boolean,
|
2021-05-17 21:51:21 +02:00
|
|
|
noOptimization?: boolean,
|
2019-05-07 04:35:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function ChannelThumbnail(props: Props) {
|
2020-06-08 15:16:56 +02:00
|
|
|
const {
|
|
|
|
thumbnail: rawThumbnail,
|
|
|
|
uri,
|
|
|
|
className,
|
|
|
|
thumbnailPreview: rawThumbnailPreview,
|
|
|
|
obscure,
|
|
|
|
small = false,
|
2021-04-23 21:59:48 +02:00
|
|
|
xsmall = false,
|
2020-06-08 15:16:56 +02:00
|
|
|
allowGifs = false,
|
2020-07-23 16:22:57 +02:00
|
|
|
claim,
|
|
|
|
doResolveUri,
|
2020-08-24 20:44:41 +02:00
|
|
|
isResolving,
|
2020-11-19 18:17:47 +01:00
|
|
|
showDelayedMessage = false,
|
2021-02-16 22:09:20 +01:00
|
|
|
hideStakedIndicator = false,
|
2021-05-17 21:51:21 +02:00
|
|
|
noOptimization,
|
2020-06-08 15:16:56 +02:00
|
|
|
} = props;
|
2020-06-30 07:51:15 +02:00
|
|
|
const [thumbError, setThumbError] = React.useState(false);
|
2020-07-23 16:22:57 +02:00
|
|
|
const shouldResolve = claim === undefined;
|
2020-06-08 15:16:56 +02:00
|
|
|
const thumbnail = rawThumbnail && rawThumbnail.trim().replace(/^http:\/\//i, 'https://');
|
|
|
|
const thumbnailPreview = rawThumbnailPreview && rawThumbnailPreview.trim().replace(/^http:\/\//i, 'https://');
|
2020-05-28 19:07:04 +02:00
|
|
|
const channelThumbnail = thumbnail || thumbnailPreview;
|
2021-05-17 21:51:21 +02:00
|
|
|
const isGif = channelThumbnail && channelThumbnail.endsWith('gif');
|
2019-12-07 16:56:00 +01:00
|
|
|
const showThumb = (!obscure && !!thumbnail) || thumbnailPreview;
|
2021-05-17 21:51:21 +02:00
|
|
|
const thumbnailRef = React.useRef(null);
|
2019-05-14 04:53:04 +02:00
|
|
|
// Generate a random color class based on the first letter of the channel name
|
2019-12-04 19:07:40 +01:00
|
|
|
const { channelName } = parseURI(uri);
|
2019-10-23 09:03:45 +02:00
|
|
|
let initializer;
|
2019-12-04 19:07:40 +01:00
|
|
|
let colorClassName;
|
|
|
|
if (channelName) {
|
2019-10-23 09:03:45 +02:00
|
|
|
initializer = channelName.charCodeAt(0) - 65; // will be between 0 and 57
|
2019-12-04 19:07:40 +01:00
|
|
|
colorClassName = `channel-thumbnail__default--${Math.abs(initializer % 4)}`;
|
2019-10-23 09:03:45 +02:00
|
|
|
} else {
|
2019-12-04 19:07:40 +01:00
|
|
|
colorClassName = `channel-thumbnail__default--4`;
|
2019-10-23 09:03:45 +02:00
|
|
|
}
|
2020-06-30 07:51:15 +02:00
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (shouldResolve && uri) {
|
|
|
|
doResolveUri(uri);
|
|
|
|
}
|
|
|
|
}, [doResolveUri, shouldResolve, uri]);
|
|
|
|
|
2021-05-17 21:51:21 +02:00
|
|
|
useLazyLoading(thumbnailRef, 0.25, [showThumb, thumbError, channelThumbnail]);
|
|
|
|
|
|
|
|
if (isGif && !allowGifs) {
|
2021-02-16 22:09:20 +01:00
|
|
|
return (
|
|
|
|
<FreezeframeWrapper src={channelThumbnail} className={classnames('channel-thumbnail', className)}>
|
|
|
|
{!hideStakedIndicator && <ChannelStakedIndicator uri={uri} claim={claim} />}
|
|
|
|
</FreezeframeWrapper>
|
|
|
|
);
|
2020-06-30 07:51:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 21:51:21 +02:00
|
|
|
let url = channelThumbnail;
|
|
|
|
// @if TARGET='web'
|
|
|
|
// Pass image urls through a compression proxy, except for GIFs.
|
|
|
|
if (thumbnail && !noOptimization && !(isGif && allowGifs)) {
|
|
|
|
url = getThumbnailCdnUrl({ thumbnail });
|
|
|
|
}
|
|
|
|
// @endif
|
|
|
|
|
2019-05-07 04:35:04 +02:00
|
|
|
return (
|
2019-05-11 21:06:22 +02:00
|
|
|
<div
|
2019-06-11 20:10:58 +02:00
|
|
|
className={classnames('channel-thumbnail', className, {
|
2019-08-02 17:11:31 +02:00
|
|
|
[colorClassName]: !showThumb,
|
2019-12-04 19:07:40 +01:00
|
|
|
'channel-thumbnail--small': small,
|
2021-04-23 21:59:48 +02:00
|
|
|
'channel-thumbnail--xsmall': xsmall,
|
2020-08-24 20:44:41 +02:00
|
|
|
'channel-thumbnail--resolving': isResolving,
|
2019-05-11 21:06:22 +02:00
|
|
|
})}
|
|
|
|
>
|
2020-01-30 21:55:45 +01:00
|
|
|
{!showThumb && (
|
|
|
|
<img
|
2021-05-17 21:51:21 +02:00
|
|
|
ref={thumbnailRef}
|
2020-01-30 21:55:45 +01:00
|
|
|
alt={__('Channel profile picture')}
|
|
|
|
className="channel-thumbnail__default"
|
2021-05-17 21:51:21 +02:00
|
|
|
data-src={!thumbError && url ? url : Gerbil}
|
2021-06-01 13:12:08 +02:00
|
|
|
src={Transparent}
|
2021-01-28 19:03:32 +01:00
|
|
|
onError={() => setThumbError(true)} // if thumb fails (including due to https replace, show gerbil.
|
2020-01-30 21:55:45 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showThumb && (
|
2020-11-19 18:17:47 +01:00
|
|
|
<>
|
|
|
|
{showDelayedMessage && thumbError ? (
|
|
|
|
<div className="chanel-thumbnail--waiting">{__('This will be visible in a few minutes.')}</div>
|
|
|
|
) : (
|
|
|
|
<img
|
2021-05-17 21:51:21 +02:00
|
|
|
ref={thumbnailRef}
|
2020-11-19 18:17:47 +01:00
|
|
|
alt={__('Channel profile picture')}
|
|
|
|
className="channel-thumbnail__custom"
|
2021-05-17 21:51:21 +02:00
|
|
|
data-src={!thumbError && url ? url : Gerbil}
|
2021-06-01 13:12:08 +02:00
|
|
|
src={Transparent}
|
2020-11-19 18:17:47 +01:00
|
|
|
onError={() => setThumbError(true)} // if thumb fails (including due to https replace, show gerbil.
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2020-01-30 21:55:45 +01:00
|
|
|
)}
|
2021-02-16 22:09:20 +01:00
|
|
|
{!hideStakedIndicator && <ChannelStakedIndicator uri={uri} claim={claim} />}
|
2019-05-07 04:35:04 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChannelThumbnail;
|