stream-chat-react-native-core 5.11.3-beta.7 → 5.12.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/components/Attachment/Gallery.js +38 -22
- package/lib/commonjs/components/Attachment/Gallery.js.map +1 -1
- package/lib/commonjs/components/Attachment/ImageReloadIndicator.js +40 -0
- package/lib/commonjs/components/Attachment/ImageReloadIndicator.js.map +1 -0
- package/lib/commonjs/hooks/useLoadingImage.js +57 -16
- package/lib/commonjs/hooks/useLoadingImage.js.map +1 -1
- package/lib/commonjs/version.json +1 -1
- package/lib/module/components/Attachment/Gallery.js +38 -22
- package/lib/module/components/Attachment/Gallery.js.map +1 -1
- package/lib/module/components/Attachment/ImageReloadIndicator.js +40 -0
- package/lib/module/components/Attachment/ImageReloadIndicator.js.map +1 -0
- package/lib/module/hooks/useLoadingImage.js +57 -16
- package/lib/module/hooks/useLoadingImage.js.map +1 -1
- package/lib/module/version.json +1 -1
- package/lib/typescript/components/Attachment/ImageReloadIndicator.d.ts +6 -0
- package/lib/typescript/hooks/useLoadingImage.d.ts +3 -3
- package/package.json +1 -1
- package/src/components/Attachment/Gallery.tsx +22 -5
- package/src/components/Attachment/ImageReloadIndicator.tsx +27 -0
- package/src/components/Attachment/__tests__/Gallery.test.js +3 -1
- package/src/hooks/useLoadingImage.tsx +57 -9
- package/src/version.json +1 -1
|
@@ -4,6 +4,7 @@ import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
|
|
|
4
4
|
import type { Attachment } from 'stream-chat';
|
|
5
5
|
|
|
6
6
|
import { GalleryImage } from './GalleryImage';
|
|
7
|
+
import { ImageReloadIndicator } from './ImageReloadIndicator';
|
|
7
8
|
import { buildGallery } from './utils/buildGallery/buildGallery';
|
|
8
9
|
|
|
9
10
|
import type { Thumbnail } from './utils/buildGallery/types';
|
|
@@ -67,6 +68,11 @@ const styles = StyleSheet.create({
|
|
|
67
68
|
justifyContent: 'center',
|
|
68
69
|
position: 'absolute',
|
|
69
70
|
},
|
|
71
|
+
imageReloadContainerStyle: {
|
|
72
|
+
...StyleSheet.absoluteFillObject,
|
|
73
|
+
alignItems: 'center',
|
|
74
|
+
justifyContent: 'center',
|
|
75
|
+
},
|
|
70
76
|
moreImagesContainer: {
|
|
71
77
|
alignItems: 'center',
|
|
72
78
|
justifyContent: 'center',
|
|
@@ -452,8 +458,13 @@ const GalleryImageThumbnail = <
|
|
|
452
458
|
GalleryThumbnailProps<StreamChatGenerics>,
|
|
453
459
|
'ImageLoadingFailedIndicator' | 'ImageLoadingIndicator' | 'thumbnail' | 'borderRadius'
|
|
454
460
|
>) => {
|
|
455
|
-
const {
|
|
456
|
-
|
|
461
|
+
const {
|
|
462
|
+
isLoadingImage,
|
|
463
|
+
isLoadingImageError,
|
|
464
|
+
onReloadImage,
|
|
465
|
+
setLoadingImage,
|
|
466
|
+
setLoadingImageError,
|
|
467
|
+
} = useLoadingImage();
|
|
457
468
|
|
|
458
469
|
const {
|
|
459
470
|
theme: {
|
|
@@ -471,11 +482,17 @@ const GalleryImageThumbnail = <
|
|
|
471
482
|
}}
|
|
472
483
|
>
|
|
473
484
|
{isLoadingImageError ? (
|
|
474
|
-
|
|
485
|
+
<>
|
|
486
|
+
<ImageLoadingFailedIndicator style={styles.imageLoadingErrorIndicatorStyle} />
|
|
487
|
+
<ImageReloadIndicator
|
|
488
|
+
onReloadImage={onReloadImage}
|
|
489
|
+
style={styles.imageReloadContainerStyle}
|
|
490
|
+
/>
|
|
491
|
+
</>
|
|
475
492
|
) : (
|
|
476
493
|
<>
|
|
477
494
|
<GalleryImage
|
|
478
|
-
onError={(error) => {
|
|
495
|
+
onError={({ nativeEvent: { error } }) => {
|
|
479
496
|
console.warn(error);
|
|
480
497
|
setLoadingImage(false);
|
|
481
498
|
setLoadingImageError(true);
|
|
@@ -494,7 +511,7 @@ const GalleryImageThumbnail = <
|
|
|
494
511
|
uri={thumbnail.url}
|
|
495
512
|
/>
|
|
496
513
|
{isLoadingImage && (
|
|
497
|
-
<View style={
|
|
514
|
+
<View style={styles.imageLoadingIndicatorContainer}>
|
|
498
515
|
<ImageLoadingIndicator style={styles.imageLoadingIndicatorStyle} />
|
|
499
516
|
</View>
|
|
500
517
|
)}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Pressable } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import { useTheme } from '../../contexts/themeContext/ThemeContext';
|
|
5
|
+
import { Refresh } from '../../icons';
|
|
6
|
+
|
|
7
|
+
const REFRESH_ICON_SIZE = 24;
|
|
8
|
+
|
|
9
|
+
export const ImageReloadIndicator = ({
|
|
10
|
+
onReloadImage,
|
|
11
|
+
style,
|
|
12
|
+
}: {
|
|
13
|
+
onReloadImage: () => void;
|
|
14
|
+
style: React.ComponentProps<typeof Pressable>['style'];
|
|
15
|
+
}) => {
|
|
16
|
+
const {
|
|
17
|
+
theme: {
|
|
18
|
+
colors: { grey_dark },
|
|
19
|
+
},
|
|
20
|
+
} = useTheme();
|
|
21
|
+
|
|
22
|
+
return (
|
|
23
|
+
<Pressable onPress={onReloadImage} style={style}>
|
|
24
|
+
<Refresh height={REFRESH_ICON_SIZE} pathFill={grey_dark} width={REFRESH_ICON_SIZE} />
|
|
25
|
+
</Pressable>
|
|
26
|
+
);
|
|
27
|
+
};
|
|
@@ -270,7 +270,9 @@ describe('Gallery', () => {
|
|
|
270
270
|
expect(queryAllByTestId('gallery-container').length).toBe(1);
|
|
271
271
|
});
|
|
272
272
|
|
|
273
|
-
fireEvent(getByA11yLabel('Gallery Image'), 'error'
|
|
273
|
+
fireEvent(getByA11yLabel('Gallery Image'), 'error', {
|
|
274
|
+
nativeEvent: { error: 'error loading image' },
|
|
275
|
+
});
|
|
274
276
|
expect(getByAccessibilityHint('image-loading-error')).toBeTruthy();
|
|
275
277
|
});
|
|
276
278
|
|
|
@@ -1,16 +1,64 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useReducer, useRef } from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { useChatContext } from '../contexts/chatContext/ChatContext';
|
|
4
4
|
|
|
5
|
+
type ImageState = {
|
|
6
|
+
isLoadingImage: boolean;
|
|
7
|
+
isLoadingImageError: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type Action =
|
|
11
|
+
| { type: 'reloadImage' }
|
|
12
|
+
| { isLoadingImage: boolean; type: 'setLoadingImage' }
|
|
13
|
+
| { isLoadingImageError: boolean; type: 'setLoadingImageError' };
|
|
14
|
+
|
|
15
|
+
function reducer(prevState: ImageState, action: Action) {
|
|
16
|
+
switch (action.type) {
|
|
17
|
+
case 'reloadImage':
|
|
18
|
+
return {
|
|
19
|
+
...prevState,
|
|
20
|
+
isLoadingImage: true,
|
|
21
|
+
isLoadingImageError: false,
|
|
22
|
+
};
|
|
23
|
+
case 'setLoadingImage':
|
|
24
|
+
return { ...prevState, isLoadingImage: action.isLoadingImage };
|
|
25
|
+
case 'setLoadingImageError':
|
|
26
|
+
return { ...prevState, isLoadingImageError: action.isLoadingImageError };
|
|
27
|
+
default:
|
|
28
|
+
return prevState;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
5
31
|
export const useLoadingImage = () => {
|
|
6
|
-
const [
|
|
7
|
-
|
|
8
|
-
|
|
32
|
+
const [imageState, dispatch] = useReducer(reducer, {
|
|
33
|
+
isLoadingImage: true,
|
|
34
|
+
isLoadingImageError: false,
|
|
35
|
+
});
|
|
36
|
+
const { isLoadingImage, isLoadingImageError } = imageState;
|
|
37
|
+
const onReloadImageRef = useRef(() => dispatch({ type: 'reloadImage' }));
|
|
38
|
+
const setLoadingImageRef = useRef((isLoadingImage: boolean) =>
|
|
39
|
+
dispatch({ isLoadingImage, type: 'setLoadingImage' }),
|
|
40
|
+
);
|
|
41
|
+
const setLoadingImageErrorRef = useRef((isLoadingImageError: boolean) =>
|
|
42
|
+
dispatch({ isLoadingImageError, type: 'setLoadingImageError' }),
|
|
43
|
+
);
|
|
44
|
+
const { isOnline } = useChatContext();
|
|
45
|
+
|
|
46
|
+
// storing the value of isLoadingImageError in a ref to avoid passing as a dep to useEffect
|
|
47
|
+
const hasImageLoadedErroredRef = useRef(isLoadingImageError);
|
|
48
|
+
hasImageLoadedErroredRef.current = isLoadingImageError;
|
|
49
|
+
|
|
9
50
|
useEffect(() => {
|
|
10
|
-
if (
|
|
11
|
-
|
|
51
|
+
if (isOnline && hasImageLoadedErroredRef.current) {
|
|
52
|
+
// if there was an error previously, reload the image automatically when user comes back online
|
|
53
|
+
onReloadImageRef.current();
|
|
12
54
|
}
|
|
13
|
-
}, [
|
|
55
|
+
}, [isOnline]);
|
|
14
56
|
|
|
15
|
-
return {
|
|
57
|
+
return {
|
|
58
|
+
isLoadingImage,
|
|
59
|
+
isLoadingImageError,
|
|
60
|
+
onReloadImage: onReloadImageRef.current,
|
|
61
|
+
setLoadingImage: setLoadingImageRef.current,
|
|
62
|
+
setLoadingImageError: setLoadingImageErrorRef.current,
|
|
63
|
+
};
|
|
16
64
|
};
|
package/src/version.json
CHANGED