Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/develop/resources/scripts/components/dashboard/ssh/CreateSSHKeyForm.tsx
7454 views
1
import { Field, Form, Formik, FormikHelpers } from 'formik';
2
import { object, string } from 'yup';
3
import FormikFieldWrapper from '@/components/elements/FormikFieldWrapper';
4
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
5
import tw from 'twin.macro';
6
import Button from '@/components/elements/Button';
7
import Input, { Textarea } from '@/components/elements/Input';
8
import styled from 'styled-components';
9
import { useFlashKey } from '@/plugins/useFlash';
10
import { createSSHKey, useSSHKeys } from '@/api/account/ssh-keys';
11
12
interface Values {
13
name: string;
14
publicKey: string;
15
}
16
17
const CustomTextarea = styled(Textarea)`
18
${tw`h-32`}
19
`;
20
21
export default () => {
22
const { clearAndAddHttpError } = useFlashKey('account');
23
const { mutate } = useSSHKeys();
24
25
const submit = (values: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
26
clearAndAddHttpError();
27
28
createSSHKey(values.name, values.publicKey)
29
.then(key => {
30
resetForm();
31
mutate(data => (data || []).concat(key));
32
})
33
.catch(error => clearAndAddHttpError(error))
34
.then(() => setSubmitting(false));
35
};
36
37
return (
38
<>
39
<Formik
40
onSubmit={submit}
41
initialValues={{ name: '', publicKey: '' }}
42
validationSchema={object().shape({
43
name: string().required(),
44
publicKey: string().required(),
45
})}
46
>
47
{({ isSubmitting }) => (
48
<Form>
49
<SpinnerOverlay visible={isSubmitting} />
50
<FormikFieldWrapper label={'SSH Key Name'} name={'name'} css={tw`mb-6`}>
51
<Field name={'name'} as={Input} />
52
</FormikFieldWrapper>
53
<FormikFieldWrapper
54
label={'Public Key'}
55
name={'publicKey'}
56
description={'Enter your public SSH key.'}
57
>
58
<Field name={'publicKey'} as={CustomTextarea} />
59
</FormikFieldWrapper>
60
<div css={tw`flex justify-end mt-6`}>
61
<Button>Save</Button>
62
</div>
63
</Form>
64
)}
65
</Formik>
66
</>
67
);
68
};
69
70