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

118 lines
4.6 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
2019-12-16 03:10:01 +00:00
import Modal from '@/components/elements/Modal';
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';
import Button from '@/components/elements/Button';
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';
2019-12-16 03:10:01 +00:00
interface Values {
directoryName: string;
}
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) => {
2020-08-26 04:39:00 +00:00
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const { clearFlashes, clearAndAddHttpError } = useFlash();
2019-12-16 03:10:01 +00:00
const [ visible, setVisible ] = useState(false);
const { data, mutate } = useFileManagerSwr();
2019-12-16 03:10:01 +00:00
const directory = ServerContext.useStoreState(state => state.files.directory);
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))
2019-12-16 03:10:01 +00:00
.catch(error => {
console.error(error);
setSubmitting(false);
clearAndAddHttpError({ key: 'files:directory-modal', error });
2019-12-16 03:10:01 +00:00
});
};
return (
<>
2019-12-16 03:10:01 +00:00
<Formik
onSubmit={submit}
initialValues={{ directoryName: '' }}
validationSchema={object().shape({
directoryName: string()
.required('A valid directory name must be provided.')
.test('unique', 'Directory with that name already exists.', v => {
return v !== undefined &&
data !== undefined &&
data.filter(f => f.name.toLowerCase() === v.toLowerCase()).length < 1;
}),
})}
2019-12-16 03:10:01 +00:00
>
{({ resetForm, isSubmitting, values }) => (
<Modal
visible={visible}
dismissable={!isSubmitting}
showSpinnerOverlay={isSubmitting}
onDismissed={() => {
setVisible(false);
resetForm();
}}
>
<FlashMessageRender key={'files:directory-modal'}/>
2020-07-05 00:57:24 +00:00
<Form css={tw`m-0`}>
2019-12-16 03:10:01 +00:00
<Field
autoFocus
2019-12-16 03:10:01 +00:00
id={'directoryName'}
name={'directoryName'}
label={'Directory Name'}
/>
<p css={tw`text-xs mt-2 text-neutral-400 break-all`}>
2020-07-05 00:57:24 +00:00
<span css={tw`text-neutral-200`}>This directory will be created as</span>
&nbsp;/home/container/
2020-07-05 00:57:24 +00:00
<span css={tw`text-cyan-200`}>
{join(directory, values.directoryName).replace(/^(\.\.\/|\/)+/, '')}
</span>
2019-12-16 03:10:01 +00:00
</p>
2020-07-05 00:57:24 +00:00
<div css={tw`flex justify-end`}>
<Button css={tw`mt-8`}>
2019-12-16 03:10:01 +00:00
Create Directory
2020-07-05 00:57:24 +00:00
</Button>
2019-12-16 03:10:01 +00:00
</div>
</Form>
</Modal>
)}
</Formik>
<Button isSecondary onClick={() => setVisible(true)} className={className}>
2019-12-16 03:10:01 +00:00
Create Directory
2020-07-05 00:57:24 +00:00
</Button>
</>
2019-12-16 03:10:01 +00:00
);
};