File size: 4,842 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
import { Button, Spinner } from '@automattic/components';
import { createInterpolateElement } from '@wordpress/element';
import { sprintf } from '@wordpress/i18n';
import { useI18n } from '@wordpress/react-i18n';
import { addQueryArgs } from '@wordpress/url';
import { useMemo, useState } from 'react';
import { connect } from 'react-redux';
import FormFieldset from 'calypso/components/forms/form-fieldset';
import FormSelect from 'calypso/components/forms/form-select';
import { useSSHKeyQuery } from 'calypso/me/security-ssh-key/use-ssh-key-query';
import { useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { getCurrentUserName } from 'calypso/state/current-user/selectors';
import { errorNotice, removeNotice, successNotice } from 'calypso/state/notices/actions';
import SshKeyCard from './ssh-key-card';
import { useAtomicSshKeys } from './use-atomic-ssh-keys';
import { useAttachSshKeyMutation } from './use-attach-ssh-key';
import './ssh-keys.scss';
interface SshKeysProps {
siteId: number;
siteSlug: string;
username: string;
disabled: boolean;
}
const noticeOptions = {
duration: 3000,
};
const sshKeyAttachFailureNoticeId = 'ssh-key-attach-failure';
function SshKeys( { siteId, siteSlug, username, disabled }: SshKeysProps ) {
const { __ } = useI18n();
const dispatch = useDispatch();
const {
data: keys,
isLoading: isLoadingKeys,
isFetching: isFetchingKeys,
} = useAtomicSshKeys( siteId, {
enabled: ! disabled,
} );
const { data: userKeys, isLoading: isLoadingUserKeys } = useSSHKeyQuery();
const { attachSshKey, isPending: attachingKey } = useAttachSshKeyMutation( siteId, {
onMutate: () => {
dispatch( removeNotice( sshKeyAttachFailureNoticeId ) );
},
onSuccess: () => {
dispatch( recordTracksEvent( 'calypso_hosting_configuration_ssh_key_attach_success' ) );
dispatch( successNotice( __( 'SSH key attached to site.' ), noticeOptions ) );
},
onError: ( error ) => {
dispatch(
recordTracksEvent( 'calypso_hosting_configuration_ssh_key_attach_failure', {
code: error.code,
} )
);
dispatch(
errorNotice(
// translators: "reason" is why adding the ssh key failed.
sprintf( __( 'Failed to attach SSH key: %(reason)s' ), { reason: error.message } ),
{
...noticeOptions,
id: sshKeyAttachFailureNoticeId,
}
)
);
},
} );
const [ selectedKey, setSelectedKey ] = useState( 'default' );
function onChangeSelectedKey( event: React.ChangeEvent< HTMLSelectElement > ) {
setSelectedKey( event.target.value );
}
const userKeyIsAttached = useMemo( () => {
if ( ! keys ) {
return false;
}
return !! keys.find( ( { user_login } ) => user_login === username );
}, [ keys, username ] );
const isLoading = isLoadingKeys || isLoadingUserKeys;
const showKeysSelect = ! isLoading && ! userKeyIsAttached && userKeys && userKeys.length > 0;
const showLinkToAddUserKey = ! isLoading && ! userKeyIsAttached && userKeys?.length === 0;
const SSH_ADD_URL = addQueryArgs( '/me/security/ssh-key', {
source: 'sites/settings/sftp-ssh',
siteSlug,
} );
return (
<div className="ssh-keys">
<label htmlFor="attach-ssh-key" className="form-label">
{ __( 'SSH keys' ) }
</label>
{ keys && keys.length > 0 && (
<div className="ssh-keys__cards-container">
{ keys.map( ( sshKey ) => (
<SshKeyCard
key={ sshKey.sha256 }
sshKey={ sshKey }
deleteText={ __( 'Detach' ) }
siteId={ siteId }
disabled={ isFetchingKeys }
/>
) ) }
</div>
) }
{ isLoading && <Spinner /> }
{ showKeysSelect && (
<FormFieldset className="ssh-keys__form">
<FormSelect
id="attach-ssh-key"
className="ssh-keys__select"
onChange={ onChangeSelectedKey }
value={ selectedKey }
>
{ userKeys?.map( ( key ) => (
<option value={ key.name } key={ key.sha256 }>
{ username }-{ key.name }
</option>
) ) }
</FormSelect>
<Button
primary
onClick={ () => attachSshKey( { name: selectedKey } ) }
disabled={ attachingKey }
busy={ attachingKey }
>
<span>{ __( 'Attach SSH key to site' ) }</span>
</Button>
</FormFieldset>
) }
{ showLinkToAddUserKey && (
<div>
{ createInterpolateElement(
__( '<a>Add an SSH key</a> and attach it to your site to enable passwordless login.' ),
{
a: (
<a
href={ SSH_ADD_URL }
onClick={ () => {
dispatch(
recordTracksEvent( 'calypso_hosting_configuration_add_ssh_key_link_click' )
);
} }
/>
),
}
) }
</div>
) }
</div>
);
}
export default connect( ( state ) => {
return {
username: getCurrentUserName( state ),
};
} )( SshKeys );
|