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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-09-13 19:18:21 +04:30
|
|
|
|
2024-03-09 00:24:48 +01:00
|
|
|
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
|
|
|
|
2024-03-09 00:24:48 +01:00
|
|
|
function getTwoStateType(type: TwoStatePreferenceProps['twoStateType']) {
|
2023-02-06 10:06:33 +01:00
|
|
|
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,
|
|
|
|
|
): {
|
2025-03-16 21:01:24 +01:00
|
|
|
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]);
|
|
|
|
|
|
2024-03-09 00:24:48 +01:00
|
|
|
const TwoStateComponent = useMemo(() => getTwoStateType(twoStateType), [twoStateType]);
|
|
|
|
|
|
2021-09-13 19:18:21 +04:30
|
|
|
return (
|
|
|
|
|
<ListItem>
|
|
|
|
|
<ListItemText primary={title} secondary={summary} />
|
2024-03-09 00:24:48 +01:00
|
|
|
<TwoStateComponent
|
|
|
|
|
{...{
|
|
|
|
|
edge: 'end',
|
|
|
|
|
checked: internalCurrentValue,
|
|
|
|
|
onChange: () => {
|
|
|
|
|
updateValue(twoStateType === 'Switch' ? 'switchState' : 'checkBoxState', !currentValue);
|
2023-02-06 10:06:33 +01:00
|
|
|
|
2024-03-09 00:24:48 +01:00
|
|
|
// 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
|
|
|
}
|
2023-02-06 10:06:33 +01:00
|
|
|
|
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
|
|
|
}
|