import React from 'react'; import InfoTip from 'components/InfoTip'; import StyledWrapper from './StyledWrapper'; import { IconEye, IconEyeOff } from '@tabler/icons'; import { useState } from 'react'; import { useDispatch } from 'react-redux'; import { updateCollectionProxy } from 'providers/ReduxStore/slices/collections'; import { saveCollectionSettings } from 'providers/ReduxStore/slices/collections/actions'; import { get } from 'lodash'; import toast from 'react-hot-toast'; import Button from 'ui/Button'; const ProxySettings = ({ collection }) => { const dispatch = useDispatch(); const initialProxyConfig = { inherit: true, config: { protocol: 'http', hostname: '', port: '', auth: { username: '', password: '' }, bypassProxy: '' } }; // Get proxy from draft.brunoConfig if it exists, otherwise from brunoConfig const currentProxyConfig = collection.draft?.brunoConfig ? get(collection, 'draft.brunoConfig.proxy', initialProxyConfig) : get(collection, 'brunoConfig.proxy', initialProxyConfig); const [passwordVisible, setPasswordVisible] = useState(false); const validateHostnameOnChange = (hostname) => { if (hostname && hostname.length > 1024) { toast.error('Hostname must be less than 1024 characters'); return false; } return true; }; const validatePortOnChange = (port) => { if (!port || port === '') { return true; // Allow empty port during typing } const portNum = Number(port); if (isNaN(portNum)) { toast.error('Port must be a valid number'); return false; } if (portNum < 1 || portNum > 65535) { toast.error('Port must be between 1 and 65535'); return false; } return true; }; const validateAuthUsernameOnChange = (username) => { if (username && username.length > 1024) { toast.error('Username must be less than 1024 characters'); return false; } return true; }; const validateAuthPasswordOnChange = (password) => { if (password && password.length > 1024) { toast.error('Password must be less than 1024 characters'); return false; } return true; }; const validateBypassProxyOnChange = (bypassProxy) => { if (bypassProxy && bypassProxy.length > 1024) { toast.error('Bypass proxy must be less than 1024 characters'); return false; } return true; }; // Helper to update proxy config const updateProxy = (updates) => { const updatedProxy = { ...currentProxyConfig, ...updates }; dispatch(updateCollectionProxy({ collectionUid: collection.uid, proxy: updatedProxy })); }; const handleSave = () => dispatch(saveCollectionSettings(collection.uid)); const handleEnabledChange = (e) => { const value = e.target.value; // Map UI values to new format if (value === 'inherit') { updateProxy({ disabled: false, inherit: true }); } else if (value === 'true') { updateProxy({ disabled: false, inherit: false }); } else { updateProxy({ disabled: true, inherit: false }); } }; const handleProtocolChange = (e) => { updateProxy({ config: { ...currentProxyConfig.config, protocol: e.target.value } }); }; const handleHostnameChange = (e) => { const hostname = e.target.value; if (validateHostnameOnChange(hostname)) { updateProxy({ config: { ...currentProxyConfig.config, hostname } }); } }; const handlePortChange = (e) => { const port = e.target.value ? Number(e.target.value) : ''; if (validatePortOnChange(port)) { updateProxy({ config: { ...currentProxyConfig.config, port } }); } }; const handleAuthEnabledChange = (e) => { updateProxy({ config: { ...currentProxyConfig.config, auth: { ...currentProxyConfig.config.auth, disabled: !e.target.checked } } }); }; const handleAuthUsernameChange = (e) => { const username = e.target.value; if (validateAuthUsernameOnChange(username)) { updateProxy({ config: { ...currentProxyConfig.config, auth: { ...currentProxyConfig.config.auth, username } } }); } }; const handleAuthPasswordChange = (e) => { const password = e.target.value; if (validateAuthPasswordOnChange(password)) { updateProxy({ config: { ...currentProxyConfig.config, auth: { ...currentProxyConfig.config.auth, password } } }); } }; const handleBypassProxyChange = (e) => { const bypassProxy = e.target.value; if (validateBypassProxyOnChange(bypassProxy)) { updateProxy({ config: { ...currentProxyConfig.config, bypassProxy } }); } }; // Map new format to UI values const disabled = currentProxyConfig.disabled || false; const inherit = currentProxyConfig.inherit !== undefined ? currentProxyConfig.inherit : true; const enabledValue = disabled ? 'false' : (inherit ? 'inherit' : 'true'); return (
Configure proxy settings for this collection.
{enabledValue === 'true' && ( <>
)}
); }; export default ProxySettings;