/* * 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 from '@mui/material/ListItem'; import ListItemText from '@mui/material/ListItemText'; import Switch from '@mui/material/Switch'; import ListSubheader from '@mui/material/ListSubheader'; import { TextSetting } from '@/components/settings/text/TextSetting.tsx'; import { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx'; import { MetadataDownloadSettings, ServerSettings } from '@/typings.ts'; import { requestManager } from '@/lib/requests/RequestManager.ts'; import { DownloadAheadSetting } from '@/components/settings/downloads/DownloadAheadSetting.tsx'; import { createUpdateMetadataServerSettings, useMetadataServerSettings, } from '@/lib/metadata/metadataServerSettings.ts'; import { makeToast } from '@/components/util/Toast.tsx'; import { DeleteChaptersWhileReadingSetting } from '@/components/settings/downloads/DeleteChaptersWhileReadingSetting.tsx'; import { CategoriesInclusionSetting } from '@/components/settings/CategoriesInclusionSetting.tsx'; import { NumberSetting } from '@/components/settings/NumberSetting.tsx'; import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder.tsx'; import { EmptyView } from '@/components/util/EmptyView.tsx'; import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts'; type DownloadSettingsType = Pick< ServerSettings, | 'downloadAsCbz' | 'downloadsPath' | 'autoDownloadNewChapters' | 'autoDownloadNewChaptersLimit' | 'excludeEntryWithUnreadChapters' | 'autoDownloadIgnoreReUploads' >; const extractDownloadSettings = (settings: ServerSettings): DownloadSettingsType => ({ downloadAsCbz: settings.downloadAsCbz, downloadsPath: settings.downloadsPath, autoDownloadNewChapters: settings.autoDownloadNewChapters, autoDownloadNewChaptersLimit: settings.autoDownloadNewChaptersLimit, excludeEntryWithUnreadChapters: settings.excludeEntryWithUnreadChapters, autoDownloadIgnoreReUploads: settings.autoDownloadIgnoreReUploads, }); export const DownloadSettings = () => { const { t } = useTranslation(); const { setTitle, setAction } = useContext(NavBarContext); useSetDefaultBackTo('settings'); useEffect(() => { setTitle(t('download.settings.title')); setAction(null); return () => { setTitle(''); setAction(null); }; }, [t]); const { data, loading, error, refetch } = requestManager.useGetServerSettings({ notifyOnNetworkStatusChange: true, }); const downloadSettings = data ? extractDownloadSettings(data.settings) : undefined; const [mutateSettings] = requestManager.useUpdateServerSettings(); const { settings: metadataSettings } = useMetadataServerSettings(); const updateSetting = ( setting: Setting, value: DownloadSettingsType[Setting], ) => { mutateSettings({ variables: { input: { settings: { [setting]: value } } } }).catch(() => makeToast(t('global.error.label.failed_to_save_changes'), 'error'), ); }; const updateMetadataSetting = createUpdateMetadataServerSettings(() => makeToast(t('global.error.label.failed_to_save_changes'), 'error'), ); if (loading) { return ; } if (error) { return ( refetch().catch(defaultPromiseErrorHandler('DownloadSettings::refetch'))} /> ); } return ( updateSetting('downloadsPath', path)} /> updateSetting('downloadAsCbz', e.target.checked)} /> {t('download.settings.delete_chapters.title')} } > updateMetadataSetting('deleteChaptersManuallyMarkedRead', e.target.checked)} /> updateMetadataSetting('deleteChaptersWhileReading', chapterToDelete) } /> updateMetadataSetting('deleteChaptersWithBookmark', e.target.checked)} /> {t('download.settings.auto_download.title')} } > updateSetting('autoDownloadNewChapters', e.target.checked)} /> updateSetting('autoDownloadNewChaptersLimit', autoDownloadNewChaptersLimit) } /> updateSetting('excludeEntryWithUnreadChapters', e.target.checked)} disabled={!downloadSettings?.autoDownloadNewChapters} /> updateSetting('autoDownloadIgnoreReUploads', e.target.checked)} disabled={!downloadSettings?.autoDownloadNewChapters} /> {t('download.settings.download_ahead.title')} } > ); };