1+ const React = require ( "react" )
2+
3+ module . exports . onRenderBody = (
4+ { setPostBodyComponents} ,
5+ pluginOptions
6+ ) => {
7+ if ( typeof pluginOptions . token === `undefined` ) {
8+ return null
9+ }
10+
11+ const freshchatHost = pluginOptions . host ? pluginOptions . host : `https://wchat.freshchat.com`
12+
13+ const appEmbedUrl =
14+ pluginOptions . appEmbedUrl ? pluginOptions . appEmbedUrl : `https://app.testsigma.com/ui/dashboard`
15+
16+ const renderHTML = ( ) => `
17+ window.fcSettings = {
18+ token: "${ pluginOptions . token } ",
19+ host: "${ freshchatHost } "
20+ };
21+
22+ function addFreshchatScript() {
23+ if (window.fcWidgetAdded) return;
24+ var fcScript = document.createElement('script');
25+ fcScript.setAttribute('src','https://wchat.freshchat.com/js/widget.js');
26+ fcScript.setAttribute('async', 'async');
27+ document.body.appendChild(fcScript);
28+ window.fcWidgetAdded = true;
29+ }
30+
31+ function changeSignupBtn() {
32+ var signupBtn = document.getElementById('signup-btn');
33+ if (!signupBtn) {
34+ setTimeout(changeSignupBtn, 1000);
35+ return;
36+ }
37+ document.getElementById('signup-btn').textContent = 'Go to Dashboard';
38+ document.getElementById('signup-btn').href = 'https://app.testsigma.com';
39+ }
40+
41+ // If session details is not received from app within 15 seconds, create new chat.
42+ setTimeout(addFreshchatScript, 15000);
43+
44+ window.addEventListener('message', function( event) {
45+ if (event.origin === 'https://app.testsigma.com' ||
46+ event.origin === 'https://staging.testsigma.com' ||
47+ event.origin === 'https://prestaging.testsigma.com' ||
48+ event.origin === 'https://devtesting.testsigma.com' ||
49+ event.origin === 'http://dev.testsigma.com') {
50+ if (event.data === 'appLoggedOut') {
51+ addFreshchatScript();
52+ } else {
53+ var session = JSON.parse(event.data);
54+ changeSignupBtn();
55+ window.fcSettings.externalId = session.user.email;
56+ window.fcSettings.restoreId = session.fcRestoreId;
57+ addFreshchatScript();
58+ }
59+ }
60+ });
61+ `
62+
63+ setPostBodyComponents ( [
64+ < iframe key = { `gatsby-plugin-ts-freshchat-iframe` }
65+ src = { `${ appEmbedUrl } ` }
66+ height = { 0 } width = { 0 }
67+ style = { { display : "none" } } > </ iframe > ,
68+ < script
69+ key = { `gatsby-plugin-ts-freshchat-render` }
70+ dangerouslySetInnerHTML = { { __html : renderHTML ( ) } }
71+ />
72+ ] )
73+ }
0 commit comments