'use client'; import { useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import Link from 'next/link'; import { IRootState } from '@/store'; import { toggleTheme, toggleSidebar } from '@/store/themeConfigSlice'; import Image from 'next/image'; import Dropdown from '@/components/dropdown'; import IconMenu from '@/components/icon/icon-menu'; import IconSun from '@/components/icon/icon-sun'; import IconMoon from '@/components/icon/icon-moon'; import IconUser from '@/components/icon/icon-user'; import IconLockDots from '@/components/icon/icon-lock-dots'; import IconLogout from '@/components/icon/icon-logout'; import { usePathname, useRouter } from 'next/navigation'; type UserData = { id: string; email: string; is_active: boolean }; export default function Header() { const pathname = usePathname(); const dispatch = useDispatch(); const router = useRouter(); const themeConfig = useSelector((state: IRootState) => state.themeConfig); const isRtl = themeConfig.rtlClass === 'rtl'; const [user, setUser] = useState(null); const [loadingUser, setLoadingUser] = useState(true); const API = process.env.NEXT_PUBLIC_FASTAPI_URL; // highlight active menu useEffect(() => { const selector = document.querySelector( `ul.horizontal-menu a[href="${window.location.pathname}"]` ); if (selector) { document .querySelectorAll('ul.horizontal-menu .nav-link.active, ul.horizontal-menu a.active') .forEach((el) => el.classList.remove('active')); selector.classList.add('active'); const ul: any = selector.closest('ul.sub-menu'); if (ul) { const ele: any = ul.closest('li.menu')?.querySelector('.nav-link'); setTimeout(() => ele?.classList.add('active')); } } }, [pathname]); async function loadUser(signal?: AbortSignal) { try { const res = await fetch(`${API}/auth/me`, { credentials: 'include', cache: 'no-store', signal, }); if (!res.ok) throw new Error(); const data = await res.json().catch(() => null); setUser(data?.id ? (data as UserData) : null); } catch { setUser(null); } finally { setLoadingUser(false); } } useEffect(() => { setLoadingUser(true); const controller = new AbortController(); loadUser(controller.signal); return () => controller.abort(); }, [pathname, API]); const handleLogout = async () => { try { await fetch(`${API}/auth/logout`, { method: 'POST', credentials: 'include', cache: 'no-store', }); } catch (_) { // ignore } finally { setUser(null); window.location.href = '/login'; } }; return (
{/* Logo + mobile toggler */}
logo
{/* Right-side actions */}
{/* Theme toggle */} {themeConfig.theme === 'light' ? ( ) : ( )} {/* User dropdown */}
{loadingUser ? (
) : user ? (
} >
  • {user.email}

  • Profile
  • Lock Screen
) : ( Sign In )}
); }