"use client"; import Link from "next/link"; import React, { useEffect, useState } from "react"; import { usePathname } from "next/navigation"; export default function Nav({ color = "#fab758" }) { const [menu, setMenu] = useState([]); const pathname = usePathname(); useEffect(() => { fetch("/api/get-menu") .then((res) => res.json()) .then((data) => setMenu(data.menu || [])); const handleClickOutside = (event) => { if (window.innerWidth <= 768) { if (!event.target.closest('.navbar-nav .offcanvas-body .dropdown')) { document.querySelectorAll('.navbar-nav .offcanvas-body .dropdown-menu').forEach(menu => { menu.style.display = 'none'; }); } } }; const handleDropdownEvents = () => { document.querySelectorAll('.dropdown-submenu').forEach(submenu => { submenu.addEventListener('mouseenter', function() { const dropdownMenu = this.querySelector('.dropdown-menu'); if (dropdownMenu) { dropdownMenu.style.display = 'block'; } }); submenu.addEventListener('mouseleave', function() { const dropdownMenu = this.querySelector('.dropdown-menu'); if (dropdownMenu) { dropdownMenu.style.display = 'none'; } }); }); }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', handleDropdownEvents); } else { setTimeout(handleDropdownEvents, 100); } document.addEventListener('click', handleClickOutside); return () => { document.removeEventListener('click', handleClickOutside); document.querySelectorAll('.dropdown-submenu').forEach(submenu => { submenu.removeEventListener('mouseenter', handleDropdownEvents); submenu.removeEventListener('mouseleave', handleDropdownEvents); }); }; }, []); const filterHeaderMenu = (items) => { return items.filter(item => item.position === 'Header'); }; const renderMenu = (items, level = 0) => { if (level === 0) items = filterHeaderMenu(items); return items.map((item) => { const hasChildren = item.children && item.children.length > 0; const hasHref = !!item.href && item.href !== "#"; if (hasChildren) { return (
  • 0 ? " dropdown-submenu dropend" : ""}`}>
    {hasHref && ( 0 ? "dropdown-item" : "nav-link"} !text-[.7rem] !tracking-[normal] hover:!text-[var(--current-color)] after:!text-[var(--current-color)] ${pathname === item.href ? "!text-[var(--current-color)]" : ""}`} href={item.href} style={{ paddingRight: 0 }} > {item.label} )} 0 ? "dropdown-item" : "nav-link")} !text-[.7rem] !tracking-[normal] hover:!text-[var(--current-color)] after:!text-[var(--current-color)] ${!hasHref && pathname === item.href ? "!text-[var(--current-color)]" : ""}`} href="#" onClick={(e) => { if (window.innerWidth <= 768) { e.preventDefault(); const dropdownMenu = e.target.closest('.dropdown').querySelector('.dropdown-menu'); if (dropdownMenu) { dropdownMenu.style.display = dropdownMenu.style.display === 'block' ? 'none' : 'block'; } } }} style={hasHref ? { paddingLeft: 8, minWidth: 24 } : {}} > {hasHref ? Open : item.label}
  • ); } else { return (
  • 0 ? "dropdown-item" : "nav-link"} !text-[.7rem] !tracking-[normal] hover:!text-[var(--current-color)] after:!text-[var(--current-color)] ${pathname === item.href ? "!text-[var(--current-color)]" : ""}`} href={item.href || "#"} > {item.label}
  • ); } }); }; return ( ); }