import React from 'react'; import { Check, Languages } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { SUPPORTED_LANGUAGES, getCurrentLocale, switchLocale, type SupportedLocale } from '../lib/locale'; export function LanguageSwitcher() { const { t } = useTranslation('common'); const [pendingLocale, setPendingLocale] = React.useState(null); const currentLocale = getCurrentLocale(); const changeLanguage = React.useCallback( async (locale: SupportedLocale) => { if (locale === currentLocale || pendingLocale) { return; } setPendingLocale(locale); try { await switchLocale(locale); } catch (error) { if (import.meta.env.DEV) { console.error('Failed to switch language', error); } } finally { setPendingLocale(null); } }, [currentLocale, pendingLocale] ); return ( {SUPPORTED_LANGUAGES.map(({ code, labelKey }) => { const isActive = currentLocale === code; const isPending = pendingLocale === code; return ( { event.preventDefault(); changeLanguage(code); }} className="flex items-center justify-between gap-3" disabled={isPending} > {t(labelKey)} {(isActive || isPending) && } ); })} ); }