Skip to content

Commit eeaa29f

Browse files
authored
Merge pull request #35451 from tienifr/fix/32699
Fix: show loading indicator for uncached attachments
2 parents c3a5fc3 + c5387ba commit eeaa29f

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/components/Image/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import React, {useEffect, useMemo} from 'react';
33
import {Image as RNImage} from 'react-native';
44
import {withOnyx} from 'react-native-onyx';
55
import _ from 'underscore';
6+
import useNetwork from '@hooks/useNetwork';
67
import ONYXKEYS from '@src/ONYXKEYS';
78
import {defaultProps, imagePropTypes} from './imagePropTypes';
89
import RESIZE_MODES from './resizeModes';
910

1011
function Image(props) {
1112
const {source: propsSource, isAuthTokenRequired, onLoad, session} = props;
13+
const {isOffline} = useNetwork();
14+
1215
/**
1316
* Check if the image source is a URL - if so the `encryptedAuthToken` is appended
1417
* to the source.
@@ -39,7 +42,7 @@ function Image(props) {
3942
RNImage.getSize(source.uri, (width, height) => {
4043
onLoad({nativeEvent: {width, height}});
4144
});
42-
}, [onLoad, source]);
45+
}, [onLoad, source, isOffline]);
4346

4447
// Omit the props which the underlying RNImage won't use
4548
const forwardedProps = _.omit(props, ['source', 'onLoad', 'session', 'isAuthTokenRequired']);

src/components/ImageWithSizeCalculation.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import delay from 'lodash/delay';
22
import React, {useEffect, useMemo, useRef, useState} from 'react';
33
import type {ImageSourcePropType, StyleProp, ViewStyle} from 'react-native';
44
import {View} from 'react-native';
5+
import useNetwork from '@hooks/useNetwork';
56
import useThemeStyles from '@hooks/useThemeStyles';
67
import Log from '@libs/Log';
78
import FullscreenLoadingIndicator from './FullscreenLoadingIndicator';
@@ -44,16 +45,27 @@ function ImageWithSizeCalculation({url, style, onMeasure, onLoadFailure, isAuthT
4445
const isLoadedRef = useRef<boolean | null>(null);
4546
const [isImageCached, setIsImageCached] = useState(true);
4647
const [isLoading, setIsLoading] = useState(false);
48+
const {isOffline} = useNetwork();
4749

4850
const source = useMemo(() => ({uri: url}), [url]);
4951

5052
const onError = () => {
5153
Log.hmmm('Unable to fetch image to calculate size', {url});
5254
onLoadFailure?.();
55+
if (isLoadedRef.current) {
56+
isLoadedRef.current = false;
57+
setIsImageCached(false);
58+
}
59+
if (isOffline) {
60+
return;
61+
}
62+
setIsLoading(false);
5363
};
5464

5565
const imageLoadedSuccessfully = (event: OnLoadNativeEvent) => {
5666
isLoadedRef.current = true;
67+
setIsLoading(false);
68+
setIsImageCached(true);
5769
onMeasure({
5870
width: event.nativeEvent.width,
5971
height: event.nativeEvent.height,
@@ -87,10 +99,6 @@ function ImageWithSizeCalculation({url, style, onMeasure, onLoadFailure, isAuthT
8799
}
88100
setIsLoading(true);
89101
}}
90-
onLoadEnd={() => {
91-
setIsLoading(false);
92-
setIsImageCached(true);
93-
}}
94102
onError={onError}
95103
onLoad={imageLoadedSuccessfully}
96104
/>

0 commit comments

Comments
 (0)