@robylon/react-native-sdk 2.0.2-staging.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,3 @@
1
- Object.defineProperty(exports,"__esModule",{value:true});exports.openChatbot=void 0;var _reactNative=require("react-native");var _constants=require("./constants");var openChatbot=exports.openChatbot=function openChatbot(chatbotId){var additionalParams=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var params=new URLSearchParams(Object.assign({id:chatbotId},additionalParams));var url=_constants.BASE_CHATBOT_URL+"?"+params.toString();_reactNative.Linking.openURL(url);};
1
+
2
+ //# sourceMappingURL=openChatbot.js.mape",{value:true});exports.openChatbot=void 0;var _reactNative=require("react-native");var _constants=require("./constants");var openChatbot=exports.openChatbot=function openChatbot(chatbotId){var additionalParams=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};var params=new URLSearchParams(Object.assign({id:chatbotId},additionalParams));var url=_constants.BASE_CHATBOT_URL+"?"+params.toString();_reactNative.Linking.openURL(url);};
2
3
  //# sourceMappingURL=openChatbot.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_reactNative","require","_constants","openChatbot","exports","chatbotId","additionalParams","arguments","length","undefined","params","URLSearchParams","Object","assign","id","url","BASE_CHATBOT_URL","toString","Linking","openURL"],"sourceRoot":"../../src","sources":["openChatbot.ts"],"mappings":"oFAAA,IAAAA,YAAA,CAAAC,OAAA,iBACA,IAAAC,UAAA,CAAAD,OAAA,gBAEO,GAAM,CAAAE,WAAW,CAAAC,OAAA,CAAAD,WAAA,CAAG,QAAd,CAAAA,WAAWA,CACtBE,SAAiB,CAER,IADT,CAAAC,gBAAwC,CAAAC,SAAA,CAAAC,MAAA,IAAAD,SAAA,MAAAE,SAAA,CAAAF,SAAA,IAAG,CAAC,CAAC,CAE7C,GAAM,CAAAG,MAAM,CAAG,GAAI,CAAAC,eAAe,CAAAC,MAAA,CAAAC,MAAA,EAAGC,EAAE,CAAET,SAAS,EAAKC,gBAAgB,CAAE,CAAC,CAC1E,GAAM,CAAAS,GAAG,CAAMC,2BAAgB,KAAIN,MAAM,CAACO,QAAQ,CAAC,CAAG,CACtDC,oBAAO,CAACC,OAAO,CAACJ,GAAG,CAAC,CACtB,CAAC","ignoreList":[]}
