"use client"; import Link from "next/link"; import Image from "next/image"; import { usePathname, useRouter } from "next/navigation"; import { useEffect, useMemo, useState } from "react"; import LoginModal from "@/components/LoginModal"; import { fetchAnonymousMenu, isMenuActive, type MenuItem } from "@/app/store/common"; import { useDmStore } from "@/app/store"; const DEFAULT_NAV_ITEMS: Pick[] = [ { menuCd: 1, menuKornNm: "물 재이용 소개", urlPath: "/intro", newWndwYn: "N" }, { menuCd: 2, menuKornNm: "물 재이용 현황", urlPath: "/status", newWndwYn: "N" }, { menuCd: 3, menuKornNm: "물 재이용 사례", urlPath: "/example", newWndwYn: "N" }, { menuCd: 4, menuKornNm: "수요신청", urlPath: "/apply/dmdco", newWndwYn: "N" }, ]; export default function TopNav() { const router = useRouter(); const pathname = usePathname(); const { actionLogout, Sdm } = useDmStore((state) => state); const { loginUser } = Sdm; const [loginOpen, setLoginOpen] = useState(false); const [topMenus, setTopMenus] = useState([]); useEffect(() => { let isMounted = true; (async () => { try { const loaded = await fetchAnonymousMenu(); if (!isMounted) return; const level1 = loaded.filter((menu) => menu.levelNo === 1 || menu.parentId === null); setTopMenus(level1); } catch (error) { console.error("Failed to load DN top menu", error); if (!isMounted) return; setTopMenus([]); } })(); return () => { isMounted = false; }; }, []); const navItems = useMemo(() => { if (topMenus.length > 0) return topMenus; return DEFAULT_NAV_ITEMS.map((item) => ({ ...item, parentId: null, sortSeq: 0, children: [], levelNo: 1, useYn: "Y", otptYn: "Y", })); }, [topMenus]); const userName = loginUser?.dmdCoNm ?? null; const isLoggedIn = Boolean(loginUser); const openLogin = () => setLoginOpen(true); const logout = async () => { await actionLogout(); router.push("/main"); }; return ( <>
{/* ✅ 좌/중/우 정렬이 되도록 컨테이너 구성 */}
{/* 좌측: 로고 */}
HASUON
{/* 중앙: 상단 메뉴 */} {/* ✅ 우측: 로그인 버튼 / 로그인 정보 + 로그아웃 */}
{!isLoggedIn ? ( ) : ( <> {userName}님 환영합니다. )}
setLoginOpen(false)} onSuccess={() => { setLoginOpen(false); router.refresh(); }} /> ); }