2019-08-03 05:22:01 +00:00
|
|
|
import React from 'react';
|
|
|
|
import Modal, { RequiredModalProps } from '@/components/elements/Modal';
|
2020-03-19 04:32:07 +00:00
|
|
|
import { Form, Formik, FormikHelpers } from 'formik';
|
2019-08-03 05:22:01 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { join } from 'path';
|
|
|
|
import renameFile from '@/api/server/files/renameFile';
|
|
|
|
import { ServerContext } from '@/state/server';
|
2019-08-04 21:58:31 +00:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2019-08-03 05:22:01 +00:00
|
|
|
|
|
|
|
interface FormikValues {
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2019-08-06 04:02:36 +00:00
|
|
|
type Props = RequiredModalProps & { file: FileObject; useMoveTerminology?: boolean };
|
2019-08-04 21:58:31 +00:00
|
|
|
|
2019-08-06 04:02:36 +00:00
|
|
|
export default ({ file, useMoveTerminology, ...props }: Props) => {
|
2019-08-04 21:58:31 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2019-12-17 03:55:02 +00:00
|
|
|
const { pushFile, removeFile } = ServerContext.useStoreActions(actions => actions.files);
|
2019-08-03 05:22:01 +00:00
|
|
|
|
2020-03-19 04:32:07 +00:00
|
|
|
const submit = (values: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => {
|
2019-08-04 21:58:31 +00:00
|
|
|
const renameFrom = join(directory, file.name);
|
|
|
|
const renameTo = join(directory, values.name);
|
2019-08-03 05:22:01 +00:00
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
renameFile(uuid, { renameFrom, renameTo })
|
|
|
|
.then(() => {
|
2019-12-17 03:55:02 +00:00
|
|
|
if (!useMoveTerminology && values.name.split('/').length === 1) {
|
|
|
|
pushFile({ ...file, name: values.name });
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((useMoveTerminology || values.name.split('/').length > 1) && file.uuid.length > 0) {
|
|
|
|
removeFile(file.uuid);
|
|
|
|
}
|
|
|
|
|
2019-08-04 21:58:31 +00:00
|
|
|
props.onDismissed();
|
|
|
|
})
|
2019-08-03 05:22:01 +00:00
|
|
|
.catch(error => {
|
|
|
|
setSubmitting(false);
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Formik
|
|
|
|
onSubmit={submit}
|
|
|
|
initialValues={{ name: file.name }}
|
|
|
|
>
|
2019-08-06 04:02:36 +00:00
|
|
|
{({ isSubmitting, values }) => (
|
2019-08-03 05:22:01 +00:00
|
|
|
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
|
2020-07-05 00:57:24 +00:00
|
|
|
<Form css={tw`m-0`}>
|
2019-08-06 04:55:33 +00:00
|
|
|
<div
|
2020-07-05 00:57:24 +00:00
|
|
|
css={[
|
|
|
|
tw`flex`,
|
|
|
|
useMoveTerminology ? tw`items-center` : tw`items-end`,
|
|
|
|
]}
|
2019-08-06 04:55:33 +00:00
|
|
|
>
|
2020-07-05 00:57:24 +00:00
|
|
|
<div css={tw`flex-1 mr-6`}>
|
2019-08-06 04:02:36 +00:00
|
|
|
<Field
|
|
|
|
type={'string'}
|
|
|
|
id={'file_name'}
|
|
|
|
name={'name'}
|
|
|
|
label={'File Name'}
|
|
|
|
description={useMoveTerminology
|
|
|
|
? 'Enter the new name and directory of this file or folder, relative to the current directory.'
|
|
|
|
: undefined
|
|
|
|
}
|
2020-07-05 00:57:24 +00:00
|
|
|
autoFocus
|
2019-08-06 04:02:36 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
2020-07-05 00:57:24 +00:00
|
|
|
<Button>{useMoveTerminology ? 'Move' : 'Rename'}</Button>
|
2019-08-06 04:02:36 +00:00
|
|
|
</div>
|
2019-08-03 05:22:01 +00:00
|
|
|
</div>
|
2019-08-06 04:55:33 +00:00
|
|
|
{useMoveTerminology &&
|
2020-07-05 00:57:24 +00:00
|
|
|
<p css={tw`text-xs mt-2 text-neutral-400`}>
|
|
|
|
<strong css={tw`text-neutral-200`}>New location:</strong>
|
2019-08-06 04:02:36 +00:00
|
|
|
/home/container/{join(directory, values.name).replace(/^(\.\.\/|\/)+/, '')}
|
|
|
|
</p>
|
2019-08-06 04:55:33 +00:00
|
|
|
}
|
2019-08-03 05:22:01 +00:00
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
)}
|
|
|
|
</Formik>
|
|
|
|
);
|
|
|
|
};
|