"use client"; import { useState, useRef, useEffect } from "react"; import { usePathname } from "next/navigation"; import Link from "next/link"; import { motion, AnimatePresence } from "framer-motion"; import { Globe } from "lucide-react"; import { locales, localeNames, localeFlags, type Locale } from "@/lib/dictionaries"; interface LanguageSwitcherProps { locale: Locale; } export default function LanguageSwitcher({ locale }: LanguageSwitcherProps) { const [open, setOpen] = useState(false); const ref = useRef(null); const pathname = usePathname(); useEffect(() => { function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); } } if (open) document.addEventListener("mousedown", handleClick); return () => document.removeEventListener("mousedown", handleClick); }, [open]); // Build the equivalent path for the other locale function getLocalePath(targetLocale: Locale) { // Replace /en/... or /bs/... with the target locale const segments = pathname.split("/"); segments[1] = targetLocale; return segments.join("/") || `/${targetLocale}`; } return (
{open && ( {locales.map((l) => { const isActive = l === locale; return ( { setOpen(false); // Persist user's explicit language choice document.cookie = `NEXT_LOCALE=${l}; path=/; max-age=31536000; SameSite=Lax`; // Dismiss language banner since user chose explicitly document.cookie = "lang-banner-dismissed=1; path=/; max-age=2592000; SameSite=Lax"; }} className={`flex items-center gap-2.5 px-3 py-2.5 text-sm transition-colors ${ isActive ? "bg-purple-500/10 text-purple-300" : "text-text-secondary hover:bg-white/5 hover:text-text-primary" }`} > {localeFlags[l]} {localeNames[l]} {isActive && ( )} ); })} )}
); }