251121_01_ENH : 로그인 및 인증 저장, api통신 시 자동으로 accessKey 붙게 조치
This commit is contained in:
127
src/components/layout/MainLayout.tsx
Normal file
127
src/components/layout/MainLayout.tsx
Normal file
@@ -0,0 +1,127 @@
|
||||
"use client";
|
||||
|
||||
import {useState, useEffect, ReactNode} from "react";
|
||||
import { usePathname, useRouter } from "next/navigation";
|
||||
import { Header } from "@/components/layout/Header";
|
||||
import { Sidebar } from "@/components/layout/Sidebar";
|
||||
import {useFontSize} from "@/hooks/useFontSize";
|
||||
import {projectSideMenus, manageSideMenus, topMenus} from "@/config/menu.data";
|
||||
import commonConst from "@/config/commonConst";
|
||||
|
||||
type MainLayoutProps = {
|
||||
children: ReactNode;
|
||||
initialFontSize: number; // 🔥 추가
|
||||
};
|
||||
|
||||
const MainLayout = ({ children, initialFontSize }: MainLayoutProps) => {
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
|
||||
const [isProjectMenu, setIsProjectMenu] = useState(false);
|
||||
const [menuOpen, setMenuOpen] = useState(true);
|
||||
const [fontSize, setFontSize] = useFontSize(initialFontSize, { hydrateFromCookie: false });
|
||||
|
||||
const isMain = pathname === "/";
|
||||
const activeTop = topMenus.find((m) => m.match?.(pathname)) ?? topMenus[0];
|
||||
const sideMenus = pathname.startsWith("/manage") ? manageSideMenus : projectSideMenus;
|
||||
|
||||
// 2단 메뉴 open 상태
|
||||
const [openMenus, setOpenMenus] = useState<Record<string, boolean>>(() => {
|
||||
const initial: Record<string, boolean> = {};
|
||||
|
||||
sideMenus.forEach((item) => {
|
||||
if (
|
||||
item.children &&
|
||||
item.children.some(
|
||||
(child) =>
|
||||
pathname === child.path || pathname.startsWith(child.path + "/")
|
||||
)
|
||||
) {
|
||||
initial[item.id] = true;
|
||||
}
|
||||
});
|
||||
return initial;
|
||||
});
|
||||
|
||||
const handleIsProjectMenu = (isProject: boolean) => {
|
||||
setIsProjectMenu(isProject);
|
||||
}
|
||||
|
||||
const movePage = (path: string) => {
|
||||
router.push(path);
|
||||
};
|
||||
|
||||
const toggleSideMenu = (id: string) => {
|
||||
setOpenMenus((prev) => ({ ...prev, [id]: !prev[id] }));
|
||||
};
|
||||
|
||||
const decreaseFontSize = () =>{
|
||||
setFontSize(Math.max(commonConst.fontSizeMin, fontSize - 2));
|
||||
}
|
||||
|
||||
const increaseFontSize = () =>{
|
||||
setFontSize(Math.min(commonConst.fontSizeMax, fontSize + 2));
|
||||
}
|
||||
|
||||
// CSS 변수 동기화
|
||||
useEffect(() => {
|
||||
if (typeof document === "undefined") return;
|
||||
document.documentElement.style.setProperty(
|
||||
"--content-font-size",
|
||||
`${fontSize}px`,
|
||||
);
|
||||
}, [fontSize]);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
{/* 헤더 - 고정 폰트 크기 */}
|
||||
<Header
|
||||
isMain={isMain}
|
||||
menuOpen={menuOpen}
|
||||
activeTopId={activeTop.id}
|
||||
fontSize={fontSize}
|
||||
handleIsProjectMenu={handleIsProjectMenu}
|
||||
onToggleMenu={() => setMenuOpen((prev) => !prev)}
|
||||
onNavigate={movePage}
|
||||
decreaseFontSize={decreaseFontSize}
|
||||
increaseFontSize={increaseFontSize}
|
||||
/>
|
||||
|
||||
{/* 본문 */}
|
||||
<div className="flex">
|
||||
{/* 사이드바 - 고정 폰트 크기 */}
|
||||
{!isMain && (
|
||||
<Sidebar
|
||||
isProjectMenu={isProjectMenu}
|
||||
sideMenus={sideMenus}
|
||||
isOpen={menuOpen}
|
||||
pathname={pathname}
|
||||
openMenus={openMenus}
|
||||
onToggleMenu={toggleSideMenu}
|
||||
onNavigate={movePage}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 메인 콘텐츠만 가변 폰트 크기 적용 */}
|
||||
<main
|
||||
className="flex-1"
|
||||
// 전역 CSS 변수(--content-font-size)를 실제 렌더에 적용
|
||||
// SSR 단계에서의 초기 폰트 값을 FOUC 없이 유지하기 위해 fallback을 함께 지정
|
||||
style={{ fontSize: `var(--content-font-size, ${initialFontSize}px)` }}
|
||||
>
|
||||
{children}
|
||||
</main>
|
||||
</div>
|
||||
|
||||
{/* 모바일 오버레이 */}
|
||||
{!isMain && menuOpen && (
|
||||
<div
|
||||
className="fixed inset-0 z-20 lg:hidden"
|
||||
onClick={() => setMenuOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MainLayout;
|
||||
Reference in New Issue
Block a user