@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,91 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { type CobbaiInAppBannerPayload, getInAppBannerMessageKey } from '../inAppBanner';
|
|
3
|
+
import { openConversationFromBanner, setPushBannerEnabled, setPushBannerPresenter } from '../push';
|
|
4
|
+
|
|
5
|
+
type InAppBannerQueueInput = {
|
|
6
|
+
/** `inAppBanner` prop. */
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
/** Banners are never shown over the chat itself. */
|
|
9
|
+
chatSurfaceOnScreen: boolean;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type InAppBannerQueue = {
|
|
13
|
+
activeBanner: CobbaiInAppBannerPayload | null;
|
|
14
|
+
/** Shows a banner unless disabled, the chat is on screen, or this message was already shown. */
|
|
15
|
+
queueInAppBanner: (banner: CobbaiInAppBannerPayload) => void;
|
|
16
|
+
handleBannerAutoHide: () => void;
|
|
17
|
+
/** Opens the banner's conversation through the same path as a push tap. */
|
|
18
|
+
handleBannerPress: (banner: CobbaiInAppBannerPayload) => void;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The in-app banner shown for new messages while the chat is closed, fed by live bridge events
|
|
23
|
+
* and by foreground pushes (registered as the push store's banner presenter).
|
|
24
|
+
*/
|
|
25
|
+
export function useInAppBannerQueue({
|
|
26
|
+
enabled,
|
|
27
|
+
chatSurfaceOnScreen,
|
|
28
|
+
}: InAppBannerQueueInput): InAppBannerQueue {
|
|
29
|
+
const [activeBanner, setActiveBanner] = useState<CobbaiInAppBannerPayload | null>(null);
|
|
30
|
+
const shownBannerMessageKeysRef = useRef<Set<string>>(new Set());
|
|
31
|
+
const chatSurfaceOnScreenRef = useRef(false);
|
|
32
|
+
const enabledRef = useRef(enabled);
|
|
33
|
+
|
|
34
|
+
const queueInAppBanner = useCallback((nextBanner: CobbaiInAppBannerPayload): void => {
|
|
35
|
+
if (!enabledRef.current) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (chatSurfaceOnScreenRef.current) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const key = getInAppBannerMessageKey(nextBanner.conversationId, nextBanner.messageId);
|
|
44
|
+
if (shownBannerMessageKeysRef.current.has(key)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
shownBannerMessageKeysRef.current.add(key);
|
|
49
|
+
setActiveBanner(nextBanner);
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
chatSurfaceOnScreenRef.current = chatSurfaceOnScreen;
|
|
54
|
+
|
|
55
|
+
if (chatSurfaceOnScreen) {
|
|
56
|
+
setActiveBanner(null);
|
|
57
|
+
}
|
|
58
|
+
}, [chatSurfaceOnScreen]);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
enabledRef.current = enabled;
|
|
62
|
+
setPushBannerEnabled(enabled);
|
|
63
|
+
|
|
64
|
+
if (!enabled) {
|
|
65
|
+
setActiveBanner(null);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return () => {
|
|
69
|
+
setPushBannerEnabled(true);
|
|
70
|
+
};
|
|
71
|
+
}, [enabled]);
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
setPushBannerPresenter(queueInAppBanner);
|
|
75
|
+
|
|
76
|
+
return () => {
|
|
77
|
+
setPushBannerPresenter(null);
|
|
78
|
+
};
|
|
79
|
+
}, [queueInAppBanner]);
|
|
80
|
+
|
|
81
|
+
const handleBannerAutoHide = useCallback((): void => {
|
|
82
|
+
setActiveBanner(null);
|
|
83
|
+
}, []);
|
|
84
|
+
|
|
85
|
+
const handleBannerPress = useCallback((banner: CobbaiInAppBannerPayload): void => {
|
|
86
|
+
setActiveBanner(null);
|
|
87
|
+
openConversationFromBanner(banner.chatId, banner.conversationId);
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
return { activeBanner, queueInAppBanner, handleBannerAutoHide, handleBannerPress };
|
|
91
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { Dimensions, Keyboard, type KeyboardEvent, Platform } from 'react-native';
|
|
3
|
+
import { deriveKeyboardFrame, type KeyboardFrame } from '../androidKeyboardFrame';
|
|
4
|
+
|
|
5
|
+
type KeyboardMetrics = {
|
|
6
|
+
height: number;
|
|
7
|
+
screenY: number;
|
|
8
|
+
windowHeight: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const EMPTY_KEYBOARD_METRICS: KeyboardMetrics = {
|
|
12
|
+
height: 0,
|
|
13
|
+
screenY: 0,
|
|
14
|
+
windowHeight: 0,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function toKeyboardMetrics(event: KeyboardEvent): KeyboardMetrics {
|
|
18
|
+
const windowHeight = Dimensions.get('window').height;
|
|
19
|
+
return {
|
|
20
|
+
height: event.endCoordinates?.height ?? 0,
|
|
21
|
+
screenY: event.endCoordinates?.screenY ?? windowHeight,
|
|
22
|
+
windowHeight,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type KeyboardFrameInput = {
|
|
27
|
+
/** Listeners are attached only while the chat is open. */
|
|
28
|
+
visible: boolean;
|
|
29
|
+
/** The frame is applied only while the sheet is on screen. */
|
|
30
|
+
chatSurfaceOnScreen: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** Tracks the software keyboard and derives how far the sheet must be lifted above it. */
|
|
34
|
+
export function useKeyboardFrame({ visible, chatSurfaceOnScreen }: KeyboardFrameInput): {
|
|
35
|
+
keyboardFrame: KeyboardFrame;
|
|
36
|
+
resetKeyboardMetrics: () => void;
|
|
37
|
+
} {
|
|
38
|
+
const [keyboardMetrics, setKeyboardMetrics] = useState<KeyboardMetrics>(EMPTY_KEYBOARD_METRICS);
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (Platform.OS !== 'android' && Platform.OS !== 'ios') return;
|
|
42
|
+
if (!visible) return;
|
|
43
|
+
|
|
44
|
+
const handleKeyboardFrame = (event: KeyboardEvent): void => {
|
|
45
|
+
setKeyboardMetrics(toKeyboardMetrics(event));
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const handleKeyboardDidHide = (): void => {
|
|
49
|
+
setKeyboardMetrics(EMPTY_KEYBOARD_METRICS);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// iOS reports hides through keyboardWillChangeFrame too (screenY at the bottom edge).
|
|
53
|
+
const subscriptions =
|
|
54
|
+
Platform.OS === 'android'
|
|
55
|
+
? [
|
|
56
|
+
Keyboard.addListener('keyboardDidShow', handleKeyboardFrame),
|
|
57
|
+
Keyboard.addListener('keyboardDidHide', handleKeyboardDidHide),
|
|
58
|
+
]
|
|
59
|
+
: [Keyboard.addListener('keyboardWillChangeFrame', handleKeyboardFrame)];
|
|
60
|
+
|
|
61
|
+
return () => {
|
|
62
|
+
for (const subscription of subscriptions) {
|
|
63
|
+
subscription.remove();
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}, [visible]);
|
|
67
|
+
|
|
68
|
+
const keyboardFrame = useMemo(
|
|
69
|
+
() =>
|
|
70
|
+
deriveKeyboardFrame({
|
|
71
|
+
chatVisible: chatSurfaceOnScreen,
|
|
72
|
+
keyboardHeight: keyboardMetrics.height,
|
|
73
|
+
keyboardScreenY: keyboardMetrics.screenY,
|
|
74
|
+
windowHeight: keyboardMetrics.windowHeight,
|
|
75
|
+
}),
|
|
76
|
+
[
|
|
77
|
+
chatSurfaceOnScreen,
|
|
78
|
+
keyboardMetrics.height,
|
|
79
|
+
keyboardMetrics.screenY,
|
|
80
|
+
keyboardMetrics.windowHeight,
|
|
81
|
+
],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const resetKeyboardMetrics = useCallback((): void => {
|
|
85
|
+
setKeyboardMetrics(EMPTY_KEYBOARD_METRICS);
|
|
86
|
+
}, []);
|
|
87
|
+
|
|
88
|
+
return { keyboardFrame, resetKeyboardMetrics };
|
|
89
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, Easing } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_LAUNCHER_APPEARANCE,
|
|
5
|
+
LAUNCHER_DEFAULT_FALLBACK_DELAY_MS,
|
|
6
|
+
LAUNCHER_FADE_IN_DURATION_MS,
|
|
7
|
+
type LauncherAppearance,
|
|
8
|
+
shouldShowLauncher,
|
|
9
|
+
} from '../launcher';
|
|
10
|
+
|
|
11
|
+
type LauncherPresentationInput = {
|
|
12
|
+
prewarm: boolean;
|
|
13
|
+
/** `launcherVisible` prop. */
|
|
14
|
+
launcherVisible: boolean;
|
|
15
|
+
visible: boolean;
|
|
16
|
+
presented: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type LauncherPresentation = {
|
|
20
|
+
/** null until the chat reports its colors (or the fallback delay elapses). */
|
|
21
|
+
launcherAppearance: LauncherAppearance | null;
|
|
22
|
+
setLauncherAppearance: (appearance: LauncherAppearance) => void;
|
|
23
|
+
showLauncher: boolean;
|
|
24
|
+
launcherOpacity: Animated.Value;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** Launcher colors (from cobbai:launcher, with a default fallback) and its fade in/out. */
|
|
28
|
+
export function useLauncherPresentation({
|
|
29
|
+
prewarm,
|
|
30
|
+
launcherVisible,
|
|
31
|
+
visible,
|
|
32
|
+
presented,
|
|
33
|
+
}: LauncherPresentationInput): LauncherPresentation {
|
|
34
|
+
const [launcherAppearance, setLauncherAppearance] = useState<LauncherAppearance | null>(
|
|
35
|
+
prewarm ? null : DEFAULT_LAUNCHER_APPEARANCE,
|
|
36
|
+
);
|
|
37
|
+
const launcherOpacity = useRef(new Animated.Value(0)).current;
|
|
38
|
+
const previousLauncherPresentedRef = useRef(false);
|
|
39
|
+
const previousShowLauncherRef = useRef(false);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (launcherAppearance) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!prewarm) {
|
|
47
|
+
setLauncherAppearance(DEFAULT_LAUNCHER_APPEARANCE);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const fallbackTimer = setTimeout(() => {
|
|
52
|
+
setLauncherAppearance(
|
|
53
|
+
(previousAppearance) => previousAppearance ?? DEFAULT_LAUNCHER_APPEARANCE,
|
|
54
|
+
);
|
|
55
|
+
}, LAUNCHER_DEFAULT_FALLBACK_DELAY_MS);
|
|
56
|
+
|
|
57
|
+
return () => {
|
|
58
|
+
clearTimeout(fallbackTimer);
|
|
59
|
+
};
|
|
60
|
+
}, [launcherAppearance, prewarm]);
|
|
61
|
+
|
|
62
|
+
const showLauncher = shouldShowLauncher({
|
|
63
|
+
launcherVisible,
|
|
64
|
+
visible,
|
|
65
|
+
presented,
|
|
66
|
+
appearance: launcherAppearance,
|
|
67
|
+
});
|
|
68
|
+
const launcherMounted = launcherAppearance != null;
|
|
69
|
+
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
const wasPresented = previousLauncherPresentedRef.current;
|
|
72
|
+
const wasShowingLauncher = previousShowLauncherRef.current;
|
|
73
|
+
|
|
74
|
+
if (!launcherMounted || !showLauncher) {
|
|
75
|
+
launcherOpacity.stopAnimation();
|
|
76
|
+
launcherOpacity.setValue(0);
|
|
77
|
+
previousLauncherPresentedRef.current = presented;
|
|
78
|
+
previousShowLauncherRef.current = showLauncher;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const becameVisible = !wasShowingLauncher;
|
|
83
|
+
const didFinishCloseSlide = wasPresented && !presented;
|
|
84
|
+
|
|
85
|
+
if (didFinishCloseSlide) {
|
|
86
|
+
launcherOpacity.stopAnimation();
|
|
87
|
+
launcherOpacity.setValue(1);
|
|
88
|
+
previousLauncherPresentedRef.current = presented;
|
|
89
|
+
previousShowLauncherRef.current = showLauncher;
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (becameVisible) {
|
|
94
|
+
launcherOpacity.stopAnimation();
|
|
95
|
+
launcherOpacity.setValue(0);
|
|
96
|
+
|
|
97
|
+
const launcherFadeIn = Animated.timing(launcherOpacity, {
|
|
98
|
+
toValue: 1,
|
|
99
|
+
duration: LAUNCHER_FADE_IN_DURATION_MS,
|
|
100
|
+
easing: Easing.linear,
|
|
101
|
+
useNativeDriver: true,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
launcherFadeIn.start();
|
|
105
|
+
|
|
106
|
+
previousLauncherPresentedRef.current = presented;
|
|
107
|
+
previousShowLauncherRef.current = showLauncher;
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
launcherFadeIn.stop();
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
launcherOpacity.stopAnimation();
|
|
115
|
+
launcherOpacity.setValue(1);
|
|
116
|
+
previousLauncherPresentedRef.current = presented;
|
|
117
|
+
previousShowLauncherRef.current = showLauncher;
|
|
118
|
+
}, [launcherMounted, launcherOpacity, presented, showLauncher]);
|
|
119
|
+
|
|
120
|
+
return { launcherAppearance, setLauncherAppearance, showLauncher, launcherOpacity };
|
|
121
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type MutableRefObject, useEffect } from 'react';
|
|
2
|
+
import { NATIVE_TO_WEB_MESSAGES } from '../bridge/messages';
|
|
3
|
+
import { serializeNativeEvent } from '../bridge/nativeEvent';
|
|
4
|
+
import type { CobbaiWebViewRef } from '../CobbaiWebView';
|
|
5
|
+
import type { CobbaiSafeAreaInsets } from '../types';
|
|
6
|
+
|
|
7
|
+
type NativeToWebSyncInput = {
|
|
8
|
+
webViewRef: MutableRefObject<CobbaiWebViewRef | null>;
|
|
9
|
+
visible: boolean;
|
|
10
|
+
ready: boolean;
|
|
11
|
+
readySignal: number;
|
|
12
|
+
safeAreaForWeb: CobbaiSafeAreaInsets;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Native state pushed to the host page: app state and network on open,
|
|
17
|
+
* visibility once ready, and safe-area insets whenever they change.
|
|
18
|
+
*/
|
|
19
|
+
export function useNativeToWebSync({
|
|
20
|
+
webViewRef,
|
|
21
|
+
visible,
|
|
22
|
+
ready,
|
|
23
|
+
readySignal,
|
|
24
|
+
safeAreaForWeb,
|
|
25
|
+
}: NativeToWebSyncInput): void {
|
|
26
|
+
// App state and network: constant values for now, not read by the chat page.
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!visible || !webViewRef.current) return;
|
|
29
|
+
|
|
30
|
+
const appStateScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.APP_STATE, {
|
|
31
|
+
state: 'active',
|
|
32
|
+
});
|
|
33
|
+
webViewRef.current.injectJavaScript(appStateScript);
|
|
34
|
+
|
|
35
|
+
const networkScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.NETWORK, { online: true });
|
|
36
|
+
webViewRef.current.injectJavaScript(networkScript);
|
|
37
|
+
}, [visible, webViewRef]);
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (!ready || !webViewRef.current) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const visibilityScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.VISIBILITY, { visible });
|
|
45
|
+
webViewRef.current.injectJavaScript(visibilityScript);
|
|
46
|
+
}, [ready, readySignal, visible, webViewRef]);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (!webViewRef.current) return;
|
|
50
|
+
|
|
51
|
+
const safeAreaScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.SAFE_AREA, safeAreaForWeb);
|
|
52
|
+
webViewRef.current.injectJavaScript(safeAreaScript);
|
|
53
|
+
}, [ready, safeAreaForWeb, webViewRef]);
|
|
54
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { type MutableRefObject, useEffect, useState } from 'react';
|
|
2
|
+
import { AppState, type AppStateStatus } from 'react-native';
|
|
3
|
+
import { NATIVE_TO_WEB_MESSAGES } from '../bridge/messages';
|
|
4
|
+
import { serializeNativeEvent } from '../bridge/nativeEvent';
|
|
5
|
+
import type { CobbaiWebViewRef } from '../CobbaiWebView';
|
|
6
|
+
import { openChatScreen } from '../openChatScreen';
|
|
7
|
+
import {
|
|
8
|
+
clearMountedChatId,
|
|
9
|
+
clearPendingOpen,
|
|
10
|
+
clearPendingUnregister,
|
|
11
|
+
getPushStoreSnapshot,
|
|
12
|
+
setMountedChatId,
|
|
13
|
+
setPushConnected,
|
|
14
|
+
subscribePushStore,
|
|
15
|
+
} from '../push';
|
|
16
|
+
|
|
17
|
+
function formatTokenSuffix(token: string): string {
|
|
18
|
+
if (token.length <= 6) {
|
|
19
|
+
return '...<redacted>';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return `...${token.slice(-6)}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type PushIntegrationInput = {
|
|
26
|
+
chatId: string;
|
|
27
|
+
webViewRef: MutableRefObject<CobbaiWebViewRef | null>;
|
|
28
|
+
ready: boolean;
|
|
29
|
+
readySignal: number;
|
|
30
|
+
failed: boolean;
|
|
31
|
+
shouldRenderWebView: boolean;
|
|
32
|
+
chatSurfaceOnScreen: boolean;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Bridges the global push store and the mounted chat: which chat is mounted, whether it is live
|
|
37
|
+
* (foreground pushes are then dropped), token register/unregister, and opening a tapped push's
|
|
38
|
+
* conversation.
|
|
39
|
+
*/
|
|
40
|
+
export function usePushIntegration({
|
|
41
|
+
chatId,
|
|
42
|
+
webViewRef,
|
|
43
|
+
ready,
|
|
44
|
+
readySignal,
|
|
45
|
+
failed,
|
|
46
|
+
shouldRenderWebView,
|
|
47
|
+
chatSurfaceOnScreen,
|
|
48
|
+
}: PushIntegrationInput): void {
|
|
49
|
+
const [pushStore, setPushStore] = useState(getPushStoreSnapshot);
|
|
50
|
+
const [appState, setAppState] = useState<AppStateStatus>(AppState.currentState ?? 'active');
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const unsubscribe = subscribePushStore(() => {
|
|
54
|
+
setPushStore(getPushStoreSnapshot());
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return unsubscribe;
|
|
58
|
+
}, []);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
setMountedChatId(chatId);
|
|
62
|
+
|
|
63
|
+
return () => {
|
|
64
|
+
clearMountedChatId(chatId);
|
|
65
|
+
};
|
|
66
|
+
}, [chatId]);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
const subscription = AppState.addEventListener('change', (nextAppState) => {
|
|
70
|
+
setAppState(nextAppState);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return () => {
|
|
74
|
+
subscription.remove();
|
|
75
|
+
};
|
|
76
|
+
}, []);
|
|
77
|
+
|
|
78
|
+
const connected = ready && !failed && shouldRenderWebView && appState === 'active';
|
|
79
|
+
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
setPushConnected(connected);
|
|
82
|
+
}, [connected]);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
return () => {
|
|
86
|
+
setPushConnected(false);
|
|
87
|
+
};
|
|
88
|
+
}, []);
|
|
89
|
+
|
|
90
|
+
// Effects below depend on token fields, not on snapshot objects: every store change yields a
|
|
91
|
+
// new snapshot, and only a real change of these values must re-send anything.
|
|
92
|
+
const tokenValue = pushStore.token?.token;
|
|
93
|
+
const tokenProvider = pushStore.token?.provider;
|
|
94
|
+
const tokenApnsEnvironment = pushStore.token?.apnsEnvironment;
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
if (!ready || !webViewRef.current || !tokenValue || !tokenProvider) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const registerPayload: Record<string, unknown> = {
|
|
102
|
+
action: 'register',
|
|
103
|
+
token: tokenValue,
|
|
104
|
+
provider: tokenProvider,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (tokenProvider === 'apns' && tokenApnsEnvironment) {
|
|
108
|
+
registerPayload.apns_environment = tokenApnsEnvironment;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const registerScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.PUSH_TOKEN, registerPayload);
|
|
112
|
+
webViewRef.current.injectJavaScript(registerScript);
|
|
113
|
+
|
|
114
|
+
if (__DEV__) {
|
|
115
|
+
console.log(
|
|
116
|
+
`[cobbai][push] register provider=${tokenProvider} token=${formatTokenSuffix(tokenValue)}`,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
// readySignal: re-register after every ready, a remounted page starts from nothing.
|
|
120
|
+
}, [ready, readySignal, tokenApnsEnvironment, tokenProvider, tokenValue, webViewRef]);
|
|
121
|
+
|
|
122
|
+
const unregisterValue = pushStore.pendingUnregister?.token;
|
|
123
|
+
const unregisterProvider = pushStore.pendingUnregister?.provider;
|
|
124
|
+
const unregisterApnsEnvironment = pushStore.pendingUnregister?.apnsEnvironment;
|
|
125
|
+
|
|
126
|
+
useEffect(() => {
|
|
127
|
+
if (!ready || !webViewRef.current || !unregisterValue || !unregisterProvider) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const pendingUnregister = {
|
|
132
|
+
token: unregisterValue,
|
|
133
|
+
provider: unregisterProvider,
|
|
134
|
+
apnsEnvironment: unregisterApnsEnvironment,
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const unregisterScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.PUSH_TOKEN, {
|
|
138
|
+
action: 'unregister',
|
|
139
|
+
token: pendingUnregister.token,
|
|
140
|
+
provider: pendingUnregister.provider,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
webViewRef.current.injectJavaScript(unregisterScript);
|
|
144
|
+
|
|
145
|
+
if (__DEV__) {
|
|
146
|
+
console.log(
|
|
147
|
+
`[cobbai][push] unregister provider=${pendingUnregister.provider} token=${formatTokenSuffix(
|
|
148
|
+
pendingUnregister.token,
|
|
149
|
+
)}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
clearPendingUnregister(pendingUnregister);
|
|
154
|
+
}, [ready, unregisterApnsEnvironment, unregisterProvider, unregisterValue, webViewRef]);
|
|
155
|
+
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
const pendingOpen = pushStore.pendingOpen;
|
|
158
|
+
if (!pendingOpen) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (pendingOpen.chatId !== chatId) {
|
|
163
|
+
if (__DEV__) {
|
|
164
|
+
console.log('[cobbai][push] drop pending open for mismatched chat');
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
clearPendingOpen(pendingOpen);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (!chatSurfaceOnScreen) {
|
|
172
|
+
openChatScreen();
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!ready || !webViewRef.current) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const openConversationScript = serializeNativeEvent(NATIVE_TO_WEB_MESSAGES.OPEN_CONVERSATION, {
|
|
181
|
+
conversation_id: pendingOpen.conversationId,
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
webViewRef.current.injectJavaScript(openConversationScript);
|
|
185
|
+
clearPendingOpen(pendingOpen);
|
|
186
|
+
|
|
187
|
+
if (__DEV__) {
|
|
188
|
+
console.log(`[cobbai][push] open-conversation id=${pendingOpen.conversationId}`);
|
|
189
|
+
}
|
|
190
|
+
}, [chatSurfaceOnScreen, chatId, pushStore.pendingOpen, ready, webViewRef]);
|
|
191
|
+
}
|