blob: 90cfec448051efa161a6c8718947108d83f08fa2 [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';
idillona3c2fad2022-12-18 23:49:10 -050019import { useCallback, useContext, useMemo, useState } 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>
idillona3c2fad2022-12-18 23:49:10 -050036 <SettingsGroup label={t('settings_title_chat')}>
37 <SettingLinkPreview />
38 </SettingsGroup>
idillon3470d072022-11-22 15:22:34 -050039 </Stack>
40 );
41}
42
43const 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
idillon4012a702022-11-24 13:45:43 -050051const settingLanguageOptions = languagesInfos.map(({ tag, fullName }) => ({
52 label: fullName,
53 payload: tag,
54}));
idillon3470d072022-11-22 15:22:34 -050055
56const SettingLanguage = () => {
57 const { t, i18n } = useTranslation();
58
idillon4012a702022-11-24 13:45:43 -050059 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 );
idillon3470d072022-11-22 15:22:34 -050064
idillon4012a702022-11-24 13:45:43 -050065 const onChange = useCallback<SettingSelectProps<LanguageTag>['onChange']>(
66 (newValue) => {
67 if (newValue) {
68 i18n.changeLanguage(newValue.payload);
69 }
idillon3470d072022-11-22 15:22:34 -050070 },
idillon4012a702022-11-24 13:45:43 -050071 [i18n]
idillon3470d072022-11-22 15:22:34 -050072 );
73
74 return (
idillon4012a702022-11-24 13:45:43 -050075 <SettingSelect label={t('setting_language')} option={option} options={settingLanguageOptions} onChange={onChange} />
idillon3470d072022-11-22 15:22:34 -050076 );
77};
idillona3c2fad2022-12-18 23:49:10 -050078
79const 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};