this repo has no description
1const APP_BASE = "/app";
2
3function getAppPath(): string {
4 const pathname = globalThis.location.pathname;
5 if (pathname.startsWith(APP_BASE)) {
6 const path = pathname.slice(APP_BASE.length) || "/";
7 return path.startsWith("/") ? path : "/" + path;
8 }
9 return "/";
10}
11
12let currentPath = $state(getAppPath());
13
14globalThis.addEventListener("popstate", () => {
15 currentPath = getAppPath();
16});
17
18export function navigate(path: string, replace = false) {
19 const fullPath = APP_BASE + (path.startsWith("/") ? path : "/" + path);
20 if (replace) {
21 globalThis.history.replaceState(null, "", fullPath);
22 } else {
23 globalThis.history.pushState(null, "", fullPath);
24 }
25 currentPath = path.startsWith("/") ? path : "/" + path;
26}
27
28export function getCurrentPath() {
29 return currentPath;
30}
31
32export function getFullUrl(path: string): string {
33 return APP_BASE + (path.startsWith("/") ? path : "/" + path);
34}