1
+ {"version":3,"names":[],"sourceRoot":"../../src","sources":["openChatbot.tsx"],"mappings":"","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robylon/react-native-sdk",
3
- "version": "2.0.2-staging.0",
3
+ "version": "2.0.2",
4
4
  "description": "React Native SDK for Robylon",
5
5
  "main": "lib/commonjs/index.js",
6
6
  "module": "lib/module/index.js",
@@ -1,44 +0,0 @@
1
- // File: src/ChatbotWebview.tsx
2
- import React, { useEffect, useState } from "react";
3
- import { View } from "react-native";
4
- import { WebView } from "react-native-webview";
5
- import { BASE_CHATBOT_URL } from "./constants";
6
- import LoadingIndicator from "./LoadingIndicator";
7
-
8
- interface ChatbotWebviewProps {
9
- chatbotId: string;
10
- additionalParams?: Record<string, string>;
11
- }
12
-
13
- const constructUrl = (
14
- chatbotId: string,
15
- additionalParams: Record<string, string> = {}
16
- ): string => {
17
- const params = new URLSearchParams({ id: chatbotId, ...additionalParams });
18
- return `${BASE_CHATBOT_URL}?${params.toString()}`;
19
- };
20
-
21
- export const ChatbotWebview: React.FC<ChatbotWebviewProps> = ({
22
- chatbotId,
23
- additionalParams,
24
- }) => {
25
- const [url, setUrl] = useState<string>("");
26
-
27
- useEffect(() => {
28
- setUrl(constructUrl(chatbotId, additionalParams));
29
- }, [chatbotId, additionalParams]);
30
-
31
- return (
32
- <View style={{ flex: 1 }}>
33
- {url ? (
34
- <WebView
35
- source={{ uri: url }}
36
- startInLoadingState={true}
37
- renderLoading={() => <LoadingIndicator />}
38
- />
39
- ) : (
40
- <LoadingIndicator />
41
- )}
42
- </View>
43
- );
44
- };
@@ -1,378 +0,0 @@
1
- import React, { useState, useEffect, useRef, useMemo } from "react";
2
- import {
3
- View,
4
- StyleSheet,
5
- Animated,
6
- Dimensions,
7
- Platform,
8
- StatusBar,
9
- } from "react-native";
10
- import { WebView, WebViewMessageEvent } from "react-native-webview";
11
- import FloatingButton from "./FloatingButton";
12
- import { API_URL, BASE_CHATBOT_URL } from "./constants";
13
-
14
- interface ChatbotSDKProps {
15
- api_key: string;
16
- user_id?: string;
17
- user_token?: string;
18
- additional_params?: Record<string, string>;
19
- show_floating_button?: boolean;
20
- onMessage?: (message: any) => void;
21
- isFullScreen?: boolean;
22
- enableAnimation?: boolean;
23
- onOpen?: () => void;
24
- onClose?: () => void;
25
- }
26
-
27
- interface ChatbotMessage {
28
- type: string;
29
- data?: any;
30
- }
31
-
32
- interface ChatbotConfig {
33
- brand_color: string;
34
- image_url: string;
35
- welcome_message: string;
36
- }
37
-
38
- const ChatbotSDK: React.FC<ChatbotSDKProps> = ({
39
- api_key,
40
- user_id,
41
- user_token,
42
- additional_params = {},
43
- show_floating_button = true,
44
- onMessage,
45
- isFullScreen = true,
46
- enableAnimation = true,
47
- onOpen,
48
- onClose,
49
- }) => {
50
- const [isWebViewVisible, setIsWebViewVisible] = useState(false);
51
- const [chatbotConfig, setChatbotConfig] = useState<ChatbotConfig | null>(
52
- null
53
- );
54
- const [loading, setLoading] = useState(true);
55
- const webViewRef = useRef<WebView>(null);
56
- const fadeAnim = useRef(new Animated.Value(0)).current;
57
- const isFirstLoadRef = useRef(true);
58
- const webViewUrl = useMemo(
59
- () => constructUrl(),
60
- [api_key, user_id, additional_params]
61
- );
62
-
63
- const stableAdditionalParams = useMemo(
64
- () => additional_params,
65
- [JSON.stringify(additional_params)]
66
- );
67
-
68
- const triggerAppInit = () => {
69
- if (webViewRef.current) {
70
- webViewRef.current.injectJavaScript(`
71
- // Trigger APP_READY equivalent
72
- window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'APP_READY' }));
73
- true;
74
- `);
75
- }
76
- };
77
-
78
- useEffect(() => {
79
- const fetchChatbotConfig = async () => {
80
- try {
81
- const endpointUrl = `${API_URL}/users/auth/copilot/`;
82
- const payload = {
83
- client_user_id: user_id,
84
- org_id: api_key,
85
- token: user_token,
86
- extra_info: stableAdditionalParams,
87
- };
88
- const response = await fetch(endpointUrl, {
89
- method: "POST",
90
- headers: {
91
- "Content-Type": "application/json",
92
- },
93
- body: JSON.stringify(payload),
94
- });
95
-
96
- if (!response.ok) {
97
- throw new Error("Failed to fetch chatbot configuration");
98
- }
99
-
100
- const data = await response.json();
101
- const orgInfo = data?.user?.org_info;
102
-
103
- if (orgInfo) {
104
- setChatbotConfig({
105
- brand_color: orgInfo?.config?.brand_colour || "#007AFF",
106
- image_url: orgInfo?.brand_config?.launcher_logo_url || "",
107
- welcome_message: orgInfo?.config?.welcome_message || "",
108
- });
109
- }
110
- } catch (error) {
111
- console.error("Error fetching chatbot configuration:", error);
112
- } finally {
113
- setLoading(false);
114
- }
115
- };
116
-
117
- if (api_key && user_id) {
118
- fetchChatbotConfig();
119
- }
120
- }, [api_key, user_id, user_token, stableAdditionalParams]);
121
-
122
- function constructUrl(): string {
123
- if (!user_id) return "";
124
- const params = new URLSearchParams({
125
- id: api_key,
126
- user_id,
127
- ...additional_params,
128
- });
129
- return `${BASE_CHATBOT_URL}?${params.toString()}`;
130
- }
131
-
132
- const openWebView = () => {
133
- setIsWebViewVisible(true);
134
- if (enableAnimation) {
135
- Animated.timing(fadeAnim, {
136
- toValue: 1,
137
- duration: 250,
138
- useNativeDriver: true,
139
- }).start(() => {
140
- onOpen?.();
141
- if (!isFirstLoadRef.current) {
142
- // If not first load, manually trigger initialization
143
- setTimeout(triggerAppInit, 100);
144
- }
145
- });
146
- } else {
147
- fadeAnim.setValue(1);
148
- requestAnimationFrame(() => {
149
- onOpen?.();
150
- if (!isFirstLoadRef.current) {
151
- setTimeout(triggerAppInit, 100);
152
- }
153
- });
154
- }
155
- };
156
-
157
- const closeWebView = () => {
158
- if (webViewRef.current) {
159
- console.log("closeWebView");
160
- webViewRef.current.postMessage(
161
- JSON.stringify({
162
- name: "closeFrame",
163
- domain: "app-domain.com",
164
- })
165
- );
166
- }
167
-
168
- const complete = () => {
169
- setIsWebViewVisible(false);
170
- onClose?.();
171
- };
172
-
173
- if (enableAnimation) {
174
- Animated.timing(fadeAnim, {
175
- toValue: 0,
176
- duration: 200,
177
- useNativeDriver: true,
178
- }).start(complete);
179
- } else {
180
- fadeAnim.setValue(0);
181
- requestAnimationFrame(complete);
182
- }
183
- };
184
-
185
- const handleMessage = (event: WebViewMessageEvent) => {
186
- const { data } = event.nativeEvent;
187
-
188
- try {
189
- const parsedData: ChatbotMessage = JSON.parse(data);
190
-
191
- if (parsedData.type === "APP_READY") {
192
- console.log("APP_READY", webViewRef.current && isWebViewVisible);
193
- if (webViewRef.current && isWebViewVisible) {
194
- webViewRef.current.injectJavaScript(`
195
- window.postMessage(${JSON.stringify({
196
- name: "openFrame",
197
- domain: "app-domain.com",
198
- })}, '*');
199
- window.postMessage(${JSON.stringify({
200
- name: "registerUserId",
201
- action: "registerUserId",
202
- domain: "app-domain.com",
203
- data: {
204
- userId: `${user_id}`,
205
- token: `${user_token}`,
206
- },
207
- })}, '*');
208
- `);
209
- }
210
- }
211
-
212
- if (typeof parsedData !== "object" || !parsedData.type) {
213
- console.warn("Invalid message structure");
214
- return;
215
- }
216
-
217
- switch (parsedData.type) {
218
- case "close_chatbot":
219
- closeWebView();
220
- break;
221
- default:
222
- if (onMessage) onMessage(parsedData);
223
- }
224
- } catch (error) {
225
- console.error("Failed to parse or process WebView message:", error);
226
- }
227
- };
228
-
229
- const getScreenDimensions = () => {
230
- const windowHeight = Dimensions.get("window").height;
231
- const windowWidth = Dimensions.get("window").width;
232
- const statusBarHeight =
233
- Platform.OS === "ios" ? 0 : StatusBar.currentHeight || 0;
234
-
235
- return {
236
- height: windowHeight - statusBarHeight,
237
- width: windowWidth,
238
- };
239
- };
240
-
241
- const screenDimensions = getScreenDimensions();
242
-
243
- return (
244
- <View style={styles.mainContainer}>
245
- {!loading && (
246
- <>
247
- {show_floating_button && api_key && chatbotConfig && (
248
- <FloatingButton
249
- onPress={openWebView}
250
- brandColor={chatbotConfig.brand_color}
251
- imageUrl={chatbotConfig.image_url}
252
- />
253
- )}
254
- <View
255
- style={[
256
- styles.webViewWrapper,
257
- { height: isWebViewVisible ? "100%" : 0 },
258
- ]}
259
- pointerEvents={isWebViewVisible ? "auto" : "none"}
260
- >
261
- <Animated.View
262
- style={[
263
- isFullScreen
264
- ? styles.fullScreenContainer
265
- : styles.inlineContainer,
266
- { opacity: fadeAnim },
267
- ]}
268
- >
269
- <WebView
270
- ref={webViewRef}
271
- source={{ uri: webViewUrl }}
272
- onMessage={handleMessage}
273
- style={styles.webview}
274
- containerStyle={
275
- isFullScreen
276
- ? {
277
- width: screenDimensions.width,
278
- height: screenDimensions.height,
279
- }
280
- : undefined
281
- }
282
- allowsInlineMediaPlayback={true}
283
- mediaPlaybackRequiresUserAction={false}
284
- allowFileAccess={false}
285
- geolocationEnabled={false}
286
- javaScriptEnabled={true}
287
- domStorageEnabled={true}
288
- cacheEnabled={true}
289
- scrollEnabled={true}
290
- bounces={false}
291
- onShouldStartLoadWithRequest={(request) => {
292
- return request.url.startsWith(BASE_CHATBOT_URL);
293
- }}
294
- startInLoadingState={isFirstLoadRef.current}
295
- onLoadEnd={() => {
296
- webViewRef.current?.injectJavaScript(`
297
- if (!document.querySelector('meta[name="viewport"]')) {
298
- var meta = document.createElement('meta');
299
- meta.name = 'viewport';
300
- meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';
301
- document.getElementsByTagName('head')[0].appendChild(meta);
302
- }
303
-
304
- if (!document.querySelector('#injectedStyle')) {
305
- const style = document.createElement('style');
306
- style.id = 'injectedStyle';
307
- style.textContent = \`
308
- html, body, #root, #__next {
309
- height: 100% !important;
310
- min-height: 100% !important;
311
- overflow: hidden !important;
312
- }
313
- \`;
314
- document.head.appendChild(style);
315
- }
316
-
317
- window.sendToChatbot = function(message) {
318
- window.ReactNativeWebView.postMessage(JSON.stringify(message));
319
- };
320
-
321
- var closeButton = document.querySelector('.chatbot-close-button');
322
- if (closeButton) {
323
- closeButton.addEventListener('click', () => {
324
- window.sendToChatbot({ type: 'close_chatbot' });
325
- });
326
- }
327
-
328
- true;
329
- `);
330
- isFirstLoadRef.current = false;
331
- }}
332
- onError={(syntheticEvent) => {
333
- const { nativeEvent } = syntheticEvent;
334
- console.warn("WebView error:", nativeEvent);
335
- }}
336
- />
337
- </Animated.View>
338
- </View>
339
- </>
340
- )}
341
- </View>
342
- );
343
- };
344
-
345
- const styles = StyleSheet.create({
346
- mainContainer: {
347
- flex: 1,
348
- position: "relative",
349
- },
350
- webViewWrapper: {
351
- position: "absolute",
352
- top: 0,
353
- left: 0,
354
- right: 0,
355
- bottom: 0,
356
- overflow: "hidden",
357
- zIndex: 1000,
358
- },
359
- fullScreenContainer: {
360
- position: "absolute",
361
- top: 0,
362
- left: 0,
363
- right: 0,
364
- bottom: 0,
365
- backgroundColor: "white",
366
- zIndex: 1000,
367
- },
368
- inlineContainer: {
369
- flex: 1,
370
- backgroundColor: "white",
371
- },
372
- webview: {
373
- flex: 1,
374
- backgroundColor: "white",
375
- },
376
- });
377
-
378
- export default ChatbotSDK;
@@ -1,57 +0,0 @@
1
- import React from "react";
2
- import { TouchableOpacity, StyleSheet, Text, Image } from "react-native";
3
-
4
- interface FloatingButtonProps {
5
- onPress: () => void;
6
- brandColor?: string; // For dynamic branding
7
- imageUrl?: string; // For brand logo or icon
8
- }
9
-
10
- const FloatingButton: React.FC<FloatingButtonProps> = ({
11
- onPress,
12
- brandColor = "#007AFF", // Default color
13
- imageUrl,
14
- }) => {
15
- return (
16
- <TouchableOpacity
17
- style={[styles.button, { backgroundColor: brandColor }]}
18
- onPress={onPress}
19
- >
20
- {imageUrl ? (
21
- <Image source={{ uri: imageUrl }} style={styles.buttonImage} />
22
- ) : (
23
- <Text style={styles.buttonText}>Chat</Text>
24
- )}
25
- </TouchableOpacity>
26
- );
27
- };
28
-
29
- const styles = StyleSheet.create({
30
- button: {
31
- position: "absolute",
32
- bottom: 20,
33
- right: 20,
34
- borderRadius: 24,
35
- width: 48,
36
- height: 48,
37
- justifyContent: "center",
38
- alignItems: "center",
39
- elevation: 5,
40
- shadowColor: "#000",
41
- shadowOffset: { width: 0, height: 2 },
42
- shadowOpacity: 0.25,
43
- shadowRadius: 3.84,
44
- },
45
- buttonText: {
46
- color: "white",
47
- fontSize: 16,
48
- fontWeight: "bold",
49
- },
50
- buttonImage: {
51
- width: 48,
52
- height: 48,
53
- borderRadius: 24,
54
- },
55
- });
56
-
57
- export default FloatingButton;
@@ -1,11 +0,0 @@
1
- import React, { useEffect, useState } from "react";
2
- import { View, ActivityIndicator } from "react-native";
3
- import { WebView } from "react-native-webview";
4
-
5
- const LoadingIndicator: React.FC = () => (
6
- <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
7
- <ActivityIndicator size="large" color="#0000ff" />
8
- </View>
9
- );
10
-
11
- export default LoadingIndicator;
package/src/config.ts DELETED
@@ -1,4 +0,0 @@
1
- export const config = {
2
- API_URL: process.env.API_URL || 'https://api.robylon.com',
3
- DEBUG: process.env.DEBUG === 'true'
4
- };
package/src/constants.ts DELETED
@@ -1,4 +0,0 @@
1
- const BASE_CHATBOT_DOMAIN = process.env.CHATBOT_DOMAIN || "";
2
- const CHATBOT_PATH = process.env.CHATBOT_PATH || "";
3
- export const BASE_CHATBOT_URL = `${BASE_CHATBOT_DOMAIN}/${CHATBOT_PATH}`;
4
- export const API_URL = process.env.API_URL;
package/src/global.d.ts DELETED
@@ -1,6 +0,0 @@
1
- // declare module "react-native" {
2
- // export const View: any;
3
- // export const ActivityIndicator: any;
4
- // export const Linking: any;
5
- // // Add other React Native exports as needed
6
- // }
package/src/index.tsx DELETED
@@ -1,2 +0,0 @@
1
- export { default as ChatbotSDK } from "./Chatbotsdk";
2
- export { openChatbot } from "./openChatbot";
@@ -1,11 +0,0 @@
1
- import { Linking } from "react-native";
2
- import { BASE_CHATBOT_URL } from "./constants";
3
-
4
- export const openChatbot = (
5
- chatbotId: string,
6
- additionalParams: Record<string, string> = {}
7
- ): void => {
8
- const params = new URLSearchParams({ id: chatbotId, ...additionalParams });
9
- const url = `${BASE_CHATBOT_URL}?${params.toString()}`;
10
- Linking.openURL(url);
11
- };
File without changes