Skip to content

Commit b72a770

Browse files
committed
Don't execute unnecessary HTTP requests when browing a file directory
1 parent 7630020 commit b72a770

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
import React, { useEffect, useState } from 'react';
22
import { ServerContext } from '@/state/server';
3-
import { NavLink, useParams } from 'react-router-dom';
3+
import { NavLink } from 'react-router-dom';
4+
import { cleanDirectoryPath } from '@/helpers';
45

56
interface Props {
67
withinFileEditor?: boolean;
78
isNewFile?: boolean;
89
}
910

1011
export default ({ withinFileEditor, isNewFile }: Props) => {
11-
const { action } = useParams();
1212
const [ file, setFile ] = useState<string | null>(null);
1313
const id = ServerContext.useStoreState(state => state.server.data!.id);
1414
const directory = ServerContext.useStoreState(state => state.files.directory);
15-
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
1615

1716
useEffect(() => {
18-
const parts = window.location.hash.replace(/^#(\/)*/, '/').split('/');
17+
const parts = cleanDirectoryPath(window.location.hash).split('/');
1918

2019
if (withinFileEditor && !isNewFile) {
2120
setFile(parts.pop() || null);
2221
}
23-
24-
setDirectory(parts.join('/'));
25-
}, [ withinFileEditor, isNewFile, setDirectory ]);
22+
}, [ withinFileEditor, isNewFile ]);
2623

2724
const breadcrumbs = (): { name: string; path?: string }[] => directory.split('/')
2825
.filter(directory => !!directory)
@@ -39,7 +36,6 @@ export default ({ withinFileEditor, isNewFile }: Props) => {
3936
/<span className={'px-1 text-neutral-300'}>home</span>/
4037
<NavLink
4138
to={`/server/${id}/files`}
42-
onClick={() => setDirectory('/')}
4339
className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}
4440
>
4541
container
@@ -50,7 +46,6 @@ export default ({ withinFileEditor, isNewFile }: Props) => {
5046
<React.Fragment key={index}>
5147
<NavLink
5248
to={`/server/${id}/files#${crumb.path}`}
53-
onClick={() => setDirectory(crumb.path!)}
5449
className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}
5550
>
5651
{crumb.name}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ export default () => {
2222
const [ loading, setLoading ] = useState(true);
2323
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2424
const { id } = ServerContext.useStoreState(state => state.server.data!);
25-
const { contents: files, directory } = ServerContext.useStoreState(state => state.files);
25+
const { contents: files } = ServerContext.useStoreState(state => state.files);
2626
const { getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files);
2727

2828
useEffect(() => {
2929
setLoading(true);
3030
clearFlashes();
3131

32-
getDirectoryContents(window.location.hash.replace(/^#(\/)*/, '/'))
32+
getDirectoryContents(window.location.hash)
3333
.then(() => setLoading(false))
3434
.catch(error => {
3535
console.error(error.message, { error });
3636
addError({ message: httpErrorToHuman(error), key: 'files' });
3737
});
38-
}, [ directory ]);
38+
}, []);
3939

4040
return (
4141
<div className={'my-10 mb-6'}>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
22
import { faFileImport } from '@fortawesome/free-solid-svg-icons/faFileImport';
33
import { faFileAlt } from '@fortawesome/free-solid-svg-icons/faFileAlt';
44
import { faFolder } from '@fortawesome/free-solid-svg-icons/faFolder';
5-
import { bytesToHuman } from '@/helpers';
5+
import { bytesToHuman, cleanDirectoryPath } from '@/helpers';
66
import differenceInHours from 'date-fns/difference_in_hours';
77
import format from 'date-fns/format';
88
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
@@ -16,7 +16,7 @@ import useRouter from 'use-react-router';
1616
export default ({ file }: { file: FileObject }) => {
1717
const directory = ServerContext.useStoreState(state => state.files.directory);
1818
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
19-
const { match } = useRouter();
19+
const { match, history } = useRouter();
2020

2121
return (
2222
<div
@@ -27,7 +27,7 @@ export default ({ file }: { file: FileObject }) => {
2727
`}
2828
>
2929
<NavLink
30-
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${directory}/${file.name}`}
30+
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${cleanDirectoryPath(`${directory}/${file.name}`)}`}
3131
className={'flex flex-1 text-neutral-300 no-underline p-3'}
3232
onClick={e => {
3333
// Don't rely on the onClick to work with the generated URL. Because of the way this
@@ -38,7 +38,7 @@ export default ({ file }: { file: FileObject }) => {
3838
if (!file.isFile) {
3939
e.preventDefault();
4040

41-
window.location.hash = `#${directory}/${file.name}`;
41+
history.push(`#${cleanDirectoryPath(`${directory}/${file.name}`)}`);
4242
setDirectory(`${directory}/${file.name}`);
4343
}
4444
}}

resources/scripts/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ export function bytesToHuman (bytes: number): string {
1111
export const bytesToMegabytes = (bytes: number) => Math.floor(bytes / 1000 / 1000);
1212

1313
export const randomInt = (low: number, high: number) => Math.floor(Math.random() * (high - low) + low);
14+
15+
export const cleanDirectoryPath = (path: string) => path.replace(/(^#\/*)|(\/(\/*))|(^$)/g, '/');

resources/scripts/routers/ServerRouter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const ServerRouter = ({ match, location }: RouteComponentProps<{ id: string }>)
6464
<Spinner size={'large'}/>
6565
</div>
6666
:
67-
<Switch location={location} key={'server-switch'}>
67+
<Switch location={location}>
6868
<Route path={`${match.path}`} component={ServerConsole} exact/>
6969
<Route path={`${match.path}/files`} component={FileManagerContainer} exact/>
7070
<Route

resources/scripts/state/server/files.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import loadDirectory, { FileObject } from '@/api/server/files/loadDirectory';
22
import { action, Action, thunk, Thunk } from 'easy-peasy';
33
import { ServerStore } from '@/state/server/index';
4+
import { cleanDirectoryPath } from '@/helpers';
45

56
export interface ServerFileStore {
67
directory: string;
@@ -22,7 +23,7 @@ const files: ServerFileStore = {
2223
return;
2324
}
2425

25-
const contents = await loadDirectory(server.uuid, payload);
26+
const contents = await loadDirectory(server.uuid, cleanDirectoryPath(payload));
2627

2728
actions.setDirectory(payload.length === 0 ? '/' : payload);
2829
actions.setContents(contents);
@@ -47,7 +48,7 @@ const files: ServerFileStore = {
4748
}),
4849

4950
setDirectory: action((state, payload) => {
50-
state.directory = payload.length === 0 ? '/' : payload;
51+
state.directory = cleanDirectoryPath(payload)
5152
}),
5253
};
5354

0 commit comments

Comments
 (0)