144 lines
4.5 KiB
TypeScript
144 lines
4.5 KiB
TypeScript
"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 ProjectTitle from "@/components/layout/ProjectTitle";
|
|
import {cn} from "@/lib/utils";
|
|
import {FONT_SIZE} from "@/const/commonConst";
|
|
import {useSelectedProject} from "@/hooks/useSelectedProject";
|
|
|
|
type MainLayoutProps = {
|
|
children: ReactNode;
|
|
initialFontSize: number; // 🔥 추가
|
|
};
|
|
|
|
const MainLayout = ({ children, initialFontSize }: MainLayoutProps) => {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
|
|
const { selectedProject } = useSelectedProject();
|
|
const [menuOpen, setMenuOpen] = useState(true);
|
|
const [fontSize, setFontSize] = useFontSize(initialFontSize, { hydrateFromCookie: false });
|
|
|
|
const isMain = pathname === "/";
|
|
const isProjectMenu = !pathname.startsWith("/manage");
|
|
const isViewProjectTitle = !isMain && isProjectMenu && !pathname.startsWith("/project/info")
|
|
const activeTop = topMenus.find((m) => m.match?.(pathname)) ?? topMenus[0];
|
|
const sideMenus = isProjectMenu ? projectSideMenus : manageSideMenus;
|
|
|
|
// 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 movePage = (path: string) => {
|
|
router.push(path);
|
|
};
|
|
|
|
const toggleSideMenu = (id: string) => {
|
|
setOpenMenus((prev) => ({ ...prev, [id]: !prev[id] }));
|
|
};
|
|
|
|
const decreaseFontSize = () =>{
|
|
setFontSize(Math.max(FONT_SIZE.MIN, fontSize - 2));
|
|
}
|
|
|
|
const increaseFontSize = () =>{
|
|
setFontSize(Math.min(FONT_SIZE.MAX, fontSize + 2));
|
|
}
|
|
|
|
// CSS 변수 동기화
|
|
useEffect(() => {
|
|
if (typeof document === "undefined") return;
|
|
document.documentElement.style.setProperty(
|
|
"--content-font-size",
|
|
`${fontSize}px`,
|
|
);
|
|
}, [fontSize]);
|
|
|
|
useEffect(() => {
|
|
if(!isMain && isProjectMenu && !selectedProject){
|
|
alert("옳지 않은 접근입니다.");
|
|
}
|
|
}, []);
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
"bg-gray-50 flex flex-col",
|
|
isMain ? "min-h-screen" : "h-screen overflow-hidden" // ✅ 메인 제외: 바깥 스크롤 잠금
|
|
)}
|
|
>
|
|
{/* 헤더 - 고정 폰트 크기 */}
|
|
<Header
|
|
isMain={isMain}
|
|
menuOpen={menuOpen}
|
|
activeTopId={activeTop.id}
|
|
fontSize={fontSize}
|
|
onToggleMenu={() => setMenuOpen((prev) => !prev)}
|
|
onNavigate={movePage}
|
|
decreaseFontSize={decreaseFontSize}
|
|
increaseFontSize={increaseFontSize}
|
|
/>
|
|
|
|
{/* 본문 */}
|
|
<div className={cn("flex flex-1", !isMain && "min-h-0")}>
|
|
{/* 사이드바 - 고정 폰트 크기 */}
|
|
{!isMain && (
|
|
<Sidebar
|
|
isProjectMenu={isProjectMenu}
|
|
sideMenus={sideMenus}
|
|
isOpen={menuOpen}
|
|
pathname={pathname}
|
|
openMenus={openMenus}
|
|
onToggleMenu={toggleSideMenu}
|
|
onNavigate={movePage}
|
|
/>
|
|
)}
|
|
|
|
{/* 메인 콘텐츠만 가변 폰트 크기 적용 */}
|
|
<main
|
|
className={cn(
|
|
"flex-1 p-4",
|
|
!isMain && "min-h-0 overflow-y-auto"
|
|
)}
|
|
// 전역 CSS 변수(--content-font-size)를 실제 렌더에 적용
|
|
// SSR 단계에서의 초기 폰트 값을 FOUC 없이 유지하기 위해 fallback을 함께 지정
|
|
style={{ fontSize: `var(--content-font-size, ${initialFontSize}px)` }}
|
|
>
|
|
{isViewProjectTitle &&
|
|
<ProjectTitle/>
|
|
}
|
|
{children}
|
|
</main>
|
|
</div>
|
|
|
|
{/* 모바일 오버레이 */}
|
|
{!isMain && menuOpen && (
|
|
<div
|
|
className="fixed inset-0 z-20 lg:hidden"
|
|
onClick={() => setMenuOpen(false)}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default MainLayout; |