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

119 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import type { ChangeEvent } from 'react';
import { useEffect } from 'react';
import tw from 'twin.macro';
2019-07-28 03:23:51 +00:00
import { httpErrorToHuman } from '@/api/http';
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';
import { FileObject } from '@/api/server/files/loadDirectory';
2019-12-16 03:10:01 +00:00
import NewDirectoryButton from '@/components/server/files/NewDirectoryButton';
import { NavLink, useLocation } from 'react-router-dom';
import Can from '@/components/elements/Can';
2021-01-31 02:01:32 +00:00
import { ServerError } from '@/components/elements/ScreenBlock';
2022-06-20 18:16:42 +00:00
import { Button } from '@/components/elements/button/index';
import { ServerContext } from '@/state/server';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
// import FileManagerStatus from '@/components/server/files/FileManagerStatus';
2020-07-11 22:37:59 +00:00
import MassActionsBar from '@/components/server/files/MassActionsBar';
// import UploadButton from '@/components/server/files/UploadButton';
2020-08-26 04:39:00 +00:00
import ServerContentBlock from '@/components/elements/ServerContentBlock';
import { useStoreActions } from '@/state/hooks';
import ErrorBoundary from '@/components/elements/ErrorBoundary';
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
2021-01-02 00:55:09 +00:00
import { hashToPath } from '@/helpers';
2022-06-20 18:16:42 +00:00
import style from './style.module.css';
2022-11-25 20:25:03 +00:00
import FadeTransition from '@/components/elements/transitions/FadeTransition';
2019-12-16 03:10:01 +00:00
const sortFiles = (files: FileObject[]): FileObject[] => {
const sortedFiles: FileObject[] = files
.sort((a, b) => a.name.localeCompare(b.name))
.sort((a, b) => (a.isFile === b.isFile ? 0 : a.isFile ? 1 : -1));
2022-11-25 20:25:03 +00:00
return sortedFiles.filter((file, index) => index === 0 || file.name !== sortedFiles[index - 1]?.name);
2019-12-16 03:10:01 +00:00
};
2019-07-28 03:23:51 +00:00
export default () => {
2022-11-25 20:25:03 +00:00
const id = ServerContext.useStoreState(state => state.server.data!.id);
const { hash } = useLocation();
const { data: files, error, mutate } = useFileManagerSwr();
2022-11-25 20:25:03 +00:00
const directory = ServerContext.useStoreState(state => state.files.directory);
const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes);
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
2022-11-25 20:25:03 +00:00
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
2019-07-28 03:23:51 +00:00
2020-04-17 18:45:20 +00:00
useEffect(() => {
clearFlashes('files');
setSelectedFiles([]);
2021-01-02 00:55:09 +00:00
setDirectory(hashToPath(hash));
}, [hash]);
2019-07-28 03:23:51 +00:00
useEffect(() => {
2022-11-25 20:25:03 +00:00
void mutate();
}, [directory]);
2022-11-25 20:25:03 +00:00
const onSelectAllClick = (e: ChangeEvent<HTMLInputElement>) => {
setSelectedFiles(e.currentTarget.checked ? files?.map(file => file.name) || [] : []);
};
2020-04-17 18:45:20 +00:00
if (error) {
return <ServerError message={httpErrorToHuman(error)} onRetry={() => mutate()} />;
2020-04-17 18:45:20 +00:00
}
2019-07-28 03:23:51 +00:00
return (
2020-08-26 04:39:00 +00:00
<ServerContentBlock title={'File Manager'} showFlashKey={'files'}>
2022-06-20 18:16:42 +00:00
<ErrorBoundary>
2023-01-12 19:31:47 +00:00
<div className={'mb-4 flex flex-wrap-reverse md:flex-nowrap'}>
<FileManagerBreadcrumbs
renderLeft={
<FileActionCheckbox
type={'checkbox'}
css={tw`mx-4`}
checked={selectedFilesLength === (files?.length === 0 ? -1 : files?.length)}
onChange={onSelectAllClick}
/>
}
/>
2022-06-20 18:16:42 +00:00
<Can action={'file.create'}>
<div className={style.manager_actions}>
{/*<FileManagerStatus />*/}
<NewDirectoryButton />
{/*<UploadButton />*/}
2022-06-20 18:16:42 +00:00
<NavLink to={`/server/${id}/files/new${window.location.hash}`}>
<Button>New File</Button>
</NavLink>
</div>
2022-06-20 18:16:42 +00:00
</Can>
</div>
</ErrorBoundary>
{!files ? (
<Spinner size={'large'} centered />
) : (
<>
{!files.length ? (
<p css={tw`text-sm text-neutral-400 text-center`}>This directory seems to be empty.</p>
) : (
2022-11-25 20:25:03 +00:00
<FadeTransition duration="duration-150" appear show>
<div>
{files.length > 250 && (
<div css={tw`rounded bg-yellow-400 mb-px p-3`}>
<p css={tw`text-yellow-900 text-sm text-center`}>
This directory is too large to display in the browser, limiting the output
to the first 250 files.
</p>
</div>
)}
2022-11-25 20:25:03 +00:00
{sortFiles(files.slice(0, 250)).map(file => (
<FileObjectRow key={file.key} file={file} />
))}
<MassActionsBar />
</div>
2022-11-25 20:25:03 +00:00
</FadeTransition>
)}
</>
)}
2020-08-26 04:39:00 +00:00
</ServerContentBlock>
2019-07-28 03:23:51 +00:00
);
};