Files
parcer/src/components/organisms/ProxyForm.tsx

219 lines
8.2 KiB
TypeScript
Raw Normal View History

2024-10-26 05:39:45 +05:30
import React, { useState, useEffect } from 'react';
2024-10-01 00:06:56 +05:30
import { styled } from '@mui/system';
import { TextField, Button, Switch, FormControlLabel, Box, Typography, Tabs, Tab } from '@mui/material';
2024-10-26 05:37:21 +05:30
import { sendProxyConfig, getProxyConfig, testProxyConfig } from '../../api/proxy';
2024-10-01 00:09:06 +05:30
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-09-30 17:20:16 +05:30
const FormContainer = styled(Box)({
2024-09-30 23:29:06 +05:30
display: 'flex',
flexDirection: 'column',
gap: '16px',
2024-10-11 03:07:11 +05:30
marginLeft: '30px'
2024-09-30 17:20:16 +05:30
});
2024-09-30 23:26:24 +05:30
const FormControl = styled(Box)({
2024-09-30 23:29:06 +05:30
marginBottom: '16px',
2024-09-30 23:26:24 +05:30
});
2024-09-30 17:20:16 +05:30
const ProxyForm: React.FC = () => {
2024-09-30 23:29:06 +05:30
const [proxyConfig, setProxyConfig] = useState({
2024-10-02 23:40:46 +05:30
server_url: '',
2024-09-30 23:29:06 +05:30
username: '',
password: '',
});
const [requiresAuth, setRequiresAuth] = useState<boolean>(false);
2024-10-02 23:39:08 +05:30
const [errors, setErrors] = useState({
2024-10-02 23:40:46 +05:30
server_url: '',
2024-10-02 23:39:08 +05:30
username: '',
password: '',
});
const [tabIndex, setTabIndex] = useState(0);
2024-10-26 05:37:21 +05:30
const [isProxyConfigured, setIsProxyConfigured] = useState(false);
2024-10-02 23:39:19 +05:30
2024-10-01 00:09:06 +05:30
const { notify } = useGlobalInfoStore();
2024-10-02 23:39:08 +05:30
const validateForm = () => {
let valid = true;
2024-10-02 23:40:46 +05:30
let errorMessages = { server_url: '', username: '', password: '' };
2024-10-02 23:39:08 +05:30
2024-10-02 23:40:46 +05:30
if (!proxyConfig.server_url) {
errorMessages.server_url = 'Server URL is required';
2024-10-02 23:39:08 +05:30
valid = false;
}
if (requiresAuth) {
if (!proxyConfig.username) {
errorMessages.username = 'Username is required for authenticated proxies';
valid = false;
}
if (!proxyConfig.password) {
errorMessages.password = 'Password is required for authenticated proxies';
valid = false;
}
}
setErrors(errorMessages);
return valid;
};
2024-09-30 23:29:06 +05:30
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setProxyConfig({ ...proxyConfig, [name]: value });
};
2024-09-30 17:20:16 +05:30
const handleAuthToggle = (e: React.ChangeEvent<HTMLInputElement>) => {
setRequiresAuth(e.target.checked);
if (!e.target.checked) {
setProxyConfig({ ...proxyConfig, username: '', password: '' });
2024-10-02 23:39:08 +05:30
setErrors({ ...errors, username: '', password: '' });
}
};
2024-09-30 23:29:06 +05:30
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
2024-10-02 23:39:08 +05:30
if (!validateForm()) {
return;
}
2024-10-02 23:39:19 +05:30
2024-10-01 00:06:56 +05:30
await sendProxyConfig(proxyConfig).then((response) => {
if (response) {
2024-10-01 00:09:32 +05:30
notify('success', 'Proxy configuration submitted successfully');
2024-10-01 00:06:56 +05:30
} else {
2024-10-01 00:09:06 +05:30
notify('error', 'Failed to submit proxy configuration. Try again.');
2024-10-01 00:06:56 +05:30
}
});
2024-09-30 23:29:06 +05:30
};
2024-09-30 17:20:16 +05:30
const handleTabChange = (event: React.SyntheticEvent, newValue: number) => {
setTabIndex(newValue);
};
2024-10-26 05:37:21 +05:30
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.');
}
});
};
2024-10-26 05:37:56 +05:30
const fetchProxyConfig = async () => {
await getProxyConfig().then((response) => {
if (response.proxy_url) {
setIsProxyConfigured(true);
notify('success', 'Proxy configuration fetched successfully');
} else {
notify('error', 'Failed to fetch proxy configuration. Try again.');
}
});
};
2024-10-26 05:39:45 +05:30
useEffect(() => {
fetchProxyConfig();
}, []);
2024-09-30 23:29:06 +05:30
return (
2024-10-26 05:39:45 +05:30
<>
2024-10-26 05:41:10 +05:30
{
isProxyConfigured ? (
<Box sx={{ maxWidth: 600, width: '100%', textAlign: 'center', marginTop: '20px' }}>
<Typography variant="body1" gutterBottom component="div">
Proxy is already configured. You can test the configuration below.
</Typography>
<Button variant="contained" color="primary" onClick={testProxy} sx={{ marginTop: '20px'}}>
Test Proxy Configuration
</Button>
</Box>
) : (
<FormContainer>
2024-10-11 03:04:23 +05:30
<Typography variant="h6" gutterBottom component="div" style={{ marginTop: '20px' }}>
Proxy Configuration
</Typography>
<Tabs value={tabIndex} onChange={handleTabChange}>
<Tab label="Standard Proxy" />
<Tab label="Automatic Proxy Rotation" />
</Tabs>
{tabIndex === 0 && (
<Box component="form" onSubmit={handleSubmit} sx={{ maxWidth: 400, width: '100%' }}>
<FormControl>
<TextField
label="Proxy Server URL"
name="server_url"
value={proxyConfig.server_url}
onChange={handleChange}
fullWidth
required
error={!!errors.server_url}
helperText={errors.server_url || `Proxy to be used for all robots. HTTP and SOCKS proxies are supported.
Example http://myproxy.com:3128 or socks5://myproxy.com:3128.
Short form myproxy.com:3128 is considered an HTTP proxy.`}
/>
</FormControl>
<FormControl>
<FormControlLabel
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
label="Requires Authentication?"
/>
</FormControl>
{requiresAuth && (
<>
<FormControl>
<TextField
label="Username"
name="username"
value={proxyConfig.username}
onChange={handleChange}
fullWidth
required={requiresAuth}
error={!!errors.username}
helperText={errors.username || ''}
/>
</FormControl>
<FormControl>
<TextField
label="Password"
name="password"
value={proxyConfig.password}
onChange={handleChange}
type="password"
fullWidth
required={requiresAuth}
error={!!errors.password}
helperText={errors.password || ''}
/>
</FormControl>
</>
)}
<Button
variant="contained"
color="primary"
type="submit"
2024-09-30 23:29:06 +05:30
fullWidth
disabled={!proxyConfig.server_url || (requiresAuth && (!proxyConfig.username || !proxyConfig.password))}
>
Add Proxy
</Button>
</Box>
)}
{tabIndex === 1 && (
2024-10-24 18:30:17 +05:30
<Box sx={{ maxWidth: 600, width: '100%', textAlign: 'center', marginTop: '20px' }}>
<>
<Typography variant="body1" gutterBottom component="div">
Coming Soon. Join our Cloud Waitlist to get early access.
</Typography>
<Button variant="contained" color="primary" sx={{ marginTop: '20px'}}>
Join Maxun Cloud Waitlist
</Button>
2024-10-24 18:30:17 +05:30
</>
</Box>
)}
2024-09-30 23:29:06 +05:30
</FormContainer>
2024-10-26 05:41:10 +05:30
)
}
2024-10-26 05:39:45 +05:30
</>
2024-09-30 23:29:06 +05:30
);
2024-09-30 17:20:16 +05:30
};
2024-10-02 23:39:08 +05:30
export default ProxyForm;