el.xwx.moe/layouts/MainLayout.tsx

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-02-08 17:58:55 -06:00
import Navbar from "@/components/Navbar";
import Sidebar from "@/components/Sidebar";
2023-07-16 10:51:24 -05:00
import { ReactNode, useEffect } from "react";
2023-02-08 17:58:55 -06:00
import { useSession } from "next-auth/react";
import Loader from "../components/Loader";
2023-04-30 15:54:40 -05:00
import useRedirect from "@/hooks/useRedirect";
2023-02-08 17:58:55 -06:00
import { useRouter } from "next/router";
2023-06-12 23:46:32 -05:00
import ModalManagement from "@/components/ModalManagement";
2023-07-16 10:51:24 -05:00
import useModalStore from "@/store/modals";
2023-02-08 17:58:55 -06:00
interface Props {
2023-02-08 17:58:55 -06:00
children: ReactNode;
}
export default function MainLayout({ children }: Props) {
const { status, data } = useSession();
2023-02-08 17:58:55 -06:00
const router = useRouter();
2023-04-30 15:54:40 -05:00
const redirect = useRedirect();
2023-02-08 17:58:55 -06:00
const routeExists = router.route === "/_error" ? false : true;
2023-07-16 10:51:24 -05:00
const { modal } = useModalStore();
useEffect(() => {
modal
? (document.body.style.overflow = "hidden")
: (document.body.style.overflow = "auto");
}, [modal]);
2023-04-30 15:54:40 -05:00
if (status === "authenticated" && !redirect && routeExists)
2023-02-08 17:58:55 -06:00
return (
2023-06-12 23:46:32 -05:00
<>
<ModalManagement />
<div className="flex">
<div className="hidden lg:block">
<Sidebar className="fixed" />
</div>
2023-04-30 15:54:40 -05:00
2023-06-12 23:46:32 -05:00
<div className="w-full lg:ml-64 xl:ml-80">
<Navbar />
{children}
</div>
2023-02-08 17:58:55 -06:00
</div>
2023-06-12 23:46:32 -05:00
</>
2023-02-08 17:58:55 -06:00
);
2023-04-30 15:54:40 -05:00
else if ((status === "unauthenticated" && !redirect) || !routeExists)
2023-03-28 02:31:50 -05:00
return <>{children}</>;
2023-05-01 05:07:01 -05:00
else return <></>;
2023-02-08 17:58:55 -06:00
}