'use client'; import { useState, useEffect, useRef } from 'react'; import { ChevronDown, Menu, X, Globe } from 'lucide-react'; import Link from 'next/link'; import { NavigationItem } from './NavigationItem'; import { MobileMenu } from './MobileMenu'; import { DropdownMenu } from './DropdownMenu'; type Lang = 'zh' | 'zh-tw' | 'en'; interface NavigationProps { currentLang: Lang; onLanguageChange: React.Dispatch> | ((lang: Lang) => void); translations: any; } export function Navigation({ currentLang, onLanguageChange, translations }: NavigationProps) { const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [activeDropdown, setActiveDropdown] = useState(null); const [currentPath, setCurrentPath] = useState('/'); const dropdownTimeoutRef = useRef(null); const t = translations[currentLang]; // Navigation structure with dropdown menus const navigationItems = [ { key: 'home', label: t.nav.home, href: '/', current: currentPath === '/', }, { key: 'products', label: t.nav.products, href: '/products', current: currentPath.startsWith('/products'), dropdown: [ { label: currentLang === 'zh' ? '云服务器 ECS' : currentLang === 'zh-tw' ? '雲伺服器 ECS' : 'Cloud Server ECS', href: '/products/ecs', }, { label: currentLang === 'zh' ? '云数据库 RDS' : currentLang === 'zh-tw' ? '雲資料庫 RDS' : 'Cloud Database RDS', href: '/products/rds', }, { label: currentLang === 'zh' ? '负载均衡 SLB' : currentLang === 'zh-tw' ? '負載平衡 SLB' : 'Load Balancer SLB', href: '/products/slb', }, { label: currentLang === 'zh' ? '对象存储 OSS' : currentLang === 'zh-tw' ? '物件儲存 OSS' : 'Object Storage OSS', href: '/products/oss', }, ], }, { key: 'solutions', label: t.nav.solutions, href: '/solutions', current: currentPath.startsWith('/solutions'), dropdown: [ { label: currentLang === 'zh' ? '企业上云' : currentLang === 'zh-tw' ? '企業上雲' : 'Enterprise Cloud', href: '/solutions/enterprise', }, { label: currentLang === 'zh' ? '数据备份' : currentLang === 'zh-tw' ? '資料備份' : 'Data Backup', href: '/solutions/backup', }, { label: currentLang === 'zh' ? '灾难恢复' : currentLang === 'zh-tw' ? '災難恢復' : 'Disaster Recovery', href: '/solutions/recovery', }, ], }, { key: 'support', label: t.nav.support, href: '/support', current: currentPath.startsWith('/support'), }, { key: 'news', label: t.nav.news, href: '/news', current: currentPath.startsWith('/news'), }, { key: 'contact', label: t.nav.contact, href: '/contact', current: currentPath === '/contact', }, ]; // Handle dropdown interactions const handleDropdownEnter = (key: string) => { if (dropdownTimeoutRef.current) { clearTimeout(dropdownTimeoutRef.current); } setActiveDropdown(key); }; const handleDropdownLeave = () => { dropdownTimeoutRef.current = setTimeout(() => { setActiveDropdown(null); }, 150); }; // Handle keyboard navigation const handleKeyDown = (event: React.KeyboardEvent, key: string, hasDropdown: boolean) => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); if (hasDropdown) { setActiveDropdown(activeDropdown === key ? null : key); } } else if (event.key === 'Escape') { setActiveDropdown(null); } }; // Close mobile menu when clicking outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (isMobileMenuOpen && !(event.target as Element).closest('[data-mobile-menu]')) { setIsMobileMenuOpen(false); } }; document.addEventListener('click', handleClickOutside); return () => document.removeEventListener('click', handleClickOutside); }, [isMobileMenuOpen]); // Prevent body scroll when mobile menu is open useEffect(() => { if (isMobileMenuOpen) { document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = 'unset'; } return () => { document.body.style.overflow = 'unset'; }; }, [isMobileMenuOpen]); return ( ); }