Files
suwayomi-material-you-webui/src/modules/core/components/inputs/LanguageSelect.tsx

100 lines
3.7 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-03-08 21:04:42 +03:30
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2021-03-08 21:04:42 +03:30
2025-04-21 18:18:13 +02:00
import { useState } from 'react';
2021-09-09 17:51:22 +04:30
import Button from '@mui/material/Button';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import Dialog from '@mui/material/Dialog';
import Switch from '@mui/material/Switch';
import IconButton from '@mui/material/IconButton';
import FilterListIcon from '@mui/icons-material/FilterList';
import List from '@mui/material/List';
import ListItemText from '@mui/material/ListItemText';
2021-09-09 17:51:22 +04:30
import ListItem from '@mui/material/ListItem';
import { useTranslation } from 'react-i18next';
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
import { translateExtensionLanguage } from '@/modules/extension/Extensions.utils.ts';
2021-03-08 21:04:42 +03:30
interface IProps {
2025-04-21 18:18:13 +02:00
selectedLanguages: string[];
setSelectedLanguages: (languages: string[]) => void;
languages: string[];
2021-03-08 21:04:42 +03:30
}
2025-04-21 18:18:13 +02:00
export function LanguageSelect(props: IProps) {
const { t } = useTranslation();
2025-04-21 18:18:13 +02:00
const { selectedLanguages, setSelectedLanguages, languages } = props;
const [tmpSelectedLanguages, setTmpSelectedLanguages] = useState(selectedLanguages);
2021-03-08 21:04:42 +03:30
const [open, setOpen] = useState<boolean>(false);
const handleCancel = () => {
setOpen(false);
};
const handleOk = () => {
setOpen(false);
2025-04-21 18:18:13 +02:00
setSelectedLanguages(tmpSelectedLanguages);
2021-03-08 21:04:42 +03:30
};
2025-04-21 18:18:13 +02:00
const handleChange = (language: string, selected: boolean) => {
if (selected) {
setTmpSelectedLanguages([...tmpSelectedLanguages, language]);
2021-03-08 21:04:42 +03:30
} else {
2025-04-21 18:18:13 +02:00
setTmpSelectedLanguages(tmpSelectedLanguages.toSpliced(tmpSelectedLanguages.indexOf(language), 1));
2021-03-08 21:04:42 +03:30
}
};
return (
<>
<CustomTooltip title={t('settings.title')}>
<IconButton onClick={() => setOpen(true)} aria-label="display more actions" edge="end" color="inherit">
2023-11-05 17:22:29 +01:00
<FilterListIcon />
</IconButton>
</CustomTooltip>
2021-03-08 21:04:42 +03:30
<Dialog
2021-11-17 14:28:23 +03:30
sx={{
'.MuiDialog-paper': {
maxHeight: 435,
width: '80%',
},
}}
2021-03-08 21:04:42 +03:30
maxWidth="xs"
open={open}
onClose={handleCancel}
2021-03-08 21:04:42 +03:30
>
<DialogTitle>{t('global.language.title.enabled_languages')}</DialogTitle>
2021-11-17 14:28:23 +03:30
<DialogContent dividers sx={{ padding: 0 }}>
2021-03-09 16:44:09 +03:30
<List>
2025-04-21 18:18:13 +02:00
{languages.map((language) => (
<ListItem key={language}>
<ListItemText primary={translateExtensionLanguage(language)} />
2021-03-09 16:44:09 +03:30
<Switch
2025-04-21 18:18:13 +02:00
checked={tmpSelectedLanguages.includes(language)}
onChange={(e) => handleChange(language, e.target.checked)}
/>
2021-03-09 16:44:09 +03:30
</ListItem>
2021-03-08 21:04:42 +03:30
))}
2021-03-09 16:44:09 +03:30
</List>
2021-03-08 21:04:42 +03:30
</DialogContent>
<DialogActions>
<Button autoFocus onClick={handleCancel} color="primary">
{t('global.button.cancel')}
2021-03-08 21:04:42 +03:30
</Button>
<Button onClick={handleOk} color="primary">
{t('global.button.ok')}
2021-03-08 21:04:42 +03:30
</Button>
</DialogActions>
</Dialog>
</>
);
}