Skip to content

Commit 729d375

Browse files
add chameleon script (codesandbox#2734)
add chameleon script
2 parents 395c3bc + ce43d96 commit 729d375

File tree

4 files changed

+85
-15
lines changed

4 files changed

+85
-15
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
declare global {
2+
interface Window {
3+
chmln: any;
4+
chmln_resolvers: {
5+
resolve: any;
6+
reject: any;
7+
};
8+
}
9+
}
10+
11+
let _script;
12+
13+
function loadScript() {
14+
return new Promise((resolve, reject) => {
15+
window.chmln_resolvers = {
16+
resolve,
17+
reject,
18+
};
19+
const script = document.createElement('script');
20+
script.innerHTML = `/* Chameleon - better user onboarding */!function(t,n,o){var a="chmln",e="adminPreview",c="setup identify alias track clear set show on off custom help _data".split(" ");if(n[a]||(n[a]={}),n[a][e]&&(n[a][e]=!1),!n[a].root){n[a].accountToken=o,n[a].location=n.location.href.toString(),n[a].now=new Date;for(var s=0;s<c.length;s++)!function(){var t=n[a][c[s]+"_a"]=[];n[a][c[s]]=function(){t.push(arguments)}}();var i=t.createElement("script");i.src="https://fast.trychameleon.com/messo/"+o+"/messo.min.js",i.async=!0,i.onload = window.chmln_resolvers.resolve,i.onerror = window.chmln_resolvers.reject,t.head.appendChild(i)}}(document,window,"SD8v1wAhTGvsMfAUSklVZC5ucKfiTB8uw73OJ9QOxIdiGn-1Ia9sN-BDlqTksEtFejdYPw");`;
21+
document.body.appendChild(script);
22+
});
23+
}
24+
25+
export default {
26+
/*
27+
This method might be called at any point in time, with a user or not. We want to
28+
make sure that we load the script on first call and then make sure it finishes loading
29+
if any new calls are made during it loading
30+
*/
31+
loadTour(userId: string) {
32+
if (!_script) {
33+
_script = loadScript().then(() => {
34+
const cmln = document.querySelector('#chmln-editor');
35+
36+
if (cmln) {
37+
// @ts-ignore
38+
cmln.shadowRoot.childNodes[0].innerHTML +=
39+
'#chmln-toggle-item { top: auto !important; bottom: 100px !important; }';
40+
}
41+
});
42+
}
43+
44+
if (userId) {
45+
return _script.then(() => {
46+
window.chmln.identify(userId, {});
47+
});
48+
}
49+
50+
let uid;
51+
try {
52+
uid = document.cookie.match(/\bvisitor-uid=([a-z0-9-]+)(;|$)/)[1];
53+
} catch (e) {
54+
document.cookie =
55+
'visitor-uid=' +
56+
(uid = Math.random()
57+
.toString(36)
58+
.substring(2)) +
59+
'; expires=Tue, Oct 13 2037 04:24:07 UTC; path=/;';
60+
}
61+
62+
return _script.then(() => {
63+
window.chmln.identify(uid, { visitor: true });
64+
});
65+
},
66+
};

packages/app/src/app/overmind/effects/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export { default as codesandboxApi } from './codesandboxApi';
2525
export { default as themes } from './themes';
2626
export { default as executor } from './executor';
2727
export { default as stripe } from './stripe';
28+
export { default as chameleon } from './chameleon';

packages/app/src/app/overmind/index.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
import { createHook } from 'overmind-react';
21
import {
2+
IAction,
33
IConfig,
4+
IDerive,
45
IOnInitialize,
5-
IAction,
66
IOperator,
7-
IDerive,
87
IState,
98
} from 'overmind';
9+
import { createHook } from 'overmind-react';
1010
import { merge, namespaced } from 'overmind/config';
11+
12+
import * as actions from './actions';
1113
import { createConnect } from './createConnect';
1214
import * as effects from './effects';
13-
import { state } from './state';
14-
import { onInitialize } from './onInitialize';
15-
import * as actions from './actions';
16-
import * as preferences from './namespaces/preferences';
17-
import * as userNotifications from './namespaces/userNotifications';
18-
import * as patron from './namespaces/patron';
19-
import * as editor from './namespaces/editor';
20-
import * as live from './namespaces/live';
21-
import * as workspace from './namespaces/workspace';
15+
import { createModals } from './factories';
16+
import * as modals from './modals';
2217
import * as dashboard from './namespaces/dashboard';
2318
import * as deployment from './namespaces/deployment';
19+
import * as editor from './namespaces/editor';
20+
import * as explore from './namespaces/explore';
2421
import * as files from './namespaces/files';
2522
import * as git from './namespaces/git';
26-
import * as explore from './namespaces/explore';
23+
import * as live from './namespaces/live';
24+
import * as patron from './namespaces/patron';
25+
import * as preferences from './namespaces/preferences';
2726
import * as profile from './namespaces/profile';
2827
import * as server from './namespaces/server';
29-
import { createModals } from './factories';
30-
import * as modals from './modals';
28+
import * as userNotifications from './namespaces/userNotifications';
29+
import * as workspace from './namespaces/workspace';
30+
import { onInitialize } from './onInitialize';
31+
import { state } from './state';
3132

3233
export const config = merge(
3334
{

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export const sandboxChanged: AsyncAction<{ id: string }> = withLoadApp<{
121121
}
122122

123123
state.editor.isLoading = false;
124+
125+
effects.chameleon.loadTour(state.user && state.user.id);
124126
});
125127

126128
export const contentMounted: Action = ({ state, effects }) => {

0 commit comments

Comments
 (0)