@robylon/react-native-sdk 2.0.19-staging.1 → 2.0.19-staging.10
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.
- package/lib/commonjs/ChatbotWebview.js +1 -1
- package/lib/commonjs/ChatbotWebview.js.map +1 -1
- package/lib/commonjs/Chatbotsdk.js +8 -1
- package/lib/commonjs/Chatbotsdk.js.map +1 -1
- package/lib/commonjs/config.js +1 -1
- package/lib/commonjs/config.js.map +1 -1
- package/lib/commonjs/constants/errorConstants.js +1 -1
- package/lib/commonjs/constants/errorConstants.js.map +1 -1
- package/lib/commonjs/constants.js +1 -1
- package/lib/commonjs/constants.js.map +1 -1
- package/lib/commonjs/hooks/useChatbotEvents.js +1 -1
- package/lib/commonjs/hooks/useChatbotEvents.js.map +1 -1
- package/lib/commonjs/openChatbot.js +2 -1
- package/lib/commonjs/openChatbot.js.map +1 -1
- package/lib/commonjs/services/ErrorTrackingService.js +1 -1
- package/lib/commonjs/services/ErrorTrackingService.js.map +1 -1
- package/lib/commonjs/utils/debugConfig.js +26 -1
- package/lib/commonjs/utils/debugConfig.js.map +1 -1
- package/lib/commonjs/utils/systemInfo.js +28 -1
- package/lib/commonjs/utils/systemInfo.js.map +1 -1
- package/lib/commonjs/utils/webViewStorage.js +33 -1
- package/lib/commonjs/utils/webViewStorage.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/commonjs/versions/version.dev.js +2 -0
- package/lib/commonjs/versions/version.dev.js.map +1 -0
- package/lib/commonjs/versions/version.production.js +2 -0
- package/lib/commonjs/versions/version.production.js.map +1 -0
- package/lib/commonjs/versions/version.staging.js +2 -0
- package/lib/commonjs/versions/version.staging.js.map +1 -0
- package/lib/module/ChatbotWebview.js +1 -1
- package/lib/module/ChatbotWebview.js.map +1 -1
- package/lib/module/Chatbotsdk.js +8 -1
- package/lib/module/Chatbotsdk.js.map +1 -1
- package/lib/module/config.js +1 -1
- package/lib/module/config.js.map +1 -1
- package/lib/module/constants/errorConstants.js +1 -1
- package/lib/module/constants/errorConstants.js.map +1 -1
- package/lib/module/constants.js +1 -1
- package/lib/module/constants.js.map +1 -1
- package/lib/module/hooks/useChatbotEvents.js +1 -1
- package/lib/module/hooks/useChatbotEvents.js.map +1 -1
- package/lib/module/openChatbot.js +1 -2
- package/lib/module/openChatbot.js.map +1 -1
- package/lib/module/services/ErrorTrackingService.js +1 -1
- package/lib/module/services/ErrorTrackingService.js.map +1 -1
- package/lib/module/utils/debugConfig.js +26 -1
- package/lib/module/utils/debugConfig.js.map +1 -1
- package/lib/module/utils/systemInfo.js +28 -1
- package/lib/module/utils/systemInfo.js.map +1 -1
- package/lib/module/utils/webViewStorage.js +33 -1
- package/lib/module/utils/webViewStorage.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/module/versions/version.dev.js +2 -0
- package/lib/module/versions/version.dev.js.map +1 -0
- package/lib/module/versions/version.production.js +2 -0
- package/lib/module/versions/version.production.js.map +1 -0
- package/lib/module/versions/version.staging.js +2 -0
- package/lib/module/versions/version.staging.js.map +1 -0
- package/lib/typescript/Chatbotsdk.d.ts.map +1 -1
- package/lib/typescript/utils/systemInfo.d.ts +1 -1
- package/lib/typescript/version.d.ts +1 -1
- package/lib/typescript/version.d.ts.map +1 -1
- package/lib/typescript/versions/version.dev.d.ts +2 -0
- package/lib/typescript/versions/version.dev.d.ts.map +1 -0
- package/lib/typescript/versions/version.production.d.ts +2 -0
- package/lib/typescript/versions/version.production.d.ts.map +1 -0
- package/lib/typescript/versions/version.staging.d.ts +2 -0
- package/lib/typescript/versions/version.staging.d.ts.map +1 -0
- package/package.json +7 -6
- package/scripts/update-version.js +21 -8
- package/src/ChatbotWebview.tsx +44 -0
- package/src/Chatbotsdk.tsx +608 -0
- package/src/FloatingButton.tsx +74 -0
- package/src/LoadingIndicator.tsx +11 -0
- package/src/Toast.tsx +65 -0
- package/src/components/DebugButton.tsx +46 -0
- package/src/components/ErrorBoundary.tsx +38 -0
- package/src/config.ts +4 -0
- package/src/constants/errorConstants.ts +21 -0
- package/src/constants.ts +4 -0
- package/src/global.d.ts +11 -0
- package/src/hooks/useChatbotEvents.ts +93 -0
- package/src/index.tsx +10 -0
- package/src/openChatbot.ts +11 -0
- package/src/openChatbot.tsx +0 -0
- package/src/services/ErrorTrackingService.ts +217 -0
- package/src/types/events.ts +25 -0
- package/src/types/react-native-flipper-performance-plugin.d.ts +3 -0
- package/src/types/react-native-globals.d.ts +10 -0
- package/src/utils/cookieUtils.ts +7 -0
- package/src/utils/debugConfig.ts +56 -0
- package/src/utils/debugMenu.ts +14 -0
- package/src/utils/errorHandler.ts +58 -0
- package/src/utils/logger.ts +14 -0
- package/src/utils/systemInfo.ts +84 -0
- package/src/utils/webViewStorage.ts +62 -0
- package/src/version.ts +2 -0
- package/src/versions/version.dev.ts +2 -0
- package/src/versions/version.production.ts +2 -0
- package/src/versions/version.staging.ts +2 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
useState,
|
|
3
|
+
useEffect,
|
|
4
|
+
useRef,
|
|
5
|
+
useMemo,
|
|
6
|
+
useCallback,
|
|
7
|
+
} from "react";
|
|
8
|
+
import {
|
|
9
|
+
View,
|
|
10
|
+
StyleSheet,
|
|
11
|
+
Animated,
|
|
12
|
+
Dimensions,
|
|
13
|
+
Platform,
|
|
14
|
+
StatusBar,
|
|
15
|
+
PixelRatio,
|
|
16
|
+
} from "react-native";
|
|
17
|
+
import { WebView, WebViewMessageEvent } from "react-native-webview";
|
|
18
|
+
import FloatingButton from "./FloatingButton";
|
|
19
|
+
import { API_URL, BASE_CHATBOT_URL } from "./constants";
|
|
20
|
+
import { SDK_VERSION } from "./version";
|
|
21
|
+
import { getSystemInfo, getBrowserAndOSInfoScript } from "./utils/systemInfo";
|
|
22
|
+
import { useChatbotEvents } from "./hooks/useChatbotEvents";
|
|
23
|
+
import { ChatbotEventType, ChatbotEventHandler } from "./types/events";
|
|
24
|
+
import { logger } from "./utils/logger";
|
|
25
|
+
import { enableNetworkDebug, enableWebViewDebug } from "./utils/debugConfig";
|
|
26
|
+
import DebugButton from "./components/DebugButton";
|
|
27
|
+
import { ErrorBoundary } from "./components/ErrorBoundary";
|
|
28
|
+
import {
|
|
29
|
+
ErrorCodes,
|
|
30
|
+
ErrorTypes,
|
|
31
|
+
ErrorMessages,
|
|
32
|
+
} from "./constants/errorConstants";
|
|
33
|
+
import { setupGlobalErrorHandlers } from "./utils/errorHandler";
|
|
34
|
+
import { errorTracker } from "./services/ErrorTrackingService";
|
|
35
|
+
import { generateUUID } from "./utils/cookieUtils";
|
|
36
|
+
import {
|
|
37
|
+
StorageMessageType,
|
|
38
|
+
StorageMessage,
|
|
39
|
+
getStorageScript,
|
|
40
|
+
createStorageMessage,
|
|
41
|
+
} from "./utils/webViewStorage";
|
|
42
|
+
|
|
43
|
+
export interface ChatbotProps {
|
|
44
|
+
api_key: string;
|
|
45
|
+
user_id?: string | null | number;
|
|
46
|
+
user_token?: string;
|
|
47
|
+
user_profile?: {
|
|
48
|
+
email?: string;
|
|
49
|
+
name?: string;
|
|
50
|
+
mobile?: string;
|
|
51
|
+
is_test_user?: boolean;
|
|
52
|
+
};
|
|
53
|
+
onEvent?: ChatbotEventHandler;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface InternalChatbotProps extends ChatbotProps {
|
|
57
|
+
show_floating_button?: boolean;
|
|
58
|
+
onMessage?: (message: any) => void;
|
|
59
|
+
isFullScreen?: boolean;
|
|
60
|
+
enableAnimation?: boolean;
|
|
61
|
+
onOpen?: () => void;
|
|
62
|
+
onClose?: () => void;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface ChatbotMessage {
|
|
66
|
+
type: string;
|
|
67
|
+
data?: any;
|
|
68
|
+
level?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface ChatbotConfig {
|
|
72
|
+
brand_color: string;
|
|
73
|
+
image_url: string;
|
|
74
|
+
welcome_message: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const Chatbot: React.FC<InternalChatbotProps> = ({
|
|
78
|
+
api_key,
|
|
79
|
+
user_id,
|
|
80
|
+
user_token,
|
|
81
|
+
show_floating_button = true,
|
|
82
|
+
onEvent,
|
|
83
|
+
onMessage,
|
|
84
|
+
isFullScreen = true,
|
|
85
|
+
enableAnimation = true,
|
|
86
|
+
onOpen,
|
|
87
|
+
onClose,
|
|
88
|
+
user_profile,
|
|
89
|
+
}) => {
|
|
90
|
+
const [isWebViewVisible, setIsWebViewVisible] = useState(false);
|
|
91
|
+
const [chatbotConfig, setChatbotConfig] = useState<ChatbotConfig | null>(
|
|
92
|
+
null
|
|
93
|
+
);
|
|
94
|
+
const [loading, setLoading] = useState(true);
|
|
95
|
+
const webViewRef = useRef<WebView>(null);
|
|
96
|
+
const fadeAnim = useRef(new Animated.Value(0)).current;
|
|
97
|
+
const isFirstLoadRef = useRef(true);
|
|
98
|
+
const [toastVisible, setToastVisible] = useState(false);
|
|
99
|
+
const [toastMessage, setToastMessage] = useState("");
|
|
100
|
+
const [effectiveUserId, setEffectiveUserId] = useState<string>();
|
|
101
|
+
const [isStorageReady, setIsStorageReady] = useState(false);
|
|
102
|
+
const pendingStorageOps = useRef<Array<() => void>>([]);
|
|
103
|
+
const memoryCache = useRef<Record<string, string>>({});
|
|
104
|
+
|
|
105
|
+
const [systemInfo, setSystemInfo] = useState({ os: "", browser: "" });
|
|
106
|
+
|
|
107
|
+
// Create chatbotConfig object for events
|
|
108
|
+
const chatbotConfigForEvents = useMemo(
|
|
109
|
+
() => ({
|
|
110
|
+
userId: effectiveUserId,
|
|
111
|
+
isAnonymous: !user_id && user_id !== 0,
|
|
112
|
+
// Add other config properties as needed
|
|
113
|
+
}),
|
|
114
|
+
[effectiveUserId, user_id]
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// Initialize event handling
|
|
118
|
+
const { emitEvent, onInternalEvent } = useChatbotEvents({
|
|
119
|
+
api_key,
|
|
120
|
+
chatbotConfig: chatbotConfigForEvents ?? {},
|
|
121
|
+
user_profile: user_profile,
|
|
122
|
+
onEvent: onEvent,
|
|
123
|
+
systemInfo: systemInfo ?? { os: "", browser: "" },
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const triggerAppInit = () => {
|
|
127
|
+
if (webViewRef.current) {
|
|
128
|
+
webViewRef.current.injectJavaScript(`
|
|
129
|
+
// Trigger APP_READY equivalent
|
|
130
|
+
window.ReactNativeWebView.postMessage(JSON.stringify({ type: 'APP_READY' }));
|
|
131
|
+
true;
|
|
132
|
+
`);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const executeStorageOp = useCallback(
|
|
137
|
+
(operation: () => void) => {
|
|
138
|
+
if (isStorageReady) {
|
|
139
|
+
operation();
|
|
140
|
+
} else {
|
|
141
|
+
pendingStorageOps.current.push(operation);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
[isStorageReady]
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
// Handle WebView messages
|
|
148
|
+
const handleMessage = useCallback(
|
|
149
|
+
(event: WebViewMessageEvent) => {
|
|
150
|
+
const { data } = event.nativeEvent;
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
const parsedData: ChatbotMessage = JSON.parse(data);
|
|
154
|
+
|
|
155
|
+
// Handle storage messages
|
|
156
|
+
if (
|
|
157
|
+
Object.values(StorageMessageType).includes(
|
|
158
|
+
parsedData.type as StorageMessageType
|
|
159
|
+
)
|
|
160
|
+
) {
|
|
161
|
+
const storageMessage = parsedData as StorageMessage;
|
|
162
|
+
switch (storageMessage.type) {
|
|
163
|
+
case StorageMessageType.STORAGE_READY:
|
|
164
|
+
setIsStorageReady(true);
|
|
165
|
+
pendingStorageOps.current.forEach((op) => op());
|
|
166
|
+
pendingStorageOps.current = [];
|
|
167
|
+
// Try to get stored ID immediately after storage is ready
|
|
168
|
+
webViewRef.current?.injectJavaScript(
|
|
169
|
+
createStorageMessage(
|
|
170
|
+
StorageMessageType.GET_STORAGE,
|
|
171
|
+
"rblyn_anon"
|
|
172
|
+
)
|
|
173
|
+
);
|
|
174
|
+
break;
|
|
175
|
+
case StorageMessageType.STORAGE_RESPONSE:
|
|
176
|
+
if (storageMessage.key && storageMessage.value) {
|
|
177
|
+
memoryCache.current[storageMessage.key] = storageMessage.value;
|
|
178
|
+
if (storageMessage.key === "rblyn_anon") {
|
|
179
|
+
setEffectiveUserId(storageMessage.value);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
case StorageMessageType.STORAGE_ERROR:
|
|
184
|
+
logger.error("WebView storage error:", storageMessage.error);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Add debug logging
|
|
191
|
+
if (__DEV__) {
|
|
192
|
+
if (parsedData?.type === "CONSOLE") {
|
|
193
|
+
console.log(`WebView ${parsedData?.level}:`, parsedData?.data);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (parsedData?.type === "ERROR") {
|
|
197
|
+
console.error("WebView Error:", parsedData?.data);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (parsedData?.type === "SYSTEM_INFO") {
|
|
203
|
+
setSystemInfo(parsedData?.data ?? { os: "", browser: "" });
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (parsedData?.type === "APP_READY") {
|
|
208
|
+
emitEvent(ChatbotEventType.CHATBOT_APP_READY);
|
|
209
|
+
if (webViewRef.current && isWebViewVisible) {
|
|
210
|
+
webViewRef.current?.injectJavaScript(getBrowserAndOSInfoScript());
|
|
211
|
+
|
|
212
|
+
const messageData = {
|
|
213
|
+
name: "registerUserId",
|
|
214
|
+
action: "registerUserId",
|
|
215
|
+
data: {
|
|
216
|
+
userId: effectiveUserId,
|
|
217
|
+
token: user_token ? `${user_token}` : undefined,
|
|
218
|
+
userProfile: {
|
|
219
|
+
...user_profile,
|
|
220
|
+
...getSystemInfo(systemInfo),
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
logger.debug("messageData====>", JSON.stringify(messageData));
|
|
225
|
+
webViewRef.current?.injectJavaScript(`
|
|
226
|
+
window.postMessage(${JSON.stringify({
|
|
227
|
+
name: "openFrame",
|
|
228
|
+
domain: "app-domain.com",
|
|
229
|
+
})}, '*');
|
|
230
|
+
window.postMessage(${JSON.stringify(messageData)}, '*');
|
|
231
|
+
`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Handle other message types
|
|
236
|
+
switch (parsedData?.type) {
|
|
237
|
+
case "close_chatbot":
|
|
238
|
+
closeWebView();
|
|
239
|
+
break;
|
|
240
|
+
case "CHATBOT_LOADED":
|
|
241
|
+
emitEvent(ChatbotEventType.CHATBOT_LOADED, parsedData?.data);
|
|
242
|
+
break;
|
|
243
|
+
case "CHAT_INITIALIZED":
|
|
244
|
+
emitEvent(ChatbotEventType.CHAT_INITIALIZED, parsedData?.data);
|
|
245
|
+
break;
|
|
246
|
+
case "SESSION_REFRESHED":
|
|
247
|
+
emitEvent(ChatbotEventType.SESSION_REFRESHED, parsedData?.data);
|
|
248
|
+
break;
|
|
249
|
+
case "CHAT_INITIALIZATION_FAILED":
|
|
250
|
+
emitEvent(
|
|
251
|
+
ChatbotEventType.CHAT_INITIALIZATION_FAILED,
|
|
252
|
+
parsedData?.data
|
|
253
|
+
);
|
|
254
|
+
break;
|
|
255
|
+
default:
|
|
256
|
+
if (onMessage) onMessage(parsedData ?? {});
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
errorTracker.trackError(
|
|
260
|
+
error instanceof Error
|
|
261
|
+
? error
|
|
262
|
+
: new Error("Failed to parse WebView message"),
|
|
263
|
+
"ChatbotSDK",
|
|
264
|
+
{
|
|
265
|
+
type: ErrorTypes.RUNTIME_ERROR,
|
|
266
|
+
context: { messageData: data },
|
|
267
|
+
}
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
[
|
|
272
|
+
emitEvent,
|
|
273
|
+
isWebViewVisible,
|
|
274
|
+
onMessage,
|
|
275
|
+
systemInfo,
|
|
276
|
+
effectiveUserId,
|
|
277
|
+
user_token,
|
|
278
|
+
user_profile,
|
|
279
|
+
]
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
// Initialize user ID
|
|
283
|
+
useEffect(() => {
|
|
284
|
+
const initializeUserId = async () => {
|
|
285
|
+
let userId = user_id ? String(user_id) : undefined;
|
|
286
|
+
|
|
287
|
+
if (!userId) {
|
|
288
|
+
// Try memory cache first
|
|
289
|
+
userId = memoryCache.current["rblyn_anon"];
|
|
290
|
+
|
|
291
|
+
if (!userId) {
|
|
292
|
+
userId = generateUUID();
|
|
293
|
+
executeStorageOp(() => {
|
|
294
|
+
webViewRef.current?.injectJavaScript(
|
|
295
|
+
createStorageMessage(
|
|
296
|
+
StorageMessageType.SET_STORAGE,
|
|
297
|
+
"rblyn_anon",
|
|
298
|
+
userId!
|
|
299
|
+
)
|
|
300
|
+
);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
setEffectiveUserId(userId);
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
initializeUserId();
|
|
309
|
+
}, [user_id, executeStorageOp]);
|
|
310
|
+
|
|
311
|
+
// Initialize WebView storage
|
|
312
|
+
useEffect(() => {
|
|
313
|
+
if (webViewRef.current) {
|
|
314
|
+
webViewRef.current.injectJavaScript(getStorageScript());
|
|
315
|
+
// Try to get stored ID once storage is ready
|
|
316
|
+
executeStorageOp(() => {
|
|
317
|
+
webViewRef.current?.injectJavaScript(
|
|
318
|
+
createStorageMessage(StorageMessageType.GET_STORAGE, "rblyn_anon")
|
|
319
|
+
);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
}, []);
|
|
323
|
+
|
|
324
|
+
useEffect(() => {
|
|
325
|
+
const fetchChatbotConfig = async () => {
|
|
326
|
+
try {
|
|
327
|
+
const endpointUrl = `${API_URL}/chat/chatbot/get/`;
|
|
328
|
+
const payload = {
|
|
329
|
+
client_user_id: effectiveUserId,
|
|
330
|
+
org_id: api_key,
|
|
331
|
+
token: user_token,
|
|
332
|
+
extra_info: {},
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
// setToastMessage(endpointUrl + " : " + BASE_CHATBOT_URL);
|
|
336
|
+
// setToastVisible(true);
|
|
337
|
+
|
|
338
|
+
const response = await fetch(endpointUrl, {
|
|
339
|
+
method: "POST",
|
|
340
|
+
headers: {
|
|
341
|
+
"Content-Type": "application/json",
|
|
342
|
+
},
|
|
343
|
+
body: JSON.stringify(payload),
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
if (!response.ok) {
|
|
347
|
+
throw new Error("Failed to fetch chatbot configuration");
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const data = await response.json();
|
|
351
|
+
const orgInfo = data?.user?.org_info;
|
|
352
|
+
|
|
353
|
+
if (orgInfo) {
|
|
354
|
+
setChatbotConfig({
|
|
355
|
+
brand_color: orgInfo?.config?.brand_colour || "#007AFF",
|
|
356
|
+
image_url: orgInfo?.brand_config?.launcher_logo_url || "",
|
|
357
|
+
welcome_message: orgInfo?.config?.welcome_message || "",
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
} catch (error) {
|
|
361
|
+
errorTracker.trackError(
|
|
362
|
+
error instanceof Error
|
|
363
|
+
? error
|
|
364
|
+
: new Error("Failed to fetch chatbot configuration"),
|
|
365
|
+
"ChatbotSDK",
|
|
366
|
+
{
|
|
367
|
+
type: ErrorTypes.NETWORK_ERROR,
|
|
368
|
+
context: { api_key, user_id },
|
|
369
|
+
}
|
|
370
|
+
);
|
|
371
|
+
} finally {
|
|
372
|
+
setLoading(false);
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
if (api_key && effectiveUserId) {
|
|
377
|
+
fetchChatbotConfig();
|
|
378
|
+
}
|
|
379
|
+
}, [api_key, effectiveUserId, user_token]);
|
|
380
|
+
|
|
381
|
+
function constructUrl(): string {
|
|
382
|
+
if (!effectiveUserId) return "";
|
|
383
|
+
const params = new URLSearchParams({
|
|
384
|
+
id: api_key,
|
|
385
|
+
});
|
|
386
|
+
console.log("BASE_CHATBOT_URL SDK_VERSION", BASE_CHATBOT_URL, SDK_VERSION);
|
|
387
|
+
return `${BASE_CHATBOT_URL}?${params.toString()}`;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Handle opening the chatbot
|
|
391
|
+
const openWebView = useCallback(() => {
|
|
392
|
+
setIsWebViewVisible(true);
|
|
393
|
+
if (enableAnimation) {
|
|
394
|
+
Animated.timing(fadeAnim, {
|
|
395
|
+
toValue: 1,
|
|
396
|
+
duration: 250,
|
|
397
|
+
useNativeDriver: true,
|
|
398
|
+
}).start(() => {
|
|
399
|
+
onOpen?.();
|
|
400
|
+
emitEvent(ChatbotEventType.CHATBOT_OPENED);
|
|
401
|
+
if (!isFirstLoadRef.current) {
|
|
402
|
+
setTimeout(triggerAppInit, 100);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
} else {
|
|
406
|
+
fadeAnim.setValue(1);
|
|
407
|
+
requestAnimationFrame(() => {
|
|
408
|
+
onOpen?.();
|
|
409
|
+
emitEvent(ChatbotEventType.CHATBOT_OPENED);
|
|
410
|
+
if (!isFirstLoadRef.current) {
|
|
411
|
+
setTimeout(triggerAppInit, 100);
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}, [enableAnimation, fadeAnim, onOpen, emitEvent]);
|
|
416
|
+
|
|
417
|
+
// Handle closing the chatbot
|
|
418
|
+
const closeWebView = useCallback(() => {
|
|
419
|
+
if (webViewRef.current) {
|
|
420
|
+
webViewRef.current.postMessage(
|
|
421
|
+
JSON.stringify({
|
|
422
|
+
name: "closeFrame",
|
|
423
|
+
domain: "app-domain.com",
|
|
424
|
+
})
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const complete = () => {
|
|
429
|
+
setIsWebViewVisible(false);
|
|
430
|
+
onClose?.();
|
|
431
|
+
emitEvent(ChatbotEventType.CHATBOT_CLOSED);
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
if (enableAnimation) {
|
|
435
|
+
Animated.timing(fadeAnim, {
|
|
436
|
+
toValue: 0,
|
|
437
|
+
duration: 200,
|
|
438
|
+
useNativeDriver: true,
|
|
439
|
+
}).start(complete);
|
|
440
|
+
} else {
|
|
441
|
+
fadeAnim.setValue(0);
|
|
442
|
+
requestAnimationFrame(complete);
|
|
443
|
+
}
|
|
444
|
+
}, [enableAnimation, fadeAnim, onClose, emitEvent]);
|
|
445
|
+
|
|
446
|
+
// Emit button loaded event when component mounts
|
|
447
|
+
useEffect(() => {
|
|
448
|
+
if (show_floating_button && chatbotConfig) {
|
|
449
|
+
emitEvent(ChatbotEventType.CHATBOT_BUTTON_LOADED);
|
|
450
|
+
}
|
|
451
|
+
}, [show_floating_button, chatbotConfig, emitEvent]);
|
|
452
|
+
|
|
453
|
+
const getScreenDimensions = () => {
|
|
454
|
+
const windowHeight = Dimensions.get("window").height;
|
|
455
|
+
const windowWidth = Dimensions.get("window").width;
|
|
456
|
+
const statusBarHeight =
|
|
457
|
+
Platform.OS === "ios" ? 0 : StatusBar.currentHeight || 0;
|
|
458
|
+
|
|
459
|
+
return {
|
|
460
|
+
height: windowHeight - statusBarHeight,
|
|
461
|
+
width: windowWidth,
|
|
462
|
+
};
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
const screenDimensions = getScreenDimensions();
|
|
466
|
+
|
|
467
|
+
useEffect(() => {
|
|
468
|
+
if (__DEV__) {
|
|
469
|
+
enableNetworkDebug();
|
|
470
|
+
}
|
|
471
|
+
}, []);
|
|
472
|
+
|
|
473
|
+
// Deliberate error for testing error boundary
|
|
474
|
+
// useEffect(() => {
|
|
475
|
+
// if (__DEV__) {
|
|
476
|
+
// // Set up global error handlers
|
|
477
|
+
// setupGlobalErrorHandlers();
|
|
478
|
+
|
|
479
|
+
// // Test async error
|
|
480
|
+
// setTimeout(() => {
|
|
481
|
+
// Promise.reject(new Error("Test async error"));
|
|
482
|
+
// }, 1000);
|
|
483
|
+
|
|
484
|
+
// // Test sync error
|
|
485
|
+
// setTimeout(() => {
|
|
486
|
+
// throw new Error("Test sync error");
|
|
487
|
+
// }, 2000);
|
|
488
|
+
// }
|
|
489
|
+
// }, []);
|
|
490
|
+
|
|
491
|
+
return (
|
|
492
|
+
<ErrorBoundary componentName="ChatbotSDK">
|
|
493
|
+
<View style={styles.mainContainer}>
|
|
494
|
+
{!loading && (
|
|
495
|
+
<>
|
|
496
|
+
<ErrorBoundary componentName="FloatingButton">
|
|
497
|
+
{show_floating_button && api_key && chatbotConfig && (
|
|
498
|
+
<FloatingButton
|
|
499
|
+
onPress={() => {
|
|
500
|
+
emitEvent(ChatbotEventType.CHATBOT_BUTTON_CLICKED);
|
|
501
|
+
openWebView();
|
|
502
|
+
}}
|
|
503
|
+
brandColor={chatbotConfig.brand_color}
|
|
504
|
+
imageUrl={chatbotConfig.image_url}
|
|
505
|
+
/>
|
|
506
|
+
)}
|
|
507
|
+
</ErrorBoundary>
|
|
508
|
+
<View
|
|
509
|
+
style={[
|
|
510
|
+
styles.webViewWrapper,
|
|
511
|
+
{ height: isWebViewVisible ? "100%" : 0 },
|
|
512
|
+
]}
|
|
513
|
+
pointerEvents={isWebViewVisible ? "auto" : "none"}
|
|
514
|
+
>
|
|
515
|
+
<Animated.View
|
|
516
|
+
style={[styles.fullScreenContainer, { opacity: fadeAnim }]}
|
|
517
|
+
>
|
|
518
|
+
<ErrorBoundary componentName="ChatbotWebView">
|
|
519
|
+
<WebView
|
|
520
|
+
ref={webViewRef}
|
|
521
|
+
source={{ uri: constructUrl() }}
|
|
522
|
+
onMessage={handleMessage}
|
|
523
|
+
style={styles.webview}
|
|
524
|
+
containerStyle={
|
|
525
|
+
isFullScreen
|
|
526
|
+
? {
|
|
527
|
+
width: screenDimensions.width,
|
|
528
|
+
height: screenDimensions.height,
|
|
529
|
+
}
|
|
530
|
+
: undefined
|
|
531
|
+
}
|
|
532
|
+
allowsInlineMediaPlayback={true}
|
|
533
|
+
mediaPlaybackRequiresUserAction={false}
|
|
534
|
+
allowFileAccess={false}
|
|
535
|
+
geolocationEnabled={false}
|
|
536
|
+
javaScriptEnabled={true}
|
|
537
|
+
domStorageEnabled={true}
|
|
538
|
+
cacheEnabled={true}
|
|
539
|
+
scrollEnabled={true}
|
|
540
|
+
bounces={false}
|
|
541
|
+
onShouldStartLoadWithRequest={(request) => {
|
|
542
|
+
return request.url.startsWith(BASE_CHATBOT_URL);
|
|
543
|
+
}}
|
|
544
|
+
startInLoadingState={isFirstLoadRef.current}
|
|
545
|
+
onLoadEnd={() => {
|
|
546
|
+
isFirstLoadRef.current = false;
|
|
547
|
+
}}
|
|
548
|
+
onError={(syntheticEvent) => {
|
|
549
|
+
const { nativeEvent } = syntheticEvent;
|
|
550
|
+
errorTracker.trackError(
|
|
551
|
+
new Error(nativeEvent.description),
|
|
552
|
+
"ChatbotWebView",
|
|
553
|
+
{
|
|
554
|
+
type: ErrorTypes.RUNTIME_ERROR,
|
|
555
|
+
context: {
|
|
556
|
+
url: nativeEvent.url,
|
|
557
|
+
code: nativeEvent.code,
|
|
558
|
+
},
|
|
559
|
+
}
|
|
560
|
+
);
|
|
561
|
+
}}
|
|
562
|
+
/>
|
|
563
|
+
</ErrorBoundary>
|
|
564
|
+
</Animated.View>
|
|
565
|
+
</View>
|
|
566
|
+
</>
|
|
567
|
+
)}
|
|
568
|
+
{/* <Toast message={toastMessage} visible={toastVisible} duration={10000} /> */}
|
|
569
|
+
{__DEV__ && <DebugButton />}
|
|
570
|
+
</View>
|
|
571
|
+
</ErrorBoundary>
|
|
572
|
+
);
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
const styles = StyleSheet.create({
|
|
576
|
+
mainContainer: {
|
|
577
|
+
flex: 1,
|
|
578
|
+
position: "relative",
|
|
579
|
+
},
|
|
580
|
+
webViewWrapper: {
|
|
581
|
+
position: "absolute",
|
|
582
|
+
top: 0,
|
|
583
|
+
left: 0,
|
|
584
|
+
right: 0,
|
|
585
|
+
bottom: 0,
|
|
586
|
+
overflow: "hidden",
|
|
587
|
+
zIndex: 1000,
|
|
588
|
+
},
|
|
589
|
+
fullScreenContainer: {
|
|
590
|
+
position: "absolute",
|
|
591
|
+
top: 0,
|
|
592
|
+
left: 0,
|
|
593
|
+
right: 0,
|
|
594
|
+
bottom: 0,
|
|
595
|
+
backgroundColor: "white",
|
|
596
|
+
zIndex: 1000,
|
|
597
|
+
},
|
|
598
|
+
inlineContainer: {
|
|
599
|
+
flex: 1,
|
|
600
|
+
backgroundColor: "white",
|
|
601
|
+
},
|
|
602
|
+
webview: {
|
|
603
|
+
flex: 1,
|
|
604
|
+
backgroundColor: "white",
|
|
605
|
+
},
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
export default Chatbot;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { TouchableOpacity, StyleSheet, Text, Image } from "react-native";
|
|
3
|
+
import { errorTracker } from "./services/ErrorTrackingService";
|
|
4
|
+
import { ErrorTypes } from "./constants/errorConstants";
|
|
5
|
+
|
|
6
|
+
interface FloatingButtonProps {
|
|
7
|
+
onPress: () => void;
|
|
8
|
+
brandColor?: string; // For dynamic branding
|
|
9
|
+
imageUrl?: string; // For brand logo or icon
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const FloatingButton: React.FC<FloatingButtonProps> = ({
|
|
13
|
+
onPress,
|
|
14
|
+
brandColor = "#007AFF", // Default color
|
|
15
|
+
imageUrl,
|
|
16
|
+
}) => {
|
|
17
|
+
const handleImageError = () => {
|
|
18
|
+
errorTracker.trackError(
|
|
19
|
+
new Error("Failed to load button image"),
|
|
20
|
+
"FloatingButton",
|
|
21
|
+
{
|
|
22
|
+
type: ErrorTypes.RUNTIME_ERROR,
|
|
23
|
+
context: { imageUrl },
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<TouchableOpacity
|
|
30
|
+
style={[styles.button, { backgroundColor: brandColor }]}
|
|
31
|
+
onPress={onPress}
|
|
32
|
+
>
|
|
33
|
+
{imageUrl ? (
|
|
34
|
+
<Image
|
|
35
|
+
source={{ uri: imageUrl }}
|
|
36
|
+
style={styles.buttonImage}
|
|
37
|
+
onError={handleImageError}
|
|
38
|
+
/>
|
|
39
|
+
) : (
|
|
40
|
+
<Text style={styles.buttonText}>Chat</Text>
|
|
41
|
+
)}
|
|
42
|
+
</TouchableOpacity>
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const styles = StyleSheet.create({
|
|
47
|
+
button: {
|
|
48
|
+
position: "absolute",
|
|
49
|
+
bottom: 20,
|
|
50
|
+
right: 20,
|
|
51
|
+
borderRadius: 24,
|
|
52
|
+
width: 48,
|
|
53
|
+
height: 48,
|
|
54
|
+
justifyContent: "center",
|
|
55
|
+
alignItems: "center",
|
|
56
|
+
elevation: 5,
|
|
57
|
+
shadowColor: "#000",
|
|
58
|
+
shadowOffset: { width: 0, height: 2 },
|
|
59
|
+
shadowOpacity: 0.25,
|
|
60
|
+
shadowRadius: 3.84,
|
|
61
|
+
},
|
|
62
|
+
buttonText: {
|
|
63
|
+
color: "white",
|
|
64
|
+
fontSize: 16,
|
|
65
|
+
fontWeight: "bold",
|
|
66
|
+
},
|
|
67
|
+
buttonImage: {
|
|
68
|
+
width: 48,
|
|
69
|
+
height: 48,
|
|
70
|
+
borderRadius: 24,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export default FloatingButton;
|
|
@@ -0,0 +1,11 @@
|
|
|
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;
|