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 ( ) } export function LanguageSettingsManager(): React.ReactElement { const [currentLocale, setCurrentLocale] = useState(getLocale()) const [loading, setLoading] = useState(true) useEffect(() => { void loadLocaleIntoState({ setCurrentLocale, setLoading }) }, []) const handleLocaleChange = async (locale: Locale): Promise => { await applyLocaleChange({ locale, setCurrentLocale }) } const onLocaleClick = (locale: Locale): void => { void handleLocaleChange(locale) } if (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() }