import { useState, useEffect } from 'react' import { setLocale, getLocale, type Locale } from '@/lib/i18n' import { t } from '@/lib/i18n' import { localeStorage } from '@/lib/localeStorage' interface LocaleOptionProps { locale: Locale label: string currentLocale: Locale onClick: (locale: Locale) => void } function LocaleOption({ locale, label, currentLocale, onClick }: LocaleOptionProps): React.ReactElement { const isActive = currentLocale === locale return ( onClick(locale)} className={`px-4 py-2 rounded-lg font-medium transition-colors border ${ isActive ? 'bg-neon-cyan/20 text-neon-cyan border-neon-cyan/50' : 'bg-cyber-darker text-cyber-accent hover:text-neon-cyan border-neon-cyan/30 hover:border-neon-cyan/50' }`} > {label} ) } export function LanguageSettingsManager(): React.ReactElement { const [currentLocale, setCurrentLocale] = useState(getLocale()) const [loading, setLoading] = useState(true) useEffect(() => { void loadLocaleIntoState({ setCurrentLocale, setLoading }) }, []) const onLocaleClick = (locale: Locale): void => { void applyLocaleChange({ locale, setCurrentLocale }) } return } function LanguageSettingsPanel(params: { loading: boolean currentLocale: Locale onLocaleClick: (locale: Locale) => void }): React.ReactElement { if (params.loading) { return ( {t('settings.language.loading')} ) } return ( {t('settings.language.title')} {t('settings.language.description')} ) } async function loadLocaleIntoState(params: { setCurrentLocale: (locale: Locale) => void setLoading: (loading: boolean) => void }): Promise { try { await localeStorage.migrateFromLocalStorage() const savedLocale = await localeStorage.getLocale() if (savedLocale) { setLocale(savedLocale) params.setCurrentLocale(savedLocale) } } catch (e) { console.error('Error loading locale:', e) } finally { params.setLoading(false) } } async function applyLocaleChange(params: { locale: Locale; setCurrentLocale: (locale: Locale) => void }): Promise { setLocale(params.locale) params.setCurrentLocale(params.locale) try { await localeStorage.saveLocale(params.locale) } catch (e) { console.error('Error saving locale:', e) } window.location.reload() }
{t('settings.language.description')}