2023-11-20 22:14:24 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
FormControl,
|
|
|
|
|
ListItemText,
|
|
|
|
|
MenuItem,
|
|
|
|
|
Select,
|
|
|
|
|
Stack,
|
|
|
|
|
Typography,
|
|
|
|
|
} from '@mui/material';
|
|
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogActions from '@mui/material/DialogActions';
|
|
|
|
|
import ListItemButton from '@mui/material/ListItemButton';
|
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import DialogContentText from '@mui/material/DialogContentText';
|
|
|
|
|
import InfoIcon from '@mui/icons-material/Info';
|
|
|
|
|
import { TranslationKey } from '@/typings.ts';
|
|
|
|
|
|
|
|
|
|
export type SelectSettingValueDisplayInfo = {
|
2024-02-22 00:25:12 +01:00
|
|
|
text: TranslationKey | string;
|
|
|
|
|
description?: TranslationKey | string;
|
|
|
|
|
disclaimer?: TranslationKey | string;
|
2023-11-20 22:14:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type SelectSettingValue<Value> = [Value: Value, DisplayInfo: SelectSettingValueDisplayInfo];
|
|
|
|
|
|
|
|
|
|
export const SelectSetting = <SettingValue extends string | number>({
|
|
|
|
|
settingName,
|
|
|
|
|
dialogDescription,
|
|
|
|
|
value,
|
|
|
|
|
defaultValue,
|
|
|
|
|
values,
|
|
|
|
|
handleChange,
|
|
|
|
|
disabled = false,
|
|
|
|
|
}: {
|
|
|
|
|
settingName: string;
|
|
|
|
|
dialogDescription?: string;
|
|
|
|
|
value?: SettingValue;
|
|
|
|
|
defaultValue: SettingValue;
|
|
|
|
|
values: SelectSettingValue<SettingValue>[];
|
|
|
|
|
handleChange: (value: SettingValue) => void;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false);
|
|
|
|
|
const [dialogValue, setDialogValue] = useState(value ?? defaultValue);
|
|
|
|
|
|
|
|
|
|
const valueDisplayText = useMemo(() => values.find(([key]) => key === value)?.[1]?.text, [value]);
|
|
|
|
|
const dialogValueDisplayInfo = useMemo(() => values.find(([key]) => key === dialogValue)![1], [dialogValue]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDialogValue(value);
|
|
|
|
|
}, [value]);
|
|
|
|
|
|
|
|
|
|
const closeDialog = (resetValue: boolean = true) => {
|
|
|
|
|
if (resetValue) {
|
|
|
|
|
setDialogValue(value ?? defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsDialogOpen(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateSetting = () => {
|
|
|
|
|
closeDialog(false);
|
|
|
|
|
handleChange(dialogValue);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<ListItemButton disabled={disabled} onClick={() => setIsDialogOpen(true)}>
|
|
|
|
|
<ListItemText
|
|
|
|
|
primary={settingName}
|
2024-02-22 00:25:12 +01:00
|
|
|
secondary={valueDisplayText ? t(valueDisplayText as TranslationKey) : t('global.label.loading')}
|
2023-11-20 22:14:24 +01:00
|
|
|
secondaryTypographyProps={{ style: { display: 'flex', flexDirection: 'column' } }}
|
|
|
|
|
/>
|
|
|
|
|
</ListItemButton>
|
|
|
|
|
|
|
|
|
|
<Dialog open={isDialogOpen} onClose={() => closeDialog()} fullWidth>
|
|
|
|
|
<DialogContent>
|
|
|
|
|
<DialogTitle sx={{ paddingLeft: 0 }}>{settingName}</DialogTitle>
|
|
|
|
|
{!!dialogDescription && (
|
|
|
|
|
<DialogContentText sx={{ paddingBottom: '10px' }}>{dialogDescription}</DialogContentText>
|
|
|
|
|
)}
|
|
|
|
|
{(!!dialogValueDisplayInfo.description || !!dialogValueDisplayInfo.disclaimer) && (
|
|
|
|
|
<DialogContentText sx={{ paddingBottom: '10px' }} component="div">
|
|
|
|
|
{dialogValueDisplayInfo.description && (
|
|
|
|
|
<Typography
|
|
|
|
|
variant="body1"
|
|
|
|
|
sx={{
|
|
|
|
|
whiteSpace: 'pre-line',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-02-22 00:25:12 +01:00
|
|
|
{t(dialogValueDisplayInfo.description as TranslationKey)}
|
2023-11-20 22:14:24 +01:00
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
{dialogValueDisplayInfo.disclaimer && (
|
|
|
|
|
<Stack direction="row" alignItems="center">
|
|
|
|
|
<InfoIcon color="warning" />
|
|
|
|
|
<Typography
|
|
|
|
|
variant="body1"
|
|
|
|
|
sx={{
|
|
|
|
|
marginLeft: '10px',
|
|
|
|
|
marginTop: '5px',
|
|
|
|
|
whiteSpace: 'pre-line',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-02-22 00:25:12 +01:00
|
|
|
{t(dialogValueDisplayInfo.disclaimer as TranslationKey)}
|
2023-11-20 22:14:24 +01:00
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
)}
|
|
|
|
|
</DialogContentText>
|
|
|
|
|
)}
|
|
|
|
|
<FormControl fullWidth>
|
|
|
|
|
<Select
|
|
|
|
|
id="dialog-select"
|
|
|
|
|
value={dialogValue}
|
|
|
|
|
onChange={(e) => setDialogValue(e.target.value as SettingValue)}
|
|
|
|
|
>
|
|
|
|
|
{values.map(([selectValue, { text: selectText }]) => (
|
|
|
|
|
<MenuItem key={selectValue} value={selectValue}>
|
2024-02-22 00:25:12 +01:00
|
|
|
{t(selectText as TranslationKey)}
|
2023-11-20 22:14:24 +01:00
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
<Button onClick={() => closeDialog()} color="primary">
|
|
|
|
|
{t('global.button.cancel')}
|
|
|
|
|
</Button>
|
|
|
|
|
<Button onClick={() => updateSetting()} color="primary">
|
|
|
|
|
{t('global.button.ok')}
|
|
|
|
|
</Button>
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</Dialog>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|