260515_01_ENH: 클라이언트 수정사항 반영 및 다른 조사들 data 연결
This commit is contained in:
@@ -1,18 +1,20 @@
|
||||
"use client"
|
||||
|
||||
import {ReactNode, useEffect, useState} from "react";
|
||||
import {useRouter, useSearchParams} from "next/navigation";
|
||||
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} from "@/config/menu.data";
|
||||
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;
|
||||
@@ -22,17 +24,63 @@ 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 [selectedDiv, setSelectedDiv] = useState<DivKey>(getDivKey());
|
||||
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));
|
||||
@@ -46,6 +94,11 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
logoutMutation.mutate();
|
||||
};
|
||||
|
||||
const handleSelectMenu = (menuId: string) => {
|
||||
if (!activeTopMenuId) return;
|
||||
selectMenu(activeTopMenuId, menuId);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const handleAuthExpired = () => {
|
||||
logout();
|
||||
@@ -55,30 +108,59 @@ const MainLayout = ({ children }: MainLayoutProps) => {
|
||||
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={selectedDiv}>
|
||||
<LayoutCategoryProvider
|
||||
selectedDiv={selectedMenuId as DivKey}
|
||||
statisticsParams={statisticsParams}
|
||||
onSelectMenu={handleSelectMenu}
|
||||
>
|
||||
<Header
|
||||
topMenus={TOP_MENUS}
|
||||
userName={auth.user?.userName || ""}
|
||||
isAuthenticated={isRouteAdmin}
|
||||
isAdmin={isRouteAdmin}
|
||||
isAuthenticated={!!auth.user}
|
||||
baseDateLabel={baseDateLabel}
|
||||
onNavigate={movePage}
|
||||
onLogin={openLoginDialog}
|
||||
onLogout={handleLogout}
|
||||
/>
|
||||
<div className="flex h-[calc(100dvh-4rem)] min-h-0 overflow-hidden">
|
||||
<div className="flex min-h-0 flex-1 overflow-hidden">
|
||||
<Sidebar
|
||||
sideMenus={SIDE_MENUS}
|
||||
sideMenus={sideMenus}
|
||||
isOpen={menuOpen}
|
||||
selectedDiv={selectedDiv}
|
||||
onChangeDiv={setSelectedDiv}
|
||||
selectedMenuId={selectedMenuId}
|
||||
onChangeMenu={handleSelectMenu}
|
||||
/>
|
||||
<div className="h-full min-h-0 flex-1 overflow-y-scroll overscroll-contain p-4">
|
||||
<main className="h-full min-h-0 flex-1">
|
||||
{children}
|
||||
</main>
|
||||
<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 />
|
||||
|
||||
Reference in New Issue
Block a user