Skip to content

Commit 60775c6

Browse files
committed
Update handling of links in the file manager
1 parent f7def01 commit 60775c6

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,28 @@ import { Actions, useStoreActions } from 'easy-peasy';
55
import { ApplicationStore } from '@/state';
66
import { httpErrorToHuman } from '@/api/http';
77
import { CSSTransition } from 'react-transition-group';
8-
import { Link } from 'react-router-dom';
98
import Spinner from '@/components/elements/Spinner';
109
import FileObjectRow from '@/components/server/files/FileObjectRow';
1110

1211
export default () => {
1312
const [ loading, setLoading ] = useState(true);
1413
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
15-
const { contents: files } = ServerContext.useStoreState(state => state.files);
16-
const getDirectoryContents = ServerContext.useStoreActions(actions => actions.files.getDirectoryContents);
17-
18-
const urlDirectory = window.location.hash.replace(/^#(\/)+/, '/');
14+
const { contents: files, directory } = ServerContext.useStoreState(state => state.files);
15+
const { setDirectory, getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files);
1916

2017
const load = () => {
2118
setLoading(true);
2219
clearFlashes();
2320

24-
getDirectoryContents(urlDirectory)
21+
getDirectoryContents(window.location.hash.replace(/^#(\/)*/, '/'))
2522
.then(() => setLoading(false))
2623
.catch(error => {
27-
if (error.response && error.response.status === 404) {
28-
window.location.hash = '#/';
29-
return;
30-
}
31-
3224
console.error(error.message, { error });
3325
addError({ message: httpErrorToHuman(error), key: 'files' });
3426
});
3527
};
3628

37-
const breadcrumbs = (): { name: string; path?: string }[] => urlDirectory.split('/')
29+
const breadcrumbs = (): { name: string; path?: string }[] => directory.split('/')
3830
.filter(directory => !!directory)
3931
.map((directory, index, dirs) => {
4032
if (index === dirs.length - 1) {
@@ -44,27 +36,32 @@ export default () => {
4436
return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` };
4537
});
4638

47-
useEffect(() => load(), []);
39+
useEffect(() => load(), [ directory ]);
4840

4941
return (
5042
<div className={'my-10 mb-6'}>
51-
<FlashMessageRender byKey={'files'}/>
43+
<FlashMessageRender byKey={'files'} className={'mb-4'}/>
5244
<React.Fragment>
5345
<div className={'flex items-center text-sm mb-4 text-neutral-500'}>
5446
/<span className={'px-1 text-neutral-300'}>home</span>/
55-
<Link to={'#'} className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}>
47+
<a
48+
href={'#'}
49+
onClick={() => setDirectory('/')}
50+
className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}
51+
>
5652
container
57-
</Link>/
53+
</a>/
5854
{
5955
breadcrumbs().map((crumb, index) => (
6056
crumb.path ?
6157
<React.Fragment key={index}>
62-
<Link
63-
to={`#${crumb.path}`}
58+
<a
59+
href={`#${crumb.path}`}
60+
onClick={() => setDirectory(crumb.path!)}
6461
className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}
6562
>
6663
{crumb.name}
67-
</Link>/
64+
</a>/
6865
</React.Fragment>
6966
:
7067
<span key={index} className={'px-1 text-neutral-300'}>{crumb.name}</span>

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ServerContext } from '@/state/server';
1313

1414
export default ({ file }: { file: FileObject }) => {
1515
const directory = ServerContext.useStoreState(state => state.files.directory);
16+
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
1617

1718
return (
1819
<div
@@ -26,7 +27,17 @@ export default ({ file }: { file: FileObject }) => {
2627
href={file.isFile ? undefined : `#${directory}/${file.name}`}
2728
className={'flex flex-1 text-neutral-300 no-underline'}
2829
onClick={e => {
29-
file.isFile && e.preventDefault();
30+
e.preventDefault();
31+
32+
// Don't rely on the onClick to work with the generated URL. Because of the way this
33+
// component re-renders you'll get redirected into a nested directory structure since
34+
// it'll cause the directory variable to update right away when you click.
35+
//
36+
// Just trust me future me, leave this be.
37+
if (!file.isFile) {
38+
window.location.hash = `#${directory}/${file.name}`;
39+
setDirectory(`${directory}/${file.name}`);
40+
}
3041
}}
3142
>
3243
<div className={'flex-none text-neutral-400 mr-4 text-lg pl-3'}>

resources/scripts/state/server/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ServerFileStore {
1313
}
1414

1515
const files: ServerFileStore = {
16-
directory: '',
16+
directory: '/',
1717
contents: [],
1818

1919
getDirectoryContents: thunk(async (actions, payload, { getStoreState }) => {

0 commit comments

Comments
 (0)