-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathqueryStateParams.ts
40 lines (29 loc) · 1.12 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 { getClerkQueryParam } from '../utils';
export const decodeBase64Json = (value: string) => {
return JSON.parse(atob(value));
};
export const encodeBase64Json = (value: { path: string; componentName: string }) => {
return btoa(JSON.stringify(value));
};
export const readAndRemoveStateParam = () => {
const urlClerkState = getClerkQueryParam('__clerk_state') ?? '';
return urlClerkState ? decodeBase64Json(urlClerkState) : null;
};
type SerializeAndAppendModalStateProps = { url: string; currentPath: string; componentName: string };
export const serializeAndAppendModalState = ({
url,
currentPath,
componentName,
}: SerializeAndAppendModalStateProps) => {
const regexPattern = /CLERK-ROUTER\/VIRTUAL\/.*\//;
const redirectParams = {
path: currentPath.replace(regexPattern, '') || '',
componentName,
};
const encodedRedirectParams = encodeBase64Json(redirectParams);
const urlWithParams = new URL(url);
const searchParams = urlWithParams.searchParams;
searchParams.set('__clerk_state', encodedRedirectParams);
urlWithParams.search = searchParams.toString();
return urlWithParams.toString();
};