2019-07-28 03:23:51 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { Actions, useStoreActions } from 'easy-peasy';
|
|
|
|
import { ApplicationStore } from '@/state';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import { CSSTransition } from 'react-transition-group';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2019-07-28 03:36:27 +00:00
|
|
|
import FileObjectRow from '@/components/server/files/FileObjectRow';
|
2019-10-26 20:16:27 +00:00
|
|
|
import FileManagerBreadcrumbs from '@/components/server/files/FileManagerBreadcrumbs';
|
2019-12-16 03:10:01 +00:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
|
|
|
import NewDirectoryButton from '@/components/server/files/NewDirectoryButton';
|
2019-12-22 00:38:40 +00:00
|
|
|
import { Link } from 'react-router-dom';
|
2020-03-30 04:42:02 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-17 18:17:01 +00:00
|
|
|
import PageContentBlock from '@/components/elements/PageContentBlock';
|
2020-04-17 18:45:20 +00:00
|
|
|
import ServerError from '@/components/screens/ServerError';
|
2020-07-05 00:57:24 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2019-12-16 03:10:01 +00:00
|
|
|
|
|
|
|
const sortFiles = (files: FileObject[]): FileObject[] => {
|
|
|
|
return files.sort((a, b) => a.name.localeCompare(b.name))
|
|
|
|
.sort((a, b) => a.isFile === b.isFile ? 0 : (a.isFile ? 1 : -1));
|
|
|
|
};
|
2019-07-28 03:23:51 +00:00
|
|
|
|
|
|
|
export default () => {
|
2020-04-17 18:45:20 +00:00
|
|
|
const [ error, setError ] = useState('');
|
2019-07-28 03:23:51 +00:00
|
|
|
const [ loading, setLoading ] = useState(true);
|
2020-07-05 00:57:24 +00:00
|
|
|
const { clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-12-22 00:38:40 +00:00
|
|
|
const { id } = ServerContext.useStoreState(state => state.server.data!);
|
2020-04-10 20:57:24 +00:00
|
|
|
const { contents: files } = ServerContext.useStoreState(state => state.files);
|
2019-10-26 20:16:27 +00:00
|
|
|
const { getDirectoryContents } = ServerContext.useStoreActions(actions => actions.files);
|
2019-07-28 03:23:51 +00:00
|
|
|
|
2020-04-17 18:45:20 +00:00
|
|
|
const loadContents = () => {
|
|
|
|
setError('');
|
2019-07-28 03:23:51 +00:00
|
|
|
clearFlashes();
|
2020-04-17 18:45:20 +00:00
|
|
|
setLoading(true);
|
2020-04-10 20:57:24 +00:00
|
|
|
getDirectoryContents(window.location.hash)
|
2019-08-04 21:58:31 +00:00
|
|
|
.then(() => setLoading(false))
|
2019-07-28 03:23:51 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error.message, { error });
|
2020-04-17 18:45:20 +00:00
|
|
|
setError(httpErrorToHuman(error));
|
2019-07-28 03:23:51 +00:00
|
|
|
});
|
2020-04-17 18:45:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadContents();
|
2020-04-10 20:57:24 +00:00
|
|
|
}, []);
|
2019-07-28 03:23:51 +00:00
|
|
|
|
2020-04-17 18:45:20 +00:00
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<ServerError
|
|
|
|
message={error}
|
|
|
|
onRetry={() => loadContents()}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-28 03:23:51 +00:00
|
|
|
return (
|
2020-04-17 18:17:01 +00:00
|
|
|
<PageContentBlock>
|
2020-07-05 00:57:24 +00:00
|
|
|
<FlashMessageRender byKey={'files'} css={tw`mb-4`}/>
|
2019-07-28 03:23:51 +00:00
|
|
|
<React.Fragment>
|
2019-10-26 20:16:27 +00:00
|
|
|
<FileManagerBreadcrumbs/>
|
2019-07-28 03:23:51 +00:00
|
|
|
{
|
|
|
|
loading ?
|
2020-07-05 00:57:24 +00:00
|
|
|
<Spinner size={'large'} centered/>
|
2019-07-28 03:23:51 +00:00
|
|
|
:
|
2019-12-16 03:10:01 +00:00
|
|
|
<React.Fragment>
|
|
|
|
{!files.length ?
|
2020-07-05 00:57:24 +00:00
|
|
|
<p css={tw`text-sm text-neutral-400 text-center`}>
|
2019-12-16 03:10:01 +00:00
|
|
|
This directory seems to be empty.
|
|
|
|
</p>
|
|
|
|
:
|
2020-07-05 20:56:04 +00:00
|
|
|
<CSSTransition classNames={'fade'} timeout={150} appear in>
|
2019-12-16 03:10:01 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<div>
|
|
|
|
{files.length > 250 ?
|
|
|
|
<React.Fragment>
|
2020-07-05 00:57:24 +00:00
|
|
|
<div css={tw`rounded bg-yellow-400 mb-px p-3`}>
|
|
|
|
<p css={tw`text-yellow-900 text-sm text-center`}>
|
2019-12-16 03:10:01 +00:00
|
|
|
This directory is too large to display in the browser,
|
2019-12-22 00:40:28 +00:00
|
|
|
limiting the output to the first 250 files.
|
2019-12-16 03:10:01 +00:00
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
{
|
|
|
|
sortFiles(files.slice(0, 250)).map(file => (
|
|
|
|
<FileObjectRow key={file.uuid} file={file}/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
</React.Fragment>
|
|
|
|
:
|
|
|
|
sortFiles(files).map(file => (
|
2019-09-29 22:25:23 +00:00
|
|
|
<FileObjectRow key={file.uuid} file={file}/>
|
|
|
|
))
|
|
|
|
}
|
2019-12-16 03:10:01 +00:00
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
</CSSTransition>
|
|
|
|
}
|
2020-03-30 04:42:02 +00:00
|
|
|
<Can action={'file.create'}>
|
2020-07-05 00:57:24 +00:00
|
|
|
<div css={tw`flex justify-end mt-8`}>
|
2020-03-30 04:42:02 +00:00
|
|
|
<NewDirectoryButton/>
|
2020-07-05 00:57:24 +00:00
|
|
|
<Button
|
|
|
|
// @ts-ignore
|
|
|
|
as={Link}
|
2020-03-30 04:42:02 +00:00
|
|
|
to={`/server/${id}/files/new${window.location.hash}`}
|
|
|
|
>
|
|
|
|
New File
|
2020-07-05 00:57:24 +00:00
|
|
|
</Button>
|
2020-03-30 04:42:02 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
2019-12-16 03:10:01 +00:00
|
|
|
</React.Fragment>
|
2019-07-28 03:23:51 +00:00
|
|
|
}
|
|
|
|
</React.Fragment>
|
2020-04-17 18:17:01 +00:00
|
|
|
</PageContentBlock>
|
2019-07-28 03:23:51 +00:00
|
|
|
);
|
|
|
|
};
|