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:
31
src/util/usePersistedValue.tsx
Normal file
31
src/util/usePersistedValue.tsx
Normal 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];
|
||||
};
|
||||
Reference in New Issue
Block a user