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