-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathindex.tsx
445 lines (386 loc) · 13.6 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import {
NativeModules,
NativeEventEmitter,
Platform,
type EmitterSubscription,
} from 'react-native';
const { IntercomModule, IntercomEventEmitter } = NativeModules;
export enum Visibility {
GONE = 'GONE',
VISIBLE = 'VISIBLE',
}
type VisibilityType = keyof typeof Visibility;
export enum LogLevel {
ASSERT = 'ASSERT',
DEBUG = 'DEBUG',
DISABLED = 'DISABLED',
ERROR = 'ERROR',
INFO = 'INFO',
VERBOSE = 'VERBOSE',
WARN = 'WARN',
}
type LogLevelType = keyof typeof LogLevel;
export const IntercomEvents = {
IntercomUnreadCountDidChange:
IntercomEventEmitter.UNREAD_COUNT_CHANGE_NOTIFICATION,
IntercomWindowDidHide: IntercomEventEmitter.WINDOW_DID_HIDE_NOTIFICATION,
IntercomWindowDidShow: IntercomEventEmitter.WINDOW_DID_SHOW_NOTIFICATION,
IntercomHelpCenterWindowDidShow:
IntercomEventEmitter.WINDOW_DID_SHOW_NOTIFICATION,
IntercomHelpCenterWindowDidHide:
IntercomEventEmitter.WINDOW_DID_HIDE_NOTIFICATION,
};
type EventType =
| 'IntercomUnreadConversationCountDidChangeNotification'
| 'IntercomWindowDidHideNotification'
| 'IntercomWindowDidShowNotification';
export type CustomAttributes = {
[key: string]: boolean | string | number;
};
export type MetaData = {
[key: string]: any;
};
export type UserAttributes = {
companies?: Company[];
customAttributes?: CustomAttributes;
email?: string;
languageOverride?: string;
name?: string;
phone?: string;
signedUpAt?: number;
unsubscribedFromEmails?: boolean;
userId?: string;
};
export type Company = {
createdAt?: number;
customAttributes?: CustomAttributes;
id: string;
monthlySpend?: number;
name?: string;
plan?: string;
};
export type HelpCenterArticle = {
it: string;
title: string;
};
export type HelpCenterSection = {
title: string;
articles: HelpCenterArticle;
};
export type HelpCenterCollectionItem = {
id: string;
title: string;
summary: string;
};
export type HelpCenterCollectionContent = {
id: string;
title: string;
summary: string;
articles: HelpCenterArticle[];
sections: HelpCenterSection[];
};
export type HelpCenterArticleSearchResult = {
id: string;
title: string;
matchingSnippet: string;
summary: string;
};
export enum Space {
home = 'HOME',
helpCenter = 'HELP_CENTER',
messages = 'MESSAGES',
tickets = 'TICKETS',
}
export type IntercomType = {
/**
* Login a unidentified user.
* This is a user that doesn't have any identifiable information such as a `userId` or `email`.
* @return {Promise<boolean>} A promise to the token.
*/
loginUnidentifiedUser: () => Promise<boolean>;
/**
* Login a user with identifiable information.
* Valid identifiers are `userId` and `email` which must be set in the {@link UserAttributes} object.
* @param params The {@link UserAttributes} object that contains the user's `email` or `userId`.
*/
loginUserWithUserAttributes: (params: UserAttributes) => Promise<boolean>;
/**
* Log a user out of their Intercom session.
* This will dismiss any Intercom UI and clear Intercom's local cache.
*/
logout(): Promise<boolean>;
/**
* Set `hash` string if you are using Identity Verification for your Intercom workspace.
* @note This should be called before any user login takes place.
*
* Identity Verification helps to make sure that conversations between you and your users are kept private, and that one
* user can't impersonate another. If Identity Verification is enabled for your app, Intercom for iOS will sign all requests
* going to the Intercom servers with tokens. It requires your mobile application to have its own server which authenticates the app's users,
* and which can store a secret.
*
* @see More information on Identity Verification can be found {@link https://developers.intercom.com/installing-intercom/docs/react-native-identity-verification here}
* @param hash A HMAC digest of the user ID or email.
*/
setUserHash(hash: string): Promise<boolean>;
/**
* Update a user in Intercom with data specified in {@link UserAttributes}.
* Full details of the data data attributes that can be stored on a user can be found in {@link UserAttributes}.
*
* @param userAttributes The {@link UserAttributes} object with the user data.
*/
updateUser(userAttributes: UserAttributes): Promise<boolean>;
/**
* Determines if there is currently a user logged in.
*/
isUserLoggedIn(): Promise<boolean>;
/**
* Gets a logged in user's attributes
*
* @return {Promise<UserAttributes>} A promise to the user's attributes with `email`and/or `userId` populated..
*/
fetchLoggedInUserAttributes: () => Promise<UserAttributes>;
/**
* Log an event with a given name and metaData.
* You can log events in Intercom based on user actions in your app.
*
* @param eventName The name of the event.
* @param metaData Metadata Objects support a few simple types that Intercom can present on your behalf,
* see the @{https://developers.intercom.com/intercom-api-reference/reference/event-model Intercom API docs}
*/
logEvent(eventName: string, metaData?: MetaData): Promise<boolean>;
/**
* Present Intercom as a modal overlay in your app.
* The `Home` space is displayed by default.
*/
present(): Promise<boolean>;
/**
* Present an Intercom `space` as a modal overlay in your app
* @see {@link Space} for a list of valid spaces.
*
* @param space The Intercom space to be presented.
*/
presentSpace(space: Space): Promise<boolean>;
/**
* Present Intercom content.
*
* An {@link IntercomContent} object.
*/
presentContent(content: Content): Promise<boolean>;
/**
* Present the message composer.
*
* @param initialMessage An optional message that is used to pre-populate the composer with some text.
*/
presentMessageComposer(initialMessage?: string): Promise<boolean>;
/**
* Fetch all Help Center collections.
*
* @return {Promise<Array<HelpCenterCollectionItem>>} An array of {@link HelpCenterCollectionItem} objects.
*/
fetchHelpCenterCollections: () => Promise<Array<HelpCenterCollectionItem>>;
/**
* Fetch the contents of a Help Center collection.
*
* @param id The ID of the Help Center collection.
*
* @return {Promise<HelpCenterCollectionContent>} A {@link HelpCenterCollectionContent} object.
*/
fetchHelpCenterCollection: (
id: string
) => Promise<HelpCenterCollectionContent>;
/**
* Search the Help Center.
*
* @param term The search term.
*
* @return {Promise<HelpCenterArticleSearchResult>} An array of {@link HelpCenterArticleSearchResult} objects.
*/
searchHelpCenter: (
term: string
) => Promise<Array<HelpCenterArticleSearchResult>>;
/**
* Fetch the current number of unread conversations for the logged in User.
* @return {Promise<number>} the number of unread conversations.
*/
getUnreadConversationCount(): Promise<number>;
/**
* Hide all Intercom windows that are currently displayed.
* This will hide the Messenger, Help Center, Articles, and in-product messages (eg. Mobile Carousels, chats, and posts).
*/
hideIntercom(): Promise<boolean>;
/**
* Set a fixed bottom padding for in app messages and the Intercom Launcher.
* @param bottomPadding The size of the bottom padding in points.
*/
setBottomPadding(bottomPadding: number): Promise<boolean>;
/**
* Show or hide the Intercom InApp Messages in your app.
* @note All InApp Messages are visible by default.
*
* @param visibility A boolean indicating if the InApps should be visible.
*/
setInAppMessageVisibility(visibility: VisibilityType): Promise<boolean>;
/**
* Show or hide the Intercom Launcher in your app.
* @note The Launcher is hidden by default.
*
* @param visibility A boolean indicating if the Intercom Launcher should be visible.
*/
setLauncherVisibility(visibility: VisibilityType): Promise<boolean>;
/**
* Change the Status Bar's style or visibility while an Intercom notification is on
* screen.
* Call this method so that Intercom's window can reflect your app's status bar accordingly.
*/
setNeedsStatusBarAppearanceUpdate(): Promise<boolean>;
/**
* Handle an Android push notification payload sent by Intercom.
*
* @note Android only. iOS handles push notifications automatically.
*/
handlePushMessage(): Promise<boolean>;
/**
* Send a device token to Intercom to enable push notifications to be sent to the User.
* @param token The device token to send to the server.
*/
sendTokenToIntercom(token: string): Promise<boolean>;
/**
* Enable logging for Intercom.
* @param logLevel The logging level to set.
*
* @note iOS will ignore the logging level and by default shows `DEBUG` logging.
*/
setLogLevel(logLevel: LogLevelType): Promise<boolean>;
/**
* Add an event listener for the supported event types.
*/
addEventListener: (
event: EventType,
callback: (response: { count?: number; visible: boolean }) => void
) => EmitterSubscription;
};
const Intercom: IntercomType = {
loginUnidentifiedUser: () => IntercomModule.loginUnidentifiedUser(),
loginUserWithUserAttributes: (userAttributes) =>
IntercomModule.loginUserWithUserAttributes(userAttributes),
logout: () => IntercomModule.logout(),
setUserHash: (hash) => IntercomModule.setUserHash(hash),
updateUser: (userAttributes) => IntercomModule.updateUser(userAttributes),
isUserLoggedIn: () => IntercomModule.isUserLoggedIn(),
fetchLoggedInUserAttributes: () =>
IntercomModule.fetchLoggedInUserAttributes(),
logEvent: (eventName, metaData = undefined) =>
IntercomModule.logEvent(eventName, metaData),
fetchHelpCenterCollections: () => IntercomModule.fetchHelpCenterCollections(),
fetchHelpCenterCollection: (id = '') =>
IntercomModule.fetchHelpCenterCollection(id),
searchHelpCenter: (term = '') => IntercomModule.searchHelpCenter(term),
present: () => IntercomModule.presentIntercom(),
presentSpace: (space) => IntercomModule.presentIntercomSpace(space),
presentContent: (content) => IntercomModule.presentContent(content),
presentMessageComposer: (initialMessage = undefined) =>
IntercomModule.presentMessageComposer(initialMessage),
getUnreadConversationCount: () => IntercomModule.getUnreadConversationCount(),
hideIntercom: () => IntercomModule.hideIntercom(),
setBottomPadding: (paddingBottom) =>
IntercomModule.setBottomPadding(paddingBottom),
setInAppMessageVisibility: (visibility) =>
IntercomModule.setInAppMessageVisibility(visibility),
setLauncherVisibility: (visibility) =>
IntercomModule.setLauncherVisibility(visibility),
setNeedsStatusBarAppearanceUpdate: Platform.select({
ios: IntercomModule.setNeedsStatusBarAppearanceUpdate,
default: async () => true,
}),
handlePushMessage: Platform.select({
android: IntercomModule.handlePushMessage,
default: async () => true,
}),
sendTokenToIntercom: (token) => IntercomModule.sendTokenToIntercom(token),
setLogLevel: (logLevel) => IntercomModule.setLogLevel(logLevel),
addEventListener: (event, callback) => {
event === IntercomEvents.IntercomUnreadCountDidChange &&
Platform.OS === 'android' &&
IntercomEventEmitter.startEventListener();
const eventEmitter = new NativeEventEmitter(IntercomEventEmitter);
const listener = eventEmitter.addListener(event, callback);
const originalRemove = listener.remove;
listener.remove = () => {
event === IntercomEvents.IntercomUnreadCountDidChange &&
Platform.OS === 'android' &&
IntercomEventEmitter.removeEventListener();
originalRemove();
};
return listener;
},
};
export default Intercom;
export enum ContentType {
Article = 'ARTICLE',
Carousel = 'CAROUSEL',
Survey = 'SURVEY',
HelpCenterCollections = 'HELP_CENTER_COLLECTIONS',
Conversation = 'CONVERSATION',
}
export interface Content {
type: ContentType;
}
export interface Article extends Content {
id: string;
}
interface Carousel extends Content {
id: string;
}
interface Survey extends Content {
id: string;
}
interface HelpCenterCollections extends Content {
ids: string[];
}
interface Conversation extends Content {
id: string;
}
export type IntercomContentType = {
/**
* Create
*/
articleWithArticleId: (articleId: string) => Article;
carouselWithCarouselId: (carouselId: string) => Carousel;
surveyWithSurveyId: (surveyId: string) => Survey;
helpCenterCollectionsWithIds: (
collectionIds: string[]
) => HelpCenterCollections;
conversationWithConversationId: (conversationId: string) => Conversation;
};
export const IntercomContent: IntercomContentType = {
articleWithArticleId(articleId) {
let articleContent = {} as Article;
articleContent.type = ContentType.Article;
articleContent.id = articleId;
return articleContent;
},
carouselWithCarouselId(carouselId) {
let carouselContent = {} as Carousel;
carouselContent.type = ContentType.Carousel;
carouselContent.id = carouselId;
return carouselContent;
},
surveyWithSurveyId(surveyId) {
let surveyContent = {} as Survey;
surveyContent.type = ContentType.Survey;
surveyContent.id = surveyId;
return surveyContent;
},
helpCenterCollectionsWithIds(collectionIds) {
let helpCenterCollectionsContent = {} as HelpCenterCollections;
helpCenterCollectionsContent.type = ContentType.HelpCenterCollections;
helpCenterCollectionsContent.ids = collectionIds;
return helpCenterCollectionsContent;
},
conversationWithConversationId(conversationId) {
let conversationContent = {} as Conversation;
conversationContent.type = ContentType.Conversation;
conversationContent.id = conversationId;
return conversationContent;
},
};