import axios from 'axios' import Button from '@material-ui/core/Button' import Switch from '@material-ui/core/Switch' 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 { StyledDialog } from 'style/CustomMaterialUiStyles' import useOnStandaloneAppOutsideClick from 'utils/useOnStandaloneAppOutsideClick' import { isStandaloneApp } from 'utils/Utils' import { SettingsHeader, FooterSection, Content } from './style' import defaultSettings from './defaultSettings' import { a11yProps, TabPanel } from './tabComponents' import PrimarySettingsComponent from './PrimarySettingsComponent' import SecondarySettingsComponent from './SecondarySettingsComponent' import MobileAppSettings from './MobileAppSettings' 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 [preloadCachePercentage, setPreloadCachePercentage] = useState(0) const [isProMode, setIsProMode] = useState(JSON.parse(localStorage.getItem('isProMode')) || false) const [isVlcUsed, setIsVlcUsed] = useState(JSON.parse(localStorage.getItem('isVlcUsed')) ?? false) useEffect(() => { axios.post(settingsHost(), { action: 'get' }).then(({ data }) => { setSettings({ ...data, CacheSize: data.CacheSize / (1024 * 1024) }) }) }, []) const ref = useOnStandaloneAppOutsideClick(handleClose) const handleSave = () => { handleClose() const sets = JSON.parse(JSON.stringify(settings)) sets.CacheSize = cacheSize * 1024 * 1024 sets.ReaderReadAHead = cachePercentage sets.PreloadCache = preloadCachePercentage axios.post(settingsHost(), { action: 'set', sets }) localStorage.setItem('isVlcUsed', isVlcUsed) } 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' || type === 'text') { sets[id] = value } setSettings(sets) } const { CacheSize, ReaderReadAHead, PreloadCache } = settings || {} useEffect(() => { if (isNaN(CacheSize) || isNaN(ReaderReadAHead) || isNaN(PreloadCache)) return setCacheSize(CacheSize) setCachePercentage(ReaderReadAHead) setPreloadCachePercentage(PreloadCache) }, [CacheSize, ReaderReadAHead, PreloadCache]) 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')} labelPlacement='start' />
{t('SettingsDialog.Tabs.Additional')}
{!isProMode &&
{t('SettingsDialog.Tabs.AdditionalDisabled')}
} } {...a11yProps(1)} /> {isStandaloneApp && }
{settings ? ( <> {isStandaloneApp && ( )} ) : ( )}
) }