|
1 | 1 | 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'; |
3 | 4 | import {withOnyx} from 'react-native-onyx'; |
4 | | -import CONST from '@src/CONST'; |
| 5 | +import _ from 'underscore'; |
5 | 6 | import ONYXKEYS from '@src/ONYXKEYS'; |
6 | | -import BaseImage from './BaseImage'; |
7 | 7 | import {defaultProps, imagePropTypes} from './imagePropTypes'; |
8 | 8 | import RESIZE_MODES from './resizeModes'; |
9 | 9 |
|
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 | + */ |
12 | 16 | 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)}`}; |
15 | 23 | } |
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 | | - |
26 | 24 | return propsSource; |
27 | 25 | // 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. |
28 | 26 | // eslint-disable-next-line react-hooks/exhaustive-deps |
29 | 27 | }, [propsSource, isAuthTokenRequired]); |
30 | 28 |
|
| 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 | + |
31 | 47 | return ( |
32 | | - <BaseImage |
| 48 | + <RNImage |
33 | 49 | // eslint-disable-next-line react/jsx-props-no-spreading |
34 | 50 | {...forwardedProps} |
35 | 51 | source={source} |
|
0 commit comments