import { Link, useLocation } from "react-router-dom";
import { Dropdown } from "antd";
import styles from "./Header.module.css";
import { useEffect, useMemo, useState } from "react";
import { useStore } from "@/store";
import type { NavChild, LocaleKey, SupportLocale } from "@/type";
const DEFAULT_NAV_ITEMS: { path: string; label: string; children?: NavChild[] }[] = [];
export default function Header() {
const location = useLocation();
const appConfig = useStore((s) => s.appConfig);
const locale = useStore((s) => s.locale);
const setLocale = useStore((s) => s.setLocale);
const supportLocales = useStore((s) => s.supportLocales);
const navItems = appConfig?.header?.navItems?.filter((item) => !item.index) ?? DEFAULT_NAV_ITEMS;
const langMenuItems: SupportLocale[] = supportLocales || [];
const logo = appConfig?.logo ?? "/images/logo.png";
const [activeNav, setActiveNav] = useState("");
const [showDropPanel, setShowDropPanel] = useState(false);
const [hoverElLeft, setHoverElLeft] = useState(0);
const handleNavEnter = (e: any, path: string) => {
const left = e.target.offsetLeft;
// 计算元素宽度
const width = e.target.offsetWidth;
setHoverElLeft(left + width / 2);
setActiveNav(path);
setShowDropPanel(true);
}
const activePanelItem = useMemo(() => {
return navItems.find((item) => item.path === activeNav)?.children || [];
}, [activeNav]);
const [showWhiteMode, setShowWhiteMode] = useState(false);
useEffect(() => {
const path = location.pathname;
if (path.includes("/detail/")) {
setShowWhiteMode(true);
} else {
setShowWhiteMode(false);
}
}, [location.pathname]);
// 监听滚动
useEffect(() => {
const handleScroll = () => {
const scrollTop = window.scrollY;
if (scrollTop > 100) {
setShowWhiteMode(true);
} else {
setShowWhiteMode(false);
}
}
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
return (