-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathindex.native.js
63 lines (56 loc) · 1.85 KB
/
index.native.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {Image as ImageComponent} from 'expo-image';
import lodashGet from 'lodash/get';
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {defaultProps, imagePropTypes} from './imagePropTypes';
import RESIZE_MODES from './resizeModes';
const dimensionsCache = new Map();
function resolveDimensions(key) {
return dimensionsCache.get(key);
}
function Image(props) {
// eslint-disable-next-line react/destructuring-assignment
const {source, isAuthTokenRequired, session, ...rest} = props;
let imageSource = source;
if (source && source.uri && typeof source.uri === 'number') {
imageSource = source.uri;
}
if (typeof imageSource !== 'number' && isAuthTokenRequired) {
const authToken = lodashGet(props, 'session.encryptedAuthToken', null);
imageSource = {
...source,
headers: authToken
? {
[CONST.CHAT_ATTACHMENT_TOKEN_KEY]: authToken,
}
: null,
};
}
return (
<ImageComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
source={imageSource}
onLoad={(evt) => {
const {width, height, url} = evt.source;
dimensionsCache.set(url, {width, height});
if (props.onLoad) {
props.onLoad({nativeEvent: {width, height}});
}
}}
/>
);
}
Image.propTypes = imagePropTypes;
Image.defaultProps = defaultProps;
Image.displayName = 'Image';
const ImageWithOnyx = withOnyx({
session: {
key: ONYXKEYS.SESSION,
},
})(Image);
ImageWithOnyx.resizeMode = RESIZE_MODES;
ImageWithOnyx.resolveDimensions = resolveDimensions;
export default ImageWithOnyx;