Interactive/pkg/view/src/layouts/RootLayout.tsx

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-02-01 15:26:17 +00:00
import Navbar from "./shared/Navbar.tsx";
import { readProfiles, useUserinfo } from "../stores/userinfo.tsx";
2024-02-05 12:31:37 +00:00
import { createEffect, createMemo, createSignal, Show } from "solid-js";
2024-02-01 15:26:17 +00:00
import { readWellKnown } from "../stores/wellKnown.tsx";
2024-02-05 17:10:22 +00:00
import { BeforeLeaveEventArgs, useLocation, useNavigate, useSearchParams } from "@solidjs/router";
2024-02-01 15:26:17 +00:00
export default function RootLayout(props: any) {
const [ready, setReady] = createSignal(false);
Promise.all([readWellKnown(), readProfiles()]).then(() => setReady(true));
const navigate = useNavigate();
const userinfo = useUserinfo();
2024-02-05 12:31:37 +00:00
const [searchParams] = useSearchParams();
2024-02-01 15:26:17 +00:00
const location = useLocation();
createEffect(() => {
if (ready()) {
2024-02-05 17:10:22 +00:00
keepGate(location.pathname + location.search);
2024-02-01 15:26:17 +00:00
}
}, [ready, userinfo]);
function keepGate(path: string, e?: BeforeLeaveEventArgs) {
2024-02-05 17:10:22 +00:00
const blacklist = ["/creator"];
2024-02-01 15:26:17 +00:00
2024-02-05 17:10:22 +00:00
if (!userinfo?.isLoggedIn && blacklist.includes(path)) {
2024-02-01 15:26:17 +00:00
if (!e?.defaultPrevented) e?.preventDefault();
2024-02-05 17:10:22 +00:00
navigate(`/auth?redirect_uri=${path}`);
2024-02-01 15:26:17 +00:00
}
}
2024-02-05 12:31:37 +00:00
const mainContentStyles = createMemo(() => {
if(!searchParams["noTitle"]) {
return "h-[calc(100vh-64px)] mt-[64px]"
} else {
return "h-[100vh]"
}
})
2024-02-01 15:26:17 +00:00
return (
<Show when={ready()} fallback={
<div class="h-screen w-screen flex justify-center items-center">
<div>
<span class="loading loading-lg loading-infinity"></span>
</div>
</div>
}>
2024-02-05 12:31:37 +00:00
<Show when={!searchParams["noTitle"]}>
<Navbar />
</Show>
<main class={mainContentStyles()}>{props.children}</main>
2024-02-01 15:26:17 +00:00
</Show>
);
}