@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,364 @@
1
+ #import "CobbaiPushRelay.h"
2
+
3
+ #import <UIKit/UIKit.h>
4
+ #import <objc/runtime.h>
5
+
6
+ NSString *const CobbaiPushTokenEvent = @"CobbaiPushToken";
7
+ NSString *const CobbaiPushTapEvent = @"CobbaiPushTap";
8
+ NSString *const CobbaiPushForegroundEvent = @"CobbaiPushForeground";
9
+
10
+ static NSString *const CobbaiAutoInstallKey = @"CobbaiPushAutoInstall";
11
+ static const NSUInteger CobbaiMaxPendingTaps = 10;
12
+
13
+ #pragma mark - Cobbai push data
14
+
15
+ static BOOL CobbaiIsCobbaiPush(NSDictionary *userInfo) {
16
+ id flag = userInfo[@"cobbai"];
17
+ return [flag isKindOfClass:[NSString class]] && [flag isEqualToString:@"1"];
18
+ }
19
+
20
+ /// The `cobbai*` string values of the payload: all the SDK reads, and always JSON-safe.
21
+ static NSDictionary *CobbaiPushData(NSDictionary *userInfo) {
22
+ NSMutableDictionary *data = [NSMutableDictionary dictionary];
23
+ [userInfo enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
24
+ if ([key isKindOfClass:[NSString class]] && [key hasPrefix:@"cobbai"] &&
25
+ [value isKindOfClass:[NSString class]]) {
26
+ data[key] = value;
27
+ }
28
+ }];
29
+ return data;
30
+ }
31
+
32
+ static NSString *CobbaiHexToken(NSData *deviceToken) {
33
+ const unsigned char *bytes = deviceToken.bytes;
34
+ NSMutableString *hex = [NSMutableString stringWithCapacity:deviceToken.length * 2];
35
+ for (NSUInteger index = 0; index < deviceToken.length; index++) {
36
+ [hex appendFormat:@"%02x", bytes[index]];
37
+ }
38
+ return hex;
39
+ }
40
+
41
+ static BOOL CobbaiIsAuthorized(UNAuthorizationStatus status) {
42
+ return status == UNAuthorizationStatusAuthorized || status == UNAuthorizationStatusProvisional ||
43
+ status == UNAuthorizationStatusEphemeral;
44
+ }
45
+
46
+ #pragma mark - App delegate hooks
47
+
48
+ /// Adds (or wraps) `application:didRegisterForRemoteNotificationsWithDeviceToken:` on the app
49
+ /// delegate's class. The app's own implementation, if any, runs first, unchanged.
50
+ static void CobbaiHookDidRegister(Class cls) {
51
+ SEL selector = @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:);
52
+ Method method = class_getInstanceMethod(cls, selector);
53
+ IMP original = method ? method_getImplementation(method) : NULL;
54
+ const char *types = method ? method_getTypeEncoding(method) : "v@:@@";
55
+
56
+ IMP replacement = imp_implementationWithBlock(^(id receiver, UIApplication *application, NSData *token) {
57
+ if (original) {
58
+ ((void (*)(id, SEL, UIApplication *, NSData *))original)(receiver, selector, application, token);
59
+ }
60
+ [[CobbaiPushRelay shared] didRegisterWithDeviceToken:token];
61
+ });
62
+
63
+ // Fails when the class implements the method itself: replace it there. Succeeds when the method
64
+ // is inherited or missing: the override calls the inherited one (or nothing).
65
+ if (!class_addMethod(cls, selector, replacement, types)) {
66
+ method_setImplementation(method, replacement);
67
+ }
68
+ }
69
+
70
+ static void CobbaiHookDidFailToRegister(Class cls) {
71
+ SEL selector = @selector(application:didFailToRegisterForRemoteNotificationsWithError:);
72
+ Method method = class_getInstanceMethod(cls, selector);
73
+ IMP original = method ? method_getImplementation(method) : NULL;
74
+ const char *types = method ? method_getTypeEncoding(method) : "v@:@@";
75
+
76
+ IMP replacement = imp_implementationWithBlock(^(id receiver, UIApplication *application, NSError *error) {
77
+ if (original) {
78
+ ((void (*)(id, SEL, UIApplication *, NSError *))original)(receiver, selector, application, error);
79
+ }
80
+ [[CobbaiPushRelay shared] didFailToRegisterWithError:error];
81
+ });
82
+
83
+ if (!class_addMethod(cls, selector, replacement, types)) {
84
+ method_setImplementation(method, replacement);
85
+ }
86
+ }
87
+
88
+ #pragma mark - Relay
89
+
90
+ @interface CobbaiPushRelay ()
91
+ @property (nonatomic, weak, nullable) id<UNUserNotificationCenterDelegate> chainedDelegate;
92
+ @property (nonatomic, copy, nullable) NSString *mountedChatId;
93
+ @property (nonatomic, assign) BOOL connected;
94
+ @property (nonatomic, assign) BOOL bannerEnabled;
95
+ @property (nonatomic, strong) NSMutableArray<NSDictionary *> *pendingTaps;
96
+ @property (nonatomic, copy, nullable) NSString *token;
97
+ @property (nonatomic, copy, nullable) CobbaiPushRelayEventSink sink;
98
+ @property (nonatomic, strong) NSMutableSet<NSString *> *hookedClasses;
99
+ @property (nonatomic, assign) BOOL installed;
100
+ @end
101
+
102
+ @implementation CobbaiPushRelay
103
+
104
+ + (void)load {
105
+ [[NSNotificationCenter defaultCenter] addObserver:self
106
+ selector:@selector(cobbai_applicationDidFinishLaunching:)
107
+ name:UIApplicationDidFinishLaunchingNotification
108
+ object:nil];
109
+ }
110
+
111
+ + (void)cobbai_applicationDidFinishLaunching:(NSNotification *)notification {
112
+ id autoInstall = [[NSBundle mainBundle] objectForInfoDictionaryKey:CobbaiAutoInstallKey];
113
+ if ([autoInstall respondsToSelector:@selector(boolValue)] && ![autoInstall boolValue]) {
114
+ return;
115
+ }
116
+ [[self shared] install];
117
+ }
118
+
119
+ + (instancetype)shared {
120
+ static CobbaiPushRelay *shared;
121
+ static dispatch_once_t once;
122
+ dispatch_once(&once, ^{
123
+ shared = [[CobbaiPushRelay alloc] init];
124
+ });
125
+ return shared;
126
+ }
127
+
128
+ - (instancetype)init {
129
+ if (self = [super init]) {
130
+ _pendingTaps = [NSMutableArray array];
131
+ _hookedClasses = [NSMutableSet set];
132
+ }
133
+ return self;
134
+ }
135
+
136
+ - (void)install {
137
+ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
138
+
139
+ // Already the delegate, or under a library that chains to us: taking the delegate again would
140
+ // make it chain back to that library, a loop.
141
+ if (![self isInPlace]) {
142
+ id<UNUserNotificationCenterDelegate> previous = center.delegate;
143
+ if (previous != self) {
144
+ self.chainedDelegate = previous;
145
+ }
146
+ center.delegate = self;
147
+ self.installed = YES;
148
+ }
149
+
150
+ [self hookApplicationDelegate];
151
+ [self registerIfAuthorized];
152
+ }
153
+
154
+ - (BOOL)isInPlace {
155
+ id<UNUserNotificationCenterDelegate> delegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
156
+ if (delegate == self) {
157
+ return YES;
158
+ }
159
+ if (delegate == nil || !self.installed) {
160
+ // Never installed, or installed and then replaced by a delegate that we did not chain to.
161
+ return NO;
162
+ }
163
+ NSString *className = NSStringFromClass([delegate class]);
164
+ return [className containsString:@"RNFBMessaging"] || [className containsString:@"NotificationCenterManager"];
165
+ }
166
+
167
+ - (void)hookApplicationDelegate {
168
+ id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate;
169
+ if (appDelegate == nil) {
170
+ return;
171
+ }
172
+
173
+ Class cls = [appDelegate class];
174
+ NSString *className = NSStringFromClass(cls);
175
+ if ([self.hookedClasses containsObject:className]) {
176
+ return;
177
+ }
178
+ [self.hookedClasses addObject:className];
179
+
180
+ CobbaiHookDidRegister(cls);
181
+ CobbaiHookDidFailToRegister(cls);
182
+ }
183
+
184
+ - (void)registerIfAuthorized {
185
+ [[UNUserNotificationCenter currentNotificationCenter]
186
+ getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {
187
+ if (!CobbaiIsAuthorized(settings.authorizationStatus)) {
188
+ return;
189
+ }
190
+ dispatch_async(dispatch_get_main_queue(), ^{
191
+ [[UIApplication sharedApplication] registerForRemoteNotifications];
192
+ });
193
+ }];
194
+ }
195
+
196
+ - (void)setMountedChatId:(NSString *)chatId connected:(BOOL)connected bannerEnabled:(BOOL)bannerEnabled {
197
+ self.mountedChatId = chatId.length > 0 ? chatId : nil;
198
+ self.connected = connected;
199
+ self.bannerEnabled = bannerEnabled;
200
+ }
201
+
202
+ - (NSDictionary *)attachSink:(CobbaiPushRelayEventSink)sink {
203
+ self.sink = sink;
204
+ NSArray *pendingTaps = [self.pendingTaps copy];
205
+ [self.pendingTaps removeAllObjects];
206
+
207
+ return @{
208
+ @"token" : self.token ?: [NSNull null],
209
+ @"apnsEnvironment" : [CobbaiPushRelay apnsEnvironment],
210
+ @"pendingTaps" : pendingTaps,
211
+ @"inPlace" : @([self isInPlace]),
212
+ };
213
+ }
214
+
215
+ - (void)detachSink {
216
+ self.sink = nil;
217
+ }
218
+
219
+ - (void)emit:(NSString *)name body:(NSDictionary *)body {
220
+ CobbaiPushRelayEventSink sink = self.sink;
221
+ if (sink) {
222
+ sink(name, body);
223
+ }
224
+ }
225
+
226
+ - (void)didRegisterWithDeviceToken:(NSData *)deviceToken {
227
+ NSString *token = CobbaiHexToken(deviceToken);
228
+ if (token.length == 0) {
229
+ return;
230
+ }
231
+ self.token = token;
232
+ [self emit:CobbaiPushTokenEvent
233
+ body:@{@"token" : token, @"apnsEnvironment" : [CobbaiPushRelay apnsEnvironment]}];
234
+ }
235
+
236
+ - (void)didFailToRegisterWithError:(NSError *)error {
237
+ #if DEBUG
238
+ NSLog(@"[cobbai][push] APNs registration failed: %@", error.localizedDescription);
239
+ #endif
240
+ }
241
+
242
+ #pragma mark UNUserNotificationCenterDelegate
243
+
244
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
245
+ willPresentNotification:(UNNotification *)notification
246
+ withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
247
+ NSDictionary *userInfo = notification.request.content.userInfo;
248
+
249
+ if (!CobbaiIsCobbaiPush(userInfo)) {
250
+ id<UNUserNotificationCenterDelegate> chained = self.chainedDelegate;
251
+ if ([chained respondsToSelector:_cmd]) {
252
+ [chained userNotificationCenter:center willPresentNotification:notification withCompletionHandler:completionHandler];
253
+ } else {
254
+ // What iOS does without a delegate.
255
+ completionHandler(UNNotificationPresentationOptionNone);
256
+ }
257
+ return;
258
+ }
259
+
260
+ NSDictionary *data = CobbaiPushData(userInfo);
261
+ NSString *chatId = data[@"cobbai_chat_id"];
262
+ BOOL chatMounted = self.sink != nil && self.mountedChatId != nil && [self.mountedChatId isEqualToString:chatId];
263
+
264
+ // Connected → nothing shown; mounted with the banner → the SDK's banner; otherwise the system.
265
+ if (chatMounted && (self.connected || self.bannerEnabled)) {
266
+ completionHandler(UNNotificationPresentationOptionNone);
267
+ UNNotificationContent *content = notification.request.content;
268
+ [self emit:CobbaiPushForegroundEvent
269
+ body:@{@"data" : data, @"title" : content.title ?: @"", @"body" : content.body ?: @""}];
270
+ return;
271
+ }
272
+
273
+ completionHandler(UNNotificationPresentationOptionBanner | UNNotificationPresentationOptionList |
274
+ UNNotificationPresentationOptionSound);
275
+ }
276
+
277
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
278
+ didReceiveNotificationResponse:(UNNotificationResponse *)response
279
+ withCompletionHandler:(void (^)(void))completionHandler {
280
+ NSDictionary *userInfo = response.notification.request.content.userInfo;
281
+
282
+ if (!CobbaiIsCobbaiPush(userInfo)) {
283
+ id<UNUserNotificationCenterDelegate> chained = self.chainedDelegate;
284
+ if ([chained respondsToSelector:_cmd]) {
285
+ [chained userNotificationCenter:center didReceiveNotificationResponse:response withCompletionHandler:completionHandler];
286
+ } else {
287
+ completionHandler();
288
+ }
289
+ return;
290
+ }
291
+
292
+ if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
293
+ NSDictionary *data = CobbaiPushData(userInfo);
294
+ if (self.sink) {
295
+ [self emit:CobbaiPushTapEvent body:@{@"data" : data}];
296
+ } else {
297
+ [self.pendingTaps addObject:data];
298
+ if (self.pendingTaps.count > CobbaiMaxPendingTaps) {
299
+ [self.pendingTaps removeObjectAtIndex:0];
300
+ }
301
+ }
302
+ }
303
+
304
+ completionHandler();
305
+ }
306
+
307
+ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
308
+ openSettingsForNotification:(nullable UNNotification *)notification {
309
+ id<UNUserNotificationCenterDelegate> chained = self.chainedDelegate;
310
+ if ([chained respondsToSelector:_cmd]) {
311
+ [chained userNotificationCenter:center openSettingsForNotification:notification];
312
+ }
313
+ }
314
+
315
+ #pragma mark - Environment and status
316
+
317
+ + (NSString *)apnsEnvironment {
318
+ #if TARGET_OS_SIMULATOR
319
+ return @"sandbox";
320
+ #else
321
+ static NSString *environment;
322
+ static dispatch_once_t once;
323
+ dispatch_once(&once, ^{
324
+ environment = @"production"; // App Store builds carry no embedded profile.
325
+
326
+ NSString *path = [[NSBundle mainBundle] pathForResource:@"embedded" ofType:@"mobileprovision"];
327
+ NSData *profile = path ? [NSData dataWithContentsOfFile:path] : nil;
328
+ if (profile == nil) {
329
+ return;
330
+ }
331
+
332
+ // The profile is a signed CMS envelope around an XML plist: read the plist part.
333
+ NSString *content = [[NSString alloc] initWithData:profile encoding:NSISOLatin1StringEncoding];
334
+ NSRange start = [content rangeOfString:@"<?xml"];
335
+ NSRange end = [content rangeOfString:@"</plist>"];
336
+ if (start.location == NSNotFound || end.location == NSNotFound || end.location < start.location) {
337
+ return;
338
+ }
339
+
340
+ NSString *plistText = [content substringWithRange:NSMakeRange(start.location, NSMaxRange(end) - start.location)];
341
+ NSData *plistData = [plistText dataUsingEncoding:NSISOLatin1StringEncoding];
342
+ id plist = [NSPropertyListSerialization propertyListWithData:plistData options:0 format:NULL error:NULL];
343
+ if (![plist isKindOfClass:[NSDictionary class]]) {
344
+ return;
345
+ }
346
+
347
+ id entitlements = plist[@"Entitlements"];
348
+ id aps = [entitlements isKindOfClass:[NSDictionary class]] ? entitlements[@"aps-environment"] : nil;
349
+ if ([aps isKindOfClass:[NSString class]] && [aps isEqualToString:@"development"]) {
350
+ environment = @"sandbox";
351
+ }
352
+ });
353
+ return environment;
354
+ #endif
355
+ }
356
+
357
+ + (NSString *)statusForAuthorization:(UNAuthorizationStatus)status {
358
+ if (CobbaiIsAuthorized(status)) {
359
+ return @"granted";
360
+ }
361
+ return status == UNAuthorizationStatusDenied ? @"denied" : @"not-asked";
362
+ }
363
+
364
+ @end
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@cobbai/react-native-sdk",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "Cobbai React Native SDK",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "react-native": "src/index.ts",
8
+ "engines": {
9
+ "node": ">=22.11"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc -p tsconfig.json",
13
+ "clean": "rm -rf dist",
14
+ "typecheck": "tsc -p tsconfig.json --noEmit",
15
+ "typecheck:test": "tsc -p tsconfig.test.json --noEmit",
16
+ "lint": "eslint .",
17
+ "format": "prettier --write .",
18
+ "format:check": "prettier --check .",
19
+ "test": "jest",
20
+ "prepack": "npm run build"
21
+ },
22
+ "keywords": [
23
+ "react-native",
24
+ "sdk",
25
+ "chat",
26
+ "webview"
27
+ ],
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/cobbaicorp/cobbai_mobile_rn_sdk.git"
31
+ },
32
+ "author": "Cobbai",
33
+ "license": "SEE LICENSE IN LICENSE",
34
+ "files": [
35
+ "dist",
36
+ "src",
37
+ "ios",
38
+ "CobbaiMobileRnSdk.podspec",
39
+ "LICENSE"
40
+ ],
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "peerDependencies": {
45
+ "react": ">=18",
46
+ "react-native": ">=0.76",
47
+ "react-native-safe-area-context": ">=4",
48
+ "react-native-webview": ">=13"
49
+ },
50
+ "devDependencies": {
51
+ "@eslint/js": "^10.0.1",
52
+ "@types/jest": "^29.5.14",
53
+ "@types/react": "18.2.79",
54
+ "eslint": "^10.14.0",
55
+ "eslint-config-prettier": "^10.1.8",
56
+ "eslint-plugin-react-hooks": "^7.1.1",
57
+ "globals": "^17.14.0",
58
+ "jest": "^29.7.0",
59
+ "prettier": "^3.9.10",
60
+ "react": "18.2.0",
61
+ "react-native": "0.74.7",
62
+ "react-native-safe-area-context": "^4.14.1",
63
+ "react-native-webview": "^14.0.1",
64
+ "react-test-renderer": "18.2.0",
65
+ "ts-jest": "^29.4.5",
66
+ "typescript": "^5.6.3",
67
+ "typescript-eslint": "^8.72.2"
68
+ }
69
+ }
@@ -0,0 +1,132 @@
1
+ import React, { useMemo } from 'react';
2
+ import { ActivityIndicator, Animated, Pressable, StyleSheet, Text, View } from 'react-native';
3
+ import { getChatUnavailableCopy } from './chatUnavailable';
4
+
5
+ type ChatPlaceholderMode = 'loading' | 'unavailable';
6
+
7
+ type ChatPlaceholderProps = {
8
+ mode: ChatPlaceholderMode;
9
+ locale: string | undefined;
10
+ opacity: Animated.Value;
11
+ onClose: () => void;
12
+ onRetry: () => void;
13
+ };
14
+
15
+ export function ChatPlaceholder(props: ChatPlaceholderProps): React.JSX.Element {
16
+ const { mode, locale, opacity, onClose, onRetry } = props;
17
+ const copy = useMemo(() => getChatUnavailableCopy(locale), [locale]);
18
+
19
+ return (
20
+ <Animated.View style={[styles.root, { opacity }]} pointerEvents="auto">
21
+ <Pressable
22
+ accessibilityRole="button"
23
+ accessibilityLabel={copy.close}
24
+ testID="cobbai-placeholder-close"
25
+ onPress={onClose}
26
+ style={styles.closeButton}
27
+ >
28
+ <View style={styles.closeIcon}>
29
+ <View style={[styles.closeBar, styles.closeBarPrimary]} />
30
+ <View style={[styles.closeBar, styles.closeBarSecondary]} />
31
+ </View>
32
+ </Pressable>
33
+
34
+ {mode === 'loading' ? (
35
+ <ActivityIndicator size="large" />
36
+ ) : (
37
+ <View style={styles.unavailableContainer} testID="cobbai-unavailable">
38
+ <Text accessibilityRole="header" style={styles.title}>
39
+ {copy.title}
40
+ </Text>
41
+ <Text numberOfLines={1} style={styles.body}>
42
+ {copy.body}
43
+ </Text>
44
+ <Pressable
45
+ accessibilityRole="button"
46
+ testID="cobbai-unavailable-retry"
47
+ onPress={onRetry}
48
+ style={styles.retryButton}
49
+ >
50
+ <Text style={styles.retryLabel}>{copy.retry}</Text>
51
+ </Pressable>
52
+ </View>
53
+ )}
54
+ </Animated.View>
55
+ );
56
+ }
57
+
58
+ const styles = StyleSheet.create({
59
+ root: {
60
+ position: 'absolute',
61
+ top: 0,
62
+ right: 0,
63
+ bottom: 0,
64
+ left: 0,
65
+ alignItems: 'center',
66
+ justifyContent: 'center',
67
+ backgroundColor: '#FFFFFF',
68
+ borderTopLeftRadius: 16,
69
+ borderTopRightRadius: 16,
70
+ },
71
+ closeButton: {
72
+ position: 'absolute',
73
+ top: 8,
74
+ right: 8,
75
+ width: 44,
76
+ height: 44,
77
+ alignItems: 'center',
78
+ justifyContent: 'center',
79
+ },
80
+ closeIcon: {
81
+ width: 20,
82
+ height: 20,
83
+ alignItems: 'center',
84
+ justifyContent: 'center',
85
+ },
86
+ closeBar: {
87
+ position: 'absolute',
88
+ width: 18,
89
+ height: 2,
90
+ borderRadius: 1,
91
+ backgroundColor: '#2D2D30',
92
+ },
93
+ closeBarPrimary: {
94
+ transform: [{ rotate: '45deg' }],
95
+ },
96
+ closeBarSecondary: {
97
+ transform: [{ rotate: '-45deg' }],
98
+ },
99
+ unavailableContainer: {
100
+ width: '100%',
101
+ paddingHorizontal: 20,
102
+ alignItems: 'center',
103
+ },
104
+ title: {
105
+ fontSize: 20,
106
+ lineHeight: 26,
107
+ fontWeight: '700',
108
+ color: '#2D2D30',
109
+ },
110
+ body: {
111
+ marginTop: 8,
112
+ fontSize: 14,
113
+ lineHeight: 20,
114
+ color: '#4B4B52',
115
+ },
116
+ retryButton: {
117
+ marginTop: 20,
118
+ minHeight: 44,
119
+ paddingHorizontal: 18,
120
+ borderRadius: 12,
121
+ borderWidth: 1,
122
+ borderColor: '#2D2D30',
123
+ alignItems: 'center',
124
+ justifyContent: 'center',
125
+ },
126
+ retryLabel: {
127
+ fontSize: 15,
128
+ lineHeight: 20,
129
+ fontWeight: '600',
130
+ color: '#2D2D30',
131
+ },
132
+ });