import React, { useState, useEffect } from 'react'; import { styled } from '@mui/system'; import { 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'; const FormContainer = styled(Box)({ display: 'flex', flexDirection: 'column', gap: '16px', marginLeft: '30px' }); const FormControl = styled(Box)({ marginBottom: '16px', }); const ProxyForm: React.FC = () => { 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) { notify('success', 'Proxy configuration submitted successfully'); } else { notify('error', `Failed to submit proxy configuration. Try again. ${response}`); console.log(`Failed to submit proxy configuration. Try again. ${response}`) } } catch (error: any) { notify('error', `${error} : Failed to submit proxy configuration`); } }; const handleTabChange = (event: React.SyntheticEvent, newValue: number) => { setTabIndex(newValue); }; const testProxy = async () => { await testProxyConfig().then((response) => { if (response.success) { notify('success', 'Proxy configuration is working'); } else { notify('error', 'Failed to test proxy configuration. Try again.'); } }); }; const fetchProxyConfig = async () => { try { const response = await getProxyConfig(); if (response.proxy_url) { setIsProxyConfigured(true); setProxy(response); notify('success', 'Proxy configuration fetched successfully'); } } catch (error: any) { notify('error', error); } }; const removeProxy = async () => { await deleteProxyConfig().then((response) => { if (response) { notify('success', 'Proxy configuration removed successfully'); setIsProxyConfigured(false); setProxy({ proxy_url: '', auth: false }); } else { notify('error', 'Failed to remove proxy configuration. Try again.'); } }); } useEffect(() => { fetchProxyConfig(); }, []); return ( <> Proxy Configuration {tabIndex === 0 && ( isProxyConfigured ? ( Proxy URL Requires Authentication {proxy.proxy_url} {proxy.auth ? 'Yes' : 'No'}
) : ( } label="Requires Authentication?" /> {requiresAuth && ( <> )} ))} {tabIndex === 1 && ( <> Coming Soon - In Open Source (Basic Rotation) & Cloud (Advanced Rotation). If you don't want to manage the infrastructure, join our cloud waitlist to get early access. )}
Proxy Instructions 1. Enter the Proxy Server URL in the format: http://myproxy.com:3128 or socks5://myproxy.com:3128. 2. If your proxy requires authentication, toggle the "Requires Authentication?" switch and provide the username and password. 3. Click "Add Proxy" to save the configuration. 4. Use the "Test Proxy" button to verify if the proxy configuration is working. 5. If needed, you can remove the proxy configuration using the "Remove Proxy" button. ); }; export default ProxyForm;