"use client"; import { useState, useTransition } from "react"; import { useRouter, usePathname } from "next/navigation"; import { Check, ChevronDown, Globe } from "lucide-react"; import { locales, localeFlags, localeNames, type Locale } from "@/i18n/config"; import { useLocale } from "next-intl"; export default function LanguageSelectRadix() { const router = useRouter(); const pathname = usePathname(); const currentLocale = useLocale() as Locale; const [isPending, startTransition] = useTransition(); const [isOpen, setIsOpen] = useState(false); const changeLanguage = (newLocale: Locale) => { if (newLocale === currentLocale) { setIsOpen(false); return; } startTransition(() => { // ✅ 1. Set cookie (middleware will sync this) document.cookie = `NEXT_LOCALE=${newLocale}; path=/; max-age=31536000; SameSite=Lax`; // ✅ 2. Build new path const segments = pathname.split("/").filter(Boolean); // Remove current locale if exists const pathWithoutLocale = segments[0] && locales.includes(segments[0] as Locale) ? segments.slice(1) : segments; // Add new locale const newPath = `/${newLocale}${ pathWithoutLocale.length ? "/" + pathWithoutLocale.join("/") : "" }`; // ✅ 3. Navigate (middleware will handle the rest) router.push(newPath); // ✅ 4. Force refresh after navigation completes setTimeout(() => { router.refresh(); }, 100); // Small delay ensures navigation completes }); setIsOpen(false); }; return (