2019-12-16 03:10:01 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
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';
|
|
|
|
import v4 from 'uuid/v4';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2020-07-11 05:10:51 +00:00
|
|
|
import { mutate } from 'swr';
|
|
|
|
import useServer from '@/plugins/useServer';
|
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
|
|
|
import { useLocation } from 'react-router';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
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.'),
|
|
|
|
});
|
|
|
|
|
2020-07-11 05:10:51 +00:00
|
|
|
const generateDirectoryData = (name: string): FileObject => ({
|
|
|
|
uuid: v4(),
|
|
|
|
name: name,
|
|
|
|
mode: '0644',
|
|
|
|
size: 0,
|
|
|
|
isFile: false,
|
|
|
|
isEditable: false,
|
|
|
|
isSymlink: false,
|
|
|
|
mimetype: '',
|
|
|
|
createdAt: new Date(),
|
|
|
|
modifiedAt: new Date(),
|
2020-07-15 04:16:49 +00:00
|
|
|
isArchiveType: () => false,
|
2020-07-11 05:10:51 +00:00
|
|
|
});
|
|
|
|
|
2019-12-16 03:10:01 +00:00
|
|
|
export default () => {
|
2020-07-11 05:10:51 +00:00
|
|
|
const { uuid } = useServer();
|
|
|
|
const { hash } = useLocation();
|
|
|
|
const { clearAndAddHttpError } = useFlash();
|
2019-12-16 03:10:01 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
|
|
|
|
2020-07-11 05:10:51 +00:00
|
|
|
const submit = ({ directoryName }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
createDirectory(uuid, directory, directoryName)
|
2019-12-16 03:10:01 +00:00
|
|
|
.then(() => {
|
2020-07-11 05:10:51 +00:00
|
|
|
mutate(
|
|
|
|
`${uuid}:files:${hash}`,
|
|
|
|
(data: FileObject[]) => [ ...data, generateDirectoryData(directoryName) ],
|
|
|
|
);
|
2019-12-16 03:10:01 +00:00
|
|
|
setVisible(false);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
setSubmitting(false);
|
2020-07-11 05:10:51 +00:00
|
|
|
clearAndAddHttpError({ key: 'files', error });
|
2019-12-16 03:10:01 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2020-07-11 05:10:51 +00:00
|
|
|
<>
|
2019-12-16 03:10:01 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
validationSchema={schema}
|
|
|
|
initialValues={{ directoryName: '' }}
|
|
|
|
>
|
|
|
|
{({ resetForm, isSubmitting, values }) => (
|
|
|
|
<Modal
|
|
|
|
visible={visible}
|
|
|
|
dismissable={!isSubmitting}
|
|
|
|
showSpinnerOverlay={isSubmitting}
|
|
|
|
onDismissed={() => {
|
|
|
|
setVisible(false);
|
|
|
|
resetForm();
|
|
|
|
}}
|
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<Form css={tw`m-0`}>
|
2019-12-16 03:10:01 +00:00
|
|
|
<Field
|
|
|
|
id={'directoryName'}
|
|
|
|
name={'directoryName'}
|
|
|
|
label={'Directory Name'}
|
|
|
|
/>
|
2020-07-05 00:57:24 +00:00
|
|
|
<p css={tw`text-xs mt-2 text-neutral-400`}>
|
|
|
|
<span css={tw`text-neutral-200`}>This directory will be created as</span>
|
2020-06-13 16:49:32 +00:00
|
|
|
/home/container/
|
2020-07-05 00:57:24 +00:00
|
|
|
<span css={tw`text-cyan-200`}>
|
2020-06-13 16:49:32 +00:00
|
|
|
{decodeURIComponent(
|
|
|
|
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>
|
2020-07-05 00:57:24 +00:00
|
|
|
<Button isSecondary css={tw`mr-2`} onClick={() => setVisible(true)}>
|
2019-12-16 03:10:01 +00:00
|
|
|
Create Directory
|
2020-07-05 00:57:24 +00:00
|
|
|
</Button>
|
2020-07-11 05:10:51 +00:00
|
|
|
</>
|
2019-12-16 03:10:01 +00:00
|
|
|
);
|
|
|
|
};
|