-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathqueryStateParams.ts
51 lines (39 loc) · 1.23 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
41
42
43
44
45
46
47
48
49
50
51
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;
startPath?: string;
currentPath?: string;
componentName: string;
};
export const appendModalState = ({
url,
startPath = '/user',
currentPath = '',
componentName,
}: SerializeAndAppendModalStateProps) => {
const regexPattern = /CLERK-ROUTER\/VIRTUAL\/.*\//;
const redirectParams = {
path: currentPath.replace(regexPattern, '') || '',
componentName,
startPath,
};
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();
};