@cobbai/react-native-sdk 1.0.0-beta.0
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/CobbaiMobileRnSdk.podspec +22 -0
- package/LICENSE +32 -0
- package/README.md +386 -0
- package/dist/ChatPlaceholder.d.ts +13 -0
- package/dist/ChatPlaceholder.d.ts.map +1 -0
- package/dist/ChatPlaceholder.js +87 -0
- package/dist/CobbaiChatScreen.d.ts +4 -0
- package/dist/CobbaiChatScreen.d.ts.map +1 -0
- package/dist/CobbaiChatScreen.js +196 -0
- package/dist/CobbaiInAppBanner.d.ts +15 -0
- package/dist/CobbaiInAppBanner.d.ts.map +1 -0
- package/dist/CobbaiInAppBanner.js +159 -0
- package/dist/CobbaiLauncher.d.ts +19 -0
- package/dist/CobbaiLauncher.d.ts.map +1 -0
- package/dist/CobbaiLauncher.js +149 -0
- package/dist/CobbaiWebView.d.ts +28 -0
- package/dist/CobbaiWebView.d.ts.map +1 -0
- package/dist/CobbaiWebView.js +141 -0
- package/dist/adapters/expoAdapter.d.ts +85 -0
- package/dist/adapters/expoAdapter.d.ts.map +1 -0
- package/dist/adapters/expoAdapter.js +152 -0
- package/dist/adapters/firebaseAdapter.d.ts +41 -0
- package/dist/adapters/firebaseAdapter.d.ts.map +1 -0
- package/dist/adapters/firebaseAdapter.js +139 -0
- package/dist/adapters/iosRelayAdapter.d.ts +26 -0
- package/dist/adapters/iosRelayAdapter.d.ts.map +1 -0
- package/dist/adapters/iosRelayAdapter.js +101 -0
- package/dist/androidKeyboardFrame.d.ts +16 -0
- package/dist/androidKeyboardFrame.d.ts.map +1 -0
- package/dist/androidKeyboardFrame.js +34 -0
- package/dist/bridge/messages.d.ts +20 -0
- package/dist/bridge/messages.d.ts.map +1 -0
- package/dist/bridge/messages.js +20 -0
- package/dist/bridge/nativeEvent.d.ts +10 -0
- package/dist/bridge/nativeEvent.d.ts.map +1 -0
- package/dist/bridge/nativeEvent.js +15 -0
- package/dist/bridge/parser.d.ts +29 -0
- package/dist/bridge/parser.d.ts.map +1 -0
- package/dist/bridge/parser.js +109 -0
- package/dist/chatPresentation.d.ts +19 -0
- package/dist/chatPresentation.d.ts.map +1 -0
- package/dist/chatPresentation.js +51 -0
- package/dist/chatScreen/useInAppBannerQueue.d.ts +22 -0
- package/dist/chatScreen/useInAppBannerQueue.d.ts.map +1 -0
- package/dist/chatScreen/useInAppBannerQueue.js +60 -0
- package/dist/chatScreen/useKeyboardFrame.d.ts +14 -0
- package/dist/chatScreen/useKeyboardFrame.d.ts.map +1 -0
- package/dist/chatScreen/useKeyboardFrame.js +63 -0
- package/dist/chatScreen/useLauncherPresentation.d.ts +20 -0
- package/dist/chatScreen/useLauncherPresentation.d.ts.map +1 -0
- package/dist/chatScreen/useLauncherPresentation.js +76 -0
- package/dist/chatScreen/useNativeToWebSync.d.ts +17 -0
- package/dist/chatScreen/useNativeToWebSync.d.ts.map +1 -0
- package/dist/chatScreen/useNativeToWebSync.js +36 -0
- package/dist/chatScreen/usePushIntegration.d.ts +19 -0
- package/dist/chatScreen/usePushIntegration.d.ts.map +1 -0
- package/dist/chatScreen/usePushIntegration.js +129 -0
- package/dist/chatScreen/useReadyLifecycle.d.ts +33 -0
- package/dist/chatScreen/useReadyLifecycle.d.ts.map +1 -0
- package/dist/chatScreen/useReadyLifecycle.js +138 -0
- package/dist/chatScreen/useSheetPresentation.d.ts +25 -0
- package/dist/chatScreen/useSheetPresentation.d.ts.map +1 -0
- package/dist/chatScreen/useSheetPresentation.js +98 -0
- package/dist/chatUnavailable.d.ts +23 -0
- package/dist/chatUnavailable.d.ts.map +1 -0
- package/dist/chatUnavailable.js +36 -0
- package/dist/externalLinks.d.ts +4 -0
- package/dist/externalLinks.d.ts.map +1 -0
- package/dist/externalLinks.js +48 -0
- package/dist/host/buildHostHtml.d.ts +7 -0
- package/dist/host/buildHostHtml.d.ts.map +1 -0
- package/dist/host/buildHostHtml.js +101 -0
- package/dist/host/hostPageKey.d.ts +7 -0
- package/dist/host/hostPageKey.d.ts.map +1 -0
- package/dist/host/hostPageKey.js +18 -0
- package/dist/inAppBanner.d.ts +19 -0
- package/dist/inAppBanner.d.ts.map +1 -0
- package/dist/inAppBanner.js +42 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/launcher.d.ts +25 -0
- package/dist/launcher.d.ts.map +1 -0
- package/dist/launcher.js +55 -0
- package/dist/launcherGlyph.d.ts +2 -0
- package/dist/launcherGlyph.d.ts.map +1 -0
- package/dist/launcherGlyph.js +5 -0
- package/dist/openChatScreen.d.ts +13 -0
- package/dist/openChatScreen.d.ts.map +1 -0
- package/dist/openChatScreen.js +63 -0
- package/dist/push.d.ts +59 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/push.js +309 -0
- package/dist/pushIntegration.d.ts +76 -0
- package/dist/pushIntegration.d.ts.map +1 -0
- package/dist/pushIntegration.js +244 -0
- package/dist/pushLogout.d.ts +57 -0
- package/dist/pushLogout.d.ts.map +1 -0
- package/dist/pushLogout.js +225 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +10 -0
- package/ios/CobbaiPush.m +96 -0
- package/ios/CobbaiPushRelay.h +59 -0
- package/ios/CobbaiPushRelay.m +364 -0
- package/package.json +69 -0
- package/src/ChatPlaceholder.tsx +132 -0
- package/src/CobbaiChatScreen.tsx +348 -0
- package/src/CobbaiInAppBanner.tsx +237 -0
- package/src/CobbaiLauncher.tsx +223 -0
- package/src/CobbaiWebView.tsx +242 -0
- package/src/adapters/expoAdapter.ts +227 -0
- package/src/adapters/firebaseAdapter.ts +201 -0
- package/src/adapters/iosRelayAdapter.ts +137 -0
- package/src/androidKeyboardFrame.ts +56 -0
- package/src/bridge/messages.ts +24 -0
- package/src/bridge/nativeEvent.ts +12 -0
- package/src/bridge/parser.ts +159 -0
- package/src/chatPresentation.ts +73 -0
- package/src/chatScreen/useInAppBannerQueue.ts +91 -0
- package/src/chatScreen/useKeyboardFrame.ts +89 -0
- package/src/chatScreen/useLauncherPresentation.ts +121 -0
- package/src/chatScreen/useNativeToWebSync.ts +54 -0
- package/src/chatScreen/usePushIntegration.ts +191 -0
- package/src/chatScreen/useReadyLifecycle.ts +186 -0
- package/src/chatScreen/useSheetPresentation.ts +147 -0
- package/src/chatUnavailable.ts +58 -0
- package/src/externalLinks.ts +56 -0
- package/src/host/buildHostHtml.ts +117 -0
- package/src/host/hostPageKey.ts +17 -0
- package/src/inAppBanner.ts +58 -0
- package/src/index.ts +53 -0
- package/src/launcher.ts +79 -0
- package/src/launcherGlyph.ts +3 -0
- package/src/openChatScreen.ts +78 -0
- package/src/push.ts +424 -0
- package/src/pushIntegration.ts +338 -0
- package/src/pushLogout.ts +277 -0
- package/src/types.ts +46 -0
- package/src/version.ts +8 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { type MutableRefObject, useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { getProcessGoneRecoveryDecision } from '../chatPresentation';
|
|
3
|
+
import { shouldAutoRetryOnOpen } from '../chatUnavailable';
|
|
4
|
+
import type { ProcessGoneEvent } from '../CobbaiWebView';
|
|
5
|
+
import type { CobbaiChatConfig, CobbaiError } from '../types';
|
|
6
|
+
|
|
7
|
+
export const READY_TIMEOUT_MS = 10_000;
|
|
8
|
+
|
|
9
|
+
function normalizeError(error: CobbaiError): CobbaiError {
|
|
10
|
+
return {
|
|
11
|
+
code: error.code || 'chat_error',
|
|
12
|
+
message: error.message || 'Unknown chat error',
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type ReadyLifecycleInput = {
|
|
17
|
+
visible: boolean;
|
|
18
|
+
/** Latest config; callbacks are read from it so a new config object restarts nothing. */
|
|
19
|
+
configRef: MutableRefObject<CobbaiChatConfig>;
|
|
20
|
+
/** Identifies the page the WebView loads (hostPageKey): a change is a new page. */
|
|
21
|
+
pageKey: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type ReadyLifecycle = {
|
|
25
|
+
ready: boolean;
|
|
26
|
+
failed: boolean;
|
|
27
|
+
/** Bumped on every cobbai:ready, including after a remount, so state can be re-sent. */
|
|
28
|
+
readySignal: number;
|
|
29
|
+
/** WebView key: bumping it remounts the WebView. */
|
|
30
|
+
webViewGeneration: number;
|
|
31
|
+
handleReady: () => void;
|
|
32
|
+
handleError: (error: CobbaiError) => void;
|
|
33
|
+
handleProcessGone: (event: ProcessGoneEvent) => void;
|
|
34
|
+
/** Remounts the WebView by hand (Retry button). */
|
|
35
|
+
retry: () => void;
|
|
36
|
+
/** The WebView went away with the sheet (no prewarm): the next open waits for ready again. */
|
|
37
|
+
resetReady: () => void;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Ready handshake, ready timeout, failure state and WebView remounts (retry, auto-retry on open,
|
|
42
|
+
* content process death).
|
|
43
|
+
*/
|
|
44
|
+
export function useReadyLifecycle({
|
|
45
|
+
visible,
|
|
46
|
+
configRef,
|
|
47
|
+
pageKey,
|
|
48
|
+
}: ReadyLifecycleInput): ReadyLifecycle {
|
|
49
|
+
const [ready, setReady] = useState(false);
|
|
50
|
+
const [failed, setFailed] = useState(false);
|
|
51
|
+
const [readySignal, setReadySignal] = useState(0);
|
|
52
|
+
const [remountCount, setRemountCount] = useState(0);
|
|
53
|
+
const [loadedPage, setLoadedPage] = useState({ key: pageKey, count: 0 });
|
|
54
|
+
// Bumped by a retry, a process-death remount, and a new page.
|
|
55
|
+
const webViewGeneration = remountCount + loadedPage.count;
|
|
56
|
+
const previousVisibleRef = useRef(false);
|
|
57
|
+
const processGoneOccurrencesRef = useRef<number[]>([]);
|
|
58
|
+
const readyTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
59
|
+
|
|
60
|
+
const clearReadyTimeout = useCallback((): void => {
|
|
61
|
+
if (readyTimeoutRef.current) {
|
|
62
|
+
clearTimeout(readyTimeoutRef.current);
|
|
63
|
+
readyTimeoutRef.current = null;
|
|
64
|
+
}
|
|
65
|
+
}, []);
|
|
66
|
+
|
|
67
|
+
const remountWebView = useCallback((): void => {
|
|
68
|
+
clearReadyTimeout();
|
|
69
|
+
setReady(false);
|
|
70
|
+
setFailed(false);
|
|
71
|
+
setRemountCount((count) => count + 1);
|
|
72
|
+
}, [clearReadyTimeout]);
|
|
73
|
+
|
|
74
|
+
// A new source is a new page (login, logout): the WebView is remounted and
|
|
75
|
+
// `ready` drops in this same render, before any effect runs. Resetting it in an effect left one
|
|
76
|
+
// commit where the other hooks saw the new config with `ready` still true and injected into the
|
|
77
|
+
// page being replaced (a logout unregister or a conversation to open was then lost), and the
|
|
78
|
+
// replaced page could still send a late cobbai:ready. The ready timer restarts through its
|
|
79
|
+
// webViewGeneration dependency.
|
|
80
|
+
if (loadedPage.key !== pageKey) {
|
|
81
|
+
setLoadedPage({ key: pageKey, count: loadedPage.count + 1 });
|
|
82
|
+
setReady(false);
|
|
83
|
+
setFailed(false);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
const wasVisible = previousVisibleRef.current;
|
|
88
|
+
const becameVisible = !wasVisible && visible;
|
|
89
|
+
previousVisibleRef.current = visible;
|
|
90
|
+
|
|
91
|
+
if (shouldAutoRetryOnOpen({ becameVisible, ready, failed })) {
|
|
92
|
+
remountWebView();
|
|
93
|
+
}
|
|
94
|
+
}, [failed, ready, remountWebView, visible]);
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
// Already failed: the unavailable state is shown and onError was reported; a ready_timeout
|
|
98
|
+
// on top would be a second error for the same failure.
|
|
99
|
+
if (!visible || ready || failed) return;
|
|
100
|
+
|
|
101
|
+
readyTimeoutRef.current = setTimeout(() => {
|
|
102
|
+
const timeoutError: CobbaiError = {
|
|
103
|
+
code: 'ready_timeout',
|
|
104
|
+
message: 'Chat did not become ready in time',
|
|
105
|
+
};
|
|
106
|
+
setFailed(true);
|
|
107
|
+
configRef.current.onError?.(timeoutError);
|
|
108
|
+
}, READY_TIMEOUT_MS);
|
|
109
|
+
|
|
110
|
+
return clearReadyTimeout;
|
|
111
|
+
}, [clearReadyTimeout, configRef, failed, ready, visible, webViewGeneration]);
|
|
112
|
+
|
|
113
|
+
const handleReady = (): void => {
|
|
114
|
+
clearReadyTimeout();
|
|
115
|
+
setFailed(false);
|
|
116
|
+
setReadySignal((currentSignal) => currentSignal + 1);
|
|
117
|
+
setReady(true);
|
|
118
|
+
configRef.current.onReady?.();
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const handleError = (error: CobbaiError): void => {
|
|
122
|
+
if (!ready) {
|
|
123
|
+
setFailed(true);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
configRef.current.onError?.(normalizeError(error));
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const handleProcessGone = (event: ProcessGoneEvent): void => {
|
|
130
|
+
const decision = getProcessGoneRecoveryDecision(processGoneOccurrencesRef.current, Date.now());
|
|
131
|
+
processGoneOccurrencesRef.current = decision.nextOccurrenceTimestampsMs;
|
|
132
|
+
|
|
133
|
+
const nextGeneration = webViewGeneration + (decision.shouldRemount ? 1 : 0);
|
|
134
|
+
|
|
135
|
+
if (__DEV__) {
|
|
136
|
+
console.log(
|
|
137
|
+
'[cobbai][process-gone]',
|
|
138
|
+
JSON.stringify({
|
|
139
|
+
platform: event.platform,
|
|
140
|
+
didCrash: event.didCrash,
|
|
141
|
+
generation: nextGeneration,
|
|
142
|
+
}),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const recoveryHint = decision.shouldRemount
|
|
147
|
+
? 'Attempting automatic remount.'
|
|
148
|
+
: 'Automatic remount disabled after repeated failures.';
|
|
149
|
+
const platformDetails =
|
|
150
|
+
event.platform === 'android'
|
|
151
|
+
? `platform=android didCrash=${event.didCrash === true ? 'true' : 'false'}`
|
|
152
|
+
: 'platform=ios';
|
|
153
|
+
|
|
154
|
+
configRef.current.onError?.({
|
|
155
|
+
code: 'webview_process_gone',
|
|
156
|
+
message: `WebView content process was terminated (${platformDetails}). ${recoveryHint}`,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
if (!decision.shouldRemount) {
|
|
160
|
+
// The WebView is dead and will not come back on its own. Leaving `ready` set would show a
|
|
161
|
+
// blank sheet whose only close button lived in the dead page (no way out on iOS); fall back
|
|
162
|
+
// to the unavailable placeholder, which has native close and retry buttons.
|
|
163
|
+
setReady(false);
|
|
164
|
+
setFailed(true);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
remountWebView();
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const resetReady = useCallback((): void => {
|
|
172
|
+
setReady(false);
|
|
173
|
+
}, []);
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
ready,
|
|
177
|
+
failed,
|
|
178
|
+
readySignal,
|
|
179
|
+
webViewGeneration,
|
|
180
|
+
handleReady,
|
|
181
|
+
handleError,
|
|
182
|
+
handleProcessGone,
|
|
183
|
+
retry: remountWebView,
|
|
184
|
+
resetReady,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, Dimensions, Easing, useWindowDimensions } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
getBackdropOpacityInterpolationRanges,
|
|
5
|
+
getOffscreenTranslateY,
|
|
6
|
+
LOADER_FADE_OUT_DURATION_MS,
|
|
7
|
+
SHEET_TRANSITION_DURATION_MS,
|
|
8
|
+
} from '../chatPresentation';
|
|
9
|
+
import { subscribeChatVisibility } from '../openChatScreen';
|
|
10
|
+
|
|
11
|
+
type SheetPresentationInput = {
|
|
12
|
+
/** Called once the close slide has finished and the sheet is off screen. */
|
|
13
|
+
onDismissed: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type SheetPresentation = {
|
|
17
|
+
/** Open requested (openChatScreen) and not yet closed. */
|
|
18
|
+
visible: boolean;
|
|
19
|
+
/** On screen, including during the close slide. */
|
|
20
|
+
presented: boolean;
|
|
21
|
+
overlayTranslateY: Animated.Value;
|
|
22
|
+
backdropOpacity: Animated.AnimatedInterpolation<number>;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Open/close state from the visibility store, and the sheet's slide and backdrop. */
|
|
26
|
+
export function useSheetPresentation({ onDismissed }: SheetPresentationInput): SheetPresentation {
|
|
27
|
+
const [visible, setVisible] = useState(false);
|
|
28
|
+
const [presented, setPresented] = useState(false);
|
|
29
|
+
const onDismissedRef = useRef(onDismissed);
|
|
30
|
+
onDismissedRef.current = onDismissed;
|
|
31
|
+
|
|
32
|
+
const overlayTranslateY = useRef(
|
|
33
|
+
new Animated.Value(getOffscreenTranslateY(Dimensions.get('window').height)),
|
|
34
|
+
).current;
|
|
35
|
+
const { height: windowHeight } = useWindowDimensions();
|
|
36
|
+
const offscreenTranslateY = useMemo(() => getOffscreenTranslateY(windowHeight), [windowHeight]);
|
|
37
|
+
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const unsubscribe = subscribeChatVisibility((nextVisible) => {
|
|
40
|
+
setVisible(nextVisible);
|
|
41
|
+
|
|
42
|
+
if (nextVisible) {
|
|
43
|
+
setPresented(true);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return unsubscribe;
|
|
48
|
+
}, []);
|
|
49
|
+
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!visible && !presented) {
|
|
52
|
+
overlayTranslateY.stopAnimation();
|
|
53
|
+
overlayTranslateY.setValue(offscreenTranslateY);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
overlayTranslateY.stopAnimation();
|
|
58
|
+
|
|
59
|
+
const slideAnimation = Animated.timing(overlayTranslateY, {
|
|
60
|
+
toValue: visible ? 0 : offscreenTranslateY,
|
|
61
|
+
duration: SHEET_TRANSITION_DURATION_MS,
|
|
62
|
+
easing: visible ? Easing.out(Easing.cubic) : Easing.in(Easing.cubic),
|
|
63
|
+
useNativeDriver: true,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
slideAnimation.start(({ finished }) => {
|
|
67
|
+
if (!finished || visible) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
setPresented(false);
|
|
72
|
+
onDismissedRef.current();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return () => {
|
|
76
|
+
slideAnimation.stop();
|
|
77
|
+
};
|
|
78
|
+
}, [offscreenTranslateY, overlayTranslateY, presented, visible]);
|
|
79
|
+
|
|
80
|
+
const backdropOpacity = useMemo(
|
|
81
|
+
() =>
|
|
82
|
+
overlayTranslateY.interpolate({
|
|
83
|
+
...getBackdropOpacityInterpolationRanges(offscreenTranslateY),
|
|
84
|
+
extrapolate: 'clamp',
|
|
85
|
+
}),
|
|
86
|
+
[offscreenTranslateY, overlayTranslateY],
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return { visible, presented, overlayTranslateY, backdropOpacity };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Keeps the loading/unavailable placeholder mounted while it is shown, and fades it out once the
|
|
94
|
+
* chat is ready (unmounted at once when the sheet is closed).
|
|
95
|
+
*/
|
|
96
|
+
export function useLoaderFade(
|
|
97
|
+
loaderVisible: boolean,
|
|
98
|
+
visible: boolean,
|
|
99
|
+
): {
|
|
100
|
+
loaderMounted: boolean;
|
|
101
|
+
loaderOpacity: Animated.Value;
|
|
102
|
+
} {
|
|
103
|
+
const [loaderMounted, setLoaderMounted] = useState(false);
|
|
104
|
+
const loaderOpacity = useRef(new Animated.Value(0)).current;
|
|
105
|
+
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
if (loaderVisible) {
|
|
108
|
+
setLoaderMounted(true);
|
|
109
|
+
loaderOpacity.stopAnimation();
|
|
110
|
+
loaderOpacity.setValue(1);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!loaderMounted) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!visible) {
|
|
119
|
+
loaderOpacity.stopAnimation();
|
|
120
|
+
loaderOpacity.setValue(0);
|
|
121
|
+
setLoaderMounted(false);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
loaderOpacity.stopAnimation();
|
|
126
|
+
const fadeOutAnimation = Animated.timing(loaderOpacity, {
|
|
127
|
+
toValue: 0,
|
|
128
|
+
duration: LOADER_FADE_OUT_DURATION_MS,
|
|
129
|
+
easing: Easing.linear,
|
|
130
|
+
useNativeDriver: true,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
fadeOutAnimation.start(({ finished }) => {
|
|
134
|
+
if (!finished) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
setLoaderMounted(false);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return () => {
|
|
142
|
+
fadeOutAnimation.stop();
|
|
143
|
+
};
|
|
144
|
+
}, [loaderMounted, loaderOpacity, loaderVisible, visible]);
|
|
145
|
+
|
|
146
|
+
return { loaderMounted, loaderOpacity };
|
|
147
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export type ChatPlaceholderState = 'hidden' | 'loading' | 'unavailable';
|
|
2
|
+
|
|
3
|
+
type PlaceholderStateInput = {
|
|
4
|
+
visible: boolean;
|
|
5
|
+
ready: boolean;
|
|
6
|
+
failed: boolean;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type AutoRetryInput = {
|
|
10
|
+
becameVisible: boolean;
|
|
11
|
+
ready: boolean;
|
|
12
|
+
failed: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type ChatUnavailableCopy = {
|
|
16
|
+
title: string;
|
|
17
|
+
body: string;
|
|
18
|
+
retry: string;
|
|
19
|
+
close: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const ENGLISH_COPY: ChatUnavailableCopy = {
|
|
23
|
+
title: 'Chat unavailable',
|
|
24
|
+
body: 'Check your connection and try again.',
|
|
25
|
+
retry: 'Try again',
|
|
26
|
+
close: 'Close',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const FRENCH_COPY: ChatUnavailableCopy = {
|
|
30
|
+
title: 'Chat indisponible',
|
|
31
|
+
body: 'Vérifiez votre connexion et réessayez.',
|
|
32
|
+
retry: 'Réessayer',
|
|
33
|
+
close: 'Fermer',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export function isFrenchLocale(locale: string | undefined): boolean {
|
|
37
|
+
if (!locale) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return locale.trim().toLowerCase().startsWith('fr');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getChatUnavailableCopy(locale: string | undefined): ChatUnavailableCopy {
|
|
45
|
+
return isFrenchLocale(locale) ? FRENCH_COPY : ENGLISH_COPY;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function deriveChatPlaceholderState(input: PlaceholderStateInput): ChatPlaceholderState {
|
|
49
|
+
if (!input.visible || input.ready) {
|
|
50
|
+
return 'hidden';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return input.failed ? 'unavailable' : 'loading';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function shouldAutoRetryOnOpen(input: AutoRetryInput): boolean {
|
|
57
|
+
return input.becameVisible && input.failed && !input.ready;
|
|
58
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const EXTERNAL_OPENABLE_PROTOCOLS = new Set(['http:', 'https:', 'mailto:', 'tel:']);
|
|
2
|
+
|
|
3
|
+
function parseUrl(value: string): URL | null {
|
|
4
|
+
try {
|
|
5
|
+
return new URL(value);
|
|
6
|
+
} catch {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function normalizePathname(pathname: string): string {
|
|
12
|
+
const trimmed = pathname.replace(/\/+$/, '');
|
|
13
|
+
return trimmed === '' ? '/' : trimmed;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isExternalOpenableUrl(url: string): boolean {
|
|
17
|
+
const parsed = parseUrl(url);
|
|
18
|
+
if (!parsed) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return EXTERNAL_OPENABLE_PROTOCOLS.has(parsed.protocol.toLowerCase());
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function isHostDocumentUrl(url: string, baseUrl: string): boolean {
|
|
26
|
+
if (url === 'about:blank') {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const parsedUrl = parseUrl(url);
|
|
31
|
+
const parsedBaseUrl = parseUrl(baseUrl);
|
|
32
|
+
|
|
33
|
+
if (!parsedUrl || !parsedBaseUrl) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return (
|
|
38
|
+
parsedUrl.origin === parsedBaseUrl.origin &&
|
|
39
|
+
normalizePathname(parsedUrl.pathname) === normalizePathname(parsedBaseUrl.pathname)
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function isFromHostOrigin(url: string | undefined, baseUrl: string): boolean {
|
|
44
|
+
if (typeof url !== 'string' || url.length === 0) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const parsedUrl = parseUrl(url);
|
|
49
|
+
const parsedBaseUrl = parseUrl(baseUrl);
|
|
50
|
+
|
|
51
|
+
if (!parsedUrl || !parsedBaseUrl) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return parsedUrl.origin === parsedBaseUrl.origin;
|
|
56
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { CobbaiChatConfig, CobbaiSafeAreaInsets } from '../types';
|
|
2
|
+
import { SDK_VERSION } from '../version';
|
|
3
|
+
|
|
4
|
+
type InitConfigPayload = {
|
|
5
|
+
auth_token?: string;
|
|
6
|
+
user_id?: string;
|
|
7
|
+
metadata?: CobbaiChatConfig['metadata'];
|
|
8
|
+
locale?: string;
|
|
9
|
+
safe_area: CobbaiSafeAreaInsets;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const ZERO_SAFE_AREA: CobbaiSafeAreaInsets = { top: 0, right: 0, bottom: 0, left: 0 };
|
|
13
|
+
|
|
14
|
+
export function normalizeBaseUrl(baseUrl: string): string {
|
|
15
|
+
return baseUrl.replace(/\/+$/, '');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** CSS pixels, integer, non-negative. */
|
|
19
|
+
export function normalizeSafeAreaInset(value: number): number {
|
|
20
|
+
if (!Number.isFinite(value)) return 0;
|
|
21
|
+
return Math.max(0, Math.round(value));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function normalizeSafeAreaInsets(
|
|
25
|
+
insets: Partial<CobbaiSafeAreaInsets> | undefined,
|
|
26
|
+
): CobbaiSafeAreaInsets {
|
|
27
|
+
return {
|
|
28
|
+
top: normalizeSafeAreaInset(insets?.top ?? 0),
|
|
29
|
+
right: normalizeSafeAreaInset(insets?.right ?? 0),
|
|
30
|
+
bottom: normalizeSafeAreaInset(insets?.bottom ?? 0),
|
|
31
|
+
left: normalizeSafeAreaInset(insets?.left ?? 0),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function escapeForInlineScript(input: string): string {
|
|
36
|
+
return input
|
|
37
|
+
.replace(/\\/g, '\\\\')
|
|
38
|
+
.replace(/'/g, "\\'")
|
|
39
|
+
.replace(/`/g, '\\`')
|
|
40
|
+
.replace(/\$\{/g, '\\${')
|
|
41
|
+
.replace(/<\//g, '<\\/');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function buildInitUrl(baseUrl: string, chatId: string): string {
|
|
45
|
+
const normalized = normalizeBaseUrl(baseUrl);
|
|
46
|
+
const params = new URLSearchParams({
|
|
47
|
+
chat_id: chatId,
|
|
48
|
+
embed_version: 'v2',
|
|
49
|
+
mode: 'mobile_sdk',
|
|
50
|
+
sdk_version: SDK_VERSION,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return `${normalized}/connectors/chats/conversations/init?${params.toString()}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function buildHostHtml(
|
|
57
|
+
config: CobbaiChatConfig,
|
|
58
|
+
safeArea: CobbaiSafeAreaInsets = ZERO_SAFE_AREA,
|
|
59
|
+
): string {
|
|
60
|
+
const initConfig: InitConfigPayload = {
|
|
61
|
+
auth_token: config.authToken,
|
|
62
|
+
user_id: config.userId,
|
|
63
|
+
metadata: config.metadata,
|
|
64
|
+
locale: config.locale,
|
|
65
|
+
safe_area: normalizeSafeAreaInsets(safeArea),
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const serializedConfig = escapeForInlineScript(JSON.stringify(initConfig));
|
|
69
|
+
const scriptSrc = escapeForInlineScript(buildInitUrl(config.baseUrl, config.chatId));
|
|
70
|
+
const chatId = escapeForInlineScript(config.chatId);
|
|
71
|
+
|
|
72
|
+
return `<!doctype html>
|
|
73
|
+
<html>
|
|
74
|
+
<head>
|
|
75
|
+
<meta charset="utf-8" />
|
|
76
|
+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover" />
|
|
77
|
+
<style>
|
|
78
|
+
html, body {
|
|
79
|
+
margin: 0;
|
|
80
|
+
padding: 0;
|
|
81
|
+
width: 100%;
|
|
82
|
+
height: 100%;
|
|
83
|
+
background: transparent;
|
|
84
|
+
-webkit-text-size-adjust: 100%;
|
|
85
|
+
}
|
|
86
|
+
#cobbai-chat-root {
|
|
87
|
+
width: 100%;
|
|
88
|
+
height: 100%;
|
|
89
|
+
}
|
|
90
|
+
</style>
|
|
91
|
+
</head>
|
|
92
|
+
<body>
|
|
93
|
+
<div id="cobbai-chat-root"></div>
|
|
94
|
+
<script>
|
|
95
|
+
(function () {
|
|
96
|
+
var chatId = '${chatId}';
|
|
97
|
+
var payload = JSON.parse('${serializedConfig}');
|
|
98
|
+
window.COBBAI_CHAT_CONFIG = window.COBBAI_CHAT_CONFIG || {};
|
|
99
|
+
window.COBBAI_CHAT_CONFIG[chatId] = payload;
|
|
100
|
+
|
|
101
|
+
var script = document.createElement('script');
|
|
102
|
+
script.src = '${scriptSrc}';
|
|
103
|
+
script.async = true;
|
|
104
|
+
script.addEventListener('error', function () {
|
|
105
|
+
if (
|
|
106
|
+
window.ReactNativeWebView &&
|
|
107
|
+
typeof window.ReactNativeWebView.postMessage === 'function'
|
|
108
|
+
) {
|
|
109
|
+
window.ReactNativeWebView.postMessage('{"type":"cobbai:error","payload":{"code":"init_script_load_error","message":"Chat script failed to load"}}');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
document.body.appendChild(script);
|
|
113
|
+
})();
|
|
114
|
+
</script>
|
|
115
|
+
</body>
|
|
116
|
+
</html>`;
|
|
117
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CobbaiChatConfig } from '../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The values the host page is built from: when this key
|
|
5
|
+
* changes, the WebView loads a new page and the previous one is gone.
|
|
6
|
+
*/
|
|
7
|
+
export function hostPageKey(config: CobbaiChatConfig): string {
|
|
8
|
+
const { baseUrl, chatId, authToken, userId, locale, metadata } = config;
|
|
9
|
+
return JSON.stringify([
|
|
10
|
+
baseUrl,
|
|
11
|
+
chatId,
|
|
12
|
+
authToken ?? null,
|
|
13
|
+
userId ?? null,
|
|
14
|
+
locale ?? null,
|
|
15
|
+
metadata ?? null,
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export const IN_APP_BANNER_APPEAR_DURATION_MS = 200;
|
|
2
|
+
export const IN_APP_BANNER_AUTO_HIDE_DURATION_MS = 5_000;
|
|
3
|
+
export const IN_APP_BANNER_FADE_OUT_DURATION_MS = 150;
|
|
4
|
+
|
|
5
|
+
export type CobbaiInAppBannerSource = 'live' | 'push';
|
|
6
|
+
|
|
7
|
+
export type CobbaiInAppBannerPayload = {
|
|
8
|
+
chatId: string;
|
|
9
|
+
conversationId: number;
|
|
10
|
+
messageId: number;
|
|
11
|
+
senderName?: string;
|
|
12
|
+
preview: string;
|
|
13
|
+
source: CobbaiInAppBannerSource;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function normalizeInAppBannerText(value: unknown): string | undefined {
|
|
17
|
+
if (typeof value !== 'string') {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const trimmed = value.trim();
|
|
22
|
+
if (trimmed.length === 0) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return trimmed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getInAppBannerMessageKey(conversationId: number, messageId: number): string {
|
|
30
|
+
return `${conversationId}:${messageId}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function getInAppBannerSenderFallback(_locale: string | undefined): string {
|
|
34
|
+
return 'Support';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function getInAppBannerOpenHint(locale: string | undefined): string {
|
|
38
|
+
if (typeof locale === 'string' && locale.toLowerCase().startsWith('fr')) {
|
|
39
|
+
return 'Ouvre la conversation';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return 'Opens the conversation';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function resolveInAppBannerSender(
|
|
46
|
+
senderName: string | undefined,
|
|
47
|
+
locale: string | undefined,
|
|
48
|
+
): string {
|
|
49
|
+
return normalizeInAppBannerText(senderName) ?? getInAppBannerSenderFallback(locale);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function getInAppBannerAccessibilityLabel(
|
|
53
|
+
banner: CobbaiInAppBannerPayload,
|
|
54
|
+
locale: string | undefined,
|
|
55
|
+
): string {
|
|
56
|
+
const sender = resolveInAppBannerSender(banner.senderName, locale);
|
|
57
|
+
return `${sender}: ${banner.preview}`;
|
|
58
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export { CobbaiChatScreen } from './CobbaiChatScreen';
|
|
2
|
+
export { SDK_VERSION } from './version';
|
|
3
|
+
export {
|
|
4
|
+
closeChatScreen,
|
|
5
|
+
getChatScreenVisibility as isChatScreenOpen,
|
|
6
|
+
openChatScreen,
|
|
7
|
+
} from './openChatScreen';
|
|
8
|
+
export {
|
|
9
|
+
clearPushToken,
|
|
10
|
+
handleCobbaiPushInForeground,
|
|
11
|
+
handleCobbaiPushTap,
|
|
12
|
+
isCobbaiPush,
|
|
13
|
+
setPushToken,
|
|
14
|
+
type CobbaiForegroundPush,
|
|
15
|
+
type CobbaiPushProvider,
|
|
16
|
+
type CobbaiPushToken,
|
|
17
|
+
} from './push';
|
|
18
|
+
export {
|
|
19
|
+
configureCobbaiPush,
|
|
20
|
+
getCobbaiPushStatus,
|
|
21
|
+
reinstallCobbaiPushRelay,
|
|
22
|
+
requestCobbaiPushPermission,
|
|
23
|
+
setCobbaiPushEnabled,
|
|
24
|
+
subscribeCobbaiPushStatus,
|
|
25
|
+
type CobbaiPushAdapter,
|
|
26
|
+
type CobbaiPushAdapterHandlers,
|
|
27
|
+
type CobbaiPushConfiguration,
|
|
28
|
+
type CobbaiPushStatus,
|
|
29
|
+
} from './pushIntegration';
|
|
30
|
+
export { logoutCobbai, type CobbaiPushStorage } from './pushLogout';
|
|
31
|
+
export {
|
|
32
|
+
expoAdapter,
|
|
33
|
+
expoNotificationData,
|
|
34
|
+
withCobbaiHandler,
|
|
35
|
+
type ExpoNotification,
|
|
36
|
+
type ExpoNotificationBehavior,
|
|
37
|
+
type ExpoNotificationHandler,
|
|
38
|
+
type ExpoNotificationResponse,
|
|
39
|
+
type ExpoNotificationsModule,
|
|
40
|
+
} from './adapters/expoAdapter';
|
|
41
|
+
export {
|
|
42
|
+
firebaseAdapter,
|
|
43
|
+
type FirebaseMessagingModule,
|
|
44
|
+
type FirebaseRemoteMessage,
|
|
45
|
+
} from './adapters/firebaseAdapter';
|
|
46
|
+
|
|
47
|
+
export type {
|
|
48
|
+
CobbaiChatConfig,
|
|
49
|
+
CobbaiChatScreenProps,
|
|
50
|
+
CobbaiError,
|
|
51
|
+
CobbaiMetadata,
|
|
52
|
+
CobbaiNewMessageEvent,
|
|
53
|
+
} from './types';
|