/* * 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 { useLayoutEffect } 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 '@/modules/core/components/settings/text/TextSetting.tsx'; import { requestManager } from '@/lib/requests/RequestManager.ts'; import { DownloadAheadSetting } from '@/modules/downloads/components/DownloadAheadSetting.tsx'; import { createUpdateMetadataServerSettings, useMetadataServerSettings, } from '@/modules/settings/services/ServerSettingsMetadata.ts'; import { makeToast } from '@/modules/core/utils/Toast.ts'; import { DeleteChaptersWhileReadingSetting } from '@/modules/downloads/components/DeleteChaptersWhileReadingSetting.tsx'; import { CategoriesInclusionSetting } from '@/modules/category/components/CategoriesInclusionSetting.tsx'; import { NumberSetting } from '@/modules/core/components/settings/NumberSetting.tsx'; import { LoadingPlaceholder } from '@/modules/core/components/feedback/LoadingPlaceholder.tsx'; import { EmptyViewAbsoluteCentered } from '@/modules/core/components/feedback/EmptyViewAbsoluteCentered.tsx'; import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts'; import { GetCategoriesSettingsQuery, GetCategoriesSettingsQueryVariables } from '@/lib/graphql/generated/graphql.ts'; import { GET_CATEGORIES_SETTINGS } from '@/lib/graphql/queries/CategoryQuery.ts'; import { MetadataDownloadSettings } from '@/modules/downloads/Downloads.types.ts'; import { ServerSettings } from '@/modules/settings/Settings.types.ts'; import { getErrorMessage } from '@/lib/HelperFunctions.ts'; import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx'; 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 } = useNavBarContext(); useLayoutEffect(() => { setTitle(t('download.title.download')); setAction(null); return () => { setTitle(''); setAction(null); }; }, [t]); const categories = requestManager.useGetCategories( GET_CATEGORIES_SETTINGS, ); const serverSettings = requestManager.useGetServerSettings({ notifyOnNetworkStatusChange: true }); const [mutateSettings] = requestManager.useUpdateServerSettings(); const { settings: metadataSettings, loading: areMetadataServerSettingsLoading, request: { error: metadataServerSettingsError, refetch: refetchMetadataServerSettings }, } = useMetadataServerSettings(); const loading = serverSettings.loading || areMetadataServerSettingsLoading || categories.loading; if (loading) { return ; } const error = serverSettings.error ?? metadataServerSettingsError ?? categories.error; if (error) { return ( { if (serverSettings.error) { serverSettings .refetch() .catch(defaultPromiseErrorHandler('DownloadSettings::refetchServerSettings')); } if (metadataServerSettingsError) { refetchMetadataServerSettings().catch( defaultPromiseErrorHandler('refetchMetadataServerSettings::'), ); } if (categories.error) { categories.refetch().catch(defaultPromiseErrorHandler('LibrarySettings::refetchCategories')); } }} /> ); } const downloadSettings = extractDownloadSettings(serverSettings.data!.settings); const updateSetting = ( setting: Setting, value: DownloadSettingsType[Setting], ) => { mutateSettings({ variables: { input: { settings: { [setting]: value } } } }).catch((e) => makeToast(t('global.error.label.failed_to_save_changes'), 'error', getErrorMessage(e)), ); }; const updateMetadataSetting = createUpdateMetadataServerSettings((e) => makeToast(t('global.error.label.failed_to_save_changes'), 'error', getErrorMessage(e)), ); 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')} } > ); };