import { personalInfo } from "@/lib/data/index"; import LanguageSwitcher from "./LanguageSwitcher"; import ThemeToggle from "./ui/theme-toggle"; import Container from "./ui/Container.tsx"; import { useTranslations, getLocalizedPath } from "@/i18n/utils"; import type { Lang } from "@/types/i18n"; import { useState, useEffect } from "react"; import { Menu, X, Home, Rocket, PenTool, User, Briefcase, Clock3 } from "lucide-react"; import { defaultLang } from "@/i18n/ui"; import { type GlassHeaderProps } from "@/types"; import { cn } from "@/lib/utils"; export default function GlassHeader({ lang: propLang }: GlassHeaderProps) { const [lang, setLang] = useState(propLang || defaultLang); const [currentPath, setCurrentPath] = useState(""); const normalizePath = (path: string) => { const clean = path.split("#")[0].split("?")[0]; if (!clean) return "/"; if (clean === "/") return "/"; return clean.endsWith("/") ? clean.slice(0, -1) : clean; }; useEffect(() => { const updatePath = () => setCurrentPath(normalizePath(window.location.pathname)); updatePath(); const syncLang = () => { const htmlLang = document.documentElement.lang as Lang; if (htmlLang && (!propLang || htmlLang !== lang)) { setLang(htmlLang); } }; syncLang(); const originalPushState = history.pushState.bind(history); const originalReplaceState = history.replaceState.bind(history); history.pushState = function (...args) { originalPushState(...args); window.dispatchEvent(new Event("locationchange")); }; history.replaceState = function (...args) { originalReplaceState(...args); window.dispatchEvent(new Event("locationchange")); }; window.addEventListener("popstate", updatePath); window.addEventListener("locationchange", updatePath); document.addEventListener("astro:after-swap", updatePath as EventListener); document.addEventListener("astro:page-load", updatePath as EventListener); document.addEventListener("astro:page-load", syncLang as EventListener); return () => { history.pushState = originalPushState; history.replaceState = originalReplaceState; window.removeEventListener("popstate", updatePath); window.removeEventListener("locationchange", updatePath); document.removeEventListener("astro:after-swap", updatePath as EventListener); document.removeEventListener("astro:page-load", updatePath as EventListener); document.removeEventListener("astro:page-load", syncLang as EventListener); }; }, [propLang, lang]); const t = useTranslations(lang); const [isMenuOpen, setIsMenuOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 0); }; window.addEventListener('scroll', handleScroll); return () => window.removeEventListener('scroll', handleScroll); }, []); const toggleMenu = () => setIsMenuOpen(!isMenuOpen); const navItems = [ { key: 'nav.home', icon: Home, href: getLocalizedPath('/', lang) }, { key: 'nav.about', icon: User, href: getLocalizedPath('/about', lang) }, { key: 'nav.projects', icon: Rocket, href: getLocalizedPath('/projects', lang) }, { key: 'nav.blog', icon: PenTool, href: getLocalizedPath('/blog', lang) }, { key: 'nav.now', icon: Clock3, href: getLocalizedPath('/now', lang) }, { key: 'nav.hire', icon: Briefcase, href: getLocalizedPath('/hire', lang) }, ]; const isActive = (path: string) => { const normalizedPath = normalizePath(path); if (normalizedPath === '/' || normalizedPath === '/zh') { return currentPath === normalizedPath; } return currentPath === normalizedPath || currentPath.startsWith(`${normalizedPath}/`); }; return (
{personalInfo.name}
{/* Desktop Navigation */}
{/* Language Switcher added here */}
{/* Mobile Menu Button */}
{/* Mobile Navigation */}
); }