Skip to content

Commit f786487

Browse files
committed
chore: added image reloader invoked when network is reconnected
1 parent 6c34292 commit f786487

File tree

1 file changed

+55
-12
lines changed

1 file changed

+55
-12
lines changed

packages/uikit-react-native/src/components/MessageRenderer/FileMessage/ImageFileMessage.tsx

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,68 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
2+
import { Platform, StyleSheet, View } from 'react-native';
23

34
import { Icon, Image, createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
4-
import { getAvailableUriFromFileMessage } from '@sendbird/uikit-utils';
5+
import { getAvailableUriFromFileMessage, useForceUpdate } from '@sendbird/uikit-utils';
56

67
import type { FileMessageProps } from './index';
78

9+
const useRetry = (hasError: boolean, retryCount = 5) => {
10+
if (Platform.OS === 'android') return '';
11+
12+
const forceUpdate = useForceUpdate();
13+
const retryCountRef = useRef(1);
14+
const retryTimeoutRef = useRef<NodeJS.Timeout>();
15+
16+
useEffect(() => {
17+
if (hasError) {
18+
const reloadReservation = () => {
19+
if (retryCountRef.current < retryCount) {
20+
retryTimeoutRef.current = setTimeout(() => {
21+
retryCountRef.current++;
22+
reloadReservation();
23+
forceUpdate();
24+
}, retryCountRef.current * 5000);
25+
}
26+
};
27+
28+
return reloadReservation();
29+
} else {
30+
clearTimeout(retryTimeoutRef.current);
31+
}
32+
}, [hasError]);
33+
34+
return retryCountRef.current;
35+
};
36+
837
const ImageFileMessage = ({ message }: FileMessageProps) => {
938
const { colors } = useUIKitTheme();
1039
const [imageNotFound, setImageNotFound] = useState(false);
1140

1241
const fileUrl = getAvailableUriFromFileMessage(message);
1342
const style = [styles.image, { backgroundColor: colors.onBackground04 }];
1443

15-
if (imageNotFound) {
16-
return <Icon containerStyle={style} icon={'thumbnail-none'} size={48} color={colors.onBackground02} />;
17-
}
44+
const key = useRetry(imageNotFound);
1845

1946
return (
20-
<Image
21-
source={{ uri: fileUrl }}
22-
style={style}
23-
resizeMode={'cover'}
24-
resizeMethod={'resize'}
25-
onError={() => setImageNotFound(true)}
26-
/>
47+
<View style={style}>
48+
<Image
49+
key={key}
50+
source={{ uri: fileUrl }}
51+
style={[StyleSheet.absoluteFill, imageNotFound && styles.hide]}
52+
resizeMode={'cover'}
53+
resizeMethod={'resize'}
54+
onError={() => setImageNotFound(true)}
55+
onLoad={() => setImageNotFound(false)}
56+
/>
57+
{imageNotFound && (
58+
<Icon
59+
containerStyle={StyleSheet.absoluteFill}
60+
icon={'thumbnail-none'}
61+
size={48}
62+
color={colors.onBackground02}
63+
/>
64+
)}
65+
</View>
2766
);
2867
};
2968

@@ -33,6 +72,10 @@ const styles = createStyleSheet({
3372
maxWidth: 240,
3473
height: 160,
3574
borderRadius: 16,
75+
overflow: 'hidden',
76+
},
77+
hide: {
78+
display: 'none',
3679
},
3780
});
3881

0 commit comments

Comments
 (0)