Persist server settings when disabling them (#462)

Some settings use 0 as the disabled state.
In this case, the previous enabled value was lost.
To prevent this, the last value now gets saved and set again when enabling the setting again
This commit is contained in:
schroda
2023-11-19 00:13:49 +01:00
committed by GitHub
parent 1fd9b4e744
commit 197ee5c94b
3 changed files with 70 additions and 13 deletions

View File

@@ -0,0 +1,31 @@
/*
* 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 { useLocalStorage } from '@/util/useLocalStorage.tsx';
export const getPersistedServerSetting = <T,>(serverValue: T | undefined, lastValue: T): T => {
const isDisabled = serverValue === 0;
if (isDisabled) {
return lastValue;
}
return serverValue ?? lastValue;
};
export const usePersistedValue = <T,>(
key: string,
defaultValue: T,
currentValue: T | undefined,
getCurrentValue: (currentValue: T | undefined, persistedValue: T) => T,
): [T, (value: T) => void] => {
const [persistedValue, setPersistedValue] = useLocalStorage(key, defaultValue);
const value = getCurrentValue(currentValue, persistedValue);
return [value, setPersistedValue];
};