"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useAuth } from "@/lib/auth"; const LINKS = [ { href: "/#map", label: "🗺️ 地图", section: "map" }, { href: "/#destinations", label: "🌍 目的地", section: "destinations" }, { href: "/#trip", label: "🗓️ 行程", section: "trip" }, { href: "/#calculator", label: "🧮 计算器", section: "calculator" }, { href: "/tools", label: "🛠️ 工具箱", section: "tools" }, { href: "/#visa", label: "📋 签证", section: "visa" }, { href: "/#blog", label: "📝 博客", section: "blog" }, ]; export default function Navbar() { const { user } = useAuth(); const pathname = usePathname(); const isHome = pathname === "/"; const [theme, setTheme] = useState<"dark" | "light">("dark"); const [menuOpen, setMenuOpen] = useState(false); const [active, setActive] = useState(""); useEffect(() => { const saved = localStorage.getItem("nomad-theme"); if (saved === "light") { setTheme("light"); document.documentElement.setAttribute("data-theme", "light"); } }, []); useEffect(() => { const navbar = document.getElementById("navbar"); const onScroll = () => navbar?.classList.toggle("scrolled", window.scrollY > 50); window.addEventListener("scroll", onScroll, { passive: true }); onScroll(); return () => window.removeEventListener("scroll", onScroll); }, []); useEffect(() => { if (!isHome) return; const homeLinks = LINKS.filter((l) => l.href.startsWith("/#")); const sections = homeLinks.map((l) => document.getElementById(l.section)).filter(Boolean); const observer = new IntersectionObserver( (entries) => entries.forEach((e) => { if (e.isIntersecting) setActive(e.target.id); }), { threshold: 0.3, rootMargin: "-80px 0px -50% 0px" } ); sections.forEach((s) => observer.observe(s!)); return () => observer.disconnect(); }, [isHome]); useEffect(() => { document.body.style.overflow = menuOpen ? "hidden" : ""; return () => { document.body.style.overflow = ""; }; }, [menuOpen]); const toggleTheme = () => { const next = theme === "dark" ? "light" : "dark"; setTheme(next); if (next === "light") document.documentElement.setAttribute("data-theme", "light"); else document.documentElement.removeAttribute("data-theme"); localStorage.setItem("nomad-theme", next); }; return ( ); }