-
Notifications
You must be signed in to change notification settings - Fork 61
/
Intercom.js
60 lines (51 loc) · 1.33 KB
/
Intercom.js
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
import * as React from "react";
import * as Scrivito from "scrivito";
import { Helmet } from "react-helmet-async";
class Intercom extends React.Component {
constructor(props) {
super(props);
this.state = { intercomAppId: "" };
}
componentDidMount() {
Scrivito.load(() => {
const rootPage = Scrivito.Obj.root();
if (!rootPage) {
return undefined;
}
return rootPage.get("intercomAppId");
}).then(intercomAppId => {
if (intercomAppId) {
Scrivito.finishLoading().then(() => {
installIntercom(intercomAppId);
this.setState({ intercomAppId });
});
}
});
}
render() {
if (!this.state.intercomAppId) {
return null;
}
return (
<Helmet>
<script
async
src={`https://widget.intercom.io/widget/${this.state.intercomAppId}`}
/>
</Helmet>
);
}
}
function installIntercom(appId) {
if (typeof window.Intercom === "function") {
window.Intercom("reattach_activator");
window.Intercom("update", { app_id: appId });
} else {
const intercom = (...args) => intercom.c(args);
intercom.q = [];
intercom.c = args => intercom.q.push(args);
window.Intercom = intercom;
window.Intercom("boot", { app_id: appId });
}
}
export default Scrivito.connect(Intercom);