Use a retry counter/limit to fetch thumbnails. #7618

Merged
Ruk33 merged 1 commit from 7617-some-channel-thumbnails-can-trigger-a-large-number-of-requests-to-fetch-thumbnail into master 2022-06-28 17:25:21 +02:00

View file

@ -50,6 +50,7 @@ function ChannelThumbnail(props: Props) {
setThumbUploadError, setThumbUploadError,
ThumbUploadError, ThumbUploadError,
} = props; } = props;
const [retries, setRetries] = React.useState(3);
const [thumbLoadError, setThumbLoadError] = React.useState(ThumbUploadError); const [thumbLoadError, setThumbLoadError] = React.useState(ThumbUploadError);
const shouldResolve = !isResolving && claim === undefined; const shouldResolve = !isResolving && claim === undefined;
const thumbnail = rawThumbnail && rawThumbnail.trim().replace(/^http:\/\//i, 'https://'); const thumbnail = rawThumbnail && rawThumbnail.trim().replace(/^http:\/\//i, 'https://');
@ -58,6 +59,15 @@ function ChannelThumbnail(props: Props) {
const channelThumbnail = thumbnailPreview || thumbnail || defaultAvatar; const channelThumbnail = thumbnailPreview || thumbnail || defaultAvatar;
const isGif = channelThumbnail && channelThumbnail.endsWith('gif'); const isGif = channelThumbnail && channelThumbnail.endsWith('gif');
const showThumb = (!obscure && !!thumbnail) || thumbnailPreview; const showThumb = (!obscure && !!thumbnail) || thumbnailPreview;
const avatarSrc = React.useMemo(() => {
if (retries <= 0) {
return defaultAvatar;
}
if (!thumbLoadError) {
return channelThumbnail;
}
return defaultAvatar;
}, [retries, thumbLoadError, channelThumbnail, defaultAvatar]);
// Generate a random color class based on the first letter of the channel name // Generate a random color class based on the first letter of the channel name
const { channelName } = parseURI(uri); const { channelName } = parseURI(uri);
@ -100,9 +110,10 @@ function ChannelThumbnail(props: Props) {
<OptimizedImage <OptimizedImage
alt={__('Channel profile picture')} alt={__('Channel profile picture')}
className={!channelThumbnail ? 'channel-thumbnail__default' : 'channel-thumbnail__custom'} className={!channelThumbnail ? 'channel-thumbnail__default' : 'channel-thumbnail__custom'}
src={(!thumbLoadError && channelThumbnail) || defaultAvatar} src={avatarSrc}
loading={noLazyLoad ? undefined : 'lazy'} loading={noLazyLoad ? undefined : 'lazy'}
onError={() => { onError={() => {
setRetries((retries) => retries - 1);
if (setThumbUploadError) { if (setThumbUploadError) {
setThumbUploadError(true); setThumbUploadError(true);
} else { } else {