import { Group, Select, Stack, Text } from '@mantine/core'; import { showNotification } from '@mantine/notifications'; import getUnicodeFlagIcon from 'country-flag-icons/unicode'; import { forwardRef, useState } from 'react'; import { useTranslation } from 'next-i18next'; import { useRouter } from 'next/router'; import { getCookie, setCookie } from 'cookies-next'; import { getLanguageByCode, Language } from '../../languages/language'; export default function LanguageSwitch() { const { t, i18n } = useTranslation('settings/general/internationalization'); const { changeLanguage } = i18n; const configLocale = getCookie('config-locale'); const { locale, locales } = useRouter(); const [selectedLanguage, setSelectedLanguage] = useState( (configLocale as string) ?? locale ); const data = locales ? locales.map((localeItem) => ({ value: localeItem, label: getLanguageByCode(localeItem).originalName, icon: getUnicodeFlagIcon(localeItem), language: getLanguageByCode(localeItem), })) : []; const onChangeSelect = (value: string) => { setSelectedLanguage(value); const newLanguage = getLanguageByCode(value); changeLanguage(value) .then(() => { setCookie('config-locale', value, { maxAge: 60 * 60 * 24 * 30, sameSite: 'strict', }); showNotification({ title: 'Language changed', message: `You changed the language to '${newLanguage.originalName}'`, color: 'green', autoClose: 5000, }); }) .catch((err) => { showNotification({ title: 'Failed to change language', message: `Failed to change to '${newLanguage.originalName}', Error:'${err}`, color: 'red', autoClose: 5000, }); }); }; return (