2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* 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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-03-08 21:04:42 +03:30
|
|
|
|
2025-04-21 19:01:52 +02:00
|
|
|
import { useMemo, 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';
|
2024-04-14 15:50:12 +02:00
|
|
|
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';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2024-11-04 01:21:50 +01:00
|
|
|
import { translateExtensionLanguage } from '@/modules/extension/Extensions.utils.ts';
|
2025-04-21 19:01:52 +02:00
|
|
|
import { langSortCmp, toUniqueLanguageCodes } from '@/modules/core/utils/Languages.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) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2025-04-21 18:18:13 +02:00
|
|
|
const { selectedLanguages, setSelectedLanguages, languages } = props;
|
2025-04-21 19:01:52 +02:00
|
|
|
const [tmpSelectedLanguages, setTmpSelectedLanguages] = useState(toUniqueLanguageCodes(selectedLanguages));
|
2021-03-08 21:04:42 +03:30
|
|
|
const [open, setOpen] = useState<boolean>(false);
|
|
|
|
|
|
2025-04-21 19:01:52 +02:00
|
|
|
const languagesSortedBySelectState = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
toUniqueLanguageCodes([...tmpSelectedLanguages.toSorted(langSortCmp), ...languages.toSorted(langSortCmp)]),
|
|
|
|
|
[languages, tmpSelectedLanguages],
|
|
|
|
|
);
|
|
|
|
|
|
2021-03-08 21:04:42 +03:30
|
|
|
const handleCancel = () => {
|
|
|
|
|
setOpen(false);
|
2025-04-21 19:01:52 +02:00
|
|
|
setTmpSelectedLanguages(toUniqueLanguageCodes(selectedLanguages));
|
2021-03-08 21:04:42 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleOk = () => {
|
|
|
|
|
setOpen(false);
|
2025-04-22 10:34:59 +02:00
|
|
|
setSelectedLanguages(toUniqueLanguageCodes(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 (
|
|
|
|
|
<>
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('settings.title')}>
|
2025-03-28 19:05:21 +01:00
|
|
|
<IconButton onClick={() => setOpen(true)} aria-label="display more actions" edge="end" color="inherit">
|
2023-11-05 17:22:29 +01:00
|
|
|
<FilterListIcon />
|
|
|
|
|
</IconButton>
|
2025-02-01 14:24:38 +01:00
|
|
|
</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}
|
2024-11-28 12:36:39 +01:00
|
|
|
onClose={handleCancel}
|
2021-03-08 21:04:42 +03:30
|
|
|
>
|
2023-03-23 13:59:32 +01:00
|
|
|
<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 19:01:52 +02:00
|
|
|
{languagesSortedBySelectState.map((language) => (
|
2025-04-21 18:18:13 +02:00
|
|
|
<ListItem key={language}>
|
|
|
|
|
<ListItemText primary={translateExtensionLanguage(language)} />
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
2025-04-21 18:18:13 +02:00
|
|
|
checked={tmpSelectedLanguages.includes(language)}
|
|
|
|
|
onChange={(e) => handleChange(language, e.target.checked)}
|
2023-12-09 14:16:59 +01:00
|
|
|
/>
|
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">
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.cancel')}
|
2021-03-08 21:04:42 +03:30
|
|
|
</Button>
|
|
|
|
|
<Button onClick={handleOk} color="primary">
|
2023-03-23 13:59:32 +01:00
|
|
|
{t('global.button.ok')}
|
2021-03-08 21:04:42 +03:30
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|