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

Migrate Avatar.js to use functional component #17217

Merged
merged 3 commits into from
Apr 12, 2023
Merged
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
119 changes: 60 additions & 59 deletions src/components/Avatar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {PureComponent} from 'react';
import React, {useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
Expand Down Expand Up @@ -60,71 +60,72 @@ const defaultProps = {
name: '',
};

class Avatar extends PureComponent {
constructor(props) {
super(props);
this.state = {
imageError: false,
};
}
function Avatar(props) {
const [imageError, setImageError] = useState(false);
const prevNetworkStatusRef = useRef(props.network.isOffline);

componentDidUpdate(prevProps) {
const isReconnecting = prevProps.network.isOffline && !this.props.network.isOffline;
if (!this.state.imageError || !isReconnecting) {
useEffect(() => {
const isReconnecting = prevNetworkStatusRef.current && !props.network.isOffline;
if (!imageError || !isReconnecting) {
return;
}
this.setState({imageError: false});
}
setImageError(false);

render() {
if (!this.props.source) {
return null;
}
// We have not added the imageError as the dependency because effect is concerned with `imageError` only when the network state changes from offline -> online
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.network.isOffline]);
Copy link
Contributor Author

@techievivek techievivek Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Started a conversation around whether or not imageError should be included in the dep array in the open-source channel here: https://expensify.slack.com/archives/C01GTK53T8Q/p1681117333835879

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment to explain why imageError shouldn't be a dependency in running side effects.


const isWorkspace = this.props.type === CONST.ICON_TYPE_WORKSPACE;
const iconSize = StyleUtils.getAvatarSize(this.props.size);

const imageStyle = [
StyleUtils.getAvatarStyle(this.props.size),
...this.props.imageStyles,
StyleUtils.getAvatarBorderRadius(this.props.size, this.props.type),
];

const iconStyle = [
StyleUtils.getAvatarStyle(this.props.size),
styles.bgTransparent,
...this.props.imageStyles,
];

const iconFillColor = isWorkspace ? StyleUtils.getDefaultWorspaceAvatarColor(this.props.name).fill : this.props.fill;
const fallbackAvatar = isWorkspace ? ReportUtils.getDefaultWorkspaceAvatar(this.props.name) : this.props.fallbackIcon;

return (
<View pointerEvents="none" style={this.props.containerStyles}>
{_.isFunction(this.props.source) || this.state.imageError
? (
<View style={iconStyle}>
<Icon
src={this.state.imageError ? fallbackAvatar : this.props.source}
height={iconSize}
width={iconSize}
fill={this.state.imageError ? themeColors.offline : iconFillColor}
additionalStyles={[
StyleUtils.getAvatarBorderStyle(this.props.size, this.props.type),
isWorkspace ? StyleUtils.getDefaultWorspaceAvatarColor(this.props.name) : {},
this.state.imageError ? StyleUtils.getBackgroundColorStyle(themeColors.fallbackIconColor) : {},
]}
/>
</View>
)
: (
<Image source={{uri: this.props.source}} style={imageStyle} onError={() => this.setState({imageError: true})} />
)}
</View>
);
useEffect(() => {
// Used to store previous network state to compare on next render
prevNetworkStatusRef.current = props.network.isOffline;
});

if (!props.source) {
return null;
}
}

const isWorkspace = props.type === CONST.ICON_TYPE_WORKSPACE;
const iconSize = StyleUtils.getAvatarSize(props.size);

const imageStyle = [
StyleUtils.getAvatarStyle(props.size),
...props.imageStyles,
StyleUtils.getAvatarBorderRadius(props.size, props.type),
];

const iconStyle = [
StyleUtils.getAvatarStyle(props.size),
styles.bgTransparent,
...props.imageStyles,
];

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

return (
<View pointerEvents="none" style={props.containerStyles}>
{_.isFunction(props.source) || imageError
? (
<View style={iconStyle}>
<Icon
src={imageError ? fallbackAvatar : props.source}
height={iconSize}
width={iconSize}
fill={imageError ? themeColors.offline : iconFillColor}
additionalStyles={[
StyleUtils.getAvatarBorderStyle(props.size, props.type),
isWorkspace ? StyleUtils.getDefaultWorkspaceAvatarColor(props.name) : {},
imageError ? StyleUtils.getBackgroundColorStyle(themeColors.fallbackIconColor) : {},
]}
/>
</View>
)
: (
<Image source={{uri: props.source}} style={imageStyle} onError={() => setImageError(true)} />
)}
</View>
);
}
Avatar.defaultProps = defaultProps;
Avatar.propTypes = propTypes;
export default withNetwork()(Avatar);
4 changes: 2 additions & 2 deletions src/styles/StyleUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function getAvatarBorderStyle(size, type) {
* @param {String} [workspaceName]
* @returns {Object}
*/
function getDefaultWorspaceAvatarColor(workspaceName) {
function getDefaultWorkspaceAvatarColor(workspaceName) {
const colorHash = ReportUtils.hashLogin(workspaceName.trim(), workspaceColorOptions.length);

return workspaceColorOptions[colorHash];
Expand Down Expand Up @@ -970,7 +970,7 @@ export {
getEmojiSuggestionItemStyle,
getEmojiSuggestionContainerStyle,
getColoredBackgroundStyle,
getDefaultWorspaceAvatarColor,
getDefaultWorkspaceAvatarColor,
getAvatarBorderRadius,
getEmojiReactionBubbleStyle,
getEmojiReactionTextStyle,
Expand Down