@@ -8,6 +8,9 @@ import { ServerContext } from '@/state/server';
88import { FileObject } from '@/api/server/files/loadDirectory' ;
99import tw from 'twin.macro' ;
1010import Button from '@/components/elements/Button' ;
11+ import useServer from '@/plugins/useServer' ;
12+ import useFileManagerSwr from '@/plugins/useFileManagerSwr' ;
13+ import useFlash from '@/plugins/useFlash' ;
1114
1215interface FormikValues {
1316 name : string ;
@@ -16,37 +19,34 @@ interface FormikValues {
1619type Props = RequiredModalProps & { file : FileObject ; useMoveTerminology ?: boolean } ;
1720
1821export default ( { file, useMoveTerminology, ...props } : Props ) => {
19- const uuid = ServerContext . useStoreState ( state => state . server . data ! . uuid ) ;
22+ const { uuid } = useServer ( ) ;
23+ const { mutate } = useFileManagerSwr ( ) ;
24+ const { clearAndAddHttpError } = useFlash ( ) ;
2025 const directory = ServerContext . useStoreState ( state => state . files . directory ) ;
21- const { pushFile, removeFile } = ServerContext . useStoreActions ( actions => actions . files ) ;
2226
23- const submit = ( values : FormikValues , { setSubmitting } : FormikHelpers < FormikValues > ) => {
24- const renameFrom = join ( directory , file . name ) ;
25- const renameTo = join ( directory , values . name ) ;
27+ const submit = ( { name } : FormikValues , { setSubmitting } : FormikHelpers < FormikValues > ) => {
28+ const len = name . split ( '/' ) . length ;
29+ if ( ! useMoveTerminology && len === 1 ) {
30+ // Rename the file within this directory.
31+ mutate ( files => files . map ( f => f . uuid === file . uuid ? { ...f , name } : f ) , false ) ;
32+ } else if ( ( useMoveTerminology || len > 1 ) && file . uuid . length ) {
33+ // Remove the file from this directory since they moved it elsewhere.
34+ mutate ( files => files . filter ( f => f . uuid !== file . uuid ) , false ) ;
35+ }
2636
37+ const renameFrom = join ( directory , file . name ) ;
38+ const renameTo = join ( directory , name ) ;
2739 renameFile ( uuid , { renameFrom, renameTo } )
28- . then ( ( ) => {
29- if ( ! useMoveTerminology && values . name . split ( '/' ) . length === 1 ) {
30- pushFile ( { ...file , name : values . name } ) ;
31- }
32-
33- if ( ( useMoveTerminology || values . name . split ( '/' ) . length > 1 ) && file . uuid . length > 0 ) {
34- removeFile ( file . uuid ) ;
35- }
36-
37- props . onDismissed ( ) ;
38- } )
40+ . then ( ( ) => props . onDismissed ( ) )
3941 . catch ( error => {
42+ mutate ( ) ;
4043 setSubmitting ( false ) ;
41- console . error ( error ) ;
44+ clearAndAddHttpError ( { key : 'files' , error } ) ;
4245 } ) ;
4346 } ;
4447
4548 return (
46- < Formik
47- onSubmit = { submit }
48- initialValues = { { name : file . name } }
49- >
49+ < Formik onSubmit = { submit } initialValues = { { name : file . name } } >
5050 { ( { isSubmitting, values } ) => (
5151 < Modal { ...props } dismissable = { ! isSubmitting } showSpinnerOverlay = { isSubmitting } >
5252 < Form css = { tw `m-0` } >
0 commit comments