Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle unlimited requested api call and add fallbackIcon props #29558

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions assets/images/receipt-placeholder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 24 additions & 13 deletions src/components/ImageWithSizeCalculation.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import _ from 'underscore';
import React, {useState, useRef, useEffect} from 'react';
import React, {useState, useEffect, useMemo} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Log from '../libs/Log';
import styles from '../styles/styles';
import FullscreenLoadingIndicator from './FullscreenLoadingIndicator';
import Image from './Image';

// Define constants for load states
const LoadState = {
INITIAL: 'initial',
LOADING: 'loading',
LOADED: 'loaded',
ERRORED: 'errored',
};

const propTypes = {
/** Url for image to display */
url: PropTypes.string.isRequired,
Expand Down Expand Up @@ -39,16 +47,17 @@
*
*/
function ImageWithSizeCalculation(props) {
const isLoadedRef = useRef(null);
const [loadState, setLoadState] = useState(LoadState.INITIAL);
const [isImageCached, setIsImageCached] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const source = useMemo(() => ({ uri: props.url }), [props.url]);

const onError = () => {
Log.hmmm('Unable to fetch image to calculate size', {url: props.url});
setLoadState(LoadState.ERRORED);
};

const imageLoadedSuccessfully = (event) => {
isLoadedRef.current = true;
setLoadState(LoadState.LOADED);
props.onMeasure({
width: event.nativeEvent.width,
height: event.nativeEvent.height,
Expand All @@ -57,39 +66,41 @@

/** Delay the loader to detect whether the image is being loaded from the cache or the internet. */
useEffect(() => {
if (isLoadedRef.current || !isLoading) {
if (loadState !== LoadState.LOADING) {
return;
}
const timeout = _.delay(() => {
if (!isLoading || isLoadedRef.current) {
if (loadState !== LoadState.LOADING) {
return;
}
setLoadState(LoadState.ERRORED);
setIsImageCached(false);
}, 200);
return () => clearTimeout(timeout);
}, [isLoading]);
}, [loadState]);

return (
<View style={[styles.w100, styles.h100, props.style]}>
<Image
style={[styles.w100, styles.h100]}
source={{uri: props.url}}
source={source}
isAuthTokenRequired={props.isAuthTokenRequired}
resizeMode={Image.resizeMode.cover}
onLoadStart={() => {
if (isLoadedRef.current || isLoading) {
return;
if (loadState === LoadState.LOADED || isOffline) {

Check failure on line 90 in src/components/ImageWithSizeCalculation.js

View workflow job for this annotation

GitHub Actions / lint

'isOffline' is not defined
setLoadState(LoadState.INITIAL);
} else {
setLoadState(LoadState.LOADING);
}
setIsLoading(true);
}}
onLoadEnd={() => {
setIsLoading(false);
setLoadState(LoadState.LOADED);
setIsImageCached(true);
}}
onError={onError}
onLoad={imageLoadedSuccessfully}
/>
{isLoading && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
{loadState === LoadState.LOADING && !isImageCached && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
</View>
);
}
Expand Down
21 changes: 16 additions & 5 deletions src/components/ThumbnailImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import * as StyleUtils from '../styles/StyleUtils';
import * as DeviceCapabilities from '../libs/DeviceCapabilities';
import useWindowDimensions from '../hooks/useWindowDimensions';
import useNetwork from '../hooks/useNetwork';
import ReceiptPlaceholderImage from '../../assets/images/receipt-placeholder.svg';

const propTypes = {
/** Source URL for the preview image */
Expand Down Expand Up @@ -91,15 +93,24 @@
);

const sizeStyles = props.shouldDynamicallyResize ? [StyleUtils.getWidthAndHeightStyle(imageWidth, imageHeight)] : [styles.w100, styles.h100];
const {isOffline} = useNetwork();

return (
<View style={[props.style, styles.overflowHidden]}>
<View style={[...sizeStyles, styles.alignItemsCenter, styles.justifyContentCenter]}>
<ImageWithSizeCalculation
url={props.previewSourceURL}
onMeasure={updateImageSize}
isAuthTokenRequired={props.isAuthTokenRequired}
/>
{isOffline ? (
<ReceiptPlaceholderImage
pointerEvents={'none'}

Check failure on line 103 in src/components/ThumbnailImage.js

View workflow job for this annotation

GitHub Actions / lint

Curly braces are unnecessary here
width={100}
style={styles.receiptPlaceholder}
/>
) : (
<ImageWithSizeCalculation
url={props.previewSourceURL}
onMeasure={updateImageSize}
isAuthTokenRequired={props.isAuthTokenRequired}
/>
)}
</View>
</View>
);
Expand Down
6 changes: 6 additions & 0 deletions src/styles/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -3899,6 +3899,12 @@ const styles = (theme) => ({
checkboxWithLabelCheckboxStyle: {
marginLeft: -2,
},

receiptPlaceholder: {
position: 'absolute',
alignItems:'center',
justifyContent:'center'
},
});

// For now we need to export the styles function that takes the theme as an argument
Expand Down
Loading