import React, { useEffect, useState } from 'react'; import Select, { components, SingleValueProps, OptionProps } from 'react-select'; import { useTranslation } from 'react-i18next'; type LanguageOption = { label: string; value: string }; type Props = { className?: string; menuPlacement?: 'auto' | 'bottom' | 'top'; width?: number; }; const LANGS: LanguageOption[] = [ { value: 'pt', label: '🇵🇹 PT' }, { value: 'en', label: '🇬🇧 EN' }, ]; const Option = (props: OptionProps) => ( {props.data.label} ); const SingleVal = (props: SingleValueProps) => ( {props.data.label} ); const LanguageSwitcher: React.FC = ({ className = '', menuPlacement = 'bottom', width = 88 }) => { const { i18n } = useTranslation(); const [mounted, setMounted] = useState(false); const [selected, setSelected] = useState(LANGS[0]); useEffect(() => { const currentLanguage = i18n.resolvedLanguage || i18n.language || 'pt'; setSelected(LANGS.find((lang) => lang.value === currentLanguage) || LANGS[0]); setMounted(true); }, [i18n.language, i18n.resolvedLanguage]); const handleChange = (opt: LanguageOption | null) => { if (!opt) return; setSelected(opt); i18n.changeLanguage(opt.value); }; if (!mounted) return null; return (