import { useState, useRef, useEffect } from 'react' import { useTranslation, type Locale } from '@/lib/use-translation' const locales: { value: Locale; label: string }[] = [ { value: 'en', label: 'English' }, { value: 'zh-CN', label: '简体中文' }, ] function LanguageIcon() { return ( ) } function CheckIcon() { return ( ) } export function LanguageSwitcher() { const { locale, setLocale, t } = useTranslation() const [isOpen, setIsOpen] = useState(false) const containerRef = useRef(null) const handleLocaleChange = (newLocale: Locale) => { setLocale(newLocale) setIsOpen(false) } // Close dropdown when clicking outside useEffect(() => { if (!isOpen) return const handleClickOutside = (event: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(event.target as Node)) { setIsOpen(false) } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, [isOpen]) // Close on escape key useEffect(() => { if (!isOpen) return const handleEscape = (event: KeyboardEvent) => { if (event.key === 'Escape') { setIsOpen(false) } } document.addEventListener('keydown', handleEscape) return () => document.removeEventListener('keydown', handleEscape) }, [isOpen]) return ( setIsOpen(!isOpen)} className="flex items-center justify-center h-8 w-8 rounded-md text-[var(--app-hint)] hover:text-[var(--app-fg)] hover:bg-[var(--app-subtle-bg)] transition-colors" title={t('language.title')} aria-label={t('language.title')} aria-expanded={isOpen} aria-haspopup="listbox" > {isOpen && ( {locales.map((loc) => { const isSelected = locale === loc.value return ( handleLocaleChange(loc.value)} className={`flex items-center justify-between w-full px-3 py-2 text-sm text-left transition-colors ${ isSelected ? 'text-[var(--app-link)] bg-[var(--app-subtle-bg)]' : 'text-[var(--app-fg)] hover:bg-[var(--app-subtle-bg)]' }`} > {loc.label} {isSelected && ( )} ) })} )} ) }