import axios from 'axios' import Dialog from '@material-ui/core/Dialog' import Button from '@material-ui/core/Button' import Checkbox from '@material-ui/core/Checkbox' import { FormControlLabel, useMediaQuery, useTheme } from '@material-ui/core' import { settingsHost } from 'utils/Hosts' import { useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import AppBar from '@material-ui/core/AppBar' import Tabs from '@material-ui/core/Tabs' import Tab from '@material-ui/core/Tab' import SwipeableViews from 'react-swipeable-views' import CircularProgress from '@material-ui/core/CircularProgress' import { SettingsHeader, FooterSection, Content } from './style' import defaultSettings from './defaultSettings' import { a11yProps, TabPanel } from './tabComponents' import PrimarySettingsComponent from './PrimarySettingsComponent' import SecondarySettingsComponent from './SecondarySettingsComponent' export default function SettingsDialog({ handleClose }) { const { t } = useTranslation() const fullScreen = useMediaQuery('@media (max-width:930px)') const { direction } = useTheme() const [settings, setSettings] = useState() const [selectedTab, setSelectedTab] = useState(0) const [cacheSize, setCacheSize] = useState(32) const [cachePercentage, setCachePercentage] = useState(40) const [isProMode, setIsProMode] = useState(JSON.parse(localStorage.getItem('isProMode')) || false) useEffect(() => { axios.post(settingsHost(), { action: 'get' }).then(({ data }) => { setSettings({ ...data, CacheSize: data.CacheSize / (1024 * 1024) }) }) }, []) const handleSave = () => { handleClose() const sets = JSON.parse(JSON.stringify(settings)) sets.CacheSize = cacheSize * 1024 * 1024 sets.ReaderReadAHead = cachePercentage axios.post(settingsHost(), { action: 'set', sets }) } const inputForm = ({ target: { type, value, checked, id } }) => { const sets = JSON.parse(JSON.stringify(settings)) if (type === 'number' || type === 'select-one') { sets[id] = Number(value) } else if (type === 'checkbox') { if ( id === 'DisableTCP' || id === 'DisableUTP' || id === 'DisableUPNP' || id === 'DisableDHT' || id === 'DisablePEX' || id === 'DisableUpload' ) sets[id] = Boolean(!checked) else sets[id] = Boolean(checked) } else if (type === 'url') { sets[id] = value } setSettings(sets) } const { CacheSize, ReaderReadAHead } = settings || {} useEffect(() => { if (!CacheSize || !ReaderReadAHead) return setCacheSize(CacheSize) setCachePercentage(ReaderReadAHead) }, [CacheSize, ReaderReadAHead]) const updateSettings = newProps => setSettings({ ...settings, ...newProps }) const handleChange = (_, newValue) => setSelectedTab(newValue) const handleChangeIndex = index => setSelectedTab(index) return (
{t('SettingsDialog.Settings')}
{ setIsProMode(checked) localStorage.setItem('isProMode', checked) if (!checked) setSelectedTab(0) }} style={{ color: 'white' }} /> } label={t('SettingsDialog.ProMode')} />
{t('SettingsDialog.Tabs.Additional')}
{!isProMode &&
{t('SettingsDialog.Tabs.AdditionalDisabled')}
} } {...a11yProps(1)} />
{settings ? ( <> ) : ( )}
) }