Skip to content

Commit

Permalink
Persist server settings when disabling them (#462)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schroda authored Nov 18, 2023
1 parent 1fd9b4e commit 197ee5c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
27 changes: 20 additions & 7 deletions src/components/settings/downloads/DownloadAheadSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import { useCallback } from 'react';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { NumberSetting } from '@/components/settings/NumberSetting.tsx';
import { getPersistedServerSetting, usePersistedValue } from '@/util/usePersistedValue.tsx';

const DEFAULT_LIMIT = 5;
const MIN_LIMIT = 2;
Expand All @@ -24,13 +25,25 @@ export const DownloadAheadSetting = () => {
const downloadAheadLimit = data?.settings.autoDownloadAheadLimit;
const shouldDownloadAhead = !!downloadAheadLimit;
const [mutateSettings] = requestManager.useUpdateServerSettings();
const [currentDownloadAheadLimit, persistDownloadAheadLimit] = usePersistedValue(
'lastDownloadAheadLimit',
DEFAULT_LIMIT,
downloadAheadLimit,
getPersistedServerSetting,
);

const updateSetting = useCallback((autoDownloadAheadLimit: number) => {
mutateSettings({ variables: { input: { settings: { autoDownloadAheadLimit } } } });
}, []);
const updateSetting = useCallback(
(autoDownloadAheadLimit: number) => {
persistDownloadAheadLimit(
autoDownloadAheadLimit === 0 ? currentDownloadAheadLimit : autoDownloadAheadLimit,
);
mutateSettings({ variables: { input: { settings: { autoDownloadAheadLimit } } } });
},
[currentDownloadAheadLimit],
);

const setDoAutoUpdates = (enable: boolean) => {
const globalUpdateInterval = enable ? DEFAULT_LIMIT : 0;
const globalUpdateInterval = enable ? currentDownloadAheadLimit : 0;
updateSetting(globalUpdateInterval);
};

Expand All @@ -51,12 +64,12 @@ export const DownloadAheadSetting = () => {
settingValue={
downloadAheadLimit !== undefined
? t('download.settings.download_ahead.label.value', {
chapters: downloadAheadLimit,
count: downloadAheadLimit,
chapters: currentDownloadAheadLimit,
count: currentDownloadAheadLimit,
})
: undefined
}
value={downloadAheadLimit ?? DEFAULT_LIMIT}
value={currentDownloadAheadLimit}
minValue={MIN_LIMIT}
maxValue={MAX_LIMIT}
defaultValue={DEFAULT_LIMIT}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ListItemSecondaryAction from '@mui/material/ListItemSecondaryAction';
import { useCallback } from 'react';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { NumberSetting } from '@/components/settings/NumberSetting.tsx';
import { getPersistedServerSetting, usePersistedValue } from '@/util/usePersistedValue.tsx';

const DEFAULT_INTERVAL_HOURS = 12;
const MIN_INTERVAL_HOURS = 6;
Expand All @@ -24,13 +25,25 @@ export const GlobalUpdateSettingsInterval = () => {
const autoUpdateIntervalHours = data?.settings.globalUpdateInterval;
const doAutoUpdates = !!autoUpdateIntervalHours;
const [mutateSettings] = requestManager.useUpdateServerSettings();
const [currentAutoUpdateIntervalHours, persistAutoUpdateIntervalHours] = usePersistedValue(
'lastGlobalUpdateInterval',
DEFAULT_INTERVAL_HOURS,
autoUpdateIntervalHours,
getPersistedServerSetting,
);

const updateSetting = useCallback((globalUpdateInterval: number) => {
mutateSettings({ variables: { input: { settings: { globalUpdateInterval } } } });
}, []);
const updateSetting = useCallback(
(globalUpdateInterval: number) => {
persistAutoUpdateIntervalHours(
globalUpdateInterval === 0 ? currentAutoUpdateIntervalHours : globalUpdateInterval,
);
mutateSettings({ variables: { input: { settings: { globalUpdateInterval } } } });
},
[currentAutoUpdateIntervalHours],
);

const setDoAutoUpdates = (enable: boolean) => {
const globalUpdateInterval = enable ? DEFAULT_INTERVAL_HOURS : 0;
const globalUpdateInterval = enable ? currentAutoUpdateIntervalHours : 0;
updateSetting(globalUpdateInterval);
};

Expand All @@ -47,11 +60,11 @@ export const GlobalUpdateSettingsInterval = () => {
settingValue={
autoUpdateIntervalHours !== undefined
? t('library.settings.global_update.auto_update.interval.label.value', {
hours: autoUpdateIntervalHours,
hours: currentAutoUpdateIntervalHours,
})
: undefined
}
value={autoUpdateIntervalHours ?? DEFAULT_INTERVAL_HOURS}
value={currentAutoUpdateIntervalHours}
minValue={MIN_INTERVAL_HOURS}
maxValue={MAX_INTERVAL_HOURS}
defaultValue={DEFAULT_INTERVAL_HOURS}
Expand Down
31 changes: 31 additions & 0 deletions src/util/usePersistedValue.tsx
Original file line number Diff line number Diff line change
@@ -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];
};

0 comments on commit 197ee5c

Please sign in to comment.