260506_01_fst: First Commit
This commit is contained in:
86
src/components/layout/MainLayout.tsx
Normal file
86
src/components/layout/MainLayout.tsx
Normal file
@@ -0,0 +1,86 @@
|
||||
"use client"
|
||||
|
||||
import {ReactNode, useEffect, useState} from "react";
|
||||
import {useRouter} from "next/navigation";
|
||||
import {DivKey} from "@/types/kfpa";
|
||||
import {useAuth} from "@/hooks/useAuth";
|
||||
import {LayoutCategoryProvider} from "./LayoutCategoryContext";
|
||||
import Header from "@/components/layout/Header";
|
||||
import {useMounted} from "@/hooks/useMounted";
|
||||
import {Sidebar} from "@/components/layout/Sidebar";
|
||||
import {TOP_MENUS, SIDE_MENUS} from "@/config/menu.data";
|
||||
import {useLogoutMutation} from "@/query/auth";
|
||||
import {useDialog} from "@/hooks/useDialog";
|
||||
import LoginDialog from "@/components/dialog/loginDialog";
|
||||
|
||||
type MainLayoutProps = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
const getDivKey = () => {
|
||||
return "-1";
|
||||
}
|
||||
|
||||
const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
const { auth, logout } = useAuth();
|
||||
const { showDialog } = useDialog();
|
||||
const logoutMutation = useLogoutMutation();
|
||||
const router = useRouter();
|
||||
const isMounted = useMounted();
|
||||
|
||||
const [menuOpen, setMenuOpen] = useState(true);
|
||||
const [selectedDiv, setSelectedDiv] = useState<DivKey>(getDivKey());
|
||||
|
||||
const movePage = (path: string) => {
|
||||
router.push(path);
|
||||
};
|
||||
|
||||
const openLoginDialog = () => {
|
||||
showDialog("loginDialog");
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
logoutMutation.mutate();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleAuthExpired = () => {
|
||||
logout();
|
||||
};
|
||||
|
||||
window.addEventListener("auth:expired", handleAuthExpired);
|
||||
return () => window.removeEventListener("auth:expired", handleAuthExpired);
|
||||
}, [logout]);
|
||||
|
||||
if(!isMounted) return null;
|
||||
|
||||
return (
|
||||
<div className="h-screen flex flex-col bg-transparent overflow-hidden">
|
||||
<LayoutCategoryProvider selectedDiv={selectedDiv}>
|
||||
<Header
|
||||
topMenus={TOP_MENUS}
|
||||
userName={auth.user?.userName || ""}
|
||||
isAuthenticated={auth.user != null}
|
||||
onNavigate={movePage}
|
||||
onLogin={openLoginDialog}
|
||||
onLogout={handleLogout}
|
||||
/>
|
||||
<div className="flex flex-1">
|
||||
<Sidebar
|
||||
sideMenus={SIDE_MENUS}
|
||||
isOpen={menuOpen}
|
||||
selectedDiv={selectedDiv}
|
||||
onChangeDiv={setSelectedDiv}
|
||||
/>
|
||||
<main className="flex-1 p-4">
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
<LoginDialog />
|
||||
|
||||
</LayoutCategoryProvider>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default MainLayout;
|
||||
Reference in New Issue
Block a user