-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetVisualBuilderRedirectionUrl.ts
More file actions
69 lines (61 loc) · 2.33 KB
/
getVisualBuilderRedirectionUrl.ts
File metadata and controls
69 lines (61 loc) · 2.33 KB
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
import Config from "../../configManager/configManager";
import { extractDetailsFromCslp } from "../../cslp";
/**
* Returns the redirection URL for the Visual builder.
* @returns {URL} The redirection URL.
*/
export default function getVisualBuilderRedirectionUrl(): URL {
const { stackDetails, clientUrlParams } = Config.get();
const { branch, apiKey, environment, locale } = stackDetails;
const { url: appUrl } = clientUrlParams;
const searchParams = new URLSearchParams();
const hash = window.location.hash;
const isHashSlash = hash.startsWith("#/");
const isHashBang = hash.startsWith("#!/");
const isNoSlash = hash.length > 1 && !isHashSlash && !isHashBang;
const isHashRouting = isHashSlash || isHashBang || isNoSlash;
const url = new URL(window.location.href);
// remove query parameters from the url
url.search = "";
if (isHashRouting) {
// if the hash is #!/about-us or #/about-us or #about-us, we want /about-us
let pathFromHash;
if (isHashBang) {
pathFromHash = hash.substring(2);
} else if (isHashSlash) {
pathFromHash = hash.substring(1);
} else {
pathFromHash = "/" + hash.substring(1);
}
// remove query parameters from the path if we have both hash routing and query string
let onlyPathname = pathFromHash.split("?")[0];
url.pathname = (url.pathname + onlyPathname).replace(/\/\//g, "/");
url.hash = "";
} else {
url.hash = "";
}
const targetUrl = url.toString().replace(/\/$/, "");
searchParams.set("target-url", targetUrl);
if (branch) {
searchParams.set("branch", branch);
}
if (environment) {
searchParams.set("environment", environment);
}
// get the locale from the data cslp attribute
const elementWithDataCslp = document.querySelector(`[data-cslp]`);
if (elementWithDataCslp) {
const cslpData = elementWithDataCslp.getAttribute(
"data-cslp"
) as string;
const { locale } = extractDetailsFromCslp(cslpData);
searchParams.set("locale", locale);
} else if (locale) {
searchParams.set("locale", locale);
}
const completeURL = new URL(
`/#!/stack/${apiKey}/visual-builder?${searchParams.toString()}`,
appUrl
);
return completeURL;
}