2025-11-18 06:19:26 +09:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
import { usePathname } from "next/navigation";
|
|
|
|
|
import MainLogoSvg from "./svgs/mainlogosvg";
|
2025-11-18 07:53:09 +09:00
|
|
|
import ChevronDownSvg from "./svgs/chevrondownsvg";
|
2025-11-18 06:19:26 +09:00
|
|
|
|
|
|
|
|
const NAV_ITEMS = [
|
2025-11-18 09:09:09 +09:00
|
|
|
{ label: "교육 과정 목록", href: "/course-list" },
|
2025-11-18 06:19:26 +09:00
|
|
|
{ label: "학습 자료실", href: "/resources" },
|
|
|
|
|
{ label: "공지사항", href: "/notices" },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export default function NavBar() {
|
|
|
|
|
const pathname = usePathname();
|
|
|
|
|
const [isUserMenuOpen, setIsUserMenuOpen] = useState(false);
|
2025-11-24 23:31:05 +09:00
|
|
|
const [userName, setUserName] = useState<string>('');
|
2025-11-18 06:19:26 +09:00
|
|
|
const userMenuRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
|
const userButtonRef = useRef<HTMLButtonElement | null>(null);
|
2025-11-18 10:42:48 +09:00
|
|
|
const hideCenterNav = /^\/[^/]+\/review$/.test(pathname);
|
2025-11-19 01:41:27 +09:00
|
|
|
const isAdminPage = pathname.startsWith('/admin');
|
2025-11-18 06:19:26 +09:00
|
|
|
|
2025-11-24 23:31:05 +09:00
|
|
|
// 사용자 정보 가져오기
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let isMounted = true;
|
|
|
|
|
|
|
|
|
|
async function fetchUserInfo() {
|
|
|
|
|
try {
|
|
|
|
|
const token = localStorage.getItem('token');
|
|
|
|
|
if (!token) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const response = await fetch('https://hrdi.coconutmeet.net/auth/me', {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
if (response.status === 401) {
|
|
|
|
|
// 토큰이 만료되었거나 유효하지 않은 경우
|
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
if (isMounted && data.name) {
|
|
|
|
|
setUserName(data.name);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('사용자 정보 조회 오류:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchUserInfo();
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
isMounted = false;
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-11-18 06:19:26 +09:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isUserMenuOpen) return;
|
|
|
|
|
const onDown = (e: MouseEvent) => {
|
|
|
|
|
const t = e.target as Node;
|
|
|
|
|
if (
|
|
|
|
|
userMenuRef.current &&
|
|
|
|
|
!userMenuRef.current.contains(t) &&
|
|
|
|
|
userButtonRef.current &&
|
|
|
|
|
!userButtonRef.current.contains(t)
|
|
|
|
|
) {
|
|
|
|
|
setIsUserMenuOpen(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const onKey = (e: KeyboardEvent) => {
|
|
|
|
|
if (e.key === "Escape") setIsUserMenuOpen(false);
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener("mousedown", onDown);
|
|
|
|
|
document.addEventListener("keydown", onKey);
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener("mousedown", onDown);
|
|
|
|
|
document.removeEventListener("keydown", onKey);
|
|
|
|
|
};
|
|
|
|
|
}, [isUserMenuOpen]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<header className="bg-[#060958] h-20">
|
|
|
|
|
<div className="mx-auto flex h-full w-full max-w-[1440px] items-center justify-between px-8">
|
|
|
|
|
<div className="flex flex-1 items-center gap-9">
|
|
|
|
|
<Link href="/" aria-label="XR LMS 홈" className="flex items-center gap-2">
|
|
|
|
|
<MainLogoSvg width={46.703} height={36} />
|
|
|
|
|
<span className="text-2xl font-extrabold leading-[1.45] text-white">XR LMS</span>
|
|
|
|
|
</Link>
|
2025-11-18 23:42:41 +09:00
|
|
|
{!hideCenterNav && !isAdminPage && (
|
2025-11-18 10:42:48 +09:00
|
|
|
<nav className="flex h-full items-center">
|
|
|
|
|
{NAV_ITEMS.map((item) => {
|
|
|
|
|
return (
|
|
|
|
|
<Link
|
|
|
|
|
key={item.href}
|
|
|
|
|
href={item.href}
|
|
|
|
|
className={["px-4 py-2 text-[16px] font-semibold text-white"].join(" ")}
|
|
|
|
|
>
|
|
|
|
|
{item.label}
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</nav>
|
|
|
|
|
)}
|
2025-11-18 06:19:26 +09:00
|
|
|
</div>
|
|
|
|
|
<div className="relative flex items-center gap-2">
|
2025-11-18 23:42:41 +09:00
|
|
|
{isAdminPage ? (
|
|
|
|
|
<>
|
|
|
|
|
<Link href="/menu/account" className="px-4 py-2 text-[16px] font-semibold text-white">
|
|
|
|
|
내 정보
|
|
|
|
|
</Link>
|
2025-11-25 12:38:11 +09:00
|
|
|
<Link href="/login" className="px-4 py-2 text-[16px] font-semibold text-white ">
|
2025-11-18 23:42:41 +09:00
|
|
|
로그아웃
|
|
|
|
|
</Link>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<Link href="/menu/courses" className="px-4 py-2 text-[16px] font-semibold text-white">
|
|
|
|
|
내 강좌실
|
2025-11-18 07:53:09 +09:00
|
|
|
</Link>
|
2025-11-18 06:19:26 +09:00
|
|
|
<button
|
2025-11-18 23:42:41 +09:00
|
|
|
ref={userButtonRef}
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setIsUserMenuOpen((v) => !v)}
|
|
|
|
|
aria-haspopup="menu"
|
|
|
|
|
aria-expanded={isUserMenuOpen}
|
|
|
|
|
className="flex items-center gap-1 px-4 py-2 text-[16px] font-semibold text-white cursor-pointer"
|
2025-11-18 06:19:26 +09:00
|
|
|
>
|
2025-11-24 23:31:05 +09:00
|
|
|
{userName || '사용자'}
|
2025-11-18 23:42:41 +09:00
|
|
|
<ChevronDownSvg
|
|
|
|
|
width={16}
|
|
|
|
|
height={16}
|
|
|
|
|
className={["transition-transform", isUserMenuOpen ? "rotate-180" : "rotate-0"].join(" ")}
|
|
|
|
|
/>
|
2025-11-18 06:19:26 +09:00
|
|
|
</button>
|
2025-11-18 23:42:41 +09:00
|
|
|
{isUserMenuOpen && (
|
|
|
|
|
<div
|
|
|
|
|
ref={userMenuRef}
|
|
|
|
|
role="menu"
|
|
|
|
|
aria-label="사용자 메뉴"
|
|
|
|
|
className="absolute right-0 top-full mt-2 bg-white rounded-lg shadow-[0_0_8px_0_rgba(0,0,0,0.25)] p-3 z-50"
|
|
|
|
|
>
|
|
|
|
|
<Link
|
|
|
|
|
role="menuitem"
|
|
|
|
|
href="/menu/account"
|
2025-11-25 12:38:11 +09:00
|
|
|
className="flex items-center w-[136px] h-10 px-2 rounded-lg text-left text-[#333C47] text-[16px] font-medium leading-normal hover:bg-[rgba(236,240,255,0.5)] focus:bg-[rgba(236,240,255,0.5)] outline-nonq"
|
2025-11-18 23:42:41 +09:00
|
|
|
onClick={() => setIsUserMenuOpen(false)}
|
|
|
|
|
>
|
|
|
|
|
내 정보 수정
|
|
|
|
|
</Link>
|
|
|
|
|
<button
|
|
|
|
|
role="menuitem"
|
2025-11-25 12:38:11 +09:00
|
|
|
className="flex items-center w-[136px] h-10 px-2 rounded-lg text-left text-[#333C47] text-[16px] font-medium leading-normal hover:bg-[rgba(236,240,255,0.5)] focus:bg-[rgba(236,240,255,0.5)] outline-none"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
// 로컬 스토리지에서 토큰 제거
|
|
|
|
|
localStorage.removeItem('token');
|
|
|
|
|
localStorage.removeItem('user');
|
|
|
|
|
// 쿠키에서 토큰 제거 (미들웨어에서 확인하므로)
|
|
|
|
|
document.cookie = 'token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
|
|
|
|
|
// 로그인 페이지로 리다이렉트
|
|
|
|
|
window.location.href = '/login';
|
|
|
|
|
}}
|
2025-11-18 23:42:41 +09:00
|
|
|
>
|
|
|
|
|
로그아웃
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
2025-11-18 06:19:26 +09:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|