174 lines
5.6 KiB
TypeScript
174 lines
5.6 KiB
TypeScript
"use client"
|
|
|
|
import {ReactNode, useEffect, useMemo, useRef, useState} from "react";
|
|
import {usePathname, useRouter, useSearchParams} 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_BY_TOP_MENU} from "@/config/menu.data";
|
|
import {useLogoutMutation} from "@/query/auth";
|
|
import {useDialog} from "@/hooks/useDialog";
|
|
import LoginDialog from "@/components/dialog/loginDialog";
|
|
import { appendAdminRouteParam, hasAdminRouteParam } from "@/lib/admin-route";
|
|
import {useLayoutSelectedMenu} from "@/hooks/useLayoutSelectedMenu";
|
|
import Footer from "@/components/layout/Footer";
|
|
|
|
type MainLayoutProps = {
|
|
children: ReactNode;
|
|
};
|
|
|
|
const getDivKey = () => {
|
|
return "-1";
|
|
}
|
|
|
|
const padDatePart = (value: number) => String(value).padStart(2, "0");
|
|
|
|
const formatStatisticsDt = (date: Date) =>
|
|
[
|
|
date.getFullYear(),
|
|
padDatePart(date.getMonth() + 1),
|
|
padDatePart(date.getDate()),
|
|
padDatePart(date.getHours()),
|
|
padDatePart(date.getMinutes()),
|
|
padDatePart(date.getSeconds()),
|
|
].join("");
|
|
|
|
const formatBaseDateLabel = (date: Date) =>
|
|
`최종 업데이트 일시 : ${String(date.getFullYear()).slice(-2)}년 ${date.getMonth() + 1}월 ${date.getDate()}일 ${padDatePart(date.getHours())}시 ${padDatePart(date.getMinutes())}분`;
|
|
|
|
const MainLayout = ({ children }: MainLayoutProps) => {
|
|
const { auth, logout } = useAuth();
|
|
const { showDialog } = useDialog();
|
|
const logoutMutation = useLogoutMutation();
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
const searchParams = useSearchParams();
|
|
const isMounted = useMounted();
|
|
const { selectedMenusByTopMenu, selectMenu } = useLayoutSelectedMenu();
|
|
const previousTopMenuIdRef = useRef<string | null>(null);
|
|
|
|
const [menuOpen, setMenuOpen] = useState(true);
|
|
const [dataBaseDate, setDataBaseDate] = useState(() => new Date());
|
|
const isRouteAdmin = hasAdminRouteParam(searchParams);
|
|
const activeTopMenu = useMemo(
|
|
() => TOP_MENUS.find((topMenu) => topMenu.match?.(pathname) ?? pathname === topMenu.path) ?? TOP_MENUS[0],
|
|
[pathname]
|
|
);
|
|
const activeTopMenuId = activeTopMenu?.id ?? "";
|
|
const shouldShowFooter = activeTopMenuId === "-1";
|
|
const sideMenus = useMemo(
|
|
() => SIDE_MENUS_BY_TOP_MENU[activeTopMenuId] ?? [],
|
|
[activeTopMenuId]
|
|
);
|
|
const selectedMenuId = useMemo(() => {
|
|
if (!sideMenus.length) return getDivKey();
|
|
|
|
const storedMenuId = activeTopMenuId ? selectedMenusByTopMenu[activeTopMenuId] : undefined;
|
|
if (storedMenuId && sideMenus.some((menu) => menu.id === storedMenuId)) {
|
|
return storedMenuId;
|
|
}
|
|
|
|
return sideMenus[0].id;
|
|
}, [activeTopMenuId, selectedMenusByTopMenu, sideMenus]);
|
|
const statisticsParams = useMemo(
|
|
() => ({
|
|
pkey: activeTopMenuId,
|
|
dt: formatStatisticsDt(dataBaseDate),
|
|
}),
|
|
[activeTopMenuId, dataBaseDate]
|
|
);
|
|
const baseDateLabel = useMemo(() => formatBaseDateLabel(dataBaseDate), [dataBaseDate]);
|
|
|
|
const movePage = (path: string) => {
|
|
router.push(appendAdminRouteParam(path, searchParams));
|
|
};
|
|
|
|
const openLoginDialog = () => {
|
|
showDialog("loginDialog");
|
|
};
|
|
|
|
const handleLogout = () => {
|
|
logoutMutation.mutate();
|
|
};
|
|
|
|
const handleSelectMenu = (menuId: string) => {
|
|
if (!activeTopMenuId) return;
|
|
selectMenu(activeTopMenuId, menuId);
|
|
};
|
|
|
|
useEffect(() => {
|
|
const handleAuthExpired = () => {
|
|
logout();
|
|
};
|
|
|
|
window.addEventListener("auth:expired", handleAuthExpired);
|
|
return () => window.removeEventListener("auth:expired", handleAuthExpired);
|
|
}, [logout]);
|
|
|
|
useEffect(() => {
|
|
if (!activeTopMenuId) return;
|
|
|
|
setDataBaseDate(new Date());
|
|
}, [activeTopMenuId, selectedMenuId]);
|
|
|
|
useEffect(() => {
|
|
if (!activeTopMenuId) return;
|
|
|
|
if (previousTopMenuIdRef.current === null) {
|
|
previousTopMenuIdRef.current = activeTopMenuId;
|
|
return;
|
|
}
|
|
|
|
if (previousTopMenuIdRef.current === activeTopMenuId) return;
|
|
|
|
previousTopMenuIdRef.current = activeTopMenuId;
|
|
selectMenu(activeTopMenuId, getDivKey());
|
|
}, [activeTopMenuId, selectMenu]);
|
|
|
|
if(!isMounted) return null;
|
|
|
|
return (
|
|
<div className="flex h-dvh flex-col overflow-hidden bg-transparent">
|
|
<LayoutCategoryProvider
|
|
selectedDiv={selectedMenuId as DivKey}
|
|
statisticsParams={statisticsParams}
|
|
onSelectMenu={handleSelectMenu}
|
|
>
|
|
<Header
|
|
topMenus={TOP_MENUS}
|
|
userName={auth.user?.userName || ""}
|
|
isAdmin={isRouteAdmin}
|
|
isAuthenticated={!!auth.user}
|
|
baseDateLabel={baseDateLabel}
|
|
onNavigate={movePage}
|
|
onLogin={openLoginDialog}
|
|
onLogout={handleLogout}
|
|
/>
|
|
<div className="flex min-h-0 flex-1 overflow-hidden">
|
|
<Sidebar
|
|
sideMenus={sideMenus}
|
|
isOpen={menuOpen}
|
|
selectedMenuId={selectedMenuId}
|
|
onChangeMenu={handleSelectMenu}
|
|
/>
|
|
<div className="flex min-h-0 flex-1 flex-col">
|
|
<div className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-4 py-3">
|
|
<main className="min-h-full">
|
|
{children}
|
|
</main>
|
|
</div>
|
|
<Footer />
|
|
</div>
|
|
</div>
|
|
<LoginDialog />
|
|
|
|
</LayoutCategoryProvider>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default MainLayout;
|