2020-09-23 03:50:44 +00:00
|
|
|
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';
|
2020-07-11 05:10:51 +00:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-08-07 03:33:17 +00:00
|
|
|
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
|
2020-09-13 17:33:12 +00:00
|
|
|
import { WithClassname } from '@/components/types';
|
2020-09-23 03:50:44 +00:00
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
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 => ({
|
2020-09-23 03:50:44 +00:00
|
|
|
key: `dir_${name.split('/', 1)[0] ?? name}`,
|
2020-09-26 03:44:40 +00:00
|
|
|
name: name.replace(/^(\/*)/, '').split('/', 1)[0] ?? name,
|
2020-07-11 05:10:51 +00:00
|
|
|
mode: '0644',
|
|
|
|
size: 0,
|
|
|
|
isFile: false,
|
|
|
|
isSymlink: false,
|
|
|
|
mimetype: '',
|
|
|
|
createdAt: new Date(),
|
|
|
|
modifiedAt: new Date(),
|
2020-07-15 04:16:49 +00:00
|
|
|
isArchiveType: () => false,
|
2020-08-30 17:25:48 +00:00
|
|
|
isEditable: () => false,
|
2020-07-11 05:10:51 +00:00
|
|
|
});
|
|
|
|
|
2020-09-13 17:33:12 +00:00
|
|
|
export default ({ className }: WithClassname) => {
|
2020-08-26 04:39:00 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
2020-09-23 03:50:44 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2019-12-16 03:10:01 +00:00
|
|
|
const [ visible, setVisible ] = useState(false);
|
2020-08-07 03:33:17 +00:00
|
|
|
|
|
|
|
const { mutate } = useFileManagerSwr();
|
2019-12-16 03:10:01 +00:00
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
|
|
|
|
2020-09-23 03:50:44 +00:00
|
|
|
useEffect(() => {
|
2020-09-23 03:52:37 +00:00
|
|
|
if (!visible) return;
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearFlashes('files:directory-modal');
|
|
|
|
};
|
2020-09-23 03:50:44 +00:00
|
|
|
}, [ visible ]);
|
|
|
|
|
2020-07-11 05:10:51 +00:00
|
|
|
const submit = ({ directoryName }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
createDirectory(uuid, directory, directoryName)
|
2020-08-07 03:33:17 +00:00
|
|
|
.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);
|
2020-09-23 03:50:44 +00:00
|
|
|
clearAndAddHttpError({ key: 'files:directory-modal', 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-09-23 03:50:44 +00:00
|
|
|
<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
|
2020-08-07 03:33:17 +00:00
|
|
|
autoFocus
|
2019-12-16 03:10:01 +00:00
|
|
|
id={'directoryName'}
|
|
|
|
name={'directoryName'}
|
|
|
|
label={'Directory Name'}
|
|
|
|
/>
|
2020-10-03 18:22:37 +00:00
|
|
|
<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>
|
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-10-23 04:18:46 +00:00
|
|
|
{decodeURIComponent(encodeURIComponent(
|
2020-10-26 00:31:24 +00:00
|
|
|
join(directory, values.directoryName).replace(/^(\.\.\/|\/)+/, ''),
|
2020-10-23 04:18:46 +00:00
|
|
|
))}
|
2020-06-13 16:49:32 +00:00
|
|
|
</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-09-13 17:33:12 +00:00
|
|
|
<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>
|
2020-07-11 05:10:51 +00:00
|
|
|
</>
|
2019-12-16 03:10:01 +00:00
|
|
|
);
|
|
|
|
};
|