-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathIntercomWebView.js
93 lines (78 loc) · 2.65 KB
/
IntercomWebView.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View, WebView, Dimensions } from 'react-native';
import Spinner from 'react-native-loading-spinner-overlay';
class IntercomWebView extends Component{
constructor(props){
super(props);
this.state = {
isLoading: true
};
this.onLoadEnd = this.onLoadEnd.bind(this);
}
componentDidMount = () => {
this.setState({
windowHeight: Dimensions.get('window').height
});
}
injectedJS = (appId, name, email, id = '', hideLauncher, userHash = '') => {
const config = {
user_id: id,
user_hash: userHash,
app_id: appId,
name: name,
email: email,
hide_default_launcher: hideLauncher
}
let strConfig = ''
try {
strConfig = JSON.stringify(config)
} catch(e){
console.log('Unable to stringify config', e)
}
return `
window.Intercom('boot', ${strConfig});
if (${hideLauncher})
window.Intercom('showMessages');
`;
}
onLoadEnd = () => {
this.setState({isLoading: false});
if (this.props.onLoadEnd)
this.props.onLoadEnd();
}
render(){
const { appId, name, email, id, hideLauncher, defaultHeight, showLoadingOverlay, userHash, onHide, ...remainingProps } = this.props;
const { isLoading, windowHeight } = this.state;
let height = defaultHeight || windowHeight;
return(
<View style={[{height: height}, this.props.style, {flex: 1, backgroundColor: 'red'}]}>
<Spinner visible={showLoadingOverlay && isLoading} />
<WebView source={require('./IntercomWebView.html')}
style={{flex: 1, backgroundColor: 'blue'}}
injectedJavaScript={this.injectedJS( appId, name, email, id, hideLauncher, userHash, onHide )}
javaScriptEnabled={true}
onLoadEnd={this.onLoadEnd}
{...remainingProps}
/>
</View>
)
}
}
IntercomWebView.propTypes = {
appId: PropTypes.string,
name: PropTypes.string,
email: PropTypes.string,
id: PropTypes.string || null,
hideLauncher: PropTypes.bool,
showLoadingOverlay: PropTypes.bool,
defaultHeight: PropTypes.number,
userHash: PropTypes.string || null
};
IntercomWebView.defaultProps = {
hideLauncher: false,
showLoadingOverlay: true,
id: null,
userHash: null
};
export default IntercomWebView;