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

296 lines
12 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';
2025-01-09 17:15:37 +05:30
import {
Alert,
AlertTitle,
TextField,
Button,
Switch,
FormControlLabel,
Box,
Typography,
Tabs,
Tab,
Table,
TableContainer,
TableHead,
TableRow,
TableBody,
TableCell,
Paper
2024-12-08 04:49:29 +05:30
} from '@mui/material';
2024-10-26 05:51:02 +05:30
import { sendProxyConfig, getProxyConfig, testProxyConfig, deleteProxyConfig } from '../../api/proxy';
2024-10-01 00:09:06 +05:30
import { useGlobalInfoStore } from '../../context/globalInfo';
2024-12-08 04:49:29 +05:30
import { useThemeMode } from '../../context/theme-provider';
2024-12-20 21:04:44 +05:30
import { useTranslation } from 'react-i18next';
2024-12-08 04:49:29 +05:30
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-12-20 21:04:44 +05:30
const { t } = useTranslation();
2024-10-26 06:19:56 +05:30
const [proxyConfigForm, setProxyConfigForm] = 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-26 06:19:56 +05:30
const [proxy, setProxy] = useState({ proxy_url: '', auth: 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-26 06:19:56 +05:30
if (!proxyConfigForm.server_url) {
2024-10-02 23:40:46 +05:30
errorMessages.server_url = 'Server URL is required';
2024-10-02 23:39:08 +05:30
valid = false;
}
if (requiresAuth) {
2024-10-26 06:19:56 +05:30
if (!proxyConfigForm.username) {
2024-10-02 23:39:08 +05:30
errorMessages.username = 'Username is required for authenticated proxies';
valid = false;
}
2024-10-26 06:19:56 +05:30
if (!proxyConfigForm.password) {
2024-10-02 23:39:08 +05:30
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;
2024-10-26 06:19:56 +05:30
setProxyConfigForm({ ...proxyConfigForm, [name]: value });
2024-09-30 23:29:06 +05:30
};
2024-09-30 17:20:16 +05:30
const handleAuthToggle = (e: React.ChangeEvent<HTMLInputElement>) => {
setRequiresAuth(e.target.checked);
if (!e.target.checked) {
2024-10-26 06:19:56 +05:30
setProxyConfigForm({ ...proxyConfigForm, 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-26 06:19:56 +05:30
try {
const response = await sendProxyConfig(proxyConfigForm);
2024-10-01 00:06:56 +05:30
if (response) {
2024-12-20 21:04:44 +05:30
notify('success', t('proxy.notifications.config_success'));
2024-10-26 19:08:47 +05:30
} else {
2024-12-20 21:04:44 +05:30
notify('error', t('proxy.notifications.config_error'));
console.log(`${t('proxy.notifications.config_error')} ${response}`)
2024-10-01 00:06:56 +05:30
}
2024-10-26 06:19:56 +05:30
} catch (error: any) {
notify('error', `${error} : ${t('proxy.notifications.config_error')}`);
2024-10-26 06:19: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) {
2024-12-20 21:04:44 +05:30
notify('success', t('proxy.notifications.test_success'));
2024-10-26 05:37:21 +05:30
} else {
2024-12-20 21:04:44 +05:30
notify('error', t('proxy.notifications.test_error'));
2024-10-26 05:37:21 +05:30
}
});
};
2024-10-26 05:37:56 +05:30
const fetchProxyConfig = async () => {
2024-10-26 06:19:56 +05:30
try {
const response = await getProxyConfig();
2024-10-26 05:37:56 +05:30
if (response.proxy_url) {
2024-10-26 20:32:47 +05:30
setIsProxyConfigured(true);
setProxy(response);
2024-12-20 21:04:44 +05:30
notify('success', t('proxy.notifications.fetch_success'));
2024-10-26 05:37:56 +05:30
}
2024-10-26 20:32:47 +05:30
} catch (error: any) {
2024-10-26 06:19:56 +05:30
notify('error', error);
}
2024-10-26 05:37:56 +05:30
};
2024-10-26 05:51:02 +05:30
const removeProxy = async () => {
await deleteProxyConfig().then((response) => {
if (response) {
2024-12-20 21:04:44 +05:30
notify('success', t('proxy.notifications.remove_success'));
2024-10-26 05:51:02 +05:30
setIsProxyConfigured(false);
2024-10-26 06:19:56 +05:30
setProxy({ proxy_url: '', auth: false });
2024-10-26 05:51:02 +05:30
} else {
2024-12-20 21:04:44 +05:30
notify('error', t('proxy.notifications.remove_error'));
2024-10-26 05:51:02 +05:30
}
});
}
2024-10-26 05:39:45 +05:30
useEffect(() => {
fetchProxyConfig();
}, []);
2024-12-08 04:49:29 +05:30
const theme = useThemeMode();
const isDarkMode = theme.darkMode;
2024-09-30 23:29:06 +05:30
return (
2024-10-26 05:39:45 +05:30
<>
2024-10-26 23:39:30 +05:30
<FormContainer>
<Typography variant="h6" gutterBottom component="div" style={{ marginTop: '20px' }}>
2024-12-20 21:04:44 +05:30
{t('proxy.title')}
2024-10-26 23:39:30 +05:30
</Typography>
2025-01-09 17:15:37 +05:30
<Tabs value={tabIndex} onChange={handleTabChange}>
2024-12-20 21:04:44 +05:30
<Tab label={t('proxy.tab_standard')} />
<Tab label={t('proxy.tab_rotation')} />
2025-01-09 17:15:37 +05:30
</Tabs>
2025-01-08 12:50:46 +05:30
2024-10-26 23:39:30 +05:30
{tabIndex === 0 && (
isProxyConfigured ? (
2024-10-28 19:35:27 +05:30
<Box sx={{ maxWidth: 600, width: '100%', marginTop: '5px' }}>
2024-11-22 20:42:18 +05:30
<TableContainer component={Paper} sx={{ marginBottom: '20px' }}>
<Table>
<TableHead>
<TableRow>
2024-12-20 21:04:44 +05:30
<TableCell><strong>{t('proxy.table.proxy_url')}</strong></TableCell>
<TableCell><strong>{t('proxy.table.requires_auth')}</strong></TableCell>
2024-11-22 20:42:18 +05:30
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<TableCell>{proxy.proxy_url}</TableCell>
<TableCell>{proxy.auth ? 'Yes' : 'No'}</TableCell>
</TableRow>
</TableBody>
</Table>
</TableContainer>
2024-10-26 23:39:30 +05:30
<Button variant="outlined" color="primary" onClick={testProxy}>
2024-12-20 21:04:44 +05:30
{t('proxy.test_proxy')}
2024-10-26 23:39:30 +05:30
</Button>
<Button variant="outlined" color="error" onClick={removeProxy} sx={{ marginLeft: '10px' }}>
2024-12-20 21:04:44 +05:30
{t('proxy.remove_proxy')}
2024-10-26 23:39:30 +05:30
</Button>
</Box>
) : (
<Box component="form" onSubmit={handleSubmit} sx={{ maxWidth: 400, width: '100%' }}>
<FormControl>
<TextField
2024-12-20 21:04:44 +05:30
label={t('proxy.server_url')}
2024-10-26 23:39:30 +05:30
name="server_url"
value={proxyConfigForm.server_url}
onChange={handleChange}
fullWidth
required
error={!!errors.server_url}
2024-12-20 21:04:44 +05:30
helperText={errors.server_url || t('proxy.server_url_helper')}
2024-10-26 23:39:30 +05:30
/>
</FormControl>
<FormControl>
<FormControlLabel
control={<Switch checked={requiresAuth} onChange={handleAuthToggle} />}
2024-12-20 21:04:44 +05:30
label={t('proxy.requires_auth')}
2024-10-26 23:39:30 +05:30
/>
</FormControl>
{requiresAuth && (
2024-10-26 05:41:39 +05:30
<>
2024-10-26 23:39:30 +05:30
<FormControl>
<TextField
2024-12-20 21:04:44 +05:30
label={t('proxy.username')}
2024-10-26 23:39:30 +05:30
name="username"
value={proxyConfigForm.username}
onChange={handleChange}
fullWidth
required={requiresAuth}
error={!!errors.username}
helperText={errors.username || ''}
/>
</FormControl>
<FormControl>
<TextField
2024-12-20 21:04:44 +05:30
label={t('proxy.password')}
2024-10-26 23:39:30 +05:30
name="password"
value={proxyConfigForm.password}
onChange={handleChange}
type="password"
fullWidth
required={requiresAuth}
error={!!errors.password}
helperText={errors.password || ''}
/>
</FormControl>
2024-10-26 05:41:39 +05:30
</>
2024-10-26 23:39:30 +05:30
)}
<Button
variant="contained"
color="primary"
type="submit"
fullWidth
disabled={!proxyConfigForm.server_url || (requiresAuth && (!proxyConfigForm.username || !proxyConfigForm.password))}
>
2024-12-20 21:04:44 +05:30
{t('proxy.add_proxy')}
2024-10-26 23:39:30 +05:30
</Button>
</Box>
))}
{tabIndex === 1 && (
2024-11-22 20:54:27 +05:30
<Box sx={{ maxWidth: 400, width: '100%', textAlign: 'center', marginTop: '20px' }}>
2024-10-26 23:39:30 +05:30
<>
<Typography variant="body1" gutterBottom component="div">
2024-12-20 21:04:44 +05:30
{t('proxy.coming_soon')}
2024-10-26 23:39:30 +05:30
</Typography>
2025-01-08 12:50:46 +05:30
2025-01-09 17:15:37 +05:30
{/* <Button variant="contained" color="primary" sx={{ marginTop: '20px',backgroundColor: '#ff00c3' }}>
2025-01-08 13:15:10 +05:30
<a style={{ color: 'white', textDecoration: 'none' }} href="https://forms.gle/hXjgqDvkEhPcaBW76">Join Maxun Cloud Waitlist</a> */}
2025-01-08 12:50:46 +05:30
2024-10-26 23:39:30 +05:30
<Button variant="contained" color="primary" sx={{ marginTop: '20px' }}>
2024-12-20 21:04:44 +05:30
<a style={{ color: 'white', textDecoration: 'none' }} href="https://forms.gle/hXjgqDvkEhPcaBW76">{t('proxy.join_waitlist')}</a>
2025-01-08 12:50:46 +05:30
2024-10-26 23:39:30 +05:30
</Button>
</>
</Box>
)}
</FormContainer>
2025-01-08 12:50:46 +05:30
2025-01-09 00:09:29 +05:30
<Alert severity="info" sx={{ marginTop: '80px', marginLeft: '50px', height: '250px', width: '600px', border: '1px solid #ff00c3' }}>
2024-12-20 21:04:44 +05:30
<AlertTitle>{t('proxy.alert.title')}</AlertTitle>
2024-11-22 21:37:21 +05:30
<br />
2024-12-20 21:04:44 +05:30
<b>{t('proxy.alert.right_way')}</b>
2024-11-22 21:37:21 +05:30
<br />
2024-12-20 21:04:44 +05:30
{t('proxy.alert.proxy_url')} http://proxy.com:1337
2024-11-22 21:37:21 +05:30
<br />
2024-12-20 21:04:44 +05:30
{t('proxy.alert.username')} myusername
2024-11-22 21:37:21 +05:30
<br />
2024-12-20 21:04:44 +05:30
{t('proxy.alert.password')} mypassword
2024-11-22 21:37:21 +05:30
<br />
<br />
2024-12-20 21:04:44 +05:30
<b>{t('proxy.alert.wrong_way')}</b>
2024-12-08 04:49:29 +05:30
<br />
2025-01-08 12:50:46 +05:30
2024-12-20 21:04:44 +05:30
{t('proxy.alert.proxy_url')} http://myusername:mypassword@proxy.com:1337
2024-11-22 21:37:21 +05:30
</Alert>
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-12-08 04:49:29 +05:30
export default ProxyForm;