-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
121 lines (113 loc) · 3.12 KB
/
index.ts
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
import { NativeModules, Platform } from 'react-native';
import { HelpScoutBeaconType } from './types';
export const HelpscoutBeaconModule = NativeModules.HelpscoutBeacon;
/**
* Identify the user
*
* @param {String} email
* @param {String} name
*/
const identify = (email: string, name?: string) => {
if (email === null) {
// console.error('[identify] Missing parameter: email');
return;
}
if (Platform.OS === 'ios') {
HelpscoutBeaconModule.identify(email, name);
} else {
if (name === undefined) {
HelpscoutBeaconModule.identify(email);
} else {
HelpscoutBeaconModule.identifyWithEmailAndName(email, name);
}
}
};
/**
* Convenience method to login via setting an attribute with key 'userId'
*
* @param {String} email
* @param {String} name
* @param {String} userId
*/
const login = (email: string, name: string, userId: string) => {
if (!email || !name || !userId) {
// console.error(
// "[login] Missing parameter. Either 'email', 'name', 'userId' or several of them are missing."
// );
return;
}
identify(email, name);
HelpscoutBeaconModule.addAttributeWithKey('userId', userId);
};
/**
* Convenience method to login via setting an attribute with key 'userId' and
* opening the Helpscout beacon (with optional signature).
*
* @param {String} email
* @param {String} name
* @param {String} userId
* @param {String} signature
*/
const loginAndOpen = (
email: string,
name: string,
userId: string,
signature?: string
) => {
login(email, name, userId);
if (signature === undefined) {
HelpscoutBeaconModule.open(null);
} else {
HelpscoutBeaconModule.open(signature);
}
};
/**
* Display the Beacon user interface.
*
* @param {String} signature
*/
const open = (signature?: string) => {
if (signature === undefined) {
HelpscoutBeaconModule.open(null);
} else {
HelpscoutBeaconModule.open(signature);
}
};
/**
* Display a specific page in the Helpscout beacon. On Android, these paths
* are supported:
* - "/ask/message/"
* - "/ask/chat/"
* - "/answers/"
*
* iOS supports additional paths e.g. "/" which displays the initial state
* of the beacon. Find supported path values here:
* - https://developer.helpscout.com/beacon-2/ios/#navigate-to-a-specific-screen
*
* @param {String} path
*/
const navigate = (path: string) => {
if (!path) {
// console.error('[navigate] Missing parameter: path');
return;
}
HelpscoutBeaconModule.navigate(path);
};
const HSBeacon = {
init: HelpscoutBeaconModule.init as HelpScoutBeaconType['init'],
identify,
login,
loginAndOpen,
open,
navigate,
logout: HelpscoutBeaconModule.logout as HelpScoutBeaconType['logout'],
addAttributeWithKey:
HelpscoutBeaconModule.addAttributeWithKey as HelpScoutBeaconType['addAttributeWithKey'],
openArticle:
HelpscoutBeaconModule.openArticle as HelpScoutBeaconType['openArticle'],
suggestArticles:
HelpscoutBeaconModule.suggestArticles as HelpScoutBeaconType['suggestArticles'],
resetSuggestions:
HelpscoutBeaconModule.resetSuggestions as HelpScoutBeaconType['resetSuggestions'],
};
export default HSBeacon as HelpScoutBeaconType;