@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.
Files changed (143) hide show
  1. package/CobbaiMobileRnSdk.podspec +22 -0
  2. package/LICENSE +32 -0
  3. package/README.md +386 -0
  4. package/dist/ChatPlaceholder.d.ts +13 -0
  5. package/dist/ChatPlaceholder.d.ts.map +1 -0
  6. package/dist/ChatPlaceholder.js +87 -0
  7. package/dist/CobbaiChatScreen.d.ts +4 -0
  8. package/dist/CobbaiChatScreen.d.ts.map +1 -0
  9. package/dist/CobbaiChatScreen.js +196 -0
  10. package/dist/CobbaiInAppBanner.d.ts +15 -0
  11. package/dist/CobbaiInAppBanner.d.ts.map +1 -0
  12. package/dist/CobbaiInAppBanner.js +159 -0
  13. package/dist/CobbaiLauncher.d.ts +19 -0
  14. package/dist/CobbaiLauncher.d.ts.map +1 -0
  15. package/dist/CobbaiLauncher.js +149 -0
  16. package/dist/CobbaiWebView.d.ts +28 -0
  17. package/dist/CobbaiWebView.d.ts.map +1 -0
  18. package/dist/CobbaiWebView.js +141 -0
  19. package/dist/adapters/expoAdapter.d.ts +85 -0
  20. package/dist/adapters/expoAdapter.d.ts.map +1 -0
  21. package/dist/adapters/expoAdapter.js +152 -0
  22. package/dist/adapters/firebaseAdapter.d.ts +41 -0
  23. package/dist/adapters/firebaseAdapter.d.ts.map +1 -0
  24. package/dist/adapters/firebaseAdapter.js +139 -0
  25. package/dist/adapters/iosRelayAdapter.d.ts +26 -0
  26. package/dist/adapters/iosRelayAdapter.d.ts.map +1 -0
  27. package/dist/adapters/iosRelayAdapter.js +101 -0
  28. package/dist/androidKeyboardFrame.d.ts +16 -0
  29. package/dist/androidKeyboardFrame.d.ts.map +1 -0
  30. package/dist/androidKeyboardFrame.js +34 -0
  31. package/dist/bridge/messages.d.ts +20 -0
  32. package/dist/bridge/messages.d.ts.map +1 -0
  33. package/dist/bridge/messages.js +20 -0
  34. package/dist/bridge/nativeEvent.d.ts +10 -0
  35. package/dist/bridge/nativeEvent.d.ts.map +1 -0
  36. package/dist/bridge/nativeEvent.js +15 -0
  37. package/dist/bridge/parser.d.ts +29 -0
  38. package/dist/bridge/parser.d.ts.map +1 -0
  39. package/dist/bridge/parser.js +109 -0
  40. package/dist/chatPresentation.d.ts +19 -0
  41. package/dist/chatPresentation.d.ts.map +1 -0
  42. package/dist/chatPresentation.js +51 -0
  43. package/dist/chatScreen/useInAppBannerQueue.d.ts +22 -0
  44. package/dist/chatScreen/useInAppBannerQueue.d.ts.map +1 -0
  45. package/dist/chatScreen/useInAppBannerQueue.js +60 -0
  46. package/dist/chatScreen/useKeyboardFrame.d.ts +14 -0
  47. package/dist/chatScreen/useKeyboardFrame.d.ts.map +1 -0
  48. package/dist/chatScreen/useKeyboardFrame.js +63 -0
  49. package/dist/chatScreen/useLauncherPresentation.d.ts +20 -0
  50. package/dist/chatScreen/useLauncherPresentation.d.ts.map +1 -0
  51. package/dist/chatScreen/useLauncherPresentation.js +76 -0
  52. package/dist/chatScreen/useNativeToWebSync.d.ts +17 -0
  53. package/dist/chatScreen/useNativeToWebSync.d.ts.map +1 -0
  54. package/dist/chatScreen/useNativeToWebSync.js +36 -0
  55. package/dist/chatScreen/usePushIntegration.d.ts +19 -0
  56. package/dist/chatScreen/usePushIntegration.d.ts.map +1 -0
  57. package/dist/chatScreen/usePushIntegration.js +129 -0
  58. package/dist/chatScreen/useReadyLifecycle.d.ts +33 -0
  59. package/dist/chatScreen/useReadyLifecycle.d.ts.map +1 -0
  60. package/dist/chatScreen/useReadyLifecycle.js +138 -0
  61. package/dist/chatScreen/useSheetPresentation.d.ts +25 -0
  62. package/dist/chatScreen/useSheetPresentation.d.ts.map +1 -0
  63. package/dist/chatScreen/useSheetPresentation.js +98 -0
  64. package/dist/chatUnavailable.d.ts +23 -0
  65. package/dist/chatUnavailable.d.ts.map +1 -0
  66. package/dist/chatUnavailable.js +36 -0
  67. package/dist/externalLinks.d.ts +4 -0
  68. package/dist/externalLinks.d.ts.map +1 -0
  69. package/dist/externalLinks.js +48 -0
  70. package/dist/host/buildHostHtml.d.ts +7 -0
  71. package/dist/host/buildHostHtml.d.ts.map +1 -0
  72. package/dist/host/buildHostHtml.js +101 -0
  73. package/dist/host/hostPageKey.d.ts +7 -0
  74. package/dist/host/hostPageKey.d.ts.map +1 -0
  75. package/dist/host/hostPageKey.js +18 -0
  76. package/dist/inAppBanner.d.ts +19 -0
  77. package/dist/inAppBanner.d.ts.map +1 -0
  78. package/dist/inAppBanner.js +42 -0
  79. package/dist/index.d.ts +10 -0
  80. package/dist/index.d.ts.map +1 -0
  81. package/dist/index.js +32 -0
  82. package/dist/launcher.d.ts +25 -0
  83. package/dist/launcher.d.ts.map +1 -0
  84. package/dist/launcher.js +55 -0
  85. package/dist/launcherGlyph.d.ts +2 -0
  86. package/dist/launcherGlyph.d.ts.map +1 -0
  87. package/dist/launcherGlyph.js +5 -0
  88. package/dist/openChatScreen.d.ts +13 -0
  89. package/dist/openChatScreen.d.ts.map +1 -0
  90. package/dist/openChatScreen.js +63 -0
  91. package/dist/push.d.ts +59 -0
  92. package/dist/push.d.ts.map +1 -0
  93. package/dist/push.js +309 -0
  94. package/dist/pushIntegration.d.ts +76 -0
  95. package/dist/pushIntegration.d.ts.map +1 -0
  96. package/dist/pushIntegration.js +244 -0
  97. package/dist/pushLogout.d.ts +57 -0
  98. package/dist/pushLogout.d.ts.map +1 -0
  99. package/dist/pushLogout.js +225 -0
  100. package/dist/types.d.ts +42 -0
  101. package/dist/types.d.ts.map +1 -0
  102. package/dist/types.js +2 -0
  103. package/dist/version.d.ts +8 -0
  104. package/dist/version.d.ts.map +1 -0
  105. package/dist/version.js +10 -0
  106. package/ios/CobbaiPush.m +96 -0
  107. package/ios/CobbaiPushRelay.h +59 -0
  108. package/ios/CobbaiPushRelay.m +364 -0
  109. package/package.json +69 -0
  110. package/src/ChatPlaceholder.tsx +132 -0
  111. package/src/CobbaiChatScreen.tsx +348 -0
  112. package/src/CobbaiInAppBanner.tsx +237 -0
  113. package/src/CobbaiLauncher.tsx +223 -0
  114. package/src/CobbaiWebView.tsx +242 -0
  115. package/src/adapters/expoAdapter.ts +227 -0
  116. package/src/adapters/firebaseAdapter.ts +201 -0
  117. package/src/adapters/iosRelayAdapter.ts +137 -0
  118. package/src/androidKeyboardFrame.ts +56 -0
  119. package/src/bridge/messages.ts +24 -0
  120. package/src/bridge/nativeEvent.ts +12 -0
  121. package/src/bridge/parser.ts +159 -0
  122. package/src/chatPresentation.ts +73 -0
  123. package/src/chatScreen/useInAppBannerQueue.ts +91 -0
  124. package/src/chatScreen/useKeyboardFrame.ts +89 -0
  125. package/src/chatScreen/useLauncherPresentation.ts +121 -0
  126. package/src/chatScreen/useNativeToWebSync.ts +54 -0
  127. package/src/chatScreen/usePushIntegration.ts +191 -0
  128. package/src/chatScreen/useReadyLifecycle.ts +186 -0
  129. package/src/chatScreen/useSheetPresentation.ts +147 -0
  130. package/src/chatUnavailable.ts +58 -0
  131. package/src/externalLinks.ts +56 -0
  132. package/src/host/buildHostHtml.ts +117 -0
  133. package/src/host/hostPageKey.ts +17 -0
  134. package/src/inAppBanner.ts +58 -0
  135. package/src/index.ts +53 -0
  136. package/src/launcher.ts +79 -0
  137. package/src/launcherGlyph.ts +3 -0
  138. package/src/openChatScreen.ts +78 -0
  139. package/src/push.ts +424 -0
  140. package/src/pushIntegration.ts +338 -0
  141. package/src/pushLogout.ts +277 -0
  142. package/src/types.ts +46 -0
  143. package/src/version.ts +8 -0
