blob: 6e651bbbafbe58e70a1eb680be7748cc28bcb0b9 [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 */
idillon4012a702022-11-24 13:45:43 -050018import { Stack, Typography } from '@mui/material';
19import { useCallback, useContext, useMemo } from 'react';
idillon3470d072022-11-22 15:22:34 -050020import { useTranslation } from 'react-i18next';
21
idillon4012a702022-11-24 13:45:43 -050022import { SettingSelect, SettingSelectProps, SettingsGroup, SettingSwitch } from '../components/Settings';
idillon3470d072022-11-22 15:22:34 -050023import { CustomThemeContext } from '../contexts/CustomThemeProvider';
idillon4012a702022-11-24 13:45:43 -050024import { languagesInfos, LanguageTag } from '../i18n';
idillon3470d072022-11-22 15:22:34 -050025
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
idillon4012a702022-11-24 13:45:43 -050048const settingLanguageOptions = languagesInfos.map(({ tag, fullName }) => ({
49 label: fullName,
50 payload: tag,
51}));
idillon3470d072022-11-22 15:22:34 -050052
53const SettingLanguage = () => {
54 const { t, i18n } = useTranslation();
55
idillon4012a702022-11-24 13:45:43 -050056 const option = useMemo(
57 // TODO: Tell Typescript the result can't be undefined
58 () => settingLanguageOptions.find((option) => option.payload === i18n.language),
59 [i18n.language]
60 );
idillon3470d072022-11-22 15:22:34 -050061
idillon4012a702022-11-24 13:45:43 -050062 const onChange = useCallback<SettingSelectProps<LanguageTag>['onChange']>(
63 (newValue) => {
64 if (newValue) {
65 i18n.changeLanguage(newValue.payload);
66 }
idillon3470d072022-11-22 15:22:34 -050067 },
idillon4012a702022-11-24 13:45:43 -050068 [i18n]
idillon3470d072022-11-22 15:22:34 -050069 );
70
71 return (
idillon4012a702022-11-24 13:45:43 -050072 <SettingSelect label={t('setting_language')} option={option} options={settingLanguageOptions} onChange={onChange} />
idillon3470d072022-11-22 15:22:34 -050073 );
74};