-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathapp.ts
197 lines (174 loc) · 6.15 KB
/
app.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import type { Clerk as ClerkType } from '../';
interface ComponentPropsControl {
setProps: (props: unknown) => void;
getProps: () => any | null;
}
const AVAILABLE_COMPONENTS = [
'clerk', // While not a component, we want to support passing options to the Clerk class.
'signIn',
'signUp',
'userButton',
'userProfile',
'createOrganization',
'organizationList',
'organizationProfile',
'organizationSwitcher',
'waitlist',
'pricingTable',
] as const;
const COMPONENT_PROPS_NAMESPACE = 'clerk-js-sandbox';
const urlParams = new URL(window.location.href).searchParams;
for (const [component, encodedProps] of urlParams.entries()) {
if (AVAILABLE_COMPONENTS.includes(component as (typeof AVAILABLE_COMPONENTS)[number])) {
localStorage.setItem(`${COMPONENT_PROPS_NAMESPACE}-${component}`, encodedProps);
}
}
function setComponentProps(component: (typeof AVAILABLE_COMPONENTS)[number], props: unknown) {
const encodedProps = JSON.stringify(props);
const url = new URL(window.location.href);
url.searchParams.set(component, encodedProps);
window.location.href = url.toString();
}
function getComponentProps(component: (typeof AVAILABLE_COMPONENTS)[number]): unknown | null {
const url = new URL(window.location.href);
const encodedProps = url.searchParams.get(component);
if (encodedProps) {
return JSON.parse(encodedProps);
}
const localEncodedProps = localStorage.getItem(`${COMPONENT_PROPS_NAMESPACE}-${component}`);
if (localEncodedProps) {
return JSON.parse(localEncodedProps);
}
return null;
}
function buildComponentControls(component: (typeof AVAILABLE_COMPONENTS)[number]): ComponentPropsControl {
return {
setProps(props) {
setComponentProps(component, props);
},
getProps() {
return getComponentProps(component);
},
};
}
const componentControls: Record<(typeof AVAILABLE_COMPONENTS)[number], ComponentPropsControl> = {
clerk: buildComponentControls('clerk'),
signIn: buildComponentControls('signIn'),
signUp: buildComponentControls('signUp'),
userButton: buildComponentControls('userButton'),
userProfile: buildComponentControls('userProfile'),
createOrganization: buildComponentControls('createOrganization'),
organizationList: buildComponentControls('organizationList'),
organizationProfile: buildComponentControls('organizationProfile'),
organizationSwitcher: buildComponentControls('organizationSwitcher'),
waitlist: buildComponentControls('waitlist'),
pricingTable: buildComponentControls('pricingTable'),
};
declare global {
interface Window {
components: Record<(typeof AVAILABLE_COMPONENTS)[number], ComponentPropsControl>;
}
}
window.components = componentControls;
const Clerk = window.Clerk;
function assertClerkIsLoaded(c: ClerkType | undefined): asserts c is ClerkType {
if (!c) {
throw new Error('Clerk is not loaded');
}
}
const app = document.getElementById('app') as HTMLDivElement;
function mountIndex(element: HTMLDivElement) {
assertClerkIsLoaded(Clerk);
const user = Clerk.user;
element.innerHTML = `<pre class="text-left whitespace-pre overflow-x-auto bg-white p-4 border border-gray-100 rounded-md text-sm"><code>${JSON.stringify({ user }, null, 2)}</code></pre>`;
}
function mountOpenSignInButton(element: HTMLDivElement, props) {
const button = document.createElement('button');
button.textContent = 'Open Sign In';
button.onclick = () => {
Clerk?.openSignIn(props);
};
element.appendChild(button);
}
function mountOpenSignUpButton(element: HTMLDivElement, props) {
const button = document.createElement('button');
button.textContent = 'Open Sign Up';
button.onclick = () => {
Clerk?.openSignUp(props);
};
element.appendChild(button);
}
function addCurrentRouteIndicator(currentRoute: string) {
const link = document.querySelector(`a[href="${currentRoute}"]`);
if (!link) {
return;
}
link.removeAttribute('aria-current');
link.setAttribute('aria-current', 'page');
}
(async () => {
assertClerkIsLoaded(Clerk);
const routes = {
'/': () => {
mountIndex(app);
},
'/sign-in': () => {
Clerk.mountSignIn(app, componentControls.signIn.getProps() ?? {});
},
'/sign-up': () => {
Clerk.mountSignUp(app, componentControls.signUp.getProps() ?? {});
},
'/user-button': () => {
Clerk.mountUserButton(app, componentControls.userButton.getProps() ?? {});
},
'/user-profile': () => {
Clerk.mountUserProfile(app, componentControls.userProfile.getProps() ?? {});
},
'/create-organization': () => {
Clerk.mountCreateOrganization(app, componentControls.createOrganization.getProps() ?? {});
},
'/organization-list': () => {
Clerk.mountOrganizationList(app, componentControls.organizationList.getProps() ?? {});
},
'/organization-profile': () => {
Clerk.mountOrganizationProfile(app, componentControls.organizationProfile.getProps() ?? {});
},
'/organization-switcher': () => {
Clerk.mountOrganizationSwitcher(app, componentControls.organizationSwitcher.getProps() ?? {});
},
'/waitlist': () => {
Clerk.mountWaitlist(app, componentControls.waitlist.getProps() ?? {});
},
'/keyless': () => {
Clerk.__unstable__updateProps({
options: {
__internal_keyless_claimKeylessApplicationUrl: 'https://dashboard.clerk.com',
__internal_keyless_copyInstanceKeysUrl: 'https://dashboard.clerk.com',
},
});
},
'/pricing-table': () => {
Clerk.__experimental_mountPricingTable(app, componentControls.pricingTable.getProps() ?? {});
},
'/open-sign-in': () => {
mountOpenSignInButton(app, componentControls.signIn.getProps() ?? {});
},
'/open-sign-up': () => {
mountOpenSignUpButton(app, componentControls.signUp.getProps() ?? {});
},
};
const route = window.location.pathname;
if (route in routes) {
const renderCurrentRoute = routes[route];
addCurrentRouteIndicator(route);
await Clerk.load({
...(componentControls.clerk.getProps() ?? {}),
signInUrl: '/sign-in',
signUpUrl: '/sign-up',
experimental: { commerce: true },
});
renderCurrentRoute();
} else {
console.error(`Unknown route: "${route}".`);
}
})();