2019-05-07 04:35:04 +02:00
|
|
|
// @flow
|
2020-06-30 07:51:15 +02:00
|
|
|
import React from 'react';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { parseURI } from 'util/lbryURI';
|
2022-03-21 17:20:33 +01:00
|
|
|
import { getImageProxyUrl } from 'util/thumbnail';
|
2019-05-11 21:06:22 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import Gerbil from './gerbil.png';
|
2020-05-28 19:07:04 +02:00
|
|
|
import FreezeframeWrapper from 'component/fileThumbnail/FreezeframeWrapper';
|
2021-07-16 05:28:17 +02:00
|
|
|
import OptimizedImage from 'component/optimizedImage';
|
2021-07-21 23:31:00 +02:00
|
|
|
import { AVATAR_DEFAULT } from 'config';
|
2022-03-09 19:05:37 +01:00
|
|
|
import useGetUserMemberships from 'effects/use-get-user-memberships';
|
|
|
|
import PremiumBadge from 'component/common/premium-badge';
|
2019-05-07 04:35:04 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
thumbnail: ?string,
|
2021-10-17 10:36:14 +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,
|
ChannelThumbnail improvements
- [x] (6332) The IntersectionObserver method of lazy-loading loads cached images visibly late on slower devices. Previously, it was also showing the "broken image" icon briefly, which we mended by placing a dummy transparent image as the initial src.
- Reverted that ugly transparent image fix.
- Use the browser's built-in `loading="lazy"` instead. Sorry, Safari.
- [x] Size-optimization did not take "device pixel ratio" into account.
- When resizing an image through the CDN, we can't just take the dimensions of the tag in pixels directly -- we need to take zooming into account, otherwise the image ends up blurry.
- Previously, we quickly disabled optimization for the channel avatar in the Channel Page because of this. Now that we know the root-cause, the change was reverted and we now go through the CDN with appropriate sizes. This also improves our Web Vital scores.
- [x] Size-optimization wasn't really implemented for all ChannelThumbnail instances.
- The CDN-optimized size was hardcoded to the largest instance, so small images like sidebar thumbnails are still loading images that are unnecessarily larger.
- There's a little-bit of hardcoding of values from CSS here, but I think it's a ok compromise (not something we change often). It also doesn't need to be exact -- the "device pixel ratio" calculate will ensure it's slightly larger than what we need.
- [x] Set `width` and `height` of `<img>` to improve CLS.
- Addresses Ligthhouse complaints, although technically the shifting was addressed at the `ClaimPreviewTile` level (sub-container dimensions are well defined).
- Notes: the values don't need to be the final CSS-adjusted sizes. It just needs to be in the right aspect ratio to help the browser pre-allocate space to avoid shifts.
- [x] Add option to disable lazy-load Channel Thumbnails
- The guidelines mentioned that items that are already in the viewport should not enable `loading="lazy"`.
- We have a few areas where it doesn't make sense to lazy-load (e.g. thumbnail in Header, channel selector dropdown, publish preview, etc.).
2021-07-05 07:20:40 +02:00
|
|
|
xsmall?: boolean,
|
2022-05-04 14:34:31 +02:00
|
|
|
xxsmall?: 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,
|
ChannelThumbnail improvements
- [x] (6332) The IntersectionObserver method of lazy-loading loads cached images visibly late on slower devices. Previously, it was also showing the "broken image" icon briefly, which we mended by placing a dummy transparent image as the initial src.
- Reverted that ugly transparent image fix.
- Use the browser's built-in `loading="lazy"` instead. Sorry, Safari.
- [x] Size-optimization did not take "device pixel ratio" into account.
- When resizing an image through the CDN, we can't just take the dimensions of the tag in pixels directly -- we need to take zooming into account, otherwise the image ends up blurry.
- Previously, we quickly disabled optimization for the channel avatar in the Channel Page because of this. Now that we know the root-cause, the change was reverted and we now go through the CDN with appropriate sizes. This also improves our Web Vital scores.
- [x] Size-optimization wasn't really implemented for all ChannelThumbnail instances.
- The CDN-optimized size was hardcoded to the largest instance, so small images like sidebar thumbnails are still loading images that are unnecessarily larger.
- There's a little-bit of hardcoding of values from CSS here, but I think it's a ok compromise (not something we change often). It also doesn't need to be exact -- the "device pixel ratio" calculate will ensure it's slightly larger than what we need.
- [x] Set `width` and `height` of `<img>` to improve CLS.
- Addresses Ligthhouse complaints, although technically the shifting was addressed at the `ClaimPreviewTile` level (sub-container dimensions are well defined).
- Notes: the values don't need to be the final CSS-adjusted sizes. It just needs to be in the right aspect ratio to help the browser pre-allocate space to avoid shifts.
- [x] Add option to disable lazy-load Channel Thumbnails
- The guidelines mentioned that items that are already in the viewport should not enable `loading="lazy"`.
- We have a few areas where it doesn't make sense to lazy-load (e.g. thumbnail in Header, channel selector dropdown, publish preview, etc.).
2021-07-05 07:20:40 +02:00
|
|
|
noLazyLoad?: boolean,
|
2021-02-16 22:09:20 +01:00
|
|
|
hideStakedIndicator?: boolean,
|
2022-02-11 19:50:55 +01:00
|
|
|
hideTooltip?: boolean,
|
2021-08-26 16:51:53 +02:00
|
|
|
setThumbUploadError: (boolean) => void,
|
|
|
|
ThumbUploadError: boolean,
|
2022-03-09 19:05:37 +01:00
|
|
|
claimsByUri: { [string]: any },
|
|
|
|
odyseeMembership: string,
|
|
|
|
doFetchUserMemberships: (claimIdCsv: string) => void,
|
|
|
|
showMemberBadge?: boolean,
|
|
|
|
isChannel?: boolean,
|
|
|
|
checkMembership: 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,
|
2022-05-04 14:34:31 +02:00
|
|
|
xxsmall,
|
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,
|
ChannelThumbnail improvements
- [x] (6332) The IntersectionObserver method of lazy-loading loads cached images visibly late on slower devices. Previously, it was also showing the "broken image" icon briefly, which we mended by placing a dummy transparent image as the initial src.
- Reverted that ugly transparent image fix.
- Use the browser's built-in `loading="lazy"` instead. Sorry, Safari.
- [x] Size-optimization did not take "device pixel ratio" into account.
- When resizing an image through the CDN, we can't just take the dimensions of the tag in pixels directly -- we need to take zooming into account, otherwise the image ends up blurry.
- Previously, we quickly disabled optimization for the channel avatar in the Channel Page because of this. Now that we know the root-cause, the change was reverted and we now go through the CDN with appropriate sizes. This also improves our Web Vital scores.
- [x] Size-optimization wasn't really implemented for all ChannelThumbnail instances.
- The CDN-optimized size was hardcoded to the largest instance, so small images like sidebar thumbnails are still loading images that are unnecessarily larger.
- There's a little-bit of hardcoding of values from CSS here, but I think it's a ok compromise (not something we change often). It also doesn't need to be exact -- the "device pixel ratio" calculate will ensure it's slightly larger than what we need.
- [x] Set `width` and `height` of `<img>` to improve CLS.
- Addresses Ligthhouse complaints, although technically the shifting was addressed at the `ClaimPreviewTile` level (sub-container dimensions are well defined).
- Notes: the values don't need to be the final CSS-adjusted sizes. It just needs to be in the right aspect ratio to help the browser pre-allocate space to avoid shifts.
- [x] Add option to disable lazy-load Channel Thumbnails
- The guidelines mentioned that items that are already in the viewport should not enable `loading="lazy"`.
- We have a few areas where it doesn't make sense to lazy-load (e.g. thumbnail in Header, channel selector dropdown, publish preview, etc.).
2021-07-05 07:20:40 +02:00
|
|
|
noLazyLoad,
|
2022-02-11 19:50:55 +01:00
|
|
|
hideTooltip,
|
2021-08-26 16:51:53 +02:00
|
|
|
setThumbUploadError,
|
|
|
|
ThumbUploadError,
|
2022-03-09 19:05:37 +01:00
|
|
|
claimsByUri,
|
|
|
|
odyseeMembership,
|
|
|
|
doFetchUserMemberships,
|
|
|
|
showMemberBadge,
|
|
|
|
isChannel,
|
|
|
|
checkMembership = true,
|
2020-06-08 15:16:56 +02:00
|
|
|
} = props;
|
2021-08-26 16:51:53 +02:00
|
|
|
const [thumbLoadError, setThumbLoadError] = React.useState(ThumbUploadError);
|
2021-11-03 15:49:27 +01:00
|
|
|
const shouldResolve = !isResolving && 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://');
|
2021-08-26 16:51:53 +02:00
|
|
|
const defaultAvatar = AVATAR_DEFAULT || Gerbil;
|
2022-03-21 04:40:32 +01:00
|
|
|
const channelThumbnail = thumbnailPreview || thumbnail || defaultAvatar;
|
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;
|
ChannelThumbnail improvements
- [x] (6332) The IntersectionObserver method of lazy-loading loads cached images visibly late on slower devices. Previously, it was also showing the "broken image" icon briefly, which we mended by placing a dummy transparent image as the initial src.
- Reverted that ugly transparent image fix.
- Use the browser's built-in `loading="lazy"` instead. Sorry, Safari.
- [x] Size-optimization did not take "device pixel ratio" into account.
- When resizing an image through the CDN, we can't just take the dimensions of the tag in pixels directly -- we need to take zooming into account, otherwise the image ends up blurry.
- Previously, we quickly disabled optimization for the channel avatar in the Channel Page because of this. Now that we know the root-cause, the change was reverted and we now go through the CDN with appropriate sizes. This also improves our Web Vital scores.
- [x] Size-optimization wasn't really implemented for all ChannelThumbnail instances.
- The CDN-optimized size was hardcoded to the largest instance, so small images like sidebar thumbnails are still loading images that are unnecessarily larger.
- There's a little-bit of hardcoding of values from CSS here, but I think it's a ok compromise (not something we change often). It also doesn't need to be exact -- the "device pixel ratio" calculate will ensure it's slightly larger than what we need.
- [x] Set `width` and `height` of `<img>` to improve CLS.
- Addresses Ligthhouse complaints, although technically the shifting was addressed at the `ClaimPreviewTile` level (sub-container dimensions are well defined).
- Notes: the values don't need to be the final CSS-adjusted sizes. It just needs to be in the right aspect ratio to help the browser pre-allocate space to avoid shifts.
- [x] Add option to disable lazy-load Channel Thumbnails
- The guidelines mentioned that items that are already in the viewport should not enable `loading="lazy"`.
- We have a few areas where it doesn't make sense to lazy-load (e.g. thumbnail in Header, channel selector dropdown, publish preview, etc.).
2021-07-05 07:20:40 +02:00
|
|
|
|
2022-03-09 19:05:37 +01:00
|
|
|
const badgeProps = {
|
|
|
|
membership: odyseeMembership,
|
|
|
|
linkPage: isChannel,
|
|
|
|
placement: isChannel ? 'bottom' : undefined,
|
|
|
|
hideTooltip,
|
|
|
|
className: isChannel ? 'profile-badge__tooltip' : undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
useGetUserMemberships(checkMembership, [uri], claimsByUri, doFetchUserMemberships, [uri]);
|
|
|
|
|
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
|
|
|
if (isGif && !allowGifs) {
|
2022-03-21 17:20:33 +01:00
|
|
|
const url = getImageProxyUrl(channelThumbnail);
|
2021-02-16 22:09:20 +01:00
|
|
|
return (
|
2022-05-05 12:58:11 +02:00
|
|
|
<FreezeframeWrapper
|
|
|
|
src={url}
|
|
|
|
className={classnames('channel-thumbnail', className, { 'channel-thumbnail--xxsmall': xxsmall })}
|
|
|
|
>
|
2022-03-09 19:05:37 +01:00
|
|
|
{showMemberBadge && <PremiumBadge {...badgeProps} />}
|
2021-02-16 22:09:20 +01:00
|
|
|
</FreezeframeWrapper>
|
|
|
|
);
|
2020-06-30 07:51:15 +02:00
|
|
|
}
|
2021-08-25 02:28:23 +02:00
|
|
|
|
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,
|
2022-05-04 14:34:31 +02:00
|
|
|
'channel-thumbnail--xxsmall': xxsmall,
|
2020-08-24 20:44:41 +02:00
|
|
|
'channel-thumbnail--resolving': isResolving,
|
2019-05-11 21:06:22 +02:00
|
|
|
})}
|
|
|
|
>
|
2021-12-15 03:43:21 +01:00
|
|
|
<OptimizedImage
|
|
|
|
alt={__('Channel profile picture')}
|
|
|
|
className={!channelThumbnail ? 'channel-thumbnail__default' : 'channel-thumbnail__custom'}
|
|
|
|
src={(!thumbLoadError && channelThumbnail) || defaultAvatar}
|
2022-05-04 14:34:31 +02:00
|
|
|
width={xxsmall ? 16 : small || xsmall ? 64 : 160}
|
|
|
|
quality={xxsmall ? 16 : small || xsmall ? 85 : 95}
|
2021-12-15 03:43:21 +01:00
|
|
|
loading={noLazyLoad ? undefined : 'lazy'}
|
|
|
|
onError={() => {
|
|
|
|
if (setThumbUploadError) {
|
|
|
|
setThumbUploadError(true);
|
|
|
|
} else {
|
|
|
|
setThumbLoadError(true);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2022-03-09 19:05:37 +01:00
|
|
|
{showMemberBadge && <PremiumBadge {...badgeProps} />}
|
2019-05-07 04:35:04 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ChannelThumbnail;
|