2023-10-08 19:35:11 +02:00
|
|
|
import { useQueryParam } from "@/hooks/useQueryParams";
|
|
|
|
|
|
|
|
export function useOverlayRouter(id: string) {
|
|
|
|
const [route, setRoute] = useQueryParam("r");
|
|
|
|
const routeParts = (route ?? "").split("/").filter((v) => v.length > 0);
|
|
|
|
const routerActive = routeParts.length > 0 && routeParts[0] === id;
|
|
|
|
|
|
|
|
function navigate(path: string) {
|
|
|
|
const newRoute = [id, ...path.split("/").filter((v) => v.length > 0)];
|
|
|
|
setRoute(newRoute.join("/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
function isActive(page: string) {
|
|
|
|
if (page === "/") return true;
|
|
|
|
const index = routeParts.indexOf(page);
|
|
|
|
if (index === -1) return false; // not active
|
|
|
|
if (index === routeParts.length - 1) return false; // active but latest route so shouldnt be counted as active
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isCurrentPage(page: string) {
|
2023-10-08 20:12:31 +02:00
|
|
|
return routerActive && route === `/${id}${page}`;
|
2023-10-08 19:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isLoaded(page: string) {
|
|
|
|
if (page === "/") return true;
|
|
|
|
return route.includes(page);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isOverlayActive() {
|
|
|
|
return routerActive;
|
|
|
|
}
|
|
|
|
|
|
|
|
function pageProps(page: string) {
|
|
|
|
return {
|
|
|
|
show: isCurrentPage(page),
|
|
|
|
active: isActive(page),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
2023-10-08 20:12:31 +02:00
|
|
|
setRoute(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function open() {
|
|
|
|
setRoute(`/${id}`);
|
2023-10-08 19:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
isOverlayActive,
|
|
|
|
navigate,
|
|
|
|
close,
|
|
|
|
isLoaded,
|
|
|
|
isCurrentPage,
|
|
|
|
pageProps,
|
|
|
|
isActive,
|
2023-10-08 20:12:31 +02:00
|
|
|
open,
|
2023-10-08 19:35:11 +02:00
|
|
|
};
|
|
|
|
}
|