2023-11-04 14:40:43 +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';
|
|
|
|
|
import { useContext, useEffect } from 'react';
|
|
|
|
|
import List from '@mui/material/List';
|
|
|
|
|
import { ListItem, ListItemText, Switch } from '@mui/material';
|
|
|
|
|
import ListSubheader from '@mui/material/ListSubheader';
|
2024-01-07 16:30:40 +01:00
|
|
|
import { TextSetting } from '@/components/settings/text/TextSetting.tsx';
|
2023-11-04 14:40:43 +01:00
|
|
|
import { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx';
|
2023-11-19 21:17:35 +01:00
|
|
|
import { MetadataServerSettingKeys, MetadataServerSettings, ServerSettings } from '@/typings.ts';
|
2023-11-04 14:40:43 +01:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
|
|
|
|
import { DownloadAheadSetting } from '@/components/settings/downloads/DownloadAheadSetting.tsx';
|
2023-11-19 21:17:35 +01:00
|
|
|
import { useMetadataServerSettings } from '@/util/metadataServerSettings.ts';
|
|
|
|
|
import { convertToGqlMeta, requestUpdateServerMetadata } from '@/util/metadata.ts';
|
|
|
|
|
import { makeToast } from '@/components/util/Toast.tsx';
|
2023-12-05 22:20:13 +01:00
|
|
|
import { DeleteChaptersWhileReadingSetting } from '@/components/settings/downloads/DeleteChaptersWhileReadingSetting.tsx';
|
2023-11-04 14:40:43 +01:00
|
|
|
|
|
|
|
|
type DownloadSettingsType = Pick<
|
|
|
|
|
ServerSettings,
|
|
|
|
|
| 'downloadAsCbz'
|
|
|
|
|
| 'downloadsPath'
|
|
|
|
|
| 'autoDownloadNewChapters'
|
|
|
|
|
| 'autoDownloadAheadLimit'
|
|
|
|
|
| 'excludeEntryWithUnreadChapters'
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
const extractDownloadSettings = (settings: ServerSettings): DownloadSettingsType => ({
|
|
|
|
|
downloadAsCbz: settings.downloadAsCbz,
|
|
|
|
|
downloadsPath: settings.downloadsPath,
|
|
|
|
|
autoDownloadNewChapters: settings.autoDownloadNewChapters,
|
|
|
|
|
autoDownloadAheadLimit: settings.autoDownloadAheadLimit,
|
|
|
|
|
excludeEntryWithUnreadChapters: settings.excludeEntryWithUnreadChapters,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const DownloadSettings = () => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { setTitle, setAction } = useContext(NavBarContext);
|
|
|
|
|
|
|
|
|
|
useSetDefaultBackTo('settings');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setTitle(t('download.settings.title'));
|
|
|
|
|
setAction(null);
|
|
|
|
|
}, [t]);
|
|
|
|
|
|
|
|
|
|
const { data } = requestManager.useGetServerSettings();
|
|
|
|
|
const downloadSettings = data ? extractDownloadSettings(data.settings) : undefined;
|
|
|
|
|
const [mutateSettings] = requestManager.useUpdateServerSettings();
|
2023-11-19 21:17:35 +01:00
|
|
|
const { metadata, settings: metadataSettings } = useMetadataServerSettings();
|
2023-11-04 14:40:43 +01:00
|
|
|
|
|
|
|
|
const updateSetting = <Setting extends keyof DownloadSettingsType>(
|
|
|
|
|
setting: Setting,
|
|
|
|
|
value: DownloadSettingsType[Setting],
|
|
|
|
|
) => {
|
2023-11-19 21:17:35 +01:00
|
|
|
mutateSettings({ variables: { input: { settings: { [setting]: value } } } }).catch(() =>
|
|
|
|
|
makeToast(t('global.error.label.failed_to_save_changes'), 'error'),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateMetadataSetting = <Setting extends MetadataServerSettingKeys>(
|
|
|
|
|
setting: Setting,
|
|
|
|
|
value: MetadataServerSettings[Setting],
|
|
|
|
|
) => {
|
|
|
|
|
if (!metadata) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
requestUpdateServerMetadata(convertToGqlMeta(metadata) ?? [], [[setting, value]]).catch(() =>
|
|
|
|
|
makeToast(t('global.error.label.failed_to_save_changes'), 'error'),
|
|
|
|
|
);
|
2023-11-04 14:40:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<List>
|
2023-11-18 19:55:44 +01:00
|
|
|
<TextSetting
|
2023-11-04 14:40:43 +01:00
|
|
|
settingName={t('download.settings.download_path.label.title')}
|
|
|
|
|
dialogDescription={t('download.settings.download_path.label.description')}
|
2023-11-18 19:55:44 +01:00
|
|
|
value={downloadSettings?.downloadsPath}
|
|
|
|
|
handleChange={(path) => updateSetting('downloadsPath', path)}
|
2023-11-04 14:40:43 +01:00
|
|
|
/>
|
|
|
|
|
<ListItem>
|
|
|
|
|
<ListItemText primary={t('download.settings.file_type.label.cbz')} />
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
edge="end"
|
|
|
|
|
checked={!!downloadSettings?.downloadAsCbz}
|
|
|
|
|
onChange={(e) => updateSetting('downloadAsCbz', e.target.checked)}
|
|
|
|
|
/>
|
2023-11-04 14:40:43 +01:00
|
|
|
</ListItem>
|
2023-11-19 21:17:35 +01:00
|
|
|
<List
|
|
|
|
|
subheader={
|
|
|
|
|
<ListSubheader component="div" id="download-settings-auto-download">
|
2023-12-05 22:20:13 +01:00
|
|
|
{t('download.settings.delete_chapters.title')}
|
2023-11-19 21:17:35 +01:00
|
|
|
</ListSubheader>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<ListItem>
|
2023-12-05 22:20:13 +01:00
|
|
|
<ListItemText primary={t('download.settings.delete_chapters.label.manually_marked_as_read')} />
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
edge="end"
|
|
|
|
|
checked={metadataSettings.deleteChaptersManuallyMarkedRead}
|
|
|
|
|
onChange={(e) => updateMetadataSetting('deleteChaptersManuallyMarkedRead', e.target.checked)}
|
|
|
|
|
/>
|
2023-11-19 21:17:35 +01:00
|
|
|
</ListItem>
|
2023-12-05 22:20:13 +01:00
|
|
|
<DeleteChaptersWhileReadingSetting
|
|
|
|
|
chapterToDelete={metadataSettings.deleteChaptersWhileReading}
|
|
|
|
|
handleChange={(chapterToDelete) =>
|
|
|
|
|
updateMetadataSetting('deleteChaptersWhileReading', chapterToDelete)
|
|
|
|
|
}
|
|
|
|
|
/>
|
2023-11-19 21:17:35 +01:00
|
|
|
<ListItem>
|
2023-12-05 22:20:13 +01:00
|
|
|
<ListItemText primary={t('download.settings.delete_chapters.label.allow_deletion_of_bookmarked')} />
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
edge="end"
|
|
|
|
|
checked={metadataSettings.deleteChaptersWithBookmark}
|
|
|
|
|
onChange={(e) => updateMetadataSetting('deleteChaptersWithBookmark', e.target.checked)}
|
|
|
|
|
/>
|
2023-11-19 21:17:35 +01:00
|
|
|
</ListItem>
|
|
|
|
|
</List>
|
2023-11-04 14:40:43 +01:00
|
|
|
<List
|
|
|
|
|
subheader={
|
|
|
|
|
<ListSubheader component="div" id="download-settings-auto-download">
|
|
|
|
|
{t('download.settings.auto_download.title')}
|
|
|
|
|
</ListSubheader>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<ListItem>
|
|
|
|
|
<ListItemText primary={t('download.settings.auto_download.label.new_chapters')} />
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
edge="end"
|
|
|
|
|
checked={!!downloadSettings?.autoDownloadNewChapters}
|
|
|
|
|
onChange={(e) => updateSetting('autoDownloadNewChapters', e.target.checked)}
|
|
|
|
|
/>
|
2023-11-04 14:40:43 +01:00
|
|
|
</ListItem>
|
2023-11-19 00:10:30 +01:00
|
|
|
<ListItem disabled={!downloadSettings?.autoDownloadNewChapters}>
|
|
|
|
|
<ListItemText primary={t('download.settings.auto_download.label.ignore_with_unread_chapters')} />
|
2023-12-09 14:16:59 +01:00
|
|
|
<Switch
|
|
|
|
|
edge="end"
|
|
|
|
|
checked={!!downloadSettings?.excludeEntryWithUnreadChapters}
|
|
|
|
|
onChange={(e) => updateSetting('excludeEntryWithUnreadChapters', e.target.checked)}
|
|
|
|
|
disabled={!downloadSettings?.autoDownloadNewChapters}
|
|
|
|
|
/>
|
2023-11-19 00:10:30 +01:00
|
|
|
</ListItem>
|
2023-11-04 14:40:43 +01:00
|
|
|
</List>
|
|
|
|
|
<List
|
|
|
|
|
subheader={
|
|
|
|
|
<ListSubheader component="div" id="download-settings-download-ahead">
|
|
|
|
|
{t('download.settings.download_ahead.title')}
|
|
|
|
|
</ListSubheader>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<DownloadAheadSetting />
|
|
|
|
|
</List>
|
|
|
|
|
</List>
|
|
|
|
|
);
|
|
|
|
|
};
|