@@ -0,0 +1,79 @@
1
+ export type LauncherAppearance = {
2
+ backgroundColor: string;
3
+ textColor: string;
4
+ };
5
+
6
+ export type LauncherVisibilityArgs = {
7
+ launcherVisible: boolean;
8
+ visible: boolean;
9
+ presented: boolean;
10
+ appearance: LauncherAppearance | null;
11
+ };
12
+
13
+ export const DEFAULT_LAUNCHER_APPEARANCE: LauncherAppearance = {
14
+ backgroundColor: '#2D2D30',
15
+ textColor: '#FFFFFF',
16
+ };
17
+
18
+ export const LAUNCHER_DEFAULT_FALLBACK_DELAY_MS = 3_000;
19
+ export const LAUNCHER_EDGE_OFFSET = 16;
20
+ export const LAUNCHER_WRAPPER_SIZE = 48;
21
+ export const LAUNCHER_GLYPH_SIZE = 30;
22
+ export const LAUNCHER_SHADOW_COLOR = '#555454';
23
+ export const LAUNCHER_FADE_IN_DURATION_MS = 150;
24
+ export const LAUNCHER_BADGE_APPEAR_DURATION_MS = 220;
25
+ export const LAUNCHER_BADGE_PULSE_DURATION_MS = 380;
26
+
27
+ export function formatUnreadBadge(total: number | null | undefined): string {
28
+ if (typeof total !== 'number' || !Number.isFinite(total) || total <= 0) {
29
+ return '';
30
+ }
31
+
32
+ if (total > 9) {
33
+ return '9+';
34
+ }
35
+
36
+ return String(total);
37
+ }
38
+
39
+ export function resolveLauncherBottomOffset(
40
+ launcherBottomOffset: number | undefined,
41
+ safeAreaBottom: number,
42
+ ): number {
43
+ if (
44
+ typeof launcherBottomOffset !== 'number' ||
45
+ !Number.isFinite(launcherBottomOffset) ||
46
+ launcherBottomOffset < 0
47
+ ) {
48
+ return safeAreaBottom;
49
+ }
50
+
51
+ return launcherBottomOffset;
52
+ }
53
+
54
+ export function shouldShowLauncher(args: LauncherVisibilityArgs): boolean {
55
+ return args.launcherVisible && !args.visible && !args.presented && args.appearance != null;
56
+ }
57
+
58
+ export function getLauncherAccessibilityLabel(locale: string | undefined): string {
59
+ if (typeof locale === 'string' && locale.toLowerCase().startsWith('fr')) {
60
+ return 'Ouvrir le chat';
61
+ }
62
+
63
+ return 'Open chat';
64
+ }
65
+
66
+ export function getLauncherUnreadAccessibilityText(
67
+ locale: string | undefined,
68
+ total: number | null | undefined,
69
+ ): string | undefined {
70
+ if (typeof total !== 'number' || !Number.isFinite(total) || total <= 0) {
71
+ return undefined;
72
+ }
73
+
74
+ if (typeof locale === 'string' && locale.toLowerCase().startsWith('fr')) {
75
+ return `${total} non lus`;
76
+ }
77
+
78
+ return `${total} unread`;
79
+ }
@@ -0,0 +1,3 @@
1
+ // Source: Cobbai fa-kit fa-chat-button icon, cropped to 512x512 and rasterized to 90x90 PNG.
2
+ export const CHAT_BUTTON_GLYPH_URI =
3
+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAABaEAYAAABoOJ1BAAAKLklEQVR42u2de3BU1R3H99zN+4WRR3hMIFEwamWswwxSSsWBYkVKmtaWh9qWPlQQS0fBYhmr2CqtWk3rWKpVWrRMK61F0GLRaZniUCkyYiUErA1ITCLEFAMEMIHE2z++35vpnllINrsnZ3fP7/PPh9179+zv3nzv5e7dc86qkKP4vu/7fqJa84bCOVfDHdfCXTO5QgadTZ/g02Ph8AC+bmu8lSillFKGdloKkBF/E3ZIbCC7WQfl3gYXjYQHfRoe3gaPnAWX02UXwSO+CG/ZBN/3Ftx5KdvvhPKr4HsehS8pgPcFfh7ezwOkYS3cvBM+UgOf5OtD4+LdL+lyIFjfBEPBJNk3woVXwgPnwsMvh0d2wWU/gMu/DZfu53o38HW/gYvuZLtPnv19a96GJ1fDrb+KXD5lFbzxHTjnJ9Hb6fwe3PZ1+DC3o2khfKAI3neE5v8A9S/C73O9w9PhE7Phrs2J2sPJdiBYO0NrQT6X/jD62pkvwYVf4OrXwUOnwaWT4PLPwWXPwaMWwcPz4cG3wEX3w7nr+QZVmgNe0B7rQQ62QP+TVvLp2dG3p4CBylh89r2U8SBczMfFLfBoPp6slzMBPrESbn0ZPlQB1/P5/Tlw3Rr4AP8napwHt5yEj3G7Tq06Q4ED+Hc8mizBtlZC9DNz+Qb4S4/D51XBow7AIzrgIT+DB/DMlream1Nra3siqfkRfCUPvA9HRy6fye1ctxHOeMJuve1H4GOD4eZTcOMc+F0Pfns+vP5rcMN7bCCDge60ux0WCQId6WUT/bSgJh8eOCf6ds7cAJ++yXalfWPNXXDW34LtShY82wVEUvSO9kQS7aqYCC5NHrFdSIL4OPLhpN1wSaPtwnSS7C6H/4r2RBJclfUF9Sf6XduVJAjtxJfxZTj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GOeTKfgtfwD9wBXf46KVwaS086Da4gH849WKsZdPq7M8v4W27hzdHrjaTB+w6jmzJKI7xfXrgxL/hZp4g9l0K1/4O3sXOU3vZfj2/ADr8HHxqdQ9vkEWfUkopZT1FSUx1Q3VDdUN30OP1V+C8TXDpAnjCLvj68fC9e+Fnb4Zfvx5u3AyffNLvEwvXRq/rk03wB6HY2vvoIrh+Pfz3O+CVJfD8GfAV13B7l8M5C+Ldn6lC2hxbidnx3kNw4c/hwSfgUk5LMLoDrngWHjMeLvsvXPBPeDcvJRbzM0GdNvG5x3mj5/KSakE7PJR3RQ7Ng/f+Fa5hJ6daTmCzfyfczKFeJ+f3dYvT7QybRpsSG4k5ALJ4TV88Ac47CLfw+eM7uGIBzQMidDqynXOegAfMgo/wEuko2wv19pIo+Hv66RbU3vI/6CAYRVP1pG0AAAAASUVORK5CYII=';
@@ -0,0 +1,78 @@
1
+ import { clearPendingOpen, getPushStoreSnapshot } from './push';
2
+
3
+ type VisibilityListener = (isVisible: boolean) => void;
4
+
5
+ type VisibilityStore = {
6
+ isVisible: boolean;
7
+ listeners: Set<VisibilityListener>;
8
+ };
9
+
10
+ const STORE_KEY = '__COBBAI_CHAT_VISIBILITY_STORE__';
11
+
12
+ function getVisibilityStore(): VisibilityStore {
13
+ const globalObject = globalThis as typeof globalThis & {
14
+ [STORE_KEY]?: VisibilityStore;
15
+ };
16
+
17
+ if (!globalObject[STORE_KEY]) {
18
+ globalObject[STORE_KEY] = {
19
+ isVisible: false,
20
+ listeners: new Set<VisibilityListener>(),
21
+ };
22
+ }
23
+
24
+ return globalObject[STORE_KEY];
25
+ }
26
+
27
+ function notifyVisibility(): void {
28
+ const store = getVisibilityStore();
29
+ store.listeners.forEach((listener) => listener(store.isVisible));
30
+ }
31
+
32
+ export function openChatScreen(): void {
33
+ const store = getVisibilityStore();
34
+ store.isVisible = true;
35
+ notifyVisibility();
36
+ }
37
+
38
+ export function closeChatScreenInternal(): void {
39
+ const store = getVisibilityStore();
40
+ store.isVisible = false;
41
+ notifyVisibility();
42
+ }
43
+
44
+ /**
45
+ * Closes the chat surface from the host app (logout, deep link, …). Same effect as the user
46
+ * closing it, except that `config.onClose` is not called: the caller already knows.
47
+ * A pending push open is dropped too, otherwise the chat screen would reopen it right away.
48
+ */
49
+ export function closeChatScreen(): void {
50
+ const pendingOpen = getPushStoreSnapshot().pendingOpen;
51
+ if (pendingOpen) {
52
+ clearPendingOpen(pendingOpen);
53
+ }
54
+
55
+ closeChatScreenInternal();
56
+ }
57
+
58
+ export function getChatScreenVisibility(): boolean {
59
+ const store = getVisibilityStore();
60
+ return store.isVisible;
61
+ }
62
+
63
+ export function subscribeChatVisibility(listener: VisibilityListener): () => void {
64
+ const store = getVisibilityStore();
65
+ store.listeners.add(listener);
66
+ listener(store.isVisible);
67
+
68
+ return () => {
69
+ const currentStore = getVisibilityStore();
70
+ currentStore.listeners.delete(listener);
71
+
72
+ // The last chat screen went away while open (typically an unmount on logout). Without this
73
+ // reset, the next CobbaiChatScreen to mount would open by itself.
74
+ if (currentStore.listeners.size === 0 && currentStore.isVisible) {
75
+ currentStore.isVisible = false;
76
+ }
77
+ };
78
+ }
package/src/push.ts ADDED
@@ -0,0 +1,424 @@
1
+ import type { CobbaiInAppBannerPayload } from './inAppBanner';
2
+ import { normalizeInAppBannerText } from './inAppBanner';
3
+
4
+ export type CobbaiPushProvider = 'apns' | 'fcm';
5
+
6
+ export type CobbaiPushToken = {
7
+ token: string;
8
+ provider: CobbaiPushProvider;
9
+ /** Required for 'apns': 'sandbox' for builds run from Xcode, 'production' for TestFlight / App Store. */
10
+ apnsEnvironment?: 'sandbox' | 'production';
11
+ };
12
+
13
+ export type CobbaiForegroundPush = {
14
+ data: unknown;
15
+ title?: string;
16
+ body?: string;
17
+ };
18
+
19
+ type PendingOpen = {
20
+ chatId: string;
21
+ conversationId: number;
22
+ };
23
+
24
+ type PushStoreListener = () => void;
25
+
26
+ type PushStore = {
27
+ token: CobbaiPushToken | null;
28
+ pendingUnregister: CobbaiPushToken | null;
29
+ pendingOpen: PendingOpen | null;
30
+ mountedChatId: string | null;
31
+ connected: boolean;
32
+ bannerEnabled: boolean;
33
+ showBanner: ((banner: CobbaiInAppBannerPayload) => void) | null;
34
+ /** `chatId:messageId` of the push taps already handled, oldest first. */
35
+ handledTaps: string[];
36
+ listeners: Set<PushStoreListener>;
37
+ };
38
+
39
+ export type PushStoreSnapshot = {
40
+ token: CobbaiPushToken | null;
41
+ pendingUnregister: CobbaiPushToken | null;
42
+ pendingOpen: PendingOpen | null;
43
+ mountedChatId: string | null;
44
+ connected: boolean;
45
+ bannerEnabled: boolean;
46
+ };
47
+
48
+ const STORE_KEY = '__COBBAI_PUSH_STORE__';
49
+ // Enough for any burst of duplicate deliveries; a tap is never delivered twice far apart.
50
+ const MAX_HANDLED_TAPS = 100;
51
+ const TOKEN_PATTERN = /^[A-Za-z0-9:_\-.]{1,4096}$/;
52
+ const POSITIVE_INTEGER_STRING_PATTERN = /^[1-9][0-9]*$/;
53
+
54
+ function getPushStore(): PushStore {
55
+ const globalObject = globalThis as typeof globalThis & {
56
+ [STORE_KEY]?: PushStore;
57
+ };
58
+
59
+ if (!globalObject[STORE_KEY]) {
60
+ globalObject[STORE_KEY] = {
61
+ token: null,
62
+ pendingUnregister: null,
63
+ pendingOpen: null,
64
+ mountedChatId: null,
65
+ connected: false,
66
+ bannerEnabled: true,
67
+ showBanner: null,
68
+ handledTaps: [],
69
+ listeners: new Set<PushStoreListener>(),
70
+ };
71
+ }
72
+
73
+ return globalObject[STORE_KEY];
74
+ }
75
+
76
+ function notifyPushStore(): void {
77
+ const store = getPushStore();
78
+ store.listeners.forEach((listener) => listener());
79
+ }
80
+
81
+ function clonePushToken(token: CobbaiPushToken): CobbaiPushToken {
82
+ return {
83
+ token: token.token,
84
+ provider: token.provider,
85
+ apnsEnvironment: token.apnsEnvironment,
86
+ };
87
+ }
88
+
89
+ function arePushTokensEqual(left: CobbaiPushToken | null, right: CobbaiPushToken | null): boolean {
90
+ if (!left || !right) {
91
+ return left === right;
92
+ }
93
+
94
+ return (
95
+ left.token === right.token &&
96
+ left.provider === right.provider &&
97
+ left.apnsEnvironment === right.apnsEnvironment
98
+ );
99
+ }
100
+
101
+ function isValidPushToken(token: CobbaiPushToken): boolean {
102
+ if (!TOKEN_PATTERN.test(token.token)) {
103
+ return false;
104
+ }
105
+
106
+ if (token.provider !== 'apns' && token.provider !== 'fcm') {
107
+ return false;
108
+ }
109
+
110
+ if (token.provider === 'apns') {
111
+ return token.apnsEnvironment === 'sandbox' || token.apnsEnvironment === 'production';
112
+ }
113
+
114
+ return true;
115
+ }
116
+
117
+ function toPushData(
118
+ data: unknown,
119
+ ): { chatId: string; conversationId: number; messageId: number } | null {
120
+ if (!data || typeof data !== 'object' || Array.isArray(data)) {
121
+ return null;
122
+ }
123
+
124
+ const record = data as Record<string, unknown>;
125
+ if (record.cobbai !== '1') {
126
+ return null;
127
+ }
128
+
129
+ if (typeof record.cobbai_chat_id !== 'string' || record.cobbai_chat_id.trim().length === 0) {
130
+ return null;
131
+ }
132
+
133
+ const chatId = record.cobbai_chat_id.trim();
134
+
135
+ const conversationIdRaw = record.cobbai_conversation_id;
136
+ const messageIdRaw = record.cobbai_message_id;
137
+
138
+ if (
139
+ typeof conversationIdRaw !== 'string' ||
140
+ typeof messageIdRaw !== 'string' ||
141
+ !POSITIVE_INTEGER_STRING_PATTERN.test(conversationIdRaw) ||
142
+ !POSITIVE_INTEGER_STRING_PATTERN.test(messageIdRaw)
143
+ ) {
144
+ return null;
145
+ }
146
+
147
+ const conversationId = Number(conversationIdRaw);
148
+ const messageId = Number(messageIdRaw);
149
+
150
+ if (!Number.isSafeInteger(conversationId) || !Number.isSafeInteger(messageId)) {
151
+ return null;
152
+ }
153
+
154
+ return {
155
+ chatId,
156
+ conversationId,
157
+ messageId,
158
+ };
159
+ }
160
+
161
+ /** What the iPhone relay needs to decide alone what the open app shows. */
162
+ export type ForegroundPresentationState = {
163
+ mountedChatId: string | null;
164
+ connected: boolean;
165
+ /** The in-app banner can show: enabled and a chat screen presents it. */
166
+ bannerEnabled: boolean;
167
+ };
168
+
169
+ export function getForegroundPresentationState(): ForegroundPresentationState {
170
+ const store = getPushStore();
171
+ return {
172
+ mountedChatId: store.mountedChatId,
173
+ connected: store.connected,
174
+ bannerEnabled: store.bannerEnabled && store.showBanner !== null,
175
+ };
176
+ }
177
+
178
+ export function subscribePushStore(listener: PushStoreListener): () => void {
179
+ const store = getPushStore();
180
+ store.listeners.add(listener);
181
+
182
+ return () => {
183
+ const currentStore = getPushStore();
184
+ currentStore.listeners.delete(listener);
185
+ };
186
+ }
187
+
188
+ export function getPushStoreSnapshot(): PushStoreSnapshot {
189
+ const store = getPushStore();
190
+
191
+ return {
192
+ token: store.token ? clonePushToken(store.token) : null,
193
+ pendingUnregister: store.pendingUnregister ? clonePushToken(store.pendingUnregister) : null,
194
+ pendingOpen: store.pendingOpen ? { ...store.pendingOpen } : null,
195
+ mountedChatId: store.mountedChatId,
196
+ connected: store.connected,
197
+ bannerEnabled: store.bannerEnabled,
198
+ };
199
+ }
200
+
201
+ export function setMountedChatId(chatId: string): void {
202
+ const store = getPushStore();
203
+ if (store.mountedChatId === chatId) {
204
+ return;
205
+ }
206
+
207
+ store.mountedChatId = chatId;
208
+ notifyPushStore();
209
+ }
210
+
211
+ export function clearMountedChatId(chatId: string): void {
212
+ const store = getPushStore();
213
+ if (store.mountedChatId !== chatId) {
214
+ return;
215
+ }
216
+
217
+ store.mountedChatId = null;
218
+ notifyPushStore();
219
+ }
220
+
221
+ export function setPushConnected(connected: boolean): void {
222
+ const store = getPushStore();
223
+ if (store.connected === connected) {
224
+ return;
225
+ }
226
+
227
+ store.connected = connected;
228
+ notifyPushStore();
229
+ }
230
+
231
+ export function setPushBannerEnabled(enabled: boolean): void {
232
+ const store = getPushStore();
233
+ if (store.bannerEnabled === enabled) {
234
+ return;
235
+ }
236
+
237
+ store.bannerEnabled = enabled;
238
+ notifyPushStore();
239
+ }
240
+
241
+ export function setPushBannerPresenter(
242
+ showBanner: ((banner: CobbaiInAppBannerPayload) => void) | null,
243
+ ): void {
244
+ const store = getPushStore();
245
+ if (store.showBanner === showBanner) {
246
+ return;
247
+ }
248
+
249
+ store.showBanner = showBanner;
250
+ notifyPushStore();
251
+ }
252
+
253
+ export function clearPendingUnregister(token: CobbaiPushToken): void {
254
+ const store = getPushStore();
255
+ if (!arePushTokensEqual(store.pendingUnregister, token)) {
256
+ return;
257
+ }
258
+
259
+ store.pendingUnregister = null;
260
+ notifyPushStore();
261
+ }
262
+
263
+ export function clearPendingOpen(pendingOpen: PendingOpen): void {
264
+ const store = getPushStore();
265
+ if (
266
+ !store.pendingOpen ||
267
+ store.pendingOpen.chatId !== pendingOpen.chatId ||
268
+ store.pendingOpen.conversationId !== pendingOpen.conversationId
269
+ ) {
270
+ return;
271
+ }
272
+
273
+ store.pendingOpen = null;
274
+ notifyPushStore();
275
+ }
276
+
277
+ export function setPushToken(token: CobbaiPushToken): boolean {
278
+ if (!isValidPushToken(token)) {
279
+ if (__DEV__) {
280
+ console.warn('[cobbai][push] ignored invalid token payload');
281
+ }
282
+ return false;
283
+ }
284
+
285
+ const store = getPushStore();
286
+ const nextToken = clonePushToken(token);
287
+ const hadPendingUnregisterForSameToken =
288
+ store.pendingUnregister != null && arePushTokensEqual(store.pendingUnregister, nextToken);
289
+ const tokenChanged = !arePushTokensEqual(store.token, nextToken);
290
+
291
+ store.token = nextToken;
292
+
293
+ if (hadPendingUnregisterForSameToken) {
294
+ store.pendingUnregister = null;
295
+ }
296
+
297
+ if (tokenChanged || hadPendingUnregisterForSameToken) {
298
+ notifyPushStore();
299
+ }
300
+
301
+ return true;
302
+ }
303
+
304
+ export function clearPushToken(): void {
305
+ const store = getPushStore();
306
+ if (!store.token) {
307
+ return;
308
+ }
309
+
310
+ store.pendingUnregister = clonePushToken(store.token);
311
+ store.token = null;
312
+ notifyPushStore();
313
+ }
314
+
315
+ export function isCobbaiPush(data: unknown): boolean {
316
+ return toPushData(data) != null;
317
+ }
318
+
319
+ /**
320
+ * A tap on a notification, the tap that launched the app included. Returns true for a Cobbai push
321
+ * of the mounted chat (or of any chat while none is mounted). The same push tap can reach the SDK
322
+ * by more than one path (an adapter and the iPhone relay, or two events of one library): each one
323
+ * is handled once.
324
+ */
325
+ export function handleCobbaiPushTap(data: unknown): boolean {
326
+ const pushData = toPushData(data);
327
+ if (!pushData) {
328
+ return false;
329
+ }
330
+
331
+ const store = getPushStore();
332
+ if (store.mountedChatId && store.mountedChatId !== pushData.chatId) {
333
+ return false;
334
+ }
335
+
336
+ const tapKey = `${pushData.chatId}:${pushData.messageId}`;
337
+ if (store.handledTaps.includes(tapKey)) {
338
+ return true;
339
+ }
340
+
341
+ store.handledTaps.push(tapKey);
342
+ if (store.handledTaps.length > MAX_HANDLED_TAPS) {
343
+ store.handledTaps.shift();
344
+ }
345
+
346
+ requestPendingOpen(pushData.chatId, pushData.conversationId);
347
+ return true;
348
+ }
349
+
350
+ /** A tap on the SDK's in-app banner: opens the conversation every time (no duplicate check). */
351
+ export function openConversationFromBanner(chatId: string, conversationId: number): void {
352
+ requestPendingOpen(chatId, conversationId);
353
+ }
354
+
355
+ function requestPendingOpen(chatId: string, conversationId: number): void {
356
+ const store = getPushStore();
357
+ store.pendingOpen = { chatId, conversationId };
358
+ notifyPushStore();
359
+ }
360
+
361
+ export function handleCobbaiPushInForeground(push: CobbaiForegroundPush): boolean {
362
+ const pushData = toPushData(push.data);
363
+ if (!pushData) {
364
+ return false;
365
+ }
366
+
367
+ const store = getPushStore();
368
+ if (!store.mountedChatId || store.mountedChatId !== pushData.chatId) {
369
+ return false;
370
+ }
371
+
372
+ if (store.connected) {
373
+ if (__DEV__) {
374
+ console.log('[cobbai][push] foreground dropped');
375
+ }
376
+
377
+ return true;
378
+ }
379
+
380
+ if (!store.bannerEnabled) {
381
+ return false;
382
+ }
383
+
384
+ const preview = normalizeInAppBannerText(push.body);
385
+ if (!preview) {
386
+ if (__DEV__) {
387
+ console.log('[cobbai][push] foreground dropped');
388
+ }
389
+
390
+ return true;
391
+ }
392
+
393
+ if (!store.showBanner) {
394
+ return false;
395
+ }
396
+
397
+ store.showBanner({
398
+ chatId: pushData.chatId,
399
+ conversationId: pushData.conversationId,
400
+ messageId: pushData.messageId,
401
+ senderName: normalizeInAppBannerText(push.title),
402
+ preview,
403
+ source: 'push',
404
+ });
405
+
406
+ if (__DEV__) {
407
+ console.log('[cobbai][push] foreground banner');
408
+ }
409
+
410
+ return true;
411
+ }
412
+
413
+ export function __resetPushStoreForTests(): void {
414
+ const store = getPushStore();
415
+ store.token = null;
416
+ store.pendingUnregister = null;
417
+ store.pendingOpen = null;
418
+ store.mountedChatId = null;
419
+ store.connected = false;
420
+ store.bannerEnabled = true;
421
+ store.showBanner = null;
422
+ store.handledTaps = [];
423
+ notifyPushStore();
424
+ }