"use client"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { Button } from "@app/components/ui/button"; import { Check, Globe, Languages } from "lucide-react"; import clsx from "clsx"; import { useTransition } from "react"; import { Locale } from "@/i18n/config"; import { setUserLocale } from "@/services/locale"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; type Props = { defaultValue: string; items: Array<{ value: string; label: string }>; label: string; }; export default function LocaleSwitcherSelect({ defaultValue, items, label }: Props) { const [isPending, startTransition] = useTransition(); const api = createApiClient(useEnvContext()); function onChange(value: string) { const locale = value as Locale; startTransition(() => { setUserLocale(locale); }); // Persist locale to the database (fire-and-forget) api.post("/user/locale", { locale }).catch(() => { // Silently ignore errors — cookie is already set as fallback }); } const selected = items.find((item) => item.value === defaultValue); return ( {items.map((item) => ( onChange(item.value)} className="flex items-center gap-2" > {item.value === defaultValue && ( )} {item.label} ))} ); }