-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathWebView.js
More file actions
157 lines (148 loc) · 4.06 KB
/
WebView.js
File metadata and controls
157 lines (148 loc) · 4.06 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import React, { useState, useMemo } from 'react';
import { StyleSheet, Linking, View, ActivityIndicator, Text } from 'react-native';
import { WebView } from 'react-native-webview';
import PropTypes from 'prop-types';
import { isJsonString, storeHelper, generateScripts, getMessage } from './utils';
const propTypes = {
websiteToken: PropTypes.string.isRequired,
baseUrl: PropTypes.string.isRequired,
cwCookie: PropTypes.string,
colorScheme: PropTypes.oneOf(['light', 'dark', 'auto']),
user: PropTypes.shape({
name: PropTypes.string,
avatar_url: PropTypes.string,
email: PropTypes.string,
identifier_hash: PropTypes.string,
}),
locale: PropTypes.string,
customAttributes: PropTypes.shape({}),
closeModal: PropTypes.func,
};
const WebViewComponent = ({
baseUrl,
websiteToken,
cwCookie = '',
locale = 'en',
colorScheme = 'light',
user = {},
customAttributes = {},
closeModal,
}) => {
const [currentUrl, setCurrentUrl] = React.useState(null);
const [loading, setLoading] = useState(true);
let widgetUrl = `${baseUrl}/widget?website_token=${websiteToken}&locale=${locale}`;
if (cwCookie) {
widgetUrl = `${widgetUrl}&cw_conversation=${cwCookie}`;
}
const injectedJavaScript = generateScripts({
user,
locale,
customAttributes,
colorScheme,
});
const onShouldStartLoadWithRequest = (request) => {
const isMessageView = currentUrl && currentUrl.includes('#/messages');
const isAttachmentUrl = !widgetUrl.includes(request.url);
// Open the attachments only in the external browser
const shouldRedirectToBrowser = isMessageView && isAttachmentUrl;
if (shouldRedirectToBrowser) {
Linking.openURL(request.url);
return false;
}
return true;
};
const handleWebViewNavigationStateChange = (newNavState) => {
setCurrentUrl(newNavState.url);
};
const opacity = useMemo(() => {
if (loading) {
return {
opacity: 0,
};
}
return {
opacity: 1,
};
}, [loading]);
const renderLoadingComponent = () => {
return (
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" color="#1f93ff" />
<Text style={styles.loadingText}>Loading...</Text>
</View>
);
};
return (
<View style={styles.container}>
<WebView
source={{
uri: widgetUrl,
}}
onMessage={(event) => {
const { data } = event.nativeEvent;
const message = getMessage(data);
if (isJsonString(message)) {
const parsedMessage = JSON.parse(message);
const { event: eventType, type } = parsedMessage;
if (eventType === 'loaded') {
const {
config: { authToken },
} = parsedMessage;
storeHelper.storeCookie(authToken);
}
if (type === 'close-widget') {
closeModal();
}
}
}}
scalesPageToFit
useWebKit
sharedCookiesEnabled
javaScriptEnabled={true}
domStorageEnabled={true}
style={[styles.WebViewStyle, opacity]}
injectedJavaScript={injectedJavaScript}
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
onNavigationStateChange={handleWebViewNavigationStateChange}
onLoadStart={() => setLoading(true)}
onLoadProgress={() => setLoading(true)}
onLoadEnd={() => setLoading(false)}
scrollEnabled
/>
{loading && renderLoadingComponent()}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
modal: {
flex: 1,
borderRadius: 4,
overflow: 'hidden',
},
webViewContainer: {
flex: 1,
},
WebViewStyle: {
flex: 1,
},
loadingContainer: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(255, 255, 255, 0.9)',
},
loadingText: {
marginTop: 10,
fontSize: 16,
color: '#666',
},
});
WebViewComponent.propTypes = propTypes;
export default WebViewComponent;