Skip to content

changes #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module '@chatwoot/react-native-widget' {
};
// This can actually be any object
customAttributes?: Record<string, unknown>;
conversationCustomAttributes?: Record<string, unknown>;
}

class ChatWootWidget extends React.Component<ChatWootWidgetProps, any> {}
Expand Down
15 changes: 4 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ const propTypes = {
avatar_url: PropTypes.string,
email: PropTypes.string,
identifier_hash: PropTypes.string,
user_id: PropTypes.number,
}),
locale: PropTypes.string,
colorScheme: PropTypes.oneOf(['dark', 'light', 'auto']),
customAttributes: PropTypes.shape({}),
conversationCustomAttributes: PropTypes.shape({}),
closeModal: PropTypes.func,
};

Expand All @@ -32,6 +34,7 @@ const ChatWootWidget = ({
locale = 'en',
colorScheme = 'light',
customAttributes = {},
conversationCustomAttributes = {},
closeModal,
}) => {
const [cwCookie, setCookie] = useState('');
Expand All @@ -50,15 +53,6 @@ const ChatWootWidget = ({
appColorScheme,
});
return (
<Modal
backdropColor={COLOR_WHITE}
coverScreen
isVisible={isModalVisible}
onBackButtonPress={closeModal}
onBackdropPress={closeModal}
style={styles.modal}>
<SafeAreaView style={[styles.headerView, { backgroundColor: headerBackgroundColor }]} />
<SafeAreaView style={[styles.mainView, { backgroundColor: mainBackgroundColor }]}>
<WebView
websiteToken={websiteToken}
cwCookie={cwCookie}
Expand All @@ -67,10 +61,9 @@ const ChatWootWidget = ({
locale={locale}
colorScheme={colorScheme}
customAttributes={customAttributes}
conversationCustomAttributes={conversationCustomAttributes}
closeModal={closeModal}
/>
</SafeAreaView>
</Modal>
);
};

Expand Down
117 changes: 83 additions & 34 deletions src/WebView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { StyleSheet, Linking } from 'react-native';
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';
Expand All @@ -12,7 +12,8 @@ const propTypes = {
name: PropTypes.string,
avatar_url: PropTypes.string,
email: PropTypes.string,
identifier_hash: PropTypes.string,
identifier_hash: PropTypes.string,,
user_id: PropTypes.number,
}),
locale: PropTypes.string,
customAttributes: PropTypes.shape({}),
Expand All @@ -30,6 +31,7 @@ const WebViewComponent = ({
closeModal,
}) => {
const [currentUrl, setCurrentUrl] = React.useState(null);
const [loading, setLoading] = useState(true);
let widgetUrl = `${baseUrl}/widget?website_token=${websiteToken}&locale=${locale}`;

if (cwCookie) {
Expand Down Expand Up @@ -59,43 +61,72 @@ const WebViewComponent = ({
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 (
<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();
<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}
injectedJavaScript={injectedJavaScript}
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
onNavigationStateChange={handleWebViewNavigationStateChange}
scrollEnabled
/>
}}
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,
Expand All @@ -104,6 +135,24 @@ const styles = StyleSheet.create({
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;
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const WOOT_PREFIX = 'chatwoot-widget:';
export const POST_MESSAGE_EVENTS = {
SET_LOCALE: 'set-locale',
SET_CUSTOM_ATTRIBUTES: 'set-custom-attributes',
SET_CONVERSATION_CUSTOM_ATTRIBUTES: 'set-conversation-custom-attributes',
SET_USER: 'set-user',
SET_COLOR_SCHEME: 'set-color-scheme',
};
Expand Down
9 changes: 8 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const createWootPostMessage = (object) => {

export const getMessage = (data) => data.replace(WOOT_PREFIX, '');

export const generateScripts = ({ colorScheme, user, locale, customAttributes }) => {
export const generateScripts = ({ colorScheme, user, locale, customAttributes, conversationCustomAttributes }) => {
let script = '';
if (user) {
const userObject = {
Expand All @@ -46,6 +46,13 @@ export const generateScripts = ({ colorScheme, user, locale, customAttributes })
};
script += createWootPostMessage(attributeObject);
}
if (conversationCustomAttributes) {
const attributeObject = {
event: POST_MESSAGE_EVENTS.SET_CONVERSATION_CUSTOM_ATTRIBUTES,
conversationCustomAttributes,
};
script += createWootPostMessage(attributeObject);
}
if (colorScheme) {
const themeObject = { event: POST_MESSAGE_EVENTS.SET_COLOR_SCHEME, darkMode: colorScheme };
script += createWootPostMessage(themeObject);
Expand Down