misc_pterodactyl-panel/resources/scripts/components/server/files/FileManagerContainer.tsx

115 lines
5.3 KiB
TypeScript
Raw Normal View History

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';
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';
import Can from '@/components/elements/Can';
import PageContentBlock from '@/components/elements/PageContentBlock';
2020-04-17 18:45:20 +00:00
import ServerError from '@/components/screens/ServerError';
import useRouter from 'use-react-router';
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);
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
2019-12-22 00:38:40 +00:00
const { id } = ServerContext.useStoreState(state => state.server.data!);
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);
getDirectoryContents(window.location.hash)
.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();
}, []);
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 (
<PageContentBlock>
<FlashMessageRender byKey={'files'} className={'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 ?
<Spinner size={'large'} centered={true}/>
2019-07-28 03:23:51 +00:00
:
2019-12-16 03:10:01 +00:00
<React.Fragment>
{!files.length ?
2019-12-22 00:40:28 +00:00
<p className={'text-sm text-neutral-400 text-center'}>
2019-12-16 03:10:01 +00:00
This directory seems to be empty.
</p>
:
<CSSTransition classNames={'fade'} timeout={250} appear={true} in={true}>
<React.Fragment>
<div>
{files.length > 250 ?
<React.Fragment>
<div className={'rounded bg-yellow-400 mb-px p-3'}>
<p className={'text-yellow-900 text-sm text-center'}>
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 => (
<FileObjectRow key={file.uuid} file={file}/>
))
}
2019-12-16 03:10:01 +00:00
</div>
</React.Fragment>
</CSSTransition>
}
<Can action={'file.create'}>
<div className={'flex justify-end mt-8'}>
<NewDirectoryButton/>
<Link
to={`/server/${id}/files/new${window.location.hash}`}
className={'btn btn-sm btn-primary'}
>
New File
</Link>
</div>
</Can>
2019-12-16 03:10:01 +00:00
</React.Fragment>
2019-07-28 03:23:51 +00:00
}
</React.Fragment>
</PageContentBlock>
2019-07-28 03:23:51 +00:00
);
};