From e2d7d30ca7e401b77179947a100c086b67bb495c Mon Sep 17 00:00:00 2001 From: hcavarsan Date: Wed, 15 Jan 2025 17:49:44 -0300 Subject: [PATCH 1/2] feat: let users type custom ports manually --- diff | 0 .../src/components/AddConfigModal/index.tsx | 98 +++++++++++++++---- package.json | 2 +- 3 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 diff diff --git a/diff b/diff new file mode 100644 index 00000000..e69de29b diff --git a/frontend/src/components/AddConfigModal/index.tsx b/frontend/src/components/AddConfigModal/index.tsx index 6d418925..6543c231 100644 --- a/frontend/src/components/AddConfigModal/index.tsx +++ b/frontend/src/components/AddConfigModal/index.tsx @@ -47,6 +47,11 @@ const AddConfigModal: React.FC = ({ { value: 'proxy', label: 'Proxy' }, ] + const CUSTOM_PORT = { + value: -1, + label: 'Custom Port...', + } as const + const protocolOptions: StringOption[] = [ { value: 'tcp', label: 'TCP' }, { value: 'udp', label: 'UDP' }, @@ -334,15 +339,22 @@ const AddConfigModal: React.FC = ({ if (actionMeta.name === 'remote_port') { const portOption = newValue as PortOption | null - setFormState(prev => ({ - ...prev, - selectedPort: portOption, - })) - setNewConfig(prev => ({ ...prev, remote_port: portOption?.value })) + if (portOption?.value === -1) { + setFormState(prev => ({ + ...prev, + selectedPort: portOption, + })) + setNewConfig(prev => ({ ...prev, remote_port: undefined })) + } else { + setFormState(prev => ({ + ...prev, + selectedPort: portOption, + })) + setNewConfig(prev => ({ ...prev, remote_port: portOption?.value })) + } return } - const option = newValue as StringOption | null switch (actionMeta.name) { @@ -413,11 +425,12 @@ const AddConfigModal: React.FC = ({ } const portOptions: PortOption[] = useMemo( - () => - portQuery.data?.map(port => ({ + () => [ + ...(portQuery.data?.map(port => ({ value: port.port, label: port.name ? `${port.name} (${port.port})` : port.port.toString(), - })) || [], + })) || []), + ], [portQuery.data], ) @@ -738,24 +751,67 @@ const AddConfigModal: React.FC = ({ Target Port * - { + const value = parseInt(e.target.value, 10) + + if (!isNaN(value)) { + setFormState(prev => ({ + ...prev, + selectedPort: CUSTOM_PORT, + })) + const syntheticEvent = { + ...e, + target: { + ...e.target, + name: 'remote_port', + value: value.toString(), + }, + } + + handleInputChange(syntheticEvent) + } + }} + bg='#161616' + border='1px solid rgba(255, 255, 255, 0.08)' + _hover={{ + borderColor: 'rgba(255, 255, 255, 0.15)', + }} + _focus={{ + borderColor: 'blue.400', + boxShadow: 'none', + }} + height='28px' + fontSize='13px' + placeholder='Enter port number' + /> + ) : ( +