Nicer errors when managing files

This commit is contained in:
Dane Everitt 2020-04-17 11:45:20 -07:00
parent 88374de38c
commit 004a5692cf
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 58 additions and 12 deletions

View file

@ -2,25 +2,55 @@ import React from 'react';
import PageContentBlock from '@/components/elements/PageContentBlock'; import PageContentBlock from '@/components/elements/PageContentBlock';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faArrowLeft } from '@fortawesome/free-solid-svg-icons/faArrowLeft'; import { faArrowLeft } from '@fortawesome/free-solid-svg-icons/faArrowLeft';
import { faSyncAlt } from '@fortawesome/free-solid-svg-icons/faSyncAlt';
import classNames from 'classnames';
import styled from 'styled-components';
interface Props { interface BaseProps {
title?: string; title?: string;
message: string; message: string;
onRetry?: () => void;
onBack?: () => void; onBack?: () => void;
} }
export default ({ title, message, onBack }: Props) => ( interface PropsWithRetry extends BaseProps {
onRetry?: () => void;
onBack?: never | undefined;
}
interface PropsWithBack extends BaseProps {
onBack?: () => void;
onRetry?: never | undefined;
}
type Props = PropsWithBack | PropsWithRetry;
const ActionButton = styled.button`
${tw`rounded-full w-8 h-8 flex items-center justify-center`};
&.hover\\:spin:hover {
animation: spin 2s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
`;
export default ({ title, message, onBack, onRetry }: Props) => (
<PageContentBlock> <PageContentBlock>
<div className={'flex justify-center'}> <div className={'flex justify-center'}>
<div className={'w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center relative'}> <div className={'w-full sm:w-3/4 md:w-1/2 p-12 md:p-20 bg-neutral-100 rounded-lg shadow-lg text-center relative'}>
{typeof onBack === 'function' && {(typeof onBack === 'function' || typeof onRetry === 'function') &&
<div className={'absolute pin-l pin-t ml-4 mt-4'}> <div className={'absolute pin-l pin-t ml-4 mt-4'}>
<button <ActionButton
onClick={() => onBack()} onClick={() => onRetry ? onRetry() : (onBack ? onBack() : null)}
className={'rounded-full btn btn-primary w-8 h-8 flex items-center justify-center'} className={classNames('btn btn-primary', { 'hover:spin': !!onRetry })}
> >
<FontAwesomeIcon icon={faArrowLeft}/> <FontAwesomeIcon icon={onRetry ? faSyncAlt : faArrowLeft}/>
</button> </ActionButton>
</div> </div>
} }
<img src={'/assets/svgs/server_error.svg'} className={'w-2/3 h-auto select-none'}/> <img src={'/assets/svgs/server_error.svg'} className={'w-2/3 h-auto select-none'}/>

View file

@ -13,6 +13,8 @@ import NewDirectoryButton from '@/components/server/files/NewDirectoryButton';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import Can from '@/components/elements/Can'; import Can from '@/components/elements/Can';
import PageContentBlock from '@/components/elements/PageContentBlock'; import PageContentBlock from '@/components/elements/PageContentBlock';
import ServerError from '@/components/screens/ServerError';
import useRouter from 'use-react-router';
const sortFiles = (files: FileObject[]): FileObject[] => { const sortFiles = (files: FileObject[]): FileObject[] => {
return files.sort((a, b) => a.name.localeCompare(b.name)) return files.sort((a, b) => a.name.localeCompare(b.name))
@ -20,24 +22,38 @@ const sortFiles = (files: FileObject[]): FileObject[] => {
}; };
export default () => { export default () => {
const [ error, setError ] = useState('');
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 { id } = ServerContext.useStoreState(state => state.server.data!); const { id } = ServerContext.useStoreState(state => state.server.data!);
const { contents: files } = ServerContext.useStoreState(state => state.files); const { contents: files } = ServerContext.useStoreState(state => state.files);
const { getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files); const { getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files);
useEffect(() => { const loadContents = () => {
setLoading(true); setError('');
clearFlashes(); clearFlashes();
setLoading(true);
getDirectoryContents(window.location.hash) getDirectoryContents(window.location.hash)
.then(() => setLoading(false)) .then(() => setLoading(false))
.catch(error => { .catch(error => {
console.error(error.message, { error }); console.error(error.message, { error });
addError({ message: httpErrorToHuman(error), key: 'files' }); setError(httpErrorToHuman(error));
}); });
};
useEffect(() => {
loadContents();
}, []); }, []);
if (error) {
return (
<ServerError
message={error}
onRetry={() => loadContents()}
/>
);
}
return ( return (
<PageContentBlock> <PageContentBlock>
<FlashMessageRender byKey={'files'} className={'mb-4'}/> <FlashMessageRender byKey={'files'} className={'mb-4'}/>