|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { useParams } from 'react-router-dom'; |
| 4 | +import CloudscapeInput from '@cloudscape-design/components/input'; |
| 5 | +import CloudscapeTextarea from '@cloudscape-design/components/textarea'; |
| 6 | + |
| 7 | +import { Box, Button, ButtonWithConfirmation, FormField, Header, Modal, SpaceBetween, Table } from 'components'; |
| 8 | + |
| 9 | +import { useBreadcrumbs, useCollection, useNotifications } from 'hooks'; |
| 10 | +import { getServerError } from 'libs'; |
| 11 | +import { ROUTES } from 'routes'; |
| 12 | +import { IPublicKey, useAddPublicKeyMutation, useDeletePublicKeysMutation, useListPublicKeysQuery } from 'services/publicKeys'; |
| 13 | + |
| 14 | +export const PublicKeys: React.FC = () => { |
| 15 | + const { t } = useTranslation(); |
| 16 | + const params = useParams(); |
| 17 | + const paramUserName = params.userName ?? ''; |
| 18 | + const [pushNotification] = useNotifications(); |
| 19 | + |
| 20 | + const [showAddModal, setShowAddModal] = useState(false); |
| 21 | + const [keyValue, setKeyValue] = useState(''); |
| 22 | + const [keyName, setKeyName] = useState(''); |
| 23 | + const [addError, setAddError] = useState(''); |
| 24 | + |
| 25 | + const { data: publicKeys = [], isLoading } = useListPublicKeysQuery(); |
| 26 | + const [addPublicKey, { isLoading: isAdding }] = useAddPublicKeyMutation(); |
| 27 | + const [deletePublicKeys, { isLoading: isDeleting }] = useDeletePublicKeysMutation(); |
| 28 | + |
| 29 | + useBreadcrumbs([ |
| 30 | + { |
| 31 | + text: t('navigation.account'), |
| 32 | + href: ROUTES.USER.LIST, |
| 33 | + }, |
| 34 | + { |
| 35 | + text: paramUserName, |
| 36 | + href: ROUTES.USER.DETAILS.FORMAT(paramUserName), |
| 37 | + }, |
| 38 | + { |
| 39 | + text: t('users.public_keys.title'), |
| 40 | + href: ROUTES.USER.PUBLIC_KEYS.FORMAT(paramUserName), |
| 41 | + }, |
| 42 | + ]); |
| 43 | + |
| 44 | + const { items, collectionProps } = useCollection(publicKeys, { |
| 45 | + selection: {}, |
| 46 | + }); |
| 47 | + |
| 48 | + const { selectedItems = [] } = collectionProps; |
| 49 | + |
| 50 | + const openAddModal = () => { |
| 51 | + setKeyValue(''); |
| 52 | + setKeyName(''); |
| 53 | + setAddError(''); |
| 54 | + setShowAddModal(true); |
| 55 | + }; |
| 56 | + |
| 57 | + const closeAddModal = () => { |
| 58 | + setShowAddModal(false); |
| 59 | + }; |
| 60 | + |
| 61 | + const handleAdd = () => { |
| 62 | + if (!keyValue.trim()) { |
| 63 | + setAddError(t('users.public_keys.key_required')); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + addPublicKey({ key: keyValue.trim(), name: keyName.trim() || undefined }) |
| 68 | + .unwrap() |
| 69 | + .then(() => { |
| 70 | + setShowAddModal(false); |
| 71 | + }) |
| 72 | + .catch((error) => { |
| 73 | + const detail = (error?.data?.detail ?? []) as { msg: string; code: string }[]; |
| 74 | + const isKeyExists = detail.some(({ code }) => code === 'resource_exists'); |
| 75 | + setAddError(isKeyExists ? t('users.public_keys.key_already_exists') : getServerError(error)); |
| 76 | + }); |
| 77 | + }; |
| 78 | + |
| 79 | + const handleDelete = () => { |
| 80 | + deletePublicKeys(selectedItems.map((k) => k.id)) |
| 81 | + .unwrap() |
| 82 | + .catch((error) => { |
| 83 | + pushNotification({ |
| 84 | + type: 'error', |
| 85 | + content: t('common.server_error', { error: getServerError(error) }), |
| 86 | + }); |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + const formatDate = (iso: string) => { |
| 91 | + return new Date(iso).toLocaleDateString(undefined, { |
| 92 | + year: 'numeric', |
| 93 | + month: 'short', |
| 94 | + day: 'numeric', |
| 95 | + }); |
| 96 | + }; |
| 97 | + |
| 98 | + const columns = [ |
| 99 | + { |
| 100 | + id: 'name', |
| 101 | + header: t('users.public_keys.name'), |
| 102 | + cell: (item: IPublicKey) => item.name, |
| 103 | + }, |
| 104 | + { |
| 105 | + id: 'fingerprint', |
| 106 | + header: t('users.public_keys.fingerprint'), |
| 107 | + cell: (item: IPublicKey) => ( |
| 108 | + <Box fontWeight="normal" variant="code"> |
| 109 | + {item.fingerprint} |
| 110 | + </Box> |
| 111 | + ), |
| 112 | + }, |
| 113 | + { |
| 114 | + id: 'type', |
| 115 | + header: t('users.public_keys.key_type'), |
| 116 | + cell: (item: IPublicKey) => item.type, |
| 117 | + }, |
| 118 | + { |
| 119 | + id: 'added_at', |
| 120 | + header: t('users.public_keys.added'), |
| 121 | + cell: (item: IPublicKey) => formatDate(item.added_at), |
| 122 | + }, |
| 123 | + ]; |
| 124 | + |
| 125 | + return ( |
| 126 | + <> |
| 127 | + <Table |
| 128 | + {...collectionProps} |
| 129 | + loading={isLoading} |
| 130 | + columnDefinitions={columns} |
| 131 | + items={items} |
| 132 | + selectionType="multi" |
| 133 | + trackBy="id" |
| 134 | + header={ |
| 135 | + <Header |
| 136 | + counter={publicKeys.length ? `(${publicKeys.length})` : undefined} |
| 137 | + actions={ |
| 138 | + <SpaceBetween size="xs" direction="horizontal"> |
| 139 | + <ButtonWithConfirmation |
| 140 | + disabled={!selectedItems.length || isDeleting} |
| 141 | + onClick={handleDelete} |
| 142 | + confirmTitle={t('users.public_keys.delete_confirm_title')} |
| 143 | + confirmContent={t('users.public_keys.delete_confirm_message')} |
| 144 | + > |
| 145 | + {t('common.delete')} |
| 146 | + </ButtonWithConfirmation> |
| 147 | + |
| 148 | + <Button variant="primary" onClick={openAddModal}> |
| 149 | + {t('users.public_keys.add_key')} |
| 150 | + </Button> |
| 151 | + </SpaceBetween> |
| 152 | + } |
| 153 | + > |
| 154 | + {t('users.public_keys.title')} |
| 155 | + </Header> |
| 156 | + } |
| 157 | + empty={ |
| 158 | + <Box textAlign="center" color="inherit"> |
| 159 | + <b>{t('users.public_keys.empty_title')}</b> |
| 160 | + <Box padding={{ bottom: 's' }} variant="p" color="inherit"> |
| 161 | + {t('users.public_keys.empty_message')} |
| 162 | + </Box> |
| 163 | + <Button onClick={openAddModal}>{t('users.public_keys.add_key')}</Button> |
| 164 | + </Box> |
| 165 | + } |
| 166 | + /> |
| 167 | + |
| 168 | + <Modal |
| 169 | + visible={showAddModal} |
| 170 | + onDismiss={closeAddModal} |
| 171 | + header={t('users.public_keys.add_key')} |
| 172 | + footer={ |
| 173 | + <Box float="right"> |
| 174 | + <SpaceBetween direction="horizontal" size="xs"> |
| 175 | + <Button variant="link" onClick={closeAddModal}> |
| 176 | + {t('common.cancel')} |
| 177 | + </Button> |
| 178 | + <Button variant="primary" loading={isAdding} onClick={handleAdd}> |
| 179 | + {t('users.public_keys.add_key')} |
| 180 | + </Button> |
| 181 | + </SpaceBetween> |
| 182 | + </Box> |
| 183 | + } |
| 184 | + > |
| 185 | + <SpaceBetween size="m"> |
| 186 | + <FormField |
| 187 | + label={t('users.public_keys.key_name_label')} |
| 188 | + description={t('users.public_keys.key_name_description')} |
| 189 | + > |
| 190 | + <CloudscapeInput |
| 191 | + value={keyName} |
| 192 | + onChange={({ detail }) => setKeyName(detail.value)} |
| 193 | + placeholder={t('users.public_keys.key_name_placeholder')} |
| 194 | + /> |
| 195 | + </FormField> |
| 196 | + |
| 197 | + <FormField |
| 198 | + label={t('users.public_keys.key_label')} |
| 199 | + description={t('users.public_keys.key_description')} |
| 200 | + errorText={addError} |
| 201 | + > |
| 202 | + <CloudscapeTextarea |
| 203 | + value={keyValue} |
| 204 | + onChange={({ detail }) => { |
| 205 | + setKeyValue(detail.value); |
| 206 | + setAddError(''); |
| 207 | + }} |
| 208 | + placeholder="ssh-ed25519 AAAA..." |
| 209 | + rows={5} |
| 210 | + /> |
| 211 | + </FormField> |
| 212 | + </SpaceBetween> |
| 213 | + </Modal> |
| 214 | + </> |
| 215 | + ); |
| 216 | +}; |
0 commit comments