Skip to content

Commit 4bcb2d6

Browse files
authored
Merge pull request #23781 from Krishna2323/krishna2323/issue/23268
Web - Loader appears for a second while switch between chats
2 parents e26d900 + e90f8ab commit 4bcb2d6

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/components/ImageWithSizeCalculation.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import React, {useState, useRef} from 'react';
1+
import _ from 'underscore';
2+
import React, {useState, useRef, useEffect} from 'react';
23
import {View} from 'react-native';
34
import PropTypes from 'prop-types';
45
import Log from '../libs/Log';
@@ -38,8 +39,9 @@ const defaultProps = {
3839
*
3940
*/
4041
function ImageWithSizeCalculation(props) {
41-
const [isLoading, setIsLoading] = useState(false);
4242
const isLoadedRef = useRef(null);
43+
const [isImageCached, setIsImageCached] = useState(true);
44+
const [isLoading, setIsLoading] = useState(false);
4345

4446
const onError = () => {
4547
Log.hmmm('Unable to fetch image to calculate size', {url: props.url});
@@ -53,6 +55,20 @@ function ImageWithSizeCalculation(props) {
5355
});
5456
};
5557

58+
/** Delay the loader to detect whether the image is being loaded from the cache or the internet. */
59+
useEffect(() => {
60+
if (isLoadedRef.current || !isLoading) {
61+
return;
62+
}
63+
const timeout = _.delay(() => {
64+
if (!isLoading || isLoadedRef.current) {
65+
return;
66+
}
67+
setIsImageCached(false);
68+
}, 200);
69+
return () => clearTimeout(timeout);
70+
}, [isLoading]);
71+
5672
return (
5773
<View style={[styles.w100, styles.h100, props.style]}>
5874
<Image
@@ -66,11 +82,14 @@ function ImageWithSizeCalculation(props) {
6682
}
6783
setIsLoading(true);
6884
}}
69-
onLoadEnd={() => setIsLoading(false)}
85+
onLoadEnd={() => {
86+
setIsLoading(false);
87+
setIsImageCached(true);
88+
}}
7089
onError={onError}
7190
onLoad={imageLoadedSuccessfully}
7291
/>
73-
{isLoading && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
92+
{isLoading && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
7493
</View>
7594
);
7695
}

0 commit comments

Comments
 (0)