import React, { useState, useEffect } from 'react'; import { styled } from '@mui/system'; import { Alert, AlertTitle, TextField, Button, Switch, FormControlLabel, Box, Typography, Tabs, Tab, Table, TableContainer, TableHead, TableRow, TableBody, TableCell, Paper } from '@mui/material'; import { sendProxyConfig, getProxyConfig, testProxyConfig, deleteProxyConfig } from '../../api/proxy'; import { useGlobalInfoStore } from '../../context/globalInfo'; import { useTranslation } from 'react-i18next'; const FormContainer = styled(Box)({ display: 'flex', flexDirection: 'column', gap: '16px', marginLeft: '30px' }); const FormControl = styled(Box)({ marginBottom: '16px', }); const ProxyForm: React.FC = () => { const { t } = useTranslation(); const [proxyConfigForm, setProxyConfigForm] = useState({ server_url: '', username: '', password: '', }); const [requiresAuth, setRequiresAuth] = useState(false); const [errors, setErrors] = useState({ server_url: '', username: '', password: '', }); const [tabIndex, setTabIndex] = useState(0); const [isProxyConfigured, setIsProxyConfigured] = useState(false); const [proxy, setProxy] = useState({ proxy_url: '', auth: false }); const { notify } = useGlobalInfoStore(); const validateForm = () => { let valid = true; let errorMessages = { server_url: '', username: '', password: '' }; if (!proxyConfigForm.server_url) { errorMessages.server_url = 'Server URL is required'; valid = false; } if (requiresAuth) { if (!proxyConfigForm.username) { errorMessages.username = 'Username is required for authenticated proxies'; valid = false; } if (!proxyConfigForm.password) { errorMessages.password = 'Password is required for authenticated proxies'; valid = false; } } setErrors(errorMessages); return valid; }; const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setProxyConfigForm({ ...proxyConfigForm, [name]: value }); }; const handleAuthToggle = (e: React.ChangeEvent) => { setRequiresAuth(e.target.checked); if (!e.target.checked) { setProxyConfigForm({ ...proxyConfigForm, username: '', password: '' }); setErrors({ ...errors, username: '', password: '' }); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } try { const response = await sendProxyConfig(proxyConfigForm); if (response) { setIsProxyConfigured(true); setProxy({ proxy_url: proxyConfigForm.server_url, auth: requiresAuth }); notify('success', t('proxy.notifications.config_success')); fetchProxyConfig(); } else { notify('error', t('proxy.notifications.config_error')); console.log(`${t('proxy.notifications.config_error')} ${response}`) } } catch (error: any) { notify('error', `${error} : ${t('proxy.notifications.config_error')}`); } }; const handleTabChange = (event: React.SyntheticEvent, newValue: number) => { setTabIndex(newValue); }; const testProxy = async () => { await testProxyConfig().then((response) => { if (response.success) { notify('success', t('proxy.notifications.test_success')); } else { notify('error', t('proxy.notifications.test_error')); } }); }; const fetchProxyConfig = async () => { try { const response = await getProxyConfig(); if (response.proxy_url) { setIsProxyConfigured(true); setProxy(response); notify('success', t('proxy.notifications.fetch_success')); } } catch (error: any) { notify('error', error); } }; const removeProxy = async () => { await deleteProxyConfig().then((response) => { if (response) { notify('success', t('proxy.notifications.remove_success')); setIsProxyConfigured(false); setProxy({ proxy_url: '', auth: false }); } else { notify('error', t('proxy.notifications.remove_error')); } }); } useEffect(() => { fetchProxyConfig(); }, []); return ( <> {t('proxy.title')} {tabIndex === 0 && ( isProxyConfigured ? ( {t('proxy.table.proxy_url')} {t('proxy.table.requires_auth')} {proxy.proxy_url} {proxy.auth ? 'Yes' : 'No'}
) : ( {errors.server_url || t('proxy.server_url_helper')} } /> } label={t('proxy.requires_auth')} /> {requiresAuth && ( <> )} ))}
{t('proxy.alert.title')}
{t('proxy.alert.right_way')}
{t('proxy.alert.proxy_url')} http://proxy.com:1337
{t('proxy.alert.username')} myusername
{t('proxy.alert.password')} mypassword

{t('proxy.alert.wrong_way')}
{t('proxy.alert.proxy_url')} http://myusername:mypassword@proxy.com:1337
); }; export default ProxyForm;