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

111 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { useContext, 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';
2022-11-25 20:25:03 +00:00
import { join } from 'pathe';
2019-12-16 03:10:01 +00:00
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';
2022-07-24 23:23:45 +00:00
import { useFlashKey } from '@/plugins/useFlash';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
import { WithClassname } from '@/components/types';
import FlashMessageRender from '@/components/FlashMessageRender';
2022-07-24 23:23:45 +00:00
import { Dialog, DialogWrapperContext } from '@/components/elements/dialog';
2022-06-20 18:16:42 +00:00
import Code from '@/components/elements/Code';
2022-07-24 23:23:45 +00:00
import asDialog from '@/hoc/asDialog';
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,
});
2022-07-24 23:23:45 +00:00
const NewDirectoryDialog = asDialog({
title: 'Create Directory',
})(() => {
2022-11-25 20:25:03 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const directory = ServerContext.useStoreState(state => state.files.directory);
const { mutate } = useFileManagerSwr();
2022-07-24 23:23:45 +00:00
const { close } = useContext(DialogWrapperContext);
const { clearAndAddHttpError } = useFlashKey('files:directory-modal');
2019-12-16 03:10:01 +00:00
useEffect(() => {
2020-09-23 03:52:37 +00:00
return () => {
2022-07-24 23:23:45 +00:00
clearAndAddHttpError();
2020-09-23 03:52:37 +00:00
};
2022-07-24 23:23:45 +00:00
}, []);
const submit = ({ directoryName }: Values, { setSubmitting }: FormikHelpers<Values>) => {
createDirectory(uuid, directory, directoryName)
2022-11-25 20:25:03 +00:00
.then(() => mutate(data => [...data!, generateDirectoryData(directoryName)], false))
2022-07-24 23:23:45 +00:00
.then(() => close())
2022-11-25 20:25:03 +00:00
.catch(error => {
2019-12-16 03:10:01 +00:00
setSubmitting(false);
2022-07-24 23:23:45 +00:00
clearAndAddHttpError(error);
2019-12-16 03:10:01 +00:00
});
};
2022-07-24 23:23:45 +00:00
return (
<Formik onSubmit={submit} validationSchema={schema} initialValues={{ directoryName: '' }}>
{({ submitForm, values }) => (
<>
<FlashMessageRender key={'files:directory-modal'} />
<Form css={tw`m-0`}>
<Field autoFocus id={'directoryName'} name={'directoryName'} label={'Name'} />
<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/
<span css={tw`text-cyan-200`}>
{join(directory, values.directoryName).replace(/^(\.\.\/|\/)+/, '')}
</span>
</Code>
</p>
</Form>
<Dialog.Footer>
<Button.Text className={'w-full sm:w-auto'} onClick={close}>
Cancel
</Button.Text>
<Button className={'w-full sm:w-auto'} onClick={submitForm}>
Create
</Button>
</Dialog.Footer>
</>
)}
</Formik>
);
});
export default ({ className }: WithClassname) => {
const [open, setOpen] = useState(false);
2019-12-16 03:10:01 +00:00
return (
<>
2022-07-24 23:23:45 +00:00
<NewDirectoryDialog open={open} onClose={setOpen.bind(this, false)} />
<Button.Text onClick={setOpen.bind(this, 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
);
};