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

124 lines
4.2 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
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';
import List from '@mui/material/List';
import ListItemText from '@mui/material/ListItemText';
import Tooltip from '@mui/material/Tooltip';
2021-09-09 17:51:22 +04:30
import ListItem from '@mui/material/ListItem';
import { useTranslation } from 'react-i18next';
2024-10-05 16:09:34 +02:00
import { cloneObject } from '@/util/cloneObject.tsx';
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 {
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) {
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 {
const clone = cloneObject(mShownLangs);
2021-03-08 21:04:42 +03:30
clone.splice(clone.indexOf(lang), 1);
setMShownLangs(clone);
}
};
return (
<>
2023-11-05 17:22:29 +01:00
<Tooltip title={t('settings.title')}>
<IconButton
onClick={() => setOpen(true)}
aria-label="display more actions"
edge="end"
color="inherit"
size="large"
>
<FilterListIcon />
</IconButton>
</Tooltip>
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>
2021-03-08 21:04:42 +03:30
{allLangs.map((lang) => (
2021-03-09 16:44:09 +03:30
<ListItem key={lang}>
<ListItemText primary={translateExtensionLanguage(lang)} />
2021-03-09 16:44:09 +03:30
<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">
{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>
</>
);
}