|
1 | | -import delay from 'lodash/delay'; |
2 | | -import React, {useEffect, useMemo, useRef, useState} from 'react'; |
| 1 | +import React, {useMemo} from 'react'; |
3 | 2 | import type {ImageSourcePropType, StyleProp, ViewStyle} from 'react-native'; |
4 | | -import {View} from 'react-native'; |
5 | | -import useNetwork from '@hooks/useNetwork'; |
6 | 3 | import useThemeStyles from '@hooks/useThemeStyles'; |
7 | 4 | import Log from '@libs/Log'; |
8 | 5 | import CONST from '@src/CONST'; |
9 | | -import AttachmentOfflineIndicator from './AttachmentOfflineIndicator'; |
10 | | -import FullscreenLoadingIndicator from './FullscreenLoadingIndicator'; |
11 | | -import Image from './Image'; |
12 | 6 | import RESIZE_MODES from './Image/resizeModes'; |
13 | 7 | import type {ImageObjectPosition} from './Image/types'; |
| 8 | +import ImageWithLoading from './ImageWithLoading'; |
14 | 9 |
|
15 | 10 | type OnMeasure = (args: {width: number; height: number}) => void; |
16 | 11 |
|
@@ -51,78 +46,31 @@ type ImageWithSizeCalculationProps = { |
51 | 46 | */ |
52 | 47 | function ImageWithSizeCalculation({url, altText, style, onMeasure, onLoadFailure, isAuthTokenRequired, objectPosition = CONST.IMAGE_OBJECT_POSITION.INITIAL}: ImageWithSizeCalculationProps) { |
53 | 48 | const styles = useThemeStyles(); |
54 | | - const isLoadedRef = useRef<boolean | null>(null); |
55 | | - const [isImageCached, setIsImageCached] = useState(true); |
56 | | - const [isLoading, setIsLoading] = useState(false); |
57 | | - const {isOffline} = useNetwork(); |
58 | 49 |
|
59 | 50 | const source = useMemo(() => (typeof url === 'string' ? {uri: url} : url), [url]); |
60 | 51 |
|
61 | 52 | const onError = () => { |
62 | 53 | Log.hmmm('Unable to fetch image to calculate size', {url}); |
63 | 54 | onLoadFailure?.(); |
64 | | - if (isLoadedRef.current) { |
65 | | - isLoadedRef.current = false; |
66 | | - setIsImageCached(false); |
67 | | - } |
68 | | - if (isOffline) { |
69 | | - return; |
70 | | - } |
71 | | - setIsLoading(false); |
72 | 55 | }; |
73 | 56 |
|
74 | | - const imageLoadedSuccessfully = (event: OnLoadNativeEvent) => { |
75 | | - isLoadedRef.current = true; |
76 | | - setIsLoading(false); |
77 | | - setIsImageCached(true); |
78 | | - onMeasure({ |
79 | | - width: event.nativeEvent.width, |
80 | | - height: event.nativeEvent.height, |
81 | | - }); |
82 | | - }; |
83 | | - |
84 | | - /** Delay the loader to detect whether the image is being loaded from the cache or the internet. */ |
85 | | - useEffect(() => { |
86 | | - if (isLoadedRef.current ?? !isLoading) { |
87 | | - return; |
88 | | - } |
89 | | - const timeout = delay(() => { |
90 | | - if (!isLoading || isLoadedRef.current) { |
91 | | - return; |
92 | | - } |
93 | | - setIsImageCached(false); |
94 | | - }, 200); |
95 | | - return () => clearTimeout(timeout); |
96 | | - }, [isLoading]); |
97 | | - |
98 | 57 | return ( |
99 | | - <View style={[styles.w100, styles.h100, style]}> |
100 | | - <Image |
101 | | - style={[styles.w100, styles.h100]} |
102 | | - source={source} |
103 | | - aria-label={altText} |
104 | | - isAuthTokenRequired={isAuthTokenRequired} |
105 | | - resizeMode={RESIZE_MODES.cover} |
106 | | - onLoadStart={() => { |
107 | | - if (isLoadedRef.current ?? isLoading) { |
108 | | - return; |
109 | | - } |
110 | | - setIsLoading(true); |
111 | | - }} |
112 | | - onError={onError} |
113 | | - onLoad={imageLoadedSuccessfully} |
114 | | - waitForSession={() => { |
115 | | - // Called when the image should wait for a valid session to reload |
116 | | - // At the moment this function is called, the image is not in cache anymore |
117 | | - isLoadedRef.current = false; |
118 | | - setIsImageCached(false); |
119 | | - setIsLoading(true); |
120 | | - }} |
121 | | - objectPosition={objectPosition} |
122 | | - /> |
123 | | - {isLoading && !isImageCached && !isOffline && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />} |
124 | | - {isLoading && !isImageCached && <AttachmentOfflineIndicator isPreview />} |
125 | | - </View> |
| 58 | + <ImageWithLoading |
| 59 | + containerStyles={[styles.w100, styles.h100, style]} |
| 60 | + style={[styles.w100, styles.h100]} |
| 61 | + source={source} |
| 62 | + aria-label={altText} |
| 63 | + isAuthTokenRequired={isAuthTokenRequired} |
| 64 | + resizeMode={RESIZE_MODES.cover} |
| 65 | + onError={onError} |
| 66 | + onLoad={(event: OnLoadNativeEvent) => { |
| 67 | + onMeasure({ |
| 68 | + width: event.nativeEvent.width, |
| 69 | + height: event.nativeEvent.height, |
| 70 | + }); |
| 71 | + }} |
| 72 | + objectPosition={objectPosition} |
| 73 | + /> |
126 | 74 | ); |
127 | 75 | } |
128 | 76 |
|
|
0 commit comments