forked from readehelpshift/helpshift-react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
99 lines (81 loc) · 2.36 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
import React from "react";
import {
NativeEventEmitter,
NativeModules,
requireNativeComponent,
View,
ViewStyle
} from "react-native";
interface HelpshiftUser {
authToken: string;
identifier: string;
name: string;
email: string;
}
interface HelpshiftConfig {
user: HelpshiftUser;
cifs: object;
apiKey: string;
domain: string;
appId: string;
width: number;
height: number;
}
interface HelpshiftProps {
config: HelpshiftConfig;
style?: ViewStyle;
}
function isHelpshiftAvailable() {
return "RNTHelpshift" in NativeModules.UIManager;
}
function getHelp() {
if (isHelpshiftAvailable()) {
return requireNativeComponent("RNTHelpshift");
}
return View;
}
const RNTHelpshift = getHelp();
const { RNHelpshift } = NativeModules;
const mockEmitter = {
once: () => console.warn(MODULE_UNAVAILABLE_WARNING),
addListener: () => console.warn(MODULE_UNAVAILABLE_WARNING),
removeListener: () => console.warn(MODULE_UNAVAILABLE_WARNING),
removeAllListeners: () => console.warn(MODULE_UNAVAILABLE_WARNING)
};
const MODULE_UNAVAILABLE_WARNING =
"Native module unavailable in project binary.";
class Helpshift extends React.PureComponent<HelpshiftProps> {
render() {
return <RNTHelpshift {...this.props} />;
}
static init = (apiKey: string, domain: string, appId: string) =>
RNHelpshift
? RNHelpshift.init(apiKey, domain, appId)
: console.warn(MODULE_UNAVAILABLE_WARNING);
// TODO: Rerender Helpshift view on iOS if using <Helpshift/>
static login = (user: HelpshiftUser) =>
RNHelpshift
? RNHelpshift.login(user)
: console.warn(MODULE_UNAVAILABLE_WARNING);
static logout = () =>
RNHelpshift
? RNHelpshift.logout()
: console.warn(MODULE_UNAVAILABLE_WARNING);
static requestUnreadMessagesCount = () =>
RNHelpshift
? RNHelpshift.requestUnreadMessagesCount()
: console.warn(MODULE_UNAVAILABLE_WARNING);
static showConversationWithCIFs = (cifs: object) =>
RNHelpshift
? RNHelpshift.showConversationWithCIFs(cifs)
: console.warn(MODULE_UNAVAILABLE_WARNING);
static showConversation = () =>
RNHelpshift
? RNHelpshift.showConversation()
: console.warn(MODULE_UNAVAILABLE_WARNING);
static isAvailable = isHelpshiftAvailable;
static eventEmitter = RNHelpshift
? new NativeEventEmitter(RNHelpshift)
: mockEmitter;
}
export default Helpshift;