Skip to content

Commit c1877ed

Browse files
authored
Merge pull request #32700 from Expensify/revert-13036-kidroca/feature/react-native-web-image-headers
[CP Staging + Prod] Revert "Image Web/Desktop: Add support for http headers"
2 parents 04457f9 + ebf5671 commit c1877ed

8 files changed

Lines changed: 101 additions & 250 deletions

File tree

patches/react-native-web+0.19.9+005+image-header-support.patch

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

src/components/Image/BaseImage.js

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

src/components/Image/BaseImage.native.js

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

src/components/Image/index.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
import lodashGet from 'lodash/get';
2-
import React, {useMemo} from 'react';
2+
import React, {useEffect, useMemo} from 'react';
3+
import {Image as RNImage} from 'react-native';
34
import {withOnyx} from 'react-native-onyx';
4-
import CONST from '@src/CONST';
5+
import _ from 'underscore';
56
import ONYXKEYS from '@src/ONYXKEYS';
6-
import BaseImage from './BaseImage';
77
import {defaultProps, imagePropTypes} from './imagePropTypes';
88
import RESIZE_MODES from './resizeModes';
99

10-
function Image({source: propsSource, isAuthTokenRequired, session, ...forwardedProps}) {
11-
// Update the source to include the auth token if required
10+
function Image(props) {
11+
const {source: propsSource, isAuthTokenRequired, onLoad, session} = props;
12+
/**
13+
* Check if the image source is a URL - if so the `encryptedAuthToken` is appended
14+
* to the source.
15+
*/
1216
const source = useMemo(() => {
13-
if (typeof lodashGet(propsSource, 'uri') === 'number') {
14-
return propsSource.uri;
17+
if (isAuthTokenRequired) {
18+
// There is currently a `react-native-web` bug preventing the authToken being passed
19+
// in the headers of the image request so the authToken is added as a query param.
20+
// On native the authToken IS passed in the image request headers
21+
const authToken = lodashGet(session, 'encryptedAuthToken', null);
22+
return {uri: `${propsSource.uri}?encryptedAuthToken=${encodeURIComponent(authToken)}`};
1523
}
16-
if (typeof propsSource !== 'number' && isAuthTokenRequired) {
17-
const authToken = lodashGet(session, 'encryptedAuthToken');
18-
return {
19-
...propsSource,
20-
headers: {
21-
[CONST.CHAT_ATTACHMENT_TOKEN_KEY]: authToken,
22-
},
23-
};
24-
}
25-
2624
return propsSource;
2725
// The session prop is not required, as it causes the image to reload whenever the session changes. For more information, please refer to issue #26034.
2826
// eslint-disable-next-line react-hooks/exhaustive-deps
2927
}, [propsSource, isAuthTokenRequired]);
3028

29+
/**
30+
* The natural image dimensions are retrieved using the updated source
31+
* and as a result the `onLoad` event needs to be manually invoked to return these dimensions
32+
*/
33+
useEffect(() => {
34+
// If an onLoad callback was specified then manually call it and pass
35+
// the natural image dimensions to match the native API
36+
if (onLoad == null) {
37+
return;
38+
}
39+
RNImage.getSize(source.uri, (width, height) => {
40+
onLoad({nativeEvent: {width, height}});
41+
});
42+
}, [onLoad, source]);
43+
44+
// Omit the props which the underlying RNImage won't use
45+
const forwardedProps = _.omit(props, ['source', 'onLoad', 'session', 'isAuthTokenRequired']);
46+
3147
return (
32-
<BaseImage
48+
<RNImage
3349
// eslint-disable-next-line react/jsx-props-no-spreading
3450
{...forwardedProps}
3551
source={source}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import lodashGet from 'lodash/get';
2+
import React from 'react';
3+
import RNFastImage from 'react-native-fast-image';
4+
import {withOnyx} from 'react-native-onyx';
5+
import CONST from '@src/CONST';
6+
import ONYXKEYS from '@src/ONYXKEYS';
7+
import {defaultProps, imagePropTypes} from './imagePropTypes';
8+
import RESIZE_MODES from './resizeModes';
9+
10+
const dimensionsCache = new Map();
11+
12+
function resolveDimensions(key) {
13+
return dimensionsCache.get(key);
14+
}
15+
16+
function Image(props) {
17+
// eslint-disable-next-line react/destructuring-assignment
18+
const {source, isAuthTokenRequired, session, ...rest} = props;
19+
20+
let imageSource = source;
21+
if (source && source.uri && typeof source.uri === 'number') {
22+
imageSource = source.uri;
23+
}
24+
if (typeof imageSource !== 'number' && isAuthTokenRequired) {
25+
const authToken = lodashGet(props, 'session.encryptedAuthToken', null);
26+
imageSource = {
27+
...source,
28+
headers: authToken
29+
? {
30+
[CONST.CHAT_ATTACHMENT_TOKEN_KEY]: authToken,
31+
}
32+
: null,
33+
};
34+
}
35+
36+
return (
37+
<RNFastImage
38+
// eslint-disable-next-line react/jsx-props-no-spreading
39+
{...rest}
40+
source={imageSource}
41+
onLoad={(evt) => {
42+
const {width, height} = evt.nativeEvent;
43+
dimensionsCache.set(source.uri, {width, height});
44+
if (props.onLoad) {
45+
props.onLoad(evt);
46+
}
47+
}}
48+
/>
49+
);
50+
}
51+
52+
Image.propTypes = imagePropTypes;
53+
Image.defaultProps = defaultProps;
54+
Image.displayName = 'Image';
55+
const ImageWithOnyx = withOnyx({
56+
session: {
57+
key: ONYXKEYS.SESSION,
58+
},
59+
})(Image);
60+
ImageWithOnyx.resizeMode = RESIZE_MODES;
61+
ImageWithOnyx.resolveDimensions = resolveDimensions;
62+
63+
export default ImageWithOnyx;

src/components/RoomHeaderAvatars.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function RoomHeaderAvatars(props) {
3434
<AttachmentModal
3535
headerTitle={props.icons[0].name}
3636
source={UserUtils.getFullSizeAvatar(props.icons[0].source, props.icons[0].id)}
37+
isAuthTokenRequired
3738
isWorkspaceAvatar={props.icons[0].type === CONST.ICON_TYPE_WORKSPACE}
3839
originalFileName={props.icons[0].name}
3940
>
@@ -78,6 +79,7 @@ function RoomHeaderAvatars(props) {
7879
<AttachmentModal
7980
headerTitle={icon.name}
8081
source={UserUtils.getFullSizeAvatar(icon.source, icon.id)}
82+
isAuthTokenRequired
8183
originalFileName={icon.name}
8284
isWorkspaceAvatar={icon.type === CONST.ICON_TYPE_WORKSPACE}
8385
>

src/pages/DetailsPage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ function DetailsPage(props) {
134134
<AttachmentModal
135135
headerTitle={details.displayName}
136136
source={UserUtils.getFullSizeAvatar(details.avatar, details.accountID)}
137+
isAuthTokenRequired
137138
originalFileName={details.originalFileName}
138139
>
139140
{({show}) => (

0 commit comments

Comments
 (0)