Fix some SWR funkiness

This commit is contained in:
Dane Everitt 2020-07-10 22:53:52 -07:00
parent 5da9824fb7
commit fa9800fa88
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
4 changed files with 30 additions and 22 deletions

View File

@ -96,7 +96,7 @@ export default ({ file }: { file: FileObject }) => {
return ( return (
<DropdownMenu <DropdownMenu
renderToggle={onClick => ( renderToggle={onClick => (
<div onClick={onClick}> <div css={tw`p-3 hover:text-white`} onClick={onClick}>
<FontAwesomeIcon icon={faEllipsisH}/> <FontAwesomeIcon icon={faEllipsisH}/>
<RenameFileModal <RenameFileModal
file={file} file={file}

View File

@ -28,6 +28,10 @@ export default () => {
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory); const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
useEffect(() => { useEffect(() => {
// We won't automatically mutate the store when the component re-mounts, otherwise because of
// my (horrible) programming this fires off way more than we intend it to.
mutate();
setDirectory(hash.length > 0 ? hash : '/'); setDirectory(hash.length > 0 ? hash : '/');
}, [ hash ]); }, [ hash ]);

View File

@ -8,6 +8,9 @@ import { ServerContext } from '@/state/server';
import { FileObject } from '@/api/server/files/loadDirectory'; import { FileObject } from '@/api/server/files/loadDirectory';
import tw from 'twin.macro'; import tw from 'twin.macro';
import Button from '@/components/elements/Button'; import Button from '@/components/elements/Button';
import useServer from '@/plugins/useServer';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
import useFlash from '@/plugins/useFlash';
interface FormikValues { interface FormikValues {
name: string; name: string;
@ -16,37 +19,34 @@ interface FormikValues {
type Props = RequiredModalProps & { file: FileObject; useMoveTerminology?: boolean }; type Props = RequiredModalProps & { file: FileObject; useMoveTerminology?: boolean };
export default ({ file, useMoveTerminology, ...props }: Props) => { export default ({ file, useMoveTerminology, ...props }: Props) => {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const { uuid } = useServer();
const { mutate } = useFileManagerSwr();
const { clearAndAddHttpError } = useFlash();
const directory = ServerContext.useStoreState(state => state.files.directory); const directory = ServerContext.useStoreState(state => state.files.directory);
const { pushFile, removeFile } = ServerContext.useStoreActions(actions => actions.files);
const submit = (values: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => { const submit = ({ name }: FormikValues, { setSubmitting }: FormikHelpers<FormikValues>) => {
const len = name.split('/').length;
if (!useMoveTerminology && len === 1) {
// Rename the file within this directory.
mutate(files => files.map(f => f.uuid === file.uuid ? { ...f, name } : f), false);
} else if ((useMoveTerminology || len > 1) && file.uuid.length) {
// Remove the file from this directory since they moved it elsewhere.
mutate(files => files.filter(f => f.uuid !== file.uuid), false);
}
const renameFrom = join(directory, file.name); const renameFrom = join(directory, file.name);
const renameTo = join(directory, values.name); const renameTo = join(directory, name);
renameFile(uuid, { renameFrom, renameTo }) renameFile(uuid, { renameFrom, renameTo })
.then(() => { .then(() => props.onDismissed())
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);
}
props.onDismissed();
})
.catch(error => { .catch(error => {
mutate();
setSubmitting(false); setSubmitting(false);
console.error(error); clearAndAddHttpError({ key: 'files', error });
}); });
}; };
return ( return (
<Formik <Formik onSubmit={submit} initialValues={{ name: file.name }}>
onSubmit={submit}
initialValues={{ name: file.name }}
>
{({ isSubmitting, values }) => ( {({ isSubmitting, values }) => (
<Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}> <Modal {...props} dismissable={!isSubmitting} showSpinnerOverlay={isSubmitting}>
<Form css={tw`m-0`}> <Form css={tw`m-0`}>

View File

@ -11,5 +11,9 @@ export default () => {
return useSWR<FileObject[]>( return useSWR<FileObject[]>(
`${uuid}:files:${hash}`, `${uuid}:files:${hash}`,
() => loadDirectory(uuid, cleanDirectoryPath(hash)), () => loadDirectory(uuid, cleanDirectoryPath(hash)),
{
revalidateOnMount: false,
refreshInterval: 0,
}
); );
}; };