2023-11-20 22:14:24 +01:00
|
|
|
/*
|
|
|
|
|
* 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 { useTranslation } from 'react-i18next';
|
2024-04-14 15:50:12 +02:00
|
|
|
import List from '@mui/material/List';
|
|
|
|
|
import ListItem from '@mui/material/ListItem';
|
|
|
|
|
import ListItemText from '@mui/material/ListItemText';
|
|
|
|
|
import Switch from '@mui/material/Switch';
|
2023-11-20 22:14:24 +01:00
|
|
|
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';
|
2024-04-28 01:00:43 +02:00
|
|
|
import { ServerSettings } from '@/typings.ts';
|
2024-09-26 12:51:46 +02:00
|
|
|
import { makeToast } from '@/components/util/Toast.tsx';
|
2023-11-20 22:14:24 +01:00
|
|
|
|
|
|
|
|
const DEFAULT_VALUE = 23;
|
|
|
|
|
const MIN_VALUE = 1;
|
|
|
|
|
const MAX_VALUE = 23; // 1 month
|
|
|
|
|
|
2024-04-28 01:00:43 +02:00
|
|
|
export const WebUIUpdateIntervalSetting = ({
|
|
|
|
|
disabled = false,
|
|
|
|
|
updateCheckInterval,
|
|
|
|
|
}: {
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
updateCheckInterval: ServerSettings['webUIUpdateCheckInterval'];
|
|
|
|
|
}) => {
|
2023-11-20 22:14:24 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const shouldAutoUpdate = !!updateCheckInterval;
|
|
|
|
|
const [mutateSettings] = requestManager.useUpdateServerSettings();
|
|
|
|
|
const [currentUpdateCheckInterval, persistUpdateCheckInterval] = usePersistedValue(
|
|
|
|
|
'lastUpdateCheckInterval',
|
|
|
|
|
DEFAULT_VALUE,
|
|
|
|
|
updateCheckInterval,
|
|
|
|
|
getPersistedServerSetting,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const updateSetting = useCallback(
|
|
|
|
|
(webUIUpdateCheckInterval: number) => {
|
|
|
|
|
persistUpdateCheckInterval(
|
|
|
|
|
webUIUpdateCheckInterval === 0 ? currentUpdateCheckInterval : webUIUpdateCheckInterval,
|
|
|
|
|
);
|
2024-09-26 12:51:46 +02:00
|
|
|
mutateSettings({ variables: { input: { settings: { webUIUpdateCheckInterval } } } }).catch(() =>
|
|
|
|
|
makeToast(t('global.error.label.failed_to_save_changes'), 'error'),
|
|
|
|
|
);
|
2023-11-20 22:14:24 +01:00
|
|
|
},
|
|
|
|
|
[currentUpdateCheckInterval],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const setDoAutoUpdates = (enable: boolean) => {
|
|
|
|
|
const globalUpdateInterval = enable ? currentUpdateCheckInterval : 0;
|
|
|
|
|
updateSetting(globalUpdateInterval);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<List>
|
2024-09-03 17:15:47 +02:00
|
|
|
<ListItem>
|
2023-11-20 22:14:24 +01:00
|
|
|
<ListItemText primary={t('settings.webui.auto_update.label.title')} />
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
edge="end"
|
|
|
|
|
checked={shouldAutoUpdate}
|
|
|
|
|
onChange={(e) => setDoAutoUpdates(e.target.checked)}
|
|
|
|
|
/>
|
2023-11-20 22:14:24 +01:00
|
|
|
</ListItem>
|
|
|
|
|
<NumberSetting
|
|
|
|
|
settingTitle={t('settings.webui.auto_update.label.interval')}
|
2024-04-28 01:01:36 +02:00
|
|
|
settingValue={t('library.settings.global_update.auto_update.interval.label.value', {
|
|
|
|
|
hours: currentUpdateCheckInterval,
|
|
|
|
|
})}
|
2023-11-20 22:14:24 +01:00
|
|
|
value={currentUpdateCheckInterval}
|
|
|
|
|
minValue={MIN_VALUE}
|
|
|
|
|
maxValue={MAX_VALUE}
|
|
|
|
|
defaultValue={DEFAULT_VALUE}
|
|
|
|
|
showSlider
|
|
|
|
|
valueUnit={t('global.time.hour_short')}
|
|
|
|
|
handleUpdate={updateSetting}
|
|
|
|
|
disabled={disabled || !shouldAutoUpdate}
|
|
|
|
|
/>
|
|
|
|
|
</List>
|
|
|
|
|
);
|
|
|
|
|
};
|