Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web - Loader appears for a second while switch between chats #23781

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/components/ImageWithSizeCalculation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {useState, useRef} from 'react';
import _ from 'underscore';
import React, {useState, useRef, useEffect} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Log from '../libs/Log';
Expand Down Expand Up @@ -38,8 +39,9 @@ const defaultProps = {
*
*/
function ImageWithSizeCalculation(props) {
const [isLoading, setIsLoading] = useState(false);
const isLoadedRef = useRef(null);
const [isImageCached, setIsImageCached] = useState(true);
const [isLoading, setIsLoading] = useState(false);

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

/** Delay the loader to detect whether the image is being loaded from the cache or the internet. */
useEffect(() => {
if (isLoadedRef.current || !isLoading) {
Krishna2323 marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const timeout = _.delay(() => {
if (!isLoading || isLoadedRef.current) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not A Blocker - it would have been nice to put this condition in the same order as on line 60

}
setIsImageCached(false);
}, 300);
return () => clearTimeout(timeout);
Krishna2323 marked this conversation as resolved.
Show resolved Hide resolved
}, [isLoading]);

return (
<View style={[styles.w100, styles.h100, props.style]}>
<Image
Expand All @@ -66,11 +82,14 @@ function ImageWithSizeCalculation(props) {
}
setIsLoading(true);
}}
onLoadEnd={() => setIsLoading(false)}
onLoadEnd={() => {
setIsLoading(false);
setIsImageCached(true);
}}
onError={onError}
onLoad={imageLoadedSuccessfully}
/>
{isLoading && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{isLoading && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
</View>
);
}
Expand Down
Loading