@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,348 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, BackHandler, Platform, StyleSheet } from 'react-native';
|
|
3
|
+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
|
4
|
+
import { CobbaiWebView, type CobbaiWebViewRef } from './CobbaiWebView';
|
|
5
|
+
import { hostPageKey } from './host/hostPageKey';
|
|
6
|
+
import { withKeyboardSafeAreaBottom } from './androidKeyboardFrame';
|
|
7
|
+
import {
|
|
8
|
+
isChatSurfaceOnScreen,
|
|
9
|
+
shouldRenderChatSurface,
|
|
10
|
+
toSheetSafeAreaInsetsForWeb,
|
|
11
|
+
} from './chatPresentation';
|
|
12
|
+
import { ChatPlaceholder } from './ChatPlaceholder';
|
|
13
|
+
import { CobbaiInAppBanner } from './CobbaiInAppBanner';
|
|
14
|
+
import type {
|
|
15
|
+
ValidatedNewMessagePayload,
|
|
16
|
+
ValidatedPushTokenRegisteredPayload,
|
|
17
|
+
} from './bridge/parser';
|
|
18
|
+
import { normalizeSafeAreaInsets } from './host/buildHostHtml';
|
|
19
|
+
import { closeChatScreenInternal, openChatScreen } from './openChatScreen';
|
|
20
|
+
import { clearPendingOpen, getPushStoreSnapshot } from './push';
|
|
21
|
+
import { rememberUnregisterSecret } from './pushLogout';
|
|
22
|
+
import { deriveChatPlaceholderState } from './chatUnavailable';
|
|
23
|
+
import {
|
|
24
|
+
getLauncherAccessibilityLabel,
|
|
25
|
+
getLauncherUnreadAccessibilityText,
|
|
26
|
+
LAUNCHER_EDGE_OFFSET,
|
|
27
|
+
resolveLauncherBottomOffset,
|
|
28
|
+
} from './launcher';
|
|
29
|
+
import { CobbaiLauncher } from './CobbaiLauncher';
|
|
30
|
+
import { useInAppBannerQueue } from './chatScreen/useInAppBannerQueue';
|
|
31
|
+
import { useKeyboardFrame } from './chatScreen/useKeyboardFrame';
|
|
32
|
+
import { useLauncherPresentation } from './chatScreen/useLauncherPresentation';
|
|
33
|
+
import { useNativeToWebSync } from './chatScreen/useNativeToWebSync';
|
|
34
|
+
import { usePushIntegration } from './chatScreen/usePushIntegration';
|
|
35
|
+
import { useReadyLifecycle } from './chatScreen/useReadyLifecycle';
|
|
36
|
+
import { useLoaderFade, useSheetPresentation } from './chatScreen/useSheetPresentation';
|
|
37
|
+
import type { CobbaiChatScreenProps } from './types';
|
|
38
|
+
|
|
39
|
+
export function CobbaiChatScreen(props: CobbaiChatScreenProps): React.JSX.Element {
|
|
40
|
+
const {
|
|
41
|
+
config,
|
|
42
|
+
prewarm = true,
|
|
43
|
+
launcherVisible = true,
|
|
44
|
+
launcherBottomOffset,
|
|
45
|
+
inAppBanner = true,
|
|
46
|
+
} = props;
|
|
47
|
+
|
|
48
|
+
// Latest config for callbacks read from effects and stable handlers: an integrator passing a
|
|
49
|
+
// new `config` object on each render must not restart the ready timer or re-subscribe handlers.
|
|
50
|
+
const configRef = useRef(config);
|
|
51
|
+
configRef.current = config;
|
|
52
|
+
const webViewRef = useRef<CobbaiWebViewRef | null>(null);
|
|
53
|
+
|
|
54
|
+
const [unreadTotal, setUnreadTotal] = useState<number | null>(null);
|
|
55
|
+
const hasReportedUnreadRef = useRef(false);
|
|
56
|
+
const lastReportedUnreadRef = useRef<number | null>(null);
|
|
57
|
+
|
|
58
|
+
const { top, right, bottom, left } = useSafeAreaInsets();
|
|
59
|
+
const safeArea = useMemo(
|
|
60
|
+
() => normalizeSafeAreaInsets({ top, right, bottom, left }),
|
|
61
|
+
[top, right, bottom, left],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
// Frozen on first render: the host HTML must not be rebuilt when insets change.
|
|
65
|
+
const initialSafeAreaRef = useRef(toSheetSafeAreaInsetsForWeb(safeArea));
|
|
66
|
+
|
|
67
|
+
// Runs when the close slide ends; the hooks it calls into are declared below.
|
|
68
|
+
const handleSheetDismissed = (): void => {
|
|
69
|
+
resetKeyboardMetrics();
|
|
70
|
+
|
|
71
|
+
if (!prewarm) {
|
|
72
|
+
// The WebView unmounts with the sheet: the next open waits for a fresh ready.
|
|
73
|
+
resetReady();
|
|
74
|
+
setUnreadTotal(null);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const pageKey = hostPageKey(config);
|
|
79
|
+
const { visible, presented, overlayTranslateY, backdropOpacity } = useSheetPresentation({
|
|
80
|
+
onDismissed: handleSheetDismissed,
|
|
81
|
+
});
|
|
82
|
+
const shouldRenderWebView = shouldRenderChatSurface(prewarm, visible, presented);
|
|
83
|
+
const chatSurfaceOnScreen = isChatSurfaceOnScreen(visible, presented);
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
ready,
|
|
87
|
+
failed,
|
|
88
|
+
readySignal,
|
|
89
|
+
webViewGeneration,
|
|
90
|
+
handleReady,
|
|
91
|
+
handleError,
|
|
92
|
+
handleProcessGone,
|
|
93
|
+
retry,
|
|
94
|
+
resetReady,
|
|
95
|
+
} = useReadyLifecycle({ visible, configRef, pageKey });
|
|
96
|
+
|
|
97
|
+
// Another page (user switch, new token…): the previous user's total is dropped until the new
|
|
98
|
+
// page reports its own, which always reaches onUnreadCountChange even if equal to the old one.
|
|
99
|
+
const unreadPageKeyRef = useRef(pageKey);
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
if (unreadPageKeyRef.current === pageKey) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
unreadPageKeyRef.current = pageKey;
|
|
106
|
+
setUnreadTotal(null);
|
|
107
|
+
hasReportedUnreadRef.current = false;
|
|
108
|
+
lastReportedUnreadRef.current = null;
|
|
109
|
+
}, [pageKey]);
|
|
110
|
+
|
|
111
|
+
const { keyboardFrame, resetKeyboardMetrics } = useKeyboardFrame({
|
|
112
|
+
visible,
|
|
113
|
+
chatSurfaceOnScreen,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const { activeBanner, queueInAppBanner, handleBannerAutoHide, handleBannerPress } =
|
|
117
|
+
useInAppBannerQueue({
|
|
118
|
+
enabled: inAppBanner,
|
|
119
|
+
chatSurfaceOnScreen,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const safeAreaForWeb = useMemo(
|
|
123
|
+
() =>
|
|
124
|
+
toSheetSafeAreaInsetsForWeb(withKeyboardSafeAreaBottom(safeArea, keyboardFrame.isApplied)),
|
|
125
|
+
[keyboardFrame.isApplied, safeArea],
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
// Effects run in hook order: keep this before usePushIntegration so that, on ready,
|
|
129
|
+
// cobbai:visibility reaches the page before cobbai:push-token and cobbai:open-conversation
|
|
130
|
+
// (the chat page counts as hidden until its first visibility message).
|
|
131
|
+
useNativeToWebSync({ webViewRef, visible, ready, readySignal, safeAreaForWeb });
|
|
132
|
+
|
|
133
|
+
usePushIntegration({
|
|
134
|
+
chatId: config.chatId,
|
|
135
|
+
webViewRef,
|
|
136
|
+
ready,
|
|
137
|
+
readySignal,
|
|
138
|
+
failed,
|
|
139
|
+
shouldRenderWebView,
|
|
140
|
+
chatSurfaceOnScreen,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const placeholderState = deriveChatPlaceholderState({ visible, ready, failed });
|
|
144
|
+
const { loaderMounted, loaderOpacity } = useLoaderFade(placeholderState !== 'hidden', visible);
|
|
145
|
+
|
|
146
|
+
const { launcherAppearance, setLauncherAppearance, showLauncher, launcherOpacity } =
|
|
147
|
+
useLauncherPresentation({
|
|
148
|
+
prewarm,
|
|
149
|
+
launcherVisible,
|
|
150
|
+
visible,
|
|
151
|
+
presented,
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
const handleClose = useCallback((): void => {
|
|
155
|
+
const pendingOpen = getPushStoreSnapshot().pendingOpen;
|
|
156
|
+
if (pendingOpen) {
|
|
157
|
+
clearPendingOpen(pendingOpen);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
closeChatScreenInternal();
|
|
161
|
+
configRef.current.onClose?.();
|
|
162
|
+
}, []);
|
|
163
|
+
|
|
164
|
+
useEffect(() => {
|
|
165
|
+
if (Platform.OS !== 'android' || !visible) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
|
|
170
|
+
handleClose();
|
|
171
|
+
return true;
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
return () => {
|
|
175
|
+
subscription.remove();
|
|
176
|
+
};
|
|
177
|
+
}, [handleClose, visible]);
|
|
178
|
+
|
|
179
|
+
const handleNewMessage = (event: ValidatedNewMessagePayload): void => {
|
|
180
|
+
config.onNewMessage?.({
|
|
181
|
+
...event,
|
|
182
|
+
chatVisible: visible,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
if (!event.preview) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
queueInAppBanner({
|
|
190
|
+
chatId: config.chatId,
|
|
191
|
+
conversationId: event.conversationId,
|
|
192
|
+
messageId: event.messageId,
|
|
193
|
+
senderName: event.senderName,
|
|
194
|
+
preview: event.preview,
|
|
195
|
+
source: 'live',
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const handlePushTokenRegistered = (registration: ValidatedPushTokenRegisteredPayload): void => {
|
|
200
|
+
rememberUnregisterSecret({
|
|
201
|
+
baseUrl: configRef.current.baseUrl,
|
|
202
|
+
chatId: configRef.current.chatId,
|
|
203
|
+
token: registration.token,
|
|
204
|
+
provider: registration.provider,
|
|
205
|
+
secret: registration.unregisterSecret,
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const handleUnread = (total: number): void => {
|
|
210
|
+
setUnreadTotal(total);
|
|
211
|
+
|
|
212
|
+
if (!hasReportedUnreadRef.current || lastReportedUnreadRef.current !== total) {
|
|
213
|
+
hasReportedUnreadRef.current = true;
|
|
214
|
+
lastReportedUnreadRef.current = total;
|
|
215
|
+
config.onUnreadCountChange?.(total);
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const overlayStyle = useMemo(
|
|
220
|
+
() => [
|
|
221
|
+
styles.overlay,
|
|
222
|
+
{ top: safeArea.top },
|
|
223
|
+
keyboardFrame.isApplied ? { bottom: keyboardFrame.overlayBottomInset } : null,
|
|
224
|
+
],
|
|
225
|
+
[keyboardFrame.isApplied, keyboardFrame.overlayBottomInset, safeArea.top],
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
const normalizedLauncherBottomOffset = useMemo(
|
|
229
|
+
() => resolveLauncherBottomOffset(launcherBottomOffset, safeArea.bottom),
|
|
230
|
+
[launcherBottomOffset, safeArea.bottom],
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const launcherAccessibilityLabel = useMemo(
|
|
234
|
+
() => getLauncherAccessibilityLabel(config.locale),
|
|
235
|
+
[config.locale],
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const launcherUnreadAccessibilityText = useMemo(
|
|
239
|
+
() => getLauncherUnreadAccessibilityText(config.locale, unreadTotal),
|
|
240
|
+
[config.locale, unreadTotal],
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
return (
|
|
244
|
+
<>
|
|
245
|
+
{launcherAppearance ? (
|
|
246
|
+
<CobbaiLauncher
|
|
247
|
+
appearance={launcherAppearance}
|
|
248
|
+
unreadTotal={unreadTotal}
|
|
249
|
+
isVisible={showLauncher}
|
|
250
|
+
right={safeArea.right + LAUNCHER_EDGE_OFFSET}
|
|
251
|
+
bottom={normalizedLauncherBottomOffset + LAUNCHER_EDGE_OFFSET}
|
|
252
|
+
opacity={launcherOpacity}
|
|
253
|
+
accessibilityLabel={launcherAccessibilityLabel}
|
|
254
|
+
accessibilityValue={
|
|
255
|
+
launcherUnreadAccessibilityText
|
|
256
|
+
? {
|
|
257
|
+
text: launcherUnreadAccessibilityText,
|
|
258
|
+
}
|
|
259
|
+
: undefined
|
|
260
|
+
}
|
|
261
|
+
onPress={() => {
|
|
262
|
+
openChatScreen();
|
|
263
|
+
}}
|
|
264
|
+
/>
|
|
265
|
+
) : null}
|
|
266
|
+
|
|
267
|
+
{inAppBanner ? (
|
|
268
|
+
<CobbaiInAppBanner
|
|
269
|
+
banner={activeBanner}
|
|
270
|
+
locale={config.locale}
|
|
271
|
+
safeArea={safeArea}
|
|
272
|
+
launcherAppearance={launcherAppearance}
|
|
273
|
+
onAutoHide={handleBannerAutoHide}
|
|
274
|
+
onPress={handleBannerPress}
|
|
275
|
+
/>
|
|
276
|
+
) : null}
|
|
277
|
+
|
|
278
|
+
{shouldRenderWebView ? (
|
|
279
|
+
<>
|
|
280
|
+
<Animated.View
|
|
281
|
+
style={[styles.backdrop, { opacity: backdropOpacity }]}
|
|
282
|
+
pointerEvents={visible ? 'auto' : 'none'}
|
|
283
|
+
accessibilityElementsHidden
|
|
284
|
+
importantForAccessibility="no-hide-descendants"
|
|
285
|
+
accessible={false}
|
|
286
|
+
/>
|
|
287
|
+
|
|
288
|
+
<Animated.View
|
|
289
|
+
style={[overlayStyle, { transform: [{ translateY: overlayTranslateY }] }]}
|
|
290
|
+
pointerEvents={visible ? 'auto' : 'none'}
|
|
291
|
+
accessibilityElementsHidden={!visible}
|
|
292
|
+
importantForAccessibility={visible ? 'auto' : 'no-hide-descendants'}
|
|
293
|
+
// iOS: while open, VoiceOver stays inside the chat instead of reaching the app behind it
|
|
294
|
+
accessibilityViewIsModal={visible}
|
|
295
|
+
>
|
|
296
|
+
<CobbaiWebView
|
|
297
|
+
config={config}
|
|
298
|
+
generation={webViewGeneration}
|
|
299
|
+
initialSafeArea={initialSafeAreaRef.current}
|
|
300
|
+
onReady={handleReady}
|
|
301
|
+
onClose={handleClose}
|
|
302
|
+
onError={handleError}
|
|
303
|
+
onNewMessage={handleNewMessage}
|
|
304
|
+
onUnread={handleUnread}
|
|
305
|
+
onPushTokenRegistered={handlePushTokenRegistered}
|
|
306
|
+
onLauncherAppearance={setLauncherAppearance}
|
|
307
|
+
onProcessGone={handleProcessGone}
|
|
308
|
+
onWebViewRef={(ref) => {
|
|
309
|
+
webViewRef.current = ref;
|
|
310
|
+
}}
|
|
311
|
+
/>
|
|
312
|
+
|
|
313
|
+
{loaderMounted ? (
|
|
314
|
+
<ChatPlaceholder
|
|
315
|
+
mode={placeholderState === 'unavailable' ? 'unavailable' : 'loading'}
|
|
316
|
+
locale={config.locale}
|
|
317
|
+
opacity={loaderOpacity}
|
|
318
|
+
onClose={handleClose}
|
|
319
|
+
onRetry={retry}
|
|
320
|
+
/>
|
|
321
|
+
) : null}
|
|
322
|
+
</Animated.View>
|
|
323
|
+
</>
|
|
324
|
+
) : null}
|
|
325
|
+
</>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const styles = StyleSheet.create({
|
|
330
|
+
backdrop: {
|
|
331
|
+
position: 'absolute',
|
|
332
|
+
top: 0,
|
|
333
|
+
right: 0,
|
|
334
|
+
bottom: 0,
|
|
335
|
+
left: 0,
|
|
336
|
+
backgroundColor: 'rgba(0, 0, 0, 0.4)',
|
|
337
|
+
zIndex: 2147483646,
|
|
338
|
+
},
|
|
339
|
+
overlay: {
|
|
340
|
+
position: 'absolute',
|
|
341
|
+
top: 0,
|
|
342
|
+
right: 0,
|
|
343
|
+
bottom: 0,
|
|
344
|
+
left: 0,
|
|
345
|
+
backgroundColor: 'transparent',
|
|
346
|
+
zIndex: 2147483647,
|
|
347
|
+
},
|
|
348
|
+
});
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
AccessibilityInfo,
|
|
4
|
+
Animated,
|
|
5
|
+
Image,
|
|
6
|
+
Platform,
|
|
7
|
+
Pressable,
|
|
8
|
+
StyleSheet,
|
|
9
|
+
Text,
|
|
10
|
+
View,
|
|
11
|
+
} from 'react-native';
|
|
12
|
+
import { CHAT_BUTTON_GLYPH_URI } from './launcherGlyph';
|
|
13
|
+
import { DEFAULT_LAUNCHER_APPEARANCE, type LauncherAppearance } from './launcher';
|
|
14
|
+
import {
|
|
15
|
+
getInAppBannerAccessibilityLabel,
|
|
16
|
+
getInAppBannerMessageKey,
|
|
17
|
+
getInAppBannerOpenHint,
|
|
18
|
+
resolveInAppBannerSender,
|
|
19
|
+
IN_APP_BANNER_APPEAR_DURATION_MS,
|
|
20
|
+
IN_APP_BANNER_AUTO_HIDE_DURATION_MS,
|
|
21
|
+
IN_APP_BANNER_FADE_OUT_DURATION_MS,
|
|
22
|
+
type CobbaiInAppBannerPayload,
|
|
23
|
+
} from './inAppBanner';
|
|
24
|
+
import type { CobbaiSafeAreaInsets } from './types';
|
|
25
|
+
|
|
26
|
+
type CobbaiInAppBannerProps = {
|
|
27
|
+
banner: CobbaiInAppBannerPayload | null;
|
|
28
|
+
locale?: string;
|
|
29
|
+
safeArea: CobbaiSafeAreaInsets;
|
|
30
|
+
launcherAppearance: LauncherAppearance | null;
|
|
31
|
+
onPress: (banner: CobbaiInAppBannerPayload) => void;
|
|
32
|
+
onAutoHide: () => void;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function CobbaiInAppBanner(props: CobbaiInAppBannerProps): React.JSX.Element | null {
|
|
36
|
+
const { banner, locale, safeArea, launcherAppearance, onPress, onAutoHide } = props;
|
|
37
|
+
|
|
38
|
+
const opacity = useRef(new Animated.Value(0)).current;
|
|
39
|
+
const translateY = useRef(new Animated.Value(-16)).current;
|
|
40
|
+
const previousMessageKeyRef = useRef<string | null>(null);
|
|
41
|
+
const autoHideTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
42
|
+
|
|
43
|
+
// The show/hide cycle is keyed on the message only. Reading the rest through refs keeps a parent
|
|
44
|
+
// re-render (new callback identity, locale, …) from restarting the auto-hide timer or snapping a
|
|
45
|
+
// fading banner back to full opacity.
|
|
46
|
+
const messageKey = banner
|
|
47
|
+
? getInAppBannerMessageKey(banner.conversationId, banner.messageId)
|
|
48
|
+
: null;
|
|
49
|
+
const bannerRef = useRef(banner);
|
|
50
|
+
const localeRef = useRef(locale);
|
|
51
|
+
const onAutoHideRef = useRef(onAutoHide);
|
|
52
|
+
bannerRef.current = banner;
|
|
53
|
+
localeRef.current = locale;
|
|
54
|
+
onAutoHideRef.current = onAutoHide;
|
|
55
|
+
|
|
56
|
+
const clearAutoHideTimer = (): void => {
|
|
57
|
+
if (!autoHideTimerRef.current) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
clearTimeout(autoHideTimerRef.current);
|
|
62
|
+
autoHideTimerRef.current = null;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
return () => {
|
|
67
|
+
clearAutoHideTimer();
|
|
68
|
+
};
|
|
69
|
+
}, []);
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
clearAutoHideTimer();
|
|
73
|
+
opacity.stopAnimation();
|
|
74
|
+
translateY.stopAnimation();
|
|
75
|
+
|
|
76
|
+
const currentBanner = bannerRef.current;
|
|
77
|
+
if (!currentBanner || messageKey == null) {
|
|
78
|
+
previousMessageKeyRef.current = null;
|
|
79
|
+
opacity.setValue(0);
|
|
80
|
+
translateY.setValue(-16);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const hadBanner = previousMessageKeyRef.current != null;
|
|
85
|
+
previousMessageKeyRef.current = messageKey;
|
|
86
|
+
|
|
87
|
+
if (!hadBanner) {
|
|
88
|
+
opacity.setValue(0);
|
|
89
|
+
translateY.setValue(-16);
|
|
90
|
+
|
|
91
|
+
Animated.parallel([
|
|
92
|
+
Animated.timing(opacity, {
|
|
93
|
+
toValue: 1,
|
|
94
|
+
duration: IN_APP_BANNER_APPEAR_DURATION_MS,
|
|
95
|
+
useNativeDriver: true,
|
|
96
|
+
}),
|
|
97
|
+
Animated.timing(translateY, {
|
|
98
|
+
toValue: 0,
|
|
99
|
+
duration: IN_APP_BANNER_APPEAR_DURATION_MS,
|
|
100
|
+
useNativeDriver: true,
|
|
101
|
+
}),
|
|
102
|
+
]).start();
|
|
103
|
+
|
|
104
|
+
if (Platform.OS === 'ios') {
|
|
105
|
+
const label = getInAppBannerAccessibilityLabel(currentBanner, localeRef.current);
|
|
106
|
+
void AccessibilityInfo.announceForAccessibility(label);
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
opacity.setValue(1);
|
|
110
|
+
translateY.setValue(0);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
autoHideTimerRef.current = setTimeout(() => {
|
|
114
|
+
Animated.timing(opacity, {
|
|
115
|
+
toValue: 0,
|
|
116
|
+
duration: IN_APP_BANNER_FADE_OUT_DURATION_MS,
|
|
117
|
+
useNativeDriver: true,
|
|
118
|
+
}).start(({ finished }) => {
|
|
119
|
+
if (!finished) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
onAutoHideRef.current();
|
|
124
|
+
});
|
|
125
|
+
}, IN_APP_BANNER_AUTO_HIDE_DURATION_MS);
|
|
126
|
+
|
|
127
|
+
return clearAutoHideTimer;
|
|
128
|
+
}, [messageKey, opacity, translateY]);
|
|
129
|
+
|
|
130
|
+
const appearance = launcherAppearance ?? DEFAULT_LAUNCHER_APPEARANCE;
|
|
131
|
+
|
|
132
|
+
const containerStyle = useMemo(
|
|
133
|
+
() => [
|
|
134
|
+
styles.container,
|
|
135
|
+
{
|
|
136
|
+
top: safeArea.top + 8,
|
|
137
|
+
left: safeArea.left + 12,
|
|
138
|
+
right: safeArea.right + 12,
|
|
139
|
+
opacity,
|
|
140
|
+
transform: [{ translateY }],
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
[opacity, safeArea.left, safeArea.right, safeArea.top, translateY],
|
|
144
|
+
);
|
|
145
|
+
|
|
146
|
+
if (!banner) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const sender = resolveInAppBannerSender(banner.senderName, locale);
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<Animated.View style={containerStyle} pointerEvents="box-none">
|
|
154
|
+
<Pressable
|
|
155
|
+
testID="cobbai-in-app-banner"
|
|
156
|
+
accessibilityRole="button"
|
|
157
|
+
accessibilityLabel={getInAppBannerAccessibilityLabel(banner, locale)}
|
|
158
|
+
accessibilityHint={getInAppBannerOpenHint(locale)}
|
|
159
|
+
accessibilityLiveRegion={Platform.OS === 'android' ? 'polite' : undefined}
|
|
160
|
+
onPress={() => {
|
|
161
|
+
onPress(banner);
|
|
162
|
+
}}
|
|
163
|
+
style={styles.card}
|
|
164
|
+
>
|
|
165
|
+
<View style={[styles.avatar, { backgroundColor: appearance.backgroundColor }]}>
|
|
166
|
+
<Image
|
|
167
|
+
source={{ uri: CHAT_BUTTON_GLYPH_URI }}
|
|
168
|
+
resizeMode="contain"
|
|
169
|
+
accessible={false}
|
|
170
|
+
importantForAccessibility="no"
|
|
171
|
+
style={[styles.glyph, { tintColor: appearance.textColor }]}
|
|
172
|
+
/>
|
|
173
|
+
</View>
|
|
174
|
+
|
|
175
|
+
<View style={styles.textContainer}>
|
|
176
|
+
<Text style={styles.sender} numberOfLines={1}>
|
|
177
|
+
{sender}
|
|
178
|
+
</Text>
|
|
179
|
+
<Text style={styles.preview} numberOfLines={2}>
|
|
180
|
+
{banner.preview}
|
|
181
|
+
</Text>
|
|
182
|
+
</View>
|
|
183
|
+
</Pressable>
|
|
184
|
+
</Animated.View>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const styles = StyleSheet.create({
|
|
189
|
+
container: {
|
|
190
|
+
position: 'absolute',
|
|
191
|
+
zIndex: 2147483644,
|
|
192
|
+
alignItems: 'center',
|
|
193
|
+
},
|
|
194
|
+
card: {
|
|
195
|
+
width: '100%',
|
|
196
|
+
maxWidth: 560,
|
|
197
|
+
borderRadius: 14,
|
|
198
|
+
padding: 12,
|
|
199
|
+
backgroundColor: '#FFFFFF',
|
|
200
|
+
flexDirection: 'row',
|
|
201
|
+
alignItems: 'center',
|
|
202
|
+
shadowColor: '#000000',
|
|
203
|
+
shadowOpacity: 0.18,
|
|
204
|
+
shadowRadius: 12,
|
|
205
|
+
shadowOffset: {
|
|
206
|
+
width: 0,
|
|
207
|
+
height: 4,
|
|
208
|
+
},
|
|
209
|
+
elevation: 8,
|
|
210
|
+
},
|
|
211
|
+
avatar: {
|
|
212
|
+
width: 36,
|
|
213
|
+
height: 36,
|
|
214
|
+
borderRadius: 18,
|
|
215
|
+
alignItems: 'center',
|
|
216
|
+
justifyContent: 'center',
|
|
217
|
+
flexShrink: 0,
|
|
218
|
+
},
|
|
219
|
+
glyph: {
|
|
220
|
+
width: 22,
|
|
221
|
+
height: 22,
|
|
222
|
+
},
|
|
223
|
+
textContainer: {
|
|
224
|
+
flex: 1,
|
|
225
|
+
marginLeft: 10,
|
|
226
|
+
},
|
|
227
|
+
sender: {
|
|
228
|
+
color: '#111827',
|
|
229
|
+
fontSize: 14,
|
|
230
|
+
fontWeight: '600',
|
|
231
|
+
},
|
|
232
|
+
preview: {
|
|
233
|
+
marginTop: 2,
|
|
234
|
+
color: '#374151',
|
|
235
|
+
fontSize: 14,
|
|
236
|
+
},
|
|
237
|
+
});
|