2024-09-30 17:20:16 +05:30
|
|
|
import React, { useState } from 'react';
|
2024-10-01 00:06:56 +05:30
|
|
|
import { styled } from '@mui/system';
|
2024-10-02 23:33:16 +05:30
|
|
|
import { TextField, Button, Switch, FormControlLabel, Box, Typography } from '@mui/material';
|
2024-10-01 00:06:56 +05:30
|
|
|
import { sendProxyConfig } 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',
|
|
|
|
|
padding: '20px',
|
|
|
|
|
borderRadius: '8px',
|
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({
|
|
|
|
|
server: '',
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
});
|
2024-10-02 23:33:16 +05:30
|
|
|
const [requiresAuth, setRequiresAuth] = useState<boolean>(false);
|
2024-10-02 23:39:08 +05:30
|
|
|
const [errors, setErrors] = useState({
|
|
|
|
|
server: '',
|
|
|
|
|
username: '',
|
|
|
|
|
password: '',
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-01 00:09:06 +05:30
|
|
|
const { notify } = useGlobalInfoStore();
|
|
|
|
|
|
2024-10-02 23:39:08 +05:30
|
|
|
const validateForm = () => {
|
|
|
|
|
let valid = true;
|
|
|
|
|
let errorMessages = { server: '', username: '', password: '' };
|
|
|
|
|
|
|
|
|
|
if (!proxyConfig.server) {
|
|
|
|
|
errorMessages.server = 'Server URL is required';
|
|
|
|
|
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
|
|
|
|
2024-10-02 23:33: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-10-02 23:33:16 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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-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
|
|
|
|
2024-09-30 23:29:06 +05:30
|
|
|
return (
|
|
|
|
|
<FormContainer>
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
2024-10-02 23:33:16 +05:30
|
|
|
<Typography variant="subtitle1" gutterBottom style={{ marginBottom: '20px', marginTop: '20px' }}>
|
|
|
|
|
Proxy Configuration
|
|
|
|
|
</Typography>
|
2024-09-30 23:29:06 +05:30
|
|
|
<FormControl>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Proxy Server URL"
|
|
|
|
|
name="server"
|
|
|
|
|
value={proxyConfig.server}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
fullWidth
|
|
|
|
|
required
|
2024-10-02 23:39:08 +05:30
|
|
|
error={!!errors.server}
|
|
|
|
|
helperText={errors.server || 'e.g., http://proxy-server.com:8080'}
|
2024-09-30 23:29:06 +05:30
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormControl>
|
2024-10-02 23:33:16 +05:30
|
|
|
<FormControlLabel
|
|
|
|
|
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
|
2024-10-02 23:39:08 +05:30
|
|
|
label="Requires Authentication?"
|
2024-09-30 23:29:06 +05:30
|
|
|
/>
|
|
|
|
|
</FormControl>
|
2024-10-02 23:33:16 +05:30
|
|
|
{requiresAuth && (
|
|
|
|
|
<>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Username"
|
|
|
|
|
name="username"
|
|
|
|
|
value={proxyConfig.username}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
fullWidth
|
2024-10-02 23:39:08 +05:30
|
|
|
required={requiresAuth}
|
|
|
|
|
error={!!errors.username}
|
|
|
|
|
helperText={errors.username || ''}
|
2024-10-02 23:33:16 +05:30
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormControl>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Password"
|
|
|
|
|
name="password"
|
|
|
|
|
value={proxyConfig.password}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
type="password"
|
|
|
|
|
fullWidth
|
2024-10-02 23:39:08 +05:30
|
|
|
required={requiresAuth}
|
|
|
|
|
error={!!errors.password}
|
|
|
|
|
helperText={errors.password || ''}
|
2024-10-02 23:33:16 +05:30
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-10-02 23:39:08 +05:30
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
type="submit"
|
|
|
|
|
fullWidth
|
|
|
|
|
disabled={!proxyConfig.server || (requiresAuth && (!proxyConfig.username || !proxyConfig.password))}
|
|
|
|
|
>
|
2024-09-30 23:29:06 +05:30
|
|
|
Add Proxy
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
</FormContainer>
|
|
|
|
|
);
|
2024-09-30 17:20:16 +05:30
|
|
|
};
|
|
|
|
|
|
2024-10-02 23:39:08 +05:30
|
|
|
export default ProxyForm;
|