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

88 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 { useState, useEffect, useMemo } from 'react';
2021-09-13 19:18:21 +04:30
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
import Switch from '@mui/material/Switch';
import Checkbox from '@mui/material/Checkbox';
2024-10-05 21:22:12 +02:00
import {
CheckBoxPreferenceProps,
SwitchPreferenceCompatProps,
TwoStatePreferenceProps,
} from '@/modules/source/Source.types.ts';
2021-09-13 19:18:21 +04:30
function getTwoStateType(type: TwoStatePreferenceProps['twoStateType']) {
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 | null | undefined;
2023-09-25 23:32:26 +02:00
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]);
const TwoStateComponent = useMemo(() => getTwoStateType(twoStateType), [twoStateType]);
2021-09-13 19:18:21 +04:30
return (
<ListItem>
<ListItemText primary={title} secondary={summary} />
<TwoStateComponent
{...{
edge: 'end',
checked: internalCurrentValue,
onChange: () => {
updateValue(twoStateType === 'Switch' ? 'switchState' : 'checkBoxState', !currentValue);
// appear smooth
setInternalCurrentValue(!currentValue);
},
}}
/>
2021-09-13 19:18:21 +04:30
</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
}