misc_pterodactyl-panel/resources/scripts/components/server/files/NewDirectoryButton.tsx

119 lines
4.8 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2019-12-16 03:10:01 +00:00
import { ServerContext } from '@/state/server';
2020-03-19 04:32:07 +00:00
import { Form, Formik, FormikHelpers } from 'formik';
2019-12-16 03:10:01 +00:00
import Field from '@/components/elements/Field';
import { join } from 'path';
import { object, string } from 'yup';
import createDirectory from '@/api/server/files/createDirectory';
2020-07-05 00:57:24 +00:00
import tw from 'twin.macro';
2022-06-20 18:16:42 +00:00
import { Button } from '@/components/elements/button/index';
import { FileObject } from '@/api/server/files/loadDirectory';
import useFlash from '@/plugins/useFlash';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
import { WithClassname } from '@/components/types';
import FlashMessageRender from '@/components/FlashMessageRender';
2022-06-20 18:16:42 +00:00
import { Dialog } from '@/components/elements/dialog';
import Portal from '@/components/elements/Portal';
import Code from '@/components/elements/Code';
2019-12-16 03:10:01 +00:00
interface Values {
directoryName: string;
}
const schema = object().shape({
directoryName: string().required('A valid directory name must be provided.'),
});
const generateDirectoryData = (name: string): FileObject => ({
key: `dir_${name.split('/', 1)[0] ?? name}`,
name: name.replace(/^(\/*)/, '').split('/', 1)[0] ?? name,
mode: 'drwxr-xr-x',
modeBits: '0755',
size: 0,
isFile: false,
isSymlink: false,
mimetype: '',
createdAt: new Date(),
modifiedAt: new Date(),
isArchiveType: () => false,
isEditable: () => false,
});
export default ({ className }: WithClassname) => {
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
const { clearFlashes, clearAndAddHttpError } = useFlash();
const [visible, setVisible] = useState(false);
const { mutate } = useFileManagerSwr();
const directory = ServerContext.useStoreState((state) => state.files.directory);
2019-12-16 03:10:01 +00:00
useEffect(() => {
2020-09-23 03:52:37 +00:00
if (!visible) return;
return () => {
clearFlashes('files:directory-modal');
};
}, [visible]);
const submit = ({ directoryName }: Values, { setSubmitting }: FormikHelpers<Values>) => {
createDirectory(uuid, directory, directoryName)
.then(() => mutate((data) => [...data, generateDirectoryData(directoryName)], false))
.then(() => setVisible(false))
.catch((error) => {
2019-12-16 03:10:01 +00:00
console.error(error);
setSubmitting(false);
clearAndAddHttpError({ key: 'files:directory-modal', error });
2019-12-16 03:10:01 +00:00
});
};
return (
<>
2022-06-20 18:16:42 +00:00
<Portal>
<Formik onSubmit={submit} validationSchema={schema} initialValues={{ directoryName: '' }}>
2022-06-20 18:16:42 +00:00
{({ resetForm, submitForm, isSubmitting: _, values }) => (
<Dialog
title={'Create Directory'}
open={visible}
onClose={() => {
setVisible(false);
resetForm();
}}
>
<FlashMessageRender key={'files:directory-modal'} />
2022-06-20 18:16:42 +00:00
<Form css={tw`m-0`}>
<Field autoFocus id={'directoryName'} name={'directoryName'} label={'Name'} />
2022-06-20 18:16:42 +00:00
<p css={tw`mt-2 text-sm md:text-base break-all`}>
<span css={tw`text-neutral-200`}>This directory will be created as&nbsp;</span>
<Code>
/home/container/
2022-06-20 18:16:42 +00:00
<span css={tw`text-cyan-200`}>
{join(directory, values.directoryName).replace(/^(\.\.\/|\/)+/, '')}
</span>
</Code>
</p>
</Form>
<Dialog.Footer>
2022-06-20 18:16:42 +00:00
<Button.Text
className={'w-full sm:w-auto'}
onClick={() => {
setVisible(false);
resetForm();
}}
>
Cancel
</Button.Text>
<Button className={'w-full sm:w-auto'} onClick={submitForm}>
Create
</Button>
</Dialog.Footer>
2022-06-20 18:16:42 +00:00
</Dialog>
)}
</Formik>
</Portal>
<Button.Text onClick={() => setVisible(true)} className={className}>
2019-12-16 03:10:01 +00:00
Create Directory
2022-06-20 18:16:42 +00:00
</Button.Text>
</>
2019-12-16 03:10:01 +00:00
);
};