import React from 'react'; import { Trans, useTranslation } from 'react-i18next'; import { Controller, useForm } from 'react-hook-form'; import i18next from 'i18next'; import cn from 'classnames'; import { getPathWithQueryString } from '../../../helpers/helpers'; import { CLIENT_ID_LINK, MOBILE_CONFIG_LINKS, STANDARD_HTTPS_PORT } from '../../../helpers/constants'; import { toNumber } from '../../../helpers/form'; import { validateConfigClientId, validateServerName, validatePort, validateIsSafePort, } from '../../../helpers/validators'; import { Input } from '../Controls/Input'; import { Select } from '../Controls/Select'; const getDownloadLink = (host: string, clientId: string, protocol: string, invalid: boolean) => { if (!host || invalid) { return ( ); } const linkParams: { host: string; client_id?: string } = { host }; if (clientId) { linkParams.client_id = clientId; } return ( {i18next.t('download_mobileconfig')} ); }; type FormValues = { host: string; clientId: string; protocol: string; port?: number; }; type Props = { initialValues?: FormValues; }; const defaultFormValues = { host: '', clientId: '', protocol: MOBILE_CONFIG_LINKS.DOT, port: undefined, }; export const MobileConfigForm = ({ initialValues }: Props) => { const { t } = useTranslation(); const { watch, control, formState: { isValid }, } = useForm({ mode: 'onBlur', defaultValues: { ...defaultFormValues, ...initialValues, }, }); const protocol = watch('protocol'); const host = watch('host'); const clientId = watch('clientId'); const port = watch('port'); const getHostName = () => { if (port && port !== STANDARD_HTTPS_PORT && protocol === MOBILE_CONFIG_LINKS.DOH) { return `${host}:${port}`; } return host; }; return (
e.preventDefault()}>
( )} />
{protocol === MOBILE_CONFIG_LINKS.DOH && (
validatePort(value) || true, safety: (value) => validateIsSafePort(value) || true, }, }} render={({ field, fieldState }) => ( { const { value } = e.target; field.onChange(toNumber(value)); }} /> )} />
)}
}}> client_id_desc
( )} />
( )} />
{getDownloadLink(getHostName(), clientId, protocol, !isValid)}
); };