"use client" import { Globe } from "lucide-react" import { usePathname, useRouter, useSearchParams } from "next/navigation" import { Suspense, useEffect, useRef, useState } from "react" import { i18n, type Locale } from "@/lib/i18n/config" const LABELS: Record = { en: "EN", zh: "中文", ja: "日本語", } function LanguageToggleInner({ className = "" }: { className?: string }) { const router = useRouter() const pathname = usePathname() || "/" const search = useSearchParams() const [open, setOpen] = useState(false) const [value, setValue] = useState(i18n.defaultLocale) const ref = useRef(null) useEffect(() => { const seg = pathname.split("/").filter(Boolean) const first = seg[0] if (first && i18n.locales.includes(first as Locale)) setValue(first as Locale) else setValue(i18n.defaultLocale) }, [pathname]) useEffect(() => { function onDoc(e: MouseEvent) { if (!ref.current) return if (!ref.current.contains(e.target as Node)) setOpen(false) } if (open) document.addEventListener("mousedown", onDoc) return () => document.removeEventListener("mousedown", onDoc) }, [open]) const changeLocale = (lang: string) => { const parts = pathname.split("/") if (parts.length > 1 && i18n.locales.includes(parts[1] as Locale)) { parts[1] = lang } else { parts.splice(1, 0, lang) } const newPath = parts.join("/") || "/" const searchStr = search?.toString() ? `?${search.toString()}` : "" setOpen(false) router.push(newPath + searchStr) } return (
{open && (
{i18n.locales.map((loc) => ( ))}
)}
) } export default function LanguageToggle({ className = "", }: { className?: string }) { return ( } > ) }