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
|
|
|
|
|
|
|
|
import React, { 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-10-05 16:09:34 +02:00
|
|
|
import { cloneObject } from '@/util/cloneObject.tsx';
|
2024-11-04 01:21:50 +01:00
|
|
|
import { translateExtensionLanguage } from '@/modules/extension/Extensions.utils.ts';
|
2021-03-08 21:04:42 +03:30
|
|
|
|
2021-10-18 14:22:33 +03:30
|
|
|
function removeAll(firstList: any[], secondList: any[]) {
|
|
|
|
|
secondList.forEach((item) => {
|
|
|
|
|
const index = firstList.indexOf(item);
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
firstList.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return firstList;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 21:04:42 +03:30
|
|
|
interface IProps {
|
2023-02-06 10:06:33 +01:00
|
|
|
shownLangs: string[];
|
|
|
|
|
setShownLangs: (arg0: string[]) => void;
|
|
|
|
|
allLangs: string[];
|
|
|
|
|
forcedLangs?: string[];
|
2021-03-08 21:04:42 +03:30
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function LangSelect(props: IProps) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2024-07-11 18:13:51 +02:00
|
|
|
const { shownLangs, setShownLangs, allLangs, forcedLangs = [] } = props;
|
2021-03-08 21:04:42 +03:30
|
|
|
// hold a copy and only sate state on parent when OK pressed, improves performance
|
2024-07-11 18:13:51 +02:00
|
|
|
const [mShownLangs, setMShownLangs] = useState(removeAll(cloneObject(shownLangs), forcedLangs));
|
2021-03-08 21:04:42 +03:30
|
|
|
const [open, setOpen] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
|
setOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleOk = () => {
|
|
|
|
|
setOpen(false);
|
|
|
|
|
setShownLangs(mShownLangs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleChange = (event: React.ChangeEvent<HTMLInputElement>, lang: string) => {
|
|
|
|
|
const { checked } = event.target as HTMLInputElement;
|
|
|
|
|
|
|
|
|
|
if (checked) {
|
|
|
|
|
setMShownLangs([...mShownLangs, lang]);
|
|
|
|
|
} else {
|
2021-05-17 01:38:59 +04:30
|
|
|
const clone = cloneObject(mShownLangs);
|
2021-03-08 21:04:42 +03:30
|
|
|
clone.splice(clone.indexOf(lang), 1);
|
|
|
|
|
setMShownLangs(clone);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2025-02-01 14:24:38 +01:00
|
|
|
<CustomTooltip title={t('settings.title')}>
|
2023-11-05 17:22:29 +01:00
|
|
|
<IconButton
|
|
|
|
|
onClick={() => setOpen(true)}
|
|
|
|
|
aria-label="display more actions"
|
|
|
|
|
edge="end"
|
|
|
|
|
color="inherit"
|
|
|
|
|
size="large"
|
|
|
|
|
>
|
|
|
|
|
<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>
|
2021-03-08 21:04:42 +03:30
|
|
|
{allLangs.map((lang) => (
|
2021-03-09 16:44:09 +03:30
|
|
|
<ListItem key={lang}>
|
2023-04-21 00:27:42 +02:00
|
|
|
<ListItemText primary={translateExtensionLanguage(lang)} />
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
checked={mShownLangs.indexOf(lang) !== -1}
|
|
|
|
|
onChange={(e) => handleChange(e, lang)}
|
|
|
|
|
/>
|
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>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|