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

117 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';
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!);
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>
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 01:30:50 +00:00
<CSSTransition classNames={'fade'} timeout={250} 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 => (
<FileObjectRow key={file.uuid} file={file}/>
))
}
2019-12-16 03:10:01 +00:00
</div>
</React.Fragment>
</CSSTransition>
}
<Can action={'file.create'}>
2020-07-05 00:57:24 +00:00
<div css={tw`flex justify-end mt-8`}>
<NewDirectoryButton/>
2020-07-05 00:57:24 +00:00
<Button
// @ts-ignore
as={Link}
to={`/server/${id}/files/new${window.location.hash}`}
>
New File
2020-07-05 00:57:24 +00:00
</Button>
</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
);
};