"use client"; import Link from "next/link"; import React, { useEffect, useState } from "react"; import Image from "next/image"; import { blockItems, blogItems, demos, docsPages, otherPages, projectPages, } from "@/data/menu"; import { usePathname } from "next/navigation"; // !text-[var(--current-color)] export default function Nav({ color = "#fab758" }) { const [menu, setMenu] = useState([]); const pathname = usePathname(); useEffect(() => { // Dynamically import Bootstrap import("bootstrap").then((Bootstrap) => { const CLASS_NAME = "has-child-dropdown-show"; // Save the original toggle function const originalToggle = Bootstrap.Dropdown.prototype.toggle; // Override the toggle function Bootstrap.Dropdown.prototype.toggle = function () { // Remove the CLASS_NAME from all dropdowns document.querySelectorAll("." + CLASS_NAME).forEach(function (e) { e.classList.remove(CLASS_NAME); }); // Traverse up through the closest dropdown parents let dd = this._element .closest(".dropdown") .parentNode.closest(".dropdown"); for (; dd && dd !== document; dd = dd.parentNode.closest(".dropdown")) { dd.classList.add(CLASS_NAME); } // Call the original toggle function return originalToggle.call(this); }; // Add event listeners for hide.bs.dropdown to remove the CLASS_NAME document.querySelectorAll(".dropdown").forEach(function (dd) { dd.addEventListener("hide.bs.dropdown", function (e) { if (this.classList.contains(CLASS_NAME)) { this.classList.remove(CLASS_NAME); e.preventDefault(); } e.stopPropagation(); }); }); }); // 获取菜单数据 fetch("/api/get-menu") .then((res) => res.json()) .then((data) => setMenu(data.menu || [])); // Optional cleanup function for any potential side effects return () => { // Cleanup code here (if needed) }; }, []); // 只在顶级菜单过滤 position const filterHeaderMenu = (items) => { return items.filter(item => item.position === 'Header'); }; // 递归渲染菜单 const renderMenu = (items, level = 0) => { // 顶级菜单过滤 position 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 (