'use client' import * as React from 'react' import Link from 'next/link' import Image from 'next/image' import { Menu, Globe, ArrowRight } from 'lucide-react' import { Button } from '@/components/ui/button' import { Sheet, SheetContent, SheetTrigger, } from '@/components/ui/sheet' import { cn } from '@/lib/utils' // 导航结构 - 中文 const navigationCN = [ { name: '首页', href: '/' }, { name: '产品矩阵', href: '/products' }, { name: '关于我们', href: '/about' }, { name: '合作加盟', href: '/contact' }, ] // 导航结构 - 英文 const navigationEN = [ { name: 'Home', href: '/en' }, { name: 'Products', href: '/products' }, { name: 'About', href: '/about' }, { name: 'Contact', href: '/contact' }, ] export function Navbar() { const [isScrolled, setIsScrolled] = React.useState(false) const [language, setLanguage] = React.useState<'zh' | 'en'>('zh') const [mounted, setMounted] = React.useState(false) const [currentPath, setCurrentPath] = React.useState('') // 初始化语言状态和当前路径 React.useEffect(() => { setMounted(true) if (typeof window !== 'undefined') { const path = window.location.pathname setLanguage(path.startsWith('/en') ? 'en' : 'zh') setCurrentPath(path) } }, []) // 监听路由变化,更新当前路径 React.useEffect(() => { if (typeof window !== 'undefined') { const handlePopState = () => { setCurrentPath(window.location.pathname) } // 监听浏览器前进后退 window.addEventListener('popstate', handlePopState) // 保存原始的pushState和replaceState const originalPushState = window.history.pushState const originalReplaceState = window.history.replaceState // 重写pushState和replaceState window.history.pushState = function(...args) { originalPushState.apply(this, args) handlePopState() } window.history.replaceState = function(...args) { originalReplaceState.apply(this, args) handlePopState() } return () => { window.removeEventListener('popstate', handlePopState) } } }, []) React.useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 20) } window.addEventListener('scroll', handleScroll) return () => window.removeEventListener('scroll', handleScroll) }, []) // 根据语言选择导航 const navigation = language === 'zh' ? navigationCN : navigationEN const currentLanguage = language // 切换语言 const toggleLanguage = () => { const newLang = currentLanguage === 'zh' ? 'en' : 'zh' setLanguage(newLang) // 跳转到对应语言版本 if (typeof window !== 'undefined') { const path = window.location.pathname let newPath = path if (newLang === 'en') { // 转到英文版(首页) if (path === '/') { newPath = '/en' } else { // 其他页面保持原样(暂时只有首页有英文版) newPath = '/en' } } else { // 转到中文版 if (path.startsWith('/en')) { newPath = '/' } else { newPath = path } } window.location.href = newPath } } return (
{/* Logo */}
Baima Home Logo
{/* Desktop Navigation */} {/* Right Side Actions */}
{/* */}
{/* Mobile Navigation */}
Baima Home Logo
{/* 语言切换 */}
{navigation.map((item) => { // 检测当前活跃链接 const isActive = mounted && currentPath === item.href; return ( {item.name} ); })}
) }