-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathqueryStateParams.ts
40 lines (28 loc) · 1.16 KB
/
queryStateParams.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
import { encodeB64, getClerkQueryParam } from '../utils';
export const buildUrl = ({ base, path }: { base: string; path: string | undefined }) => {
if (!path) {
return base;
}
return base + path;
};
export const decodeBase64Json = (value: string) => {
return JSON.parse(atob(value));
};
export const readStateParam = () => {
const urlClerkState = getClerkQueryParam('__clerk_modal_state') ?? '';
return urlClerkState ? JSON.parse(atob(urlClerkState)) : null;
};
type SerializeAndAppendModalStateProps = { url: string; currentPath: string; componentName: string };
export const appendModalState = ({ url, currentPath = '', componentName }: SerializeAndAppendModalStateProps) => {
const regexPattern = /CLERK-ROUTER\/VIRTUAL\/.*\//;
const redirectParams = {
path: currentPath.replace(regexPattern, '') || '',
componentName,
};
const encodedRedirectParams = encodeB64(JSON.stringify(redirectParams));
const urlWithParams = new URL(url);
const searchParams = urlWithParams.searchParams;
searchParams.set('__clerk_modal_state', encodedRedirectParams);
urlWithParams.search = searchParams.toString();
return urlWithParams.toString();
};