idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 18 | import { Stack, Typography } from '@mui/material'; |
idillon | a3c2fad | 2022-12-18 23:49:10 -0500 | [diff] [blame] | 19 | import { useCallback, useContext, useMemo, useState } from 'react'; |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 20 | import { useTranslation } from 'react-i18next'; |
| 21 | |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 22 | import { SettingSelect, SettingSelectProps, SettingsGroup, SettingSwitch } from '../components/Settings'; |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 23 | import { CustomThemeContext } from '../contexts/CustomThemeProvider'; |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 24 | import { languagesInfos, LanguageTag } from '../i18n'; |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 25 | |
| 26 | export default function GeneralPreferences() { |
| 27 | const { t } = useTranslation(); |
| 28 | |
| 29 | return ( |
| 30 | <Stack> |
| 31 | <Typography variant="h2">{t('settings_title_general')}</Typography> |
| 32 | <SettingsGroup label={t('settings_title_system')}> |
| 33 | <SettingTheme /> |
| 34 | <SettingLanguage /> |
| 35 | </SettingsGroup> |
idillon | a3c2fad | 2022-12-18 23:49:10 -0500 | [diff] [blame] | 36 | <SettingsGroup label={t('settings_title_chat')}> |
| 37 | <SettingLinkPreview /> |
| 38 | </SettingsGroup> |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 39 | </Stack> |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | const SettingTheme = () => { |
| 44 | const { t } = useTranslation(); |
| 45 | |
| 46 | const { mode, toggleMode } = useContext(CustomThemeContext); |
| 47 | |
| 48 | return <SettingSwitch label={t('setting_dark_theme')} onChange={toggleMode} checked={mode === 'dark'} />; |
| 49 | }; |
| 50 | |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 51 | const settingLanguageOptions = languagesInfos.map(({ tag, fullName }) => ({ |
| 52 | label: fullName, |
| 53 | payload: tag, |
| 54 | })); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 55 | |
| 56 | const SettingLanguage = () => { |
| 57 | const { t, i18n } = useTranslation(); |
| 58 | |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 59 | const option = useMemo( |
| 60 | // TODO: Tell Typescript the result can't be undefined |
| 61 | () => settingLanguageOptions.find((option) => option.payload === i18n.language), |
| 62 | [i18n.language] |
| 63 | ); |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 64 | |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 65 | const onChange = useCallback<SettingSelectProps<LanguageTag>['onChange']>( |
| 66 | (newValue) => { |
| 67 | if (newValue) { |
| 68 | i18n.changeLanguage(newValue.payload); |
| 69 | } |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 70 | }, |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 71 | [i18n] |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 72 | ); |
| 73 | |
| 74 | return ( |
idillon | 4012a70 | 2022-11-24 13:45:43 -0500 | [diff] [blame] | 75 | <SettingSelect label={t('setting_language')} option={option} options={settingLanguageOptions} onChange={onChange} /> |
idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 76 | ); |
| 77 | }; |
idillon | a3c2fad | 2022-12-18 23:49:10 -0500 | [diff] [blame] | 78 | |
| 79 | const SettingLinkPreview = () => { |
| 80 | const { t } = useTranslation(); |
| 81 | |
| 82 | const [isOn, setIsOn] = useState<boolean>(true); |
| 83 | |
| 84 | return <SettingSwitch label={t('setting_link_preview')} onChange={() => setIsOn((isOn) => !isOn)} checked={isOn} />; |
| 85 | }; |