Files
suwayomi-material-you-webui/src/components/sourceConfiguration/TwoStatePreference.tsx

83 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-09-13 19:18:21 +04:30
/*
* 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/.
*/
2021-09-13 19:18:21 +04:30
import { createElement, useState, useEffect } from 'react';
2021-09-13 19:18:21 +04:30
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import Switch from '@mui/material/Switch';
import Checkbox from '@mui/material/Checkbox';
import { CheckBoxPreferenceProps, SwitchPreferenceCompatProps, TwoStatePreferenceProps } from '@/typings';
2021-09-13 19:18:21 +04:30
function getTwoStateType(type: 'Checkbox' | 'Switch') {
if (type === 'Switch') {
return Switch;
}
2021-09-13 19:18:21 +04:30
return Checkbox;
}
2023-09-25 23:32:26 +02:00
const getTwoStateValues = (
props: TwoStatePreferenceProps,
): {
title: string;
defaultValue: boolean;
currentValue?: boolean | null | undefined;
} => {
if (props.type === 'CheckBoxPreference') {
return {
title: props.CheckBoxTitle,
defaultValue: props.CheckBoxDefault,
currentValue: props.CheckBoxCheckBoxCurrentValue,
};
}
return {
title: props.SwitchPreferenceTitle,
defaultValue: props.SwitchPreferenceDefault,
currentValue: props.SwitchPreferenceCurrentValue,
};
};
2021-09-13 19:18:21 +04:30
function TwoSatePreference(props: TwoStatePreferenceProps) {
2023-09-25 23:32:26 +02:00
const { title, defaultValue, currentValue, summary, updateValue, twoStateType } = {
...props,
...getTwoStateValues(props),
};
const [internalCurrentValue, setInternalCurrentValue] = useState(currentValue ?? defaultValue);
2021-09-13 19:18:21 +04:30
useEffect(() => {
2023-09-25 23:32:26 +02:00
setInternalCurrentValue(currentValue ?? defaultValue);
2021-09-13 19:18:21 +04:30
}, [currentValue]);
return (
<ListItem>
<ListItemText primary={title} secondary={summary} />
<ListItemSecondaryAction>
2023-09-25 23:32:26 +02:00
{createElement(getTwoStateType(twoStateType), {
edge: 'end',
checked: internalCurrentValue,
onChange: () => {
2023-09-25 23:32:26 +02:00
updateValue(twoStateType === 'Switch' ? 'switchState' : 'checkBoxState', !currentValue);
// appear smooth
setInternalCurrentValue(!currentValue);
},
})}
2021-09-13 19:18:21 +04:30
</ListItemSecondaryAction>
</ListItem>
);
}
export function CheckBoxPreference(props: CheckBoxPreferenceProps) {
2023-09-25 23:32:26 +02:00
return <TwoSatePreference {...props} twoStateType="Checkbox" />;
2021-09-13 19:18:21 +04:30
}
2021-09-13 19:18:21 +04:30
export function SwitchPreferenceCompat(props: SwitchPreferenceCompatProps) {
2023-09-25 23:32:26 +02:00
return <TwoSatePreference {...props} twoStateType="Switch" />;
2021-09-13 19:18:21 +04:30
}