blob: 3c7a2779858ab8988fced6b7373e02364424daab [file] [log] [blame]
idillon3470d072022-11-22 15:22:34 -05001/*
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 */
18import { SelectChangeEvent, Stack, Typography } from '@mui/material';
19import { useCallback, useContext, useEffect, useState } from 'react';
20import { useTranslation } from 'react-i18next';
21
22import { SettingSelect, SettingsGroup, SettingSwitch } from '../components/Settings';
23import { CustomThemeContext } from '../contexts/CustomThemeProvider';
24import { availableLanguages } from '../i18n';
25
26export 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>
36 </Stack>
37 );
38}
39
40const SettingTheme = () => {
41 const { t } = useTranslation();
42
43 const { mode, toggleMode } = useContext(CustomThemeContext);
44
45 return <SettingSwitch label={t('setting_dark_theme')} onChange={toggleMode} checked={mode === 'dark'} />;
46};
47
48const settingLanguageOptions = availableLanguages.map(({ tag, fullName }) => ({ label: fullName, value: tag }));
49
50const SettingLanguage = () => {
51 const { t, i18n } = useTranslation();
52
53 const [languageValue, setLanguageValue] = useState(i18n.language);
54
55 useEffect(() => {
56 i18n.changeLanguage(languageValue);
57 }, [languageValue, i18n]);
58
59 const onChange = useCallback(
60 (event: SelectChangeEvent<unknown>) => {
61 setLanguageValue(event.target.value as string);
62 },
63 [setLanguageValue]
64 );
65
66 return (
67 <SettingSelect
68 label={t('setting_language')}
69 value={languageValue}
70 onChange={onChange}
71 options={settingLanguageOptions}
72 />
73 );
74};