2020-09-23 03:50:44 +00:00
|
|
|
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';
|
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';
|
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.'),
|
|
|
|
});
|
|
|
|
|
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-11-29 21:46:35 +00:00
|
|
|
mode: 'drwxr-xr-x',
|
|
|
|
modeBits: '0755',
|
2020-07-11 05:10:51 +00:00
|
|
|
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) => {
|
2022-06-26 19:13:52 +00:00
|
|
|
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
|
2020-09-23 03:50:44 +00:00
|
|
|
const { clearFlashes, clearAndAddHttpError } = useFlash();
|
2022-06-26 19:13:52 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2020-08-07 03:33:17 +00:00
|
|
|
|
|
|
|
const { mutate } = useFileManagerSwr();
|
2022-06-26 19:13:52 +00:00
|
|
|
const directory = ServerContext.useStoreState((state) => state.files.directory);
|
2019-12-16 03:10:01 +00:00
|
|
|
|
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');
|
|
|
|
};
|
2022-06-26 19:13:52 +00:00
|
|
|
}, [visible]);
|
2020-09-23 03:50:44 +00:00
|
|
|
|
2020-07-11 05:10:51 +00:00
|
|
|
const submit = ({ directoryName }: Values, { setSubmitting }: FormikHelpers<Values>) => {
|
|
|
|
createDirectory(uuid, directory, directoryName)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then(() => mutate((data) => [...data, generateDirectoryData(directoryName)], false))
|
2020-08-07 03:33:17 +00:00
|
|
|
.then(() => setVisible(false))
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2019-12-16 03:10:01 +00:00
|
|
|
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
|
|
|
<>
|
2022-06-20 18:16:42 +00:00
|
|
|
<Portal>
|
2022-06-26 19:13:52 +00:00
|
|
|
<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();
|
|
|
|
}}
|
|
|
|
>
|
2022-06-26 19:13:52 +00:00
|
|
|
<FlashMessageRender key={'files:directory-modal'} />
|
2022-06-20 18:16:42 +00:00
|
|
|
<Form css={tw`m-0`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<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 </span>
|
2022-06-26 19:13:52 +00:00
|
|
|
<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>
|
2022-07-02 21:24:12 +00:00
|
|
|
<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>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Button className={'w-full sm:w-auto'} onClick={submitForm}>
|
|
|
|
Create
|
|
|
|
</Button>
|
2022-07-02 21:24:12 +00:00
|
|
|
</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>
|
2020-07-11 05:10:51 +00:00
|
|
|
</>
|
2019-12-16 03:10:01 +00:00
|
|
|
);
|
|
|
|
};
|