Skip to content

Commit

Permalink
fix: infiniti loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tienifr committed Sep 22, 2023
1 parent 67f40c4 commit 5d9649c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/AttachmentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ function AttachmentModal(props) {
file={file}
onToggleKeyboard={updateConfirmButtonVisibility}
isWorkspaceAvatar={props.isWorkspaceAvatar}
fallbackSource={props.fallbackSource}
/>
)
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ const propTypes = {
...withLocalizePropTypes,
};

function AttachmentViewImage({source, file, isAuthTokenRequired, loadComplete, onPress, isImage, onScaleChanged, translate}) {
function AttachmentViewImage({source, file, isAuthTokenRequired, loadComplete, onPress, isImage, onScaleChanged, translate, onError}) {
const children = (
<ImageView
onScaleChanged={onScaleChanged}
url={source}
fileName={file.name}
isAuthTokenRequired={isImage && isAuthTokenRequired}
onError={onError}
/>
);
return onPress ? (
Expand Down
11 changes: 10 additions & 1 deletion src/components/Attachments/AttachmentView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import AttachmentViewPdf from './AttachmentViewPdf';
import addEncryptedAuthTokenToURL from '../../../libs/addEncryptedAuthTokenToURL';
import * as StyleUtils from '../../../styles/StyleUtils';
import {attachmentViewPropTypes, attachmentViewDefaultProps} from './propTypes';
import useNetwork from '../../../hooks/useNetwork';

const propTypes = {
...attachmentViewPropTypes,
Expand Down Expand Up @@ -62,9 +63,14 @@ function AttachmentView({
translate,
isFocused,
isWorkspaceAvatar,
fallbackSource,
}) {
const [loadComplete, setLoadComplete] = useState(false);

const [imageError, setImageError] = useState(false);

useNetwork({onReconnect: () => setImageError(false)});

// Handles case where source is a component (ex: SVG)
if (_.isFunction(source)) {
let iconFillColor = '';
Expand Down Expand Up @@ -113,7 +119,7 @@ function AttachmentView({
if (isImage || (file && Str.isImage(file.name))) {
return (
<AttachmentViewImage
source={source}
source={imageError ? fallbackSource : source}
file={file}
isAuthTokenRequired={isAuthTokenRequired}
isUsedInCarousel={isUsedInCarousel}
Expand All @@ -122,6 +128,9 @@ function AttachmentView({
isImage={isImage}
onPress={onPress}
onScaleChanged={onScaleChanged}
onError={() => {
setImageError(true);
}}
/>
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/components/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from 'react';
import React, {useEffect, useState} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
Expand Down Expand Up @@ -66,6 +66,10 @@ function Avatar(props) {

useNetwork({onReconnect: () => setImageError(false)});

useEffect(() => {
setImageError(false);
}, [props.source]);

if (!props.source) {
return null;
}
Expand All @@ -81,7 +85,7 @@ function Avatar(props) {
const iconStyle = props.imageStyles && props.imageStyles.length ? [StyleUtils.getAvatarStyle(props.size), styles.bgTransparent, ...props.imageStyles] : undefined;

const iconFillColor = isWorkspace ? StyleUtils.getDefaultWorkspaceAvatarColor(props.name).fill : props.fill;
const fallbackAvatar = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatar(props.name) : props.fallbackIcon;
const fallbackAvatar = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatar(props.name) : props.fallbackIcon || Expensicons.FallbackAvatar;

return (
<View
Expand Down
1 change: 1 addition & 0 deletions src/components/AvatarWithImagePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class AvatarWithImagePicker extends React.Component {
headerTitle={this.props.headerTitle}
source={this.props.previewSource}
originalFileName={this.props.originalFileName}
fallbackSource={this.props.fallbackIcon}
>
{({show}) => (
<AttachmentPicker type={CONST.ATTACHMENT_PICKER_TYPE.IMAGE}>
Expand Down
7 changes: 6 additions & 1 deletion src/components/ImageView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ const propTypes = {

/** image file name */
fileName: PropTypes.string.isRequired,

onError: PropTypes.func,
};

const defaultProps = {
isAuthTokenRequired: false,
onError: () => {},
};

function ImageView({isAuthTokenRequired, url, fileName}) {
function ImageView({isAuthTokenRequired, url, fileName, onError}) {
const [isLoading, setIsLoading] = useState(true);
const [containerHeight, setContainerHeight] = useState(0);
const [containerWidth, setContainerWidth] = useState(0);
Expand Down Expand Up @@ -238,6 +241,7 @@ function ImageView({isAuthTokenRequired, url, fileName}) {
resizeMode={zoomScale > 1 ? Image.resizeMode.center : Image.resizeMode.contain}
onLoadStart={imageLoadingStart}
onLoad={imageLoad}
onError={onError}
/>
{(isLoading || zoomScale === 0) && <FullscreenLoadingIndicator style={[styles.opacity1, styles.bgTransparent]} />}
</View>
Expand Down Expand Up @@ -268,6 +272,7 @@ function ImageView({isAuthTokenRequired, url, fileName}) {
resizeMode={Image.resizeMode.contain}
onLoadStart={imageLoadingStart}
onLoad={imageLoad}
onError={onError}
/>
</PressableWithoutFeedback>

Expand Down
2 changes: 2 additions & 0 deletions src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ function deleteAvatar() {
value: {
[currentUserAccountID]: {
avatar: defaultAvatar,
fallbackIcon: null,
},
},
},
Expand All @@ -491,6 +492,7 @@ function deleteAvatar() {
value: {
[currentUserAccountID]: {
avatar: allPersonalDetails[currentUserAccountID].avatar,
fallbackIcon: allPersonalDetails[currentUserAccountID].fallbackIcon,
},
},
},
Expand Down
1 change: 1 addition & 0 deletions src/pages/ProfilePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ function ProfilePage(props) {
source={UserUtils.getFullSizeAvatar(avatar, accountID)}
isAuthTokenRequired
originalFileName={originalFileName}
fallbackSource={fallbackIcon}
>
{({show}) => (
<PressableWithoutFocus
Expand Down

0 comments on commit 5d9649c

Please sign in to comment.