import { Button, FormInputValidation, FormLabel, Gridicon, SegmentedControl, } from '@automattic/components'; import { useTranslate } from 'i18n-calypso'; import { FunctionComponent, useState, FormEventHandler } from 'react'; import FormInputCheckbox from 'calypso/components/forms/form-checkbox'; import FormFieldset from 'calypso/components/forms/form-fieldset'; import FormPasswordInput from 'calypso/components/forms/form-password-input'; import FormSelect from 'calypso/components/forms/form-select'; import FormSettingExplanation from 'calypso/components/forms/form-setting-explanation'; import FormTextInput from 'calypso/components/forms/form-text-input'; import FormTextArea from 'calypso/components/forms/form-textarea'; import InfoPopover from 'calypso/components/info-popover'; import { useDispatch } from 'calypso/state'; import { recordTracksEvent } from 'calypso/state/analytics/actions'; import { FormState, FormMode, FormErrors, INITIAL_FORM_INTERACTION } from '../form'; import { getHostInfoFromId } from '../host-info'; import InlineInfo from './inline-info'; import './style.scss'; interface Props { formErrors: FormErrors; formMode: FormMode; formModeSwitcher?: 'segmented' | 'simple'; disabled?: boolean; formState: FormState; onFormStateChange: ( newFormState: FormState ) => void; onModeChange: ( fromMode: FormMode ) => void; host: string; role: string; withHeader?: boolean; children?: React.ReactNode; allowFtp?: boolean; } const ServerCredentialsForm: FunctionComponent< Props > = ( { children, disabled = false, formState, formErrors, formMode, formModeSwitcher = 'segmented', onFormStateChange, onModeChange, host, role = 'main', withHeader = true, allowFtp = true, } ) => { const translate = useTranslate(); const dispatch = useDispatch(); const [ interactions, setFormInteractions ] = useState( INITIAL_FORM_INTERACTION ); const hostInfo = getHostInfoFromId( host ); const isAlternate = role === 'alternate'; formState.role = role; const handleFormChange: FormEventHandler< HTMLInputElement | HTMLSelectElement > = ( { currentTarget, } ) => { switch ( currentTarget.name ) { case 'protocol': onFormStateChange( { ...formState, protocol: currentTarget.value as 'ftp' | 'ssh' | 'dynamic-ssh', port: ( () => { let port = parseInt( String( formState.port ) ) || 22; if ( formState.port === 22 && currentTarget.value === 'ftp' ) { port = 21; } else if ( formState.port === 21 && currentTarget.value === 'ssh' ) { port = 22; } return port; } )(), } ); onModeChange( FormMode.Password ); break; case 'host': setFormInteractions( { ...interactions, host: true } ); onFormStateChange( { ...formState, host: currentTarget.value } ); break; case 'site_url': setFormInteractions( { ...interactions, site_url: true } ); onFormStateChange( { ...formState, site_url: currentTarget.value } ); break; case 'port': setFormInteractions( { ...interactions, port: true } ); onFormStateChange( { ...formState, port: isNaN( parseInt( currentTarget.value ) ) ? '' : parseInt( currentTarget.value ), } ); break; case 'user': setFormInteractions( { ...interactions, user: true } ); onFormStateChange( { ...formState, user: currentTarget.value } ); break; case 'pass': setFormInteractions( { ...interactions, pass: true } ); onFormStateChange( { ...formState, pass: currentTarget.value } ); break; case 'path': setFormInteractions( { ...interactions, path: true } ); onFormStateChange( { ...formState, path: currentTarget.value } ); break; case 'kpri': setFormInteractions( { ...interactions, kpri: true } ); onFormStateChange( { ...formState, kpri: currentTarget.value } ); break; case 'save_as_staging': setFormInteractions( { ...interactions, save_as_staging: true } ); onFormStateChange( { ...formState, save_as_staging: ( currentTarget as HTMLInputElement ).checked, } ); break; } }; const renderServerUsernameForm = () => (
{ translate( 'Username' ) } { hostInfo?.inline?.user && ( ) }
{ formErrors.user && ( interactions.user || ! formErrors.user.waitForInteraction ) && ( ) }
); const renderPasswordForm = () => (
{ renderServerUsernameForm() }
{ translate( 'Password' ) } { hostInfo?.inline?.pass && ( ) }
{ formErrors.pass && ( interactions.pass || ! formErrors.pass.waitForInteraction ) && ( ) }
); const renderPrivateKeyForm = () => ( <> { renderServerUsernameForm() }
{ translate( 'Private key' ) } { hostInfo?.inline?.kpri && ( ) }
{ formModeSwitcher === 'segmented' && ( { translate( 'Only non-encrypted private keys are supported.' ) } ) } { formErrors.kpri && ( interactions.kpri || ! formErrors.kpri.waitForInteraction ) && ( ) }
); const getSubHeaderText = () => { if ( isAlternate && hostInfo !== null && hostInfo.supportLink !== undefined ) { return translate( 'Enter the server credentials from your hosting provider. {{a}}Learn how to find and enter your credentials{{/a}}', { components: { a: ( dispatch( recordTracksEvent( 'calypso_jetpack_advanced_credentials_flow_support_link_click', { host } ) ) } /> ), }, } ); } else if ( hostInfo !== null && hostInfo.inline !== undefined ) { return translate( 'Check the information icons for details on %(hostName)s', { args: { hostName: hostInfo?.name, }, } ); } else if ( hostInfo !== null && hostInfo.supportLink !== undefined ) { return 'generic' === host ? translate( 'Read through {{a}}our support site{{/a}} to learn how to obtain your credentials.', { components: { a: ( dispatch( recordTracksEvent( 'calypso_jetpack_advanced_credentials_flow_support_link_click', { host } ) ) } /> ), }, } ) : translate( 'Read through the {{a}}%(hostName)s support site{{/a}} to learn how to obtain your credentials.', { args: { hostName: hostInfo.name, }, components: { a: ( dispatch( recordTracksEvent( 'calypso_jetpack_advanced_credentials_flow_support_link_click', { host } ) ) } /> ), }, } ); } return translate( 'Your hosting provider will be able to supply this information to you.' ); }; const getFormHeaderText = () => { if ( ! allowFtp ) { return translate( 'Provide your SSH, SFTP server credentials' ); } return translate( 'Provide your SSH, SFTP or FTP server credentials' ); }; const renderCredentialLinks = () => { if ( hostInfo !== null && hostInfo.credentialLinks !== undefined ) { if ( 'ftp' === formState.protocol && hostInfo.credentialLinks.ftp !== undefined ) { return ( ); } if ( 'ssh' === formState.protocol && hostInfo.credentialLinks.sftp !== undefined ) { return ( ); } } return null; }; return (
{ isAlternate && (
{ translate( 'Destination URL' ) }
{ formErrors.site_url && ( interactions.site_url || ! formErrors.site_url.waitForInteraction ) && ( ) }
) } { ! isAlternate && withHeader &&

{ getFormHeaderText() }

} { withHeader &&

{ getSubHeaderText() }

} { withHeader && renderCredentialLinks() } { ! allowFtp && } { allowFtp && (
{ translate( 'Credential type' ) } { hostInfo?.inline?.protocol && ( ) }
) }
{ translate( 'Server address' ) } { hostInfo?.inline?.host && ( ) }
{ formErrors.host && ( interactions.host || ! formErrors.host.waitForInteraction ) && ( ) }
{ translate( 'Port number' ) } { hostInfo?.inline?.port && ( ) }
{ formErrors.port && ( interactions.port || ! formErrors.port.waitForInteraction ) && ( ) }
{ translate( 'WordPress installation path' ) } { hostInfo?.inline?.path && ( ) }
{ formErrors.path && ( interactions.path || ! formErrors.path.waitForInteraction ) && ( ) }
{ formModeSwitcher === 'segmented' && 'ftp' !== formState.protocol && (
onModeChange( FormMode.Password ) } > { translate( 'Use password' ) } onModeChange( FormMode.PrivateKey ) } > { translate( 'Use private key' ) } { hostInfo?.inline?.mode && ( ) }
) } { formModeSwitcher === 'simple' && ( { translate( 'SSH credentials' ) } { translate( 'Your credentials will be used only to migrate the site and won’t be stored anywhere.' ) } ) } { formMode === FormMode.Password ? renderPasswordForm() : renderPrivateKeyForm() } { formModeSwitcher === 'simple' && ( <> { formMode === FormMode.Password && (

) } { formMode === FormMode.PrivateKey && (

{ translate( 'Only non-encrypted private keys are supported.' ) }{ ' ' }

) } ) } { isAlternate && ( { translate( 'Remember credentials' ) } { translate( 'Save this configuration so you can easily access it the next time you copy a site to staging.' ) } ) }
{ children }
); }; export default ServerCredentialsForm;