Update handling of links in the file manager

This commit is contained in:
Dane Everitt 2019-08-05 21:52:48 -07:00
parent f7def01442
commit 60775c6370
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 29 additions and 21 deletions

View file

@ -5,36 +5,28 @@ import { Actions, useStoreActions } from 'easy-peasy';
import { ApplicationStore } from '@/state'; import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http'; import { httpErrorToHuman } from '@/api/http';
import { CSSTransition } from 'react-transition-group'; import { CSSTransition } from 'react-transition-group';
import { Link } from 'react-router-dom';
import Spinner from '@/components/elements/Spinner'; import Spinner from '@/components/elements/Spinner';
import FileObjectRow from '@/components/server/files/FileObjectRow'; import FileObjectRow from '@/components/server/files/FileObjectRow';
export default () => { export default () => {
const [ loading, setLoading ] = useState(true); const [ loading, setLoading ] = useState(true);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
const { contents: files } = ServerContext.useStoreState(state => state.files); const { contents: files, directory } = ServerContext.useStoreState(state => state.files);
const getDirectoryContents = ServerContext.useStoreActions(actions => actions.files.getDirectoryContents); const { setDirectory, getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files);
const urlDirectory = window.location.hash.replace(/^#(\/)+/, '/');
const load = () => { const load = () => {
setLoading(true); setLoading(true);
clearFlashes(); clearFlashes();
getDirectoryContents(urlDirectory) getDirectoryContents(window.location.hash.replace(/^#(\/)*/, '/'))
.then(() => setLoading(false)) .then(() => setLoading(false))
.catch(error => { .catch(error => {
if (error.response && error.response.status === 404) {
window.location.hash = '#/';
return;
}
console.error(error.message, { error }); console.error(error.message, { error });
addError({ message: httpErrorToHuman(error), key: 'files' }); addError({ message: httpErrorToHuman(error), key: 'files' });
}); });
}; };
const breadcrumbs = (): { name: string; path?: string }[] => urlDirectory.split('/') const breadcrumbs = (): { name: string; path?: string }[] => directory.split('/')
.filter(directory => !!directory) .filter(directory => !!directory)
.map((directory, index, dirs) => { .map((directory, index, dirs) => {
if (index === dirs.length - 1) { if (index === dirs.length - 1) {
@ -44,27 +36,32 @@ export default () => {
return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` }; return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` };
}); });
useEffect(() => load(), []); useEffect(() => load(), [ directory ]);
return ( return (
<div className={'my-10 mb-6'}> <div className={'my-10 mb-6'}>
<FlashMessageRender byKey={'files'}/> <FlashMessageRender byKey={'files'} className={'mb-4'}/>
<React.Fragment> <React.Fragment>
<div className={'flex items-center text-sm mb-4 text-neutral-500'}> <div className={'flex items-center text-sm mb-4 text-neutral-500'}>
/<span className={'px-1 text-neutral-300'}>home</span>/ /<span className={'px-1 text-neutral-300'}>home</span>/
<Link to={'#'} className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}> <a
href={'#'}
onClick={() => setDirectory('/')}
className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}
>
container container
</Link>/ </a>/
{ {
breadcrumbs().map((crumb, index) => ( breadcrumbs().map((crumb, index) => (
crumb.path ? crumb.path ?
<React.Fragment key={index}> <React.Fragment key={index}>
<Link <a
to={`#${crumb.path}`} href={`#${crumb.path}`}
onClick={() => setDirectory(crumb.path!)}
className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'} className={'px-1 text-neutral-200 no-underline hover:text-neutral-100'}
> >
{crumb.name} {crumb.name}
</Link>/ </a>/
</React.Fragment> </React.Fragment>
: :
<span key={index} className={'px-1 text-neutral-300'}>{crumb.name}</span> <span key={index} className={'px-1 text-neutral-300'}>{crumb.name}</span>

View file

@ -13,6 +13,7 @@ import { ServerContext } from '@/state/server';
export default ({ file }: { file: FileObject }) => { export default ({ file }: { file: FileObject }) => {
const directory = ServerContext.useStoreState(state => state.files.directory); const directory = ServerContext.useStoreState(state => state.files.directory);
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
return ( return (
<div <div
@ -26,7 +27,17 @@ export default ({ file }: { file: FileObject }) => {
href={file.isFile ? undefined : `#${directory}/${file.name}`} href={file.isFile ? undefined : `#${directory}/${file.name}`}
className={'flex flex-1 text-neutral-300 no-underline'} className={'flex flex-1 text-neutral-300 no-underline'}
onClick={e => { onClick={e => {
file.isFile && e.preventDefault(); e.preventDefault();
// Don't rely on the onClick to work with the generated URL. Because of the way this
// component re-renders you'll get redirected into a nested directory structure since
// it'll cause the directory variable to update right away when you click.
//
// Just trust me future me, leave this be.
if (!file.isFile) {
window.location.hash = `#${directory}/${file.name}`;
setDirectory(`${directory}/${file.name}`);
}
}} }}
> >
<div className={'flex-none text-neutral-400 mr-4 text-lg pl-3'}> <div className={'flex-none text-neutral-400 mr-4 text-lg pl-3'}>

View file

@ -13,7 +13,7 @@ export interface ServerFileStore {
} }
const files: ServerFileStore = { const files: ServerFileStore = {
directory: '', directory: '/',
contents: [], contents: [],
getDirectoryContents: thunk(async (actions, payload, { getStoreState }) => { getDirectoryContents: thunk(async (actions, payload, { getStoreState }) => {