import { Alert, Box } from '@mui/material'; import { useState } from 'react'; import ToolContent from '@components/ToolContent'; import { ToolComponentProps } from '@tools/defineTool'; import ToolTextResult from '@components/result/ToolTextResult'; import { GetGroupsType } from '@components/options/ToolOptions'; import { formatPorts, generateRandomPorts, getPortRangeInfo, validateInput } from './service'; import { InitialValuesType, RandomPortResult } from './types'; import { useTranslation } from 'react-i18next'; import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; import CheckboxWithDesc from '@components/options/CheckboxWithDesc'; import SimpleRadio from '@components/options/SimpleRadio'; const initialValues: InitialValuesType = { portRange: 'registered', minPort: 1024, maxPort: 49151, count: 5, allowDuplicates: false, sortResults: false, separator: ', ' }; export default function RandomPortGenerator({ title, longDescription }: ToolComponentProps) { const { t } = useTranslation('number'); const [result, setResult] = useState(null); const [error, setError] = useState(null); const [formattedResult, setFormattedResult] = useState(''); const compute = (values: InitialValuesType) => { try { setError(null); setResult(null); setFormattedResult(''); // Validate input const validationError = validateInput(values); if (validationError) { setError(validationError); return; } // Generate random ports const randomResult = generateRandomPorts(values); setResult(randomResult); // Format for display const formatted = formatPorts(randomResult.ports, values.separator); setFormattedResult(formatted); } catch (err) { console.error('Random port generation failed:', err); setError(t('randomPortGenerator.error.generationFailed')); } }; const portOptions = [ { value: 'well-known', label: t('randomPortGenerator.options.range.wellKnown') }, { value: 'registered', label: t('randomPortGenerator.options.range.registered') }, { value: 'dynamic', label: t('randomPortGenerator.options.range.dynamic') }, { value: 'custom', label: t('randomPortGenerator.options.range.custom') } ] as const; const getGroups: GetGroupsType | null = ({ values, updateField }) => [ { title: t('randomPortGenerator.options.range.title'), component: ( {portOptions.map((option) => ( updateField('portRange', option.value)} /> ))} {values.portRange === 'custom' && ( updateField('minPort', parseInt(value) || 1024) } description={t( 'randomPortGenerator.options.range.minPortDescription' )} inputProps={{ type: 'number', min: 1, max: 65535, 'data-testid': 'min-port-input' }} /> updateField('maxPort', parseInt(value) || 49151) } description={t( 'randomPortGenerator.options.range.maxPortDescription' )} inputProps={{ type: 'number', min: 1, max: 65535, 'data-testid': 'max-port-input' }} /> )} {getPortRangeInfo(values.portRange).name}
{getPortRangeInfo(values.portRange).description}
) }, { title: t('randomPortGenerator.options.generation.title'), component: ( updateField('count', parseInt(value) || 5)} description={t( 'randomPortGenerator.options.generation.countDescription' )} inputProps={{ type: 'number', min: 1, max: 1000, 'data-testid': 'count-input' }} /> updateField('allowDuplicates', value)} description={t( 'randomPortGenerator.options.generation.allowDuplicates.description' )} /> updateField('sortResults', value)} description={t( 'randomPortGenerator.options.generation.sortResults.description' )} /> ) }, { title: t('randomPortGenerator.options.output.title'), component: ( updateField('separator', value)} description={t( 'randomPortGenerator.options.output.separatorDescription' )} inputProps={{ 'data-testid': 'separator-input' }} /> ) } ]; return ( {error && ( {error} )} {result && ( )} } toolInfo={{ title: t('randomPortGenerator.info.title'), description: longDescription || t('randomPortGenerator.info.description') }} /> ); }