import { useState } from 'react'; import { router, usePage } from '@inertiajs/react'; import { Check, Languages } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { type SharedData } from '@/types'; import { setLocale } from '@/routes'; export function DashboardLanguageSwitcher() { const page = usePage(); const { locale, supportedLocales, translations } = page.props; const locales = supportedLocales && supportedLocales.length > 0 ? supportedLocales : ['de', 'en']; const activeLocale = locales.includes(locale as string) && typeof locale === 'string' ? locale : locales[0]; const dashboardTranslations = translations && typeof translations.dashboard === 'object' && translations.dashboard !== null ? (translations.dashboard as Record) : null; const languageSwitcherTranslations = dashboardTranslations && typeof dashboardTranslations.language_switcher === 'object' ? (dashboardTranslations.language_switcher as Record) : {}; const languageCopy = languageSwitcherTranslations ?? {}; const label = languageCopy.label ?? 'Sprache'; const changeLabel = languageCopy.change ?? 'Sprache wechseln'; const [pendingLocale, setPendingLocale] = useState(null); const handleChange = (nextLocale: string) => { if (nextLocale === activeLocale || pendingLocale === nextLocale || !locales.includes(nextLocale)) { return; } setPendingLocale(nextLocale); router.post( setLocale().url, { locale: nextLocale }, { preserveScroll: true, preserveState: true, onFinish: () => setPendingLocale(null), }, ); }; return ( {changeLabel} {locales.map((code) => { const isActive = code === activeLocale; const isPending = code === pendingLocale; return ( { event.preventDefault(); handleChange(code); }} disabled={isPending} className="flex items-center justify-between gap-3" > {code} {(isActive || isPending) && } ); })} ); }