Skip to content

Commit fa9800f

Browse files
committed
Fix some SWR funkiness
1 parent 5da9824 commit fa9800f

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

resources/scripts/components/server/files/FileDropdownMenu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default ({ file }: { file: FileObject }) => {
9696
return (
9797
<DropdownMenu
9898
renderToggle={onClick => (
99-
<div onClick={onClick}>
99+
<div css={tw`p-3 hover:text-white`} onClick={onClick}>
100100
<FontAwesomeIcon icon={faEllipsisH}/>
101101
<RenameFileModal
102102
file={file}

resources/scripts/components/server/files/FileManagerContainer.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export default () => {
2828
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
2929

3030
useEffect(() => {
31+
// We won't automatically mutate the store when the component re-mounts, otherwise because of
32+
// my (horrible) programming this fires off way more than we intend it to.
33+
mutate();
34+
3135
setDirectory(hash.length > 0 ? hash : '/');
3236
}, [ hash ]);
3337

resources/scripts/components/server/files/RenameFileModal.tsx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { ServerContext } from '@/state/server';
88
import { FileObject } from '@/api/server/files/loadDirectory';
99
import tw from 'twin.macro';
1010
import Button from '@/components/elements/Button';
11+
import useServer from '@/plugins/useServer';
12+
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
13+
import useFlash from '@/plugins/useFlash';
1114

1215
interface FormikValues {
1316
name: string;
@@ -16,37 +19,34 @@ interface FormikValues {
1619
type Props = RequiredModalProps & { file: FileObject; useMoveTerminology?: boolean };
1720

1821
export 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`}>

resources/scripts/plugins/useFileManagerSwr.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ export default () => {
1111
return useSWR<FileObject[]>(
1212
`${uuid}:files:${hash}`,
1313
() => loadDirectory(uuid, cleanDirectoryPath(hash)),
14+
{
15+
revalidateOnMount: false,
16+
refreshInterval: 0,
17+
}
1418
);
1519
};

0 commit comments

Comments
 (0)