-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add retry ability for image load failure (#2011)
* feat: add reload image state to useLoadingImage hook * chore: remove image key * feat: reload gallery image * chore: change the rendering order * fix: failing gallery test
- Loading branch information
1 parent
2b47ee3
commit 3b5a315
Showing
4 changed files
with
109 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
package/src/components/Attachment/ImageReloadIndicator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import { Pressable } from 'react-native'; | ||
|
||
import { useTheme } from '../../contexts/themeContext/ThemeContext'; | ||
import { Refresh } from '../../icons'; | ||
|
||
const REFRESH_ICON_SIZE = 24; | ||
|
||
export const ImageReloadIndicator = ({ | ||
onReloadImage, | ||
style, | ||
}: { | ||
onReloadImage: () => void; | ||
style: React.ComponentProps<typeof Pressable>['style']; | ||
}) => { | ||
const { | ||
theme: { | ||
colors: { grey_dark }, | ||
}, | ||
} = useTheme(); | ||
|
||
return ( | ||
<Pressable onPress={onReloadImage} style={style}> | ||
<Refresh height={REFRESH_ICON_SIZE} pathFill={grey_dark} width={REFRESH_ICON_SIZE} /> | ||
</Pressable> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,64 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect, useReducer, useRef } from 'react'; | ||
|
||
import { useNetInfo } from '@react-native-community/netinfo'; | ||
import { useChatContext } from '../contexts/chatContext/ChatContext'; | ||
|
||
type ImageState = { | ||
isLoadingImage: boolean; | ||
isLoadingImageError: boolean; | ||
}; | ||
|
||
type Action = | ||
| { type: 'reloadImage' } | ||
| { isLoadingImage: boolean; type: 'setLoadingImage' } | ||
| { isLoadingImageError: boolean; type: 'setLoadingImageError' }; | ||
|
||
function reducer(prevState: ImageState, action: Action) { | ||
switch (action.type) { | ||
case 'reloadImage': | ||
return { | ||
...prevState, | ||
isLoadingImage: true, | ||
isLoadingImageError: false, | ||
}; | ||
case 'setLoadingImage': | ||
return { ...prevState, isLoadingImage: action.isLoadingImage }; | ||
case 'setLoadingImageError': | ||
return { ...prevState, isLoadingImageError: action.isLoadingImageError }; | ||
default: | ||
return prevState; | ||
} | ||
} | ||
export const useLoadingImage = () => { | ||
const [isLoadingImage, setLoadingImage] = useState(true); | ||
const [isLoadingImageError, setLoadingImageError] = useState(false); | ||
const { isConnected } = useNetInfo(); | ||
const [imageState, dispatch] = useReducer(reducer, { | ||
isLoadingImage: true, | ||
isLoadingImageError: false, | ||
}); | ||
const { isLoadingImage, isLoadingImageError } = imageState; | ||
const onReloadImageRef = useRef(() => dispatch({ type: 'reloadImage' })); | ||
const setLoadingImageRef = useRef((isLoadingImage: boolean) => | ||
dispatch({ isLoadingImage, type: 'setLoadingImage' }), | ||
); | ||
const setLoadingImageErrorRef = useRef((isLoadingImageError: boolean) => | ||
dispatch({ isLoadingImageError, type: 'setLoadingImageError' }), | ||
); | ||
const { isOnline } = useChatContext(); | ||
|
||
// storing the value of isLoadingImageError in a ref to avoid passing as a dep to useEffect | ||
const hasImageLoadedErroredRef = useRef(isLoadingImageError); | ||
hasImageLoadedErroredRef.current = isLoadingImageError; | ||
|
||
useEffect(() => { | ||
if (isConnected) { | ||
setLoadingImageError(false); | ||
if (isOnline && hasImageLoadedErroredRef.current) { | ||
// if there was an error previously, reload the image automatically when user comes back online | ||
onReloadImageRef.current(); | ||
} | ||
}, [isConnected]); | ||
}, [isOnline]); | ||
|
||
return { isLoadingImage, isLoadingImageError, setLoadingImage, setLoadingImageError }; | ||
return { | ||
isLoadingImage, | ||
isLoadingImageError, | ||
onReloadImage: onReloadImageRef.current, | ||
setLoadingImage: setLoadingImageRef.current, | ||
setLoadingImageError: setLoadingImageErrorRef.current, | ||
}; | ||
}; |