/* * 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, Trans } from 'react-i18next'; import { useContext, useEffect } from 'react'; import Link from '@mui/material/Link'; 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 { NavBarContext, useSetDefaultBackTo } from '@/components/context/NavbarContext.tsx'; import { requestManager } from '@/lib/requests/RequestManager.ts'; import { useLocalStorage } from '@/util/useStorage.tsx'; import { TextSetting } from '@/components/settings/text/TextSetting.tsx'; import { ServerSettings as GqlServerSettings } from '@/typings.ts'; import { NumberSetting } from '@/components/settings/NumberSetting.tsx'; import { SelectSetting } from '@/components/settings/SelectSetting.tsx'; type ServerSettingsType = Pick< GqlServerSettings, | 'ip' | 'port' | 'socksProxyEnabled' | 'socksProxyVersion' | 'socksProxyHost' | 'socksProxyPort' | 'socksProxyUsername' | 'socksProxyPassword' | 'debugLogsEnabled' | 'gqlDebugLogsEnabled' | 'systemTrayEnabled' | 'basicAuthEnabled' | 'basicAuthUsername' | 'basicAuthPassword' | 'flareSolverrEnabled' | 'flareSolverrTimeout' | 'flareSolverrUrl' | 'flareSolverrSessionName' | 'flareSolverrSessionTtl' >; const extractServerSettings = (settings: GqlServerSettings): ServerSettingsType => ({ ip: settings.ip, port: settings.port, socksProxyEnabled: settings.socksProxyEnabled, socksProxyVersion: settings.socksProxyVersion, socksProxyHost: settings.socksProxyHost, socksProxyPort: settings.socksProxyPort, socksProxyUsername: settings.socksProxyUsername, socksProxyPassword: settings.socksProxyPassword, debugLogsEnabled: settings.debugLogsEnabled, gqlDebugLogsEnabled: settings.gqlDebugLogsEnabled, systemTrayEnabled: settings.systemTrayEnabled, basicAuthEnabled: settings.basicAuthEnabled, basicAuthUsername: settings.basicAuthUsername, basicAuthPassword: settings.basicAuthPassword, flareSolverrEnabled: settings.flareSolverrEnabled, flareSolverrTimeout: settings.flareSolverrTimeout, flareSolverrUrl: settings.flareSolverrUrl, flareSolverrSessionName: settings.flareSolverrSessionName, flareSolverrSessionTtl: settings.flareSolverrSessionTtl, }); export const ServerSettings = () => { const { t } = useTranslation(); const { setTitle, setAction } = useContext(NavBarContext); useSetDefaultBackTo('settings'); useEffect(() => { setTitle(t('settings.server.title.settings')); setAction(null); return () => { setTitle(''); setAction(null); }; }, [t]); const { data } = requestManager.useGetServerSettings(); const serverSettings = data ? extractServerSettings(data.settings) : undefined; const [mutateSettings] = requestManager.useUpdateServerSettings(); const [serverAddress, setServerAddress] = useLocalStorage('serverBaseURL', window.location.origin); const handleServerAddressChange = (address: string) => { const serverBaseUrl = address.replaceAll(/(\/)+$/g, ''); setServerAddress(serverBaseUrl); requestManager.reset(); }; const updateSetting = ( setting: Setting, value: ServerSettingsType[Setting], ) => { mutateSettings({ variables: { input: { settings: { [setting]: value } } } }); }; return ( {t('global.label.client')} } > {t('settings.server.address.server.title')} } > updateSetting('ip', ip)} value={serverSettings?.ip} placeholder="0.0.0.0" /> updateSetting('port', port)} value={serverSettings?.port ?? 4567} defaultValue={4567} valueUnit={t('settings.server.address.server.label.port')} /> {t('settings.server.socks_proxy.title')} } > updateSetting('socksProxyEnabled', e.target.checked)} /> settingName={t('settings.server.socks_proxy.label.version')} value={serverSettings?.socksProxyVersion} defaultValue={5} values={[ [4, { text: '4' }], [5, { text: '5' }], ]} handleChange={(socksProxyVersion) => updateSetting('socksProxyVersion', socksProxyVersion)} /> updateSetting('socksProxyHost', proxyHost)} /> updateSetting('socksProxyPort', proxyPort)} /> updateSetting('socksProxyUsername', proxyUsername)} /> updateSetting('socksProxyPassword', proxyPassword)} isPassword /> {t('settings.server.auth.title')} } > updateSetting('basicAuthEnabled', e.target.checked)} /> updateSetting('basicAuthUsername', authUsername)} /> updateSetting('basicAuthPassword', authPassword)} /> {t('settings.server.cloudflare.title')} } > See{' '} FlareSolverr {' '} for information on how to set it up } /> updateSetting('flareSolverrEnabled', e.target.checked)} /> updateSetting('flareSolverrUrl', url)} /> updateSetting('flareSolverrTimeout', timeout)} /> updateSetting('flareSolverrSessionName', sessionName)} /> updateSetting('flareSolverrSessionTtl', sessionTTL)} /> {t('settings.server.misc.title')} } > updateSetting('debugLogsEnabled', e.target.checked)} /> updateSetting('gqlDebugLogsEnabled', e.target.checked)} /> updateSetting('systemTrayEnabled', e.target.checked)} /> ); };