From 4bfc91a30ec8b12e47d97a792762b5b4586a6e06 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Tue, 22 Sep 2020 20:50:44 -0700 Subject: [PATCH] Fix mutation of directory name with slashes in it; closes #2377 --- .../server/files/NewDirectoryButton.tsx | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/resources/scripts/components/server/files/NewDirectoryButton.tsx b/resources/scripts/components/server/files/NewDirectoryButton.tsx index 14e3d2e53..c8e39b22f 100644 --- a/resources/scripts/components/server/files/NewDirectoryButton.tsx +++ b/resources/scripts/components/server/files/NewDirectoryButton.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import Modal from '@/components/elements/Modal'; import { ServerContext } from '@/state/server'; import { Form, Formik, FormikHelpers } from 'formik'; @@ -12,6 +12,7 @@ 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'; interface Values { directoryName: string; @@ -22,8 +23,8 @@ const schema = object().shape({ }); const generateDirectoryData = (name: string): FileObject => ({ - key: `dir_${name}`, - name: name, + key: `dir_${name.split('/', 1)[0] ?? name}`, + name: name.split('/', 1)[0] ?? name, mode: '0644', size: 0, isFile: false, @@ -37,12 +38,20 @@ const generateDirectoryData = (name: string): FileObject => ({ export default ({ className }: WithClassname) => { const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); - const { clearAndAddHttpError } = useFlash(); + const { clearFlashes, clearAndAddHttpError } = useFlash(); const [ visible, setVisible ] = useState(false); const { mutate } = useFileManagerSwr(); const directory = ServerContext.useStoreState(state => state.files.directory); + useEffect(() => { + if (visible) { + return () => { + clearFlashes('files:directory-modal'); + }; + } + }, [ visible ]); + const submit = ({ directoryName }: Values, { setSubmitting }: FormikHelpers) => { createDirectory(uuid, directory, directoryName) .then(() => mutate(data => [ ...data, generateDirectoryData(directoryName) ], false)) @@ -50,7 +59,7 @@ export default ({ className }: WithClassname) => { .catch(error => { console.error(error); setSubmitting(false); - clearAndAddHttpError({ key: 'files', error }); + clearAndAddHttpError({ key: 'files:directory-modal', error }); }); }; @@ -71,6 +80,7 @@ export default ({ className }: WithClassname) => { resetForm(); }} > +