Fix blank gif avatars (#6060)

## Issue
6010: hyperchat send display issues with GIF profiles

The FreezeFrameWrapper have no `src` to freeze due to the lazy loading.

## Fix
Delay the freezing by making the lazy-load effect return a state to indicate when then `src` has been loaded.

Since the lazy-loader will `unobserve` after loading, the state will never go back to false, so we don't need to handle the case of preventing `new FreezeFrameWrapper` from being called multiple times from it's effect.
This commit is contained in:
infinite-persistence 2021-05-14 23:00:07 +08:00 committed by GitHub
parent ee1825f5f1
commit 4dc333de41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -10,11 +10,13 @@ const FreezeframeWrapper = (props) => {
// eslint-disable-next-line // eslint-disable-next-line
const { src, className, children } = props; const { src, className, children } = props;
useEffect(() => { const srcLoaded = useLazyLoading(imgRef);
freezeframe.current = new Freezeframe(imgRef.current);
}, []);
useLazyLoading(imgRef); useEffect(() => {
if (srcLoaded) {
freezeframe.current = new Freezeframe(imgRef.current);
}
}, [srcLoaded]);
return ( return (
<div className={classnames(className, 'freezeframe-wrapper')}> <div className={classnames(className, 'freezeframe-wrapper')}>

View file

@ -1,6 +1,6 @@
// @flow // @flow
import type { ElementRef } from 'react'; import type { ElementRef } from 'react';
import { useEffect } from 'react'; import React, { useEffect } from 'react';
/** /**
* Helper React hook for lazy loading images * Helper React hook for lazy loading images
@ -13,6 +13,8 @@ export default function useLazyLoading(
threshold: number = 0.25, threshold: number = 0.25,
deps: Array<any> = [] deps: Array<any> = []
) { ) {
const [srcLoaded, setSrcLoaded] = React.useState(false);
useEffect(() => { useEffect(() => {
if (!elementRef.current) { if (!elementRef.current) {
return; return;
@ -30,6 +32,7 @@ export default function useLazyLoading(
if (target.dataset.src) { if (target.dataset.src) {
// $FlowFixMe // $FlowFixMe
target.src = target.dataset.src; target.src = target.dataset.src;
setSrcLoaded(true);
return; return;
} }
@ -49,4 +52,6 @@ export default function useLazyLoading(
lazyLoadingObserver.observe(elementRef.current); lazyLoadingObserver.observe(elementRef.current);
}, deps); }, deps);
return srcLoaded;
} }