// @flow import React, { useState, useEffect } from 'react'; import Button from 'component/button'; import { Form, FormField } from 'component/common/form'; import { VALID_IPADDRESS_REGEX, VALID_HOSTNAME_REGEX, VALID_ENDPOINT_REGEX } from 'constants/protocol_regex'; type Props = { update: (CommentServerDetails) => void, onCancel: (boolean) => void, }; const isValidServerString = (serverString) => { const si = serverString.indexOf('/'); const pi = serverString.indexOf(':'); const path = si === -1 ? '' : serverString.slice(si); const hostMaybePort = si === -1 ? serverString : serverString.slice(0, si); const host = pi === -1 ? hostMaybePort : hostMaybePort.slice(0, pi); const port = pi === -1 ? '' : hostMaybePort.slice(pi + 1); const portInt = parseInt(port); return ( (host === 'localhost' || VALID_IPADDRESS_REGEX.test(host) || VALID_HOSTNAME_REGEX.test(host)) && (!path || VALID_ENDPOINT_REGEX.test(path)) && // eslint-disable-next-line (pi === -1 || (port && typeof portInt === 'number' && portInt === portInt)) ); // NaN !== NaN }; function ServerInputRow(props: Props) { const { update, onCancel } = props; const [nameString, setNameString] = useState(''); const [hostString, setHostString] = useState(''); const [useHttps, setUseHttps] = useState(true); const getHostString = () => { return `${useHttps ? 'https://' : 'http://'}${hostString}`; }; const [validServerString, setValidServerString] = useState(false); useEffect(() => { setValidServerString(isValidServerString(hostString)); }, [hostString, validServerString, setValidServerString]); function onSubmit() { const updateValue = { url: getHostString(), name: nameString }; update(updateValue); setHostString(''); setNameString(''); } return (
setNameString(e.target.value)} />
{`${useHttps ? 'https://' : 'http://'}`}
setHostString(e.target.value)} name={'serverUrl'} />
setUseHttps(!useHttps)} />
); } export default ServerInputRow;