Skip to content

Commit 07a3796

Browse files
authored
Merge pull request #15663 from kidroca/kidroca/refactor/avatar-platform-specific-code-replacement
Refactor: Remove Avatar platform specific code
2 parents c4a260e + c4abb46 commit 07a3796

6 files changed

Lines changed: 29 additions & 51 deletions

File tree

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"test:e2e": "node tests/e2e/testRunner.js --development"
4242
},
4343
"dependencies": {
44-
"@expensify/react-native-web": "0.18.12",
44+
"@expensify/react-native-web": "0.18.15",
4545
"@formatjs/intl-getcanonicallocales": "^1.5.8",
4646
"@formatjs/intl-locale": "^2.4.21",
4747
"@formatjs/intl-numberformat": "^6.2.5",

src/components/Avatar.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import themeColors from '../styles/themes/default';
88
import CONST from '../CONST';
99
import * as StyleUtils from '../styles/StyleUtils';
1010
import * as Expensicons from './Icon/Expensicons';
11-
import getAvatarDefaultSource from '../libs/getAvatarDefaultSource';
1211
import Image from './Image';
1312
import {withNetwork} from './OnyxProvider';
1413
import networkPropTypes from './networkPropTypes';
@@ -119,12 +118,7 @@ class Avatar extends PureComponent {
119118
</View>
120119
)
121120
: (
122-
<Image
123-
source={{uri: this.props.source}}
124-
defaultSource={getAvatarDefaultSource(this.props.source)}
125-
style={imageStyle}
126-
onError={() => this.setState({imageError: true})}
127-
/>
121+
<Image source={{uri: this.props.source}} style={imageStyle} onError={() => this.setState({imageError: true})} />
128122
)}
129123
</View>
130124
);

src/components/Image/index.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,23 @@ import {defaultProps, imagePropTypes} from './imagePropTypes';
88
import RESIZE_MODES from './resizeModes';
99

1010
class Image extends React.Component {
11-
constructor(props) {
12-
super(props);
13-
14-
this.debouncedConfigureImageSource = _.debounce(this.configureImageSource, 220);
15-
16-
this.state = {
17-
imageSource: undefined,
18-
};
19-
}
20-
2111
componentDidMount() {
22-
this.debouncedConfigureImageSource();
12+
this.configureOnLoad();
2313
}
2414

2515
componentDidUpdate(prevProps) {
26-
if (prevProps.source.uri === this.props.source.uri) {
16+
if (prevProps.source === this.props.source) {
2717
return;
2818
}
29-
30-
this.debouncedConfigureImageSource.cancel();
31-
this.debouncedConfigureImageSource();
19+
this.configureOnLoad();
3220
}
3321

3422
/**
3523
* Check if the image source is a URL - if so the `encryptedAuthToken` is appended
36-
* to the source. The natural image dimensions can then be retrieved using this source
37-
* and as a result the `onLoad` event needs to be maunually invoked to return these dimensions
24+
* to the source.
25+
* @returns {Object} - the configured image source
3826
*/
39-
configureImageSource() {
40-
this.props.onLoadStart();
27+
getImageSource() {
4128
const source = this.props.source;
4229
let imageSource = source;
4330
if (this.props.isAuthTokenRequired) {
@@ -47,25 +34,34 @@ class Image extends React.Component {
4734
const authToken = lodashGet(this.props, 'session.encryptedAuthToken', null);
4835
imageSource = {uri: `${source.uri}?encryptedAuthToken=${encodeURIComponent(authToken)}`};
4936
}
50-
this.setState({imageSource});
5137

38+
return imageSource;
39+
}
40+
41+
/**
42+
* The natural image dimensions are retrieved using the updated source
43+
* and as a result the `onLoad` event needs to be manually invoked to return these dimensions
44+
*/
45+
configureOnLoad() {
5246
// If an onLoad callback was specified then manually call it and pass
5347
// the natural image dimensions to match the native API
5448
if (this.props.onLoad == null) {
5549
return;
5650
}
5751

52+
const imageSource = this.getImageSource();
5853
RNImage.getSize(imageSource.uri, (width, height) => {
5954
this.props.onLoad({nativeEvent: {width, height}});
6055
});
6156
}
6257

6358
render() {
64-
// eslint-disable-next-line
65-
const { source, onLoad, ...rest } = this.props;
59+
// Omit the props which the underlying RNImage won't use
60+
const forwardedProps = _.omit(this.props, ['source', 'onLoad', 'session', 'isAuthTokenRequired']);
61+
const source = this.getImageSource();
6662

67-
// eslint-disable-next-line
68-
return <RNImage {...rest} source={this.state.imageSource} />;
63+
// eslint-disable-next-line react/jsx-props-no-spreading
64+
return <RNImage {...forwardedProps} source={source} />;
6965
}
7066
}
7167

src/libs/getAvatarDefaultSource/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/libs/getAvatarDefaultSource/index.native.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)