2020-07-11 05:10:51 +00:00
|
|
|
import React, { useEffect } from 'react';
|
2019-07-28 03:23:51 +00:00
|
|
|
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';
|
2020-07-11 05:38:07 +00:00
|
|
|
import { FileObject } from '@/api/server/files/loadDirectory';
|
2019-12-16 03:10:01 +00:00
|
|
|
import NewDirectoryButton from '@/components/server/files/NewDirectoryButton';
|
2020-09-13 17:33:12 +00:00
|
|
|
import { NavLink, useLocation } from 'react-router-dom';
|
2020-03-30 04:42:02 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
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';
|
2020-07-11 05:10:51 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
2020-07-11 05:38:07 +00:00
|
|
|
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
|
2020-07-11 22:37:59 +00:00
|
|
|
import MassActionsBar from '@/components/server/files/MassActionsBar';
|
2020-07-12 21:20:37 +00:00
|
|
|
import UploadButton from '@/components/server/files/UploadButton';
|
2020-08-26 04:39:00 +00:00
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
2020-09-07 22:03:26 +00:00
|
|
|
import { useStoreActions } from '@/state/hooks';
|
2020-10-23 04:18:46 +00:00
|
|
|
import ErrorBoundary from '@/components/elements/ErrorBoundary';
|
2020-12-09 04:34:31 +00:00
|
|
|
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
|
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-08-26 04:39:00 +00:00
|
|
|
const id = ServerContext.useStoreState(state => state.server.data!.id);
|
2020-07-11 05:10:51 +00:00
|
|
|
const { hash } = useLocation();
|
2020-07-11 05:38:07 +00:00
|
|
|
const { data: files, error, mutate } = useFileManagerSwr();
|
2020-09-10 04:32:43 +00:00
|
|
|
const directory = ServerContext.useStoreState(state => state.files.directory);
|
2020-09-07 22:03:26 +00:00
|
|
|
const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes);
|
2020-07-11 05:10:51 +00:00
|
|
|
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
|
2020-12-09 04:34:31 +00:00
|
|
|
|
2020-07-11 23:47:13 +00:00
|
|
|
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
|
2020-12-09 04:34:31 +00:00
|
|
|
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(() => {
|
2020-09-07 22:03:26 +00:00
|
|
|
clearFlashes('files');
|
2020-07-11 23:47:13 +00:00
|
|
|
setSelectedFiles([]);
|
2020-12-09 05:24:17 +00:00
|
|
|
setDirectory(hash.length > 0 ? hash : '/');
|
2020-07-11 05:10:51 +00:00
|
|
|
}, [ hash ]);
|
2019-07-28 03:23:51 +00:00
|
|
|
|
2020-09-10 04:32:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
mutate();
|
|
|
|
}, [ directory ]);
|
|
|
|
|
2020-12-09 04:34:31 +00:00
|
|
|
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSelectedFiles(e.currentTarget.checked ? (files?.map(file => file.name) || []) : []);
|
|
|
|
};
|
|
|
|
|
2020-04-17 18:45:20 +00:00
|
|
|
if (error) {
|
|
|
|
return (
|
2020-07-11 05:10:51 +00:00
|
|
|
<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'}>
|
2020-12-26 17:50:09 +00:00
|
|
|
<div css={tw`flex flex-wrap-reverse md:flex-nowrap justify-center mb-4`}>
|
2020-12-04 04:10:41 +00:00
|
|
|
<ErrorBoundary>
|
2020-12-09 04:34:31 +00:00
|
|
|
<FileManagerBreadcrumbs
|
|
|
|
renderLeft={
|
|
|
|
<FileActionCheckbox
|
|
|
|
type={'checkbox'}
|
|
|
|
css={tw`mx-4`}
|
2020-12-19 18:51:26 +00:00
|
|
|
checked={selectedFilesLength === (files?.length === 0 ? -1 : files?.length)}
|
2020-12-09 04:34:31 +00:00
|
|
|
onChange={onSelectAllClick}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2020-12-04 04:10:41 +00:00
|
|
|
</ErrorBoundary>
|
|
|
|
<Can action={'file.create'}>
|
|
|
|
<ErrorBoundary>
|
2020-12-26 17:50:09 +00:00
|
|
|
<div css={tw`flex flex-shrink-0 flex-wrap-reverse md:flex-nowrap justify-end mb-4 md:mb-0 ml-0 md:ml-auto`}>
|
2020-12-04 04:10:41 +00:00
|
|
|
<NewDirectoryButton css={tw`w-full flex-none mt-4 sm:mt-0 sm:w-auto sm:mr-4`}/>
|
|
|
|
<UploadButton css={tw`flex-1 mr-4 sm:flex-none sm:mt-0`}/>
|
|
|
|
<NavLink
|
|
|
|
to={`/server/${id}/files/new${window.location.hash}`}
|
|
|
|
css={tw`flex-1 sm:flex-none sm:mt-0`}
|
|
|
|
>
|
|
|
|
<Button css={tw`w-full`}>
|
|
|
|
New File
|
|
|
|
</Button>
|
|
|
|
</NavLink>
|
|
|
|
</div>
|
|
|
|
</ErrorBoundary>
|
|
|
|
</Can>
|
|
|
|
</div>
|
2020-07-11 05:10:51 +00:00
|
|
|
{
|
|
|
|
!files ?
|
|
|
|
<Spinner size={'large'} centered/>
|
|
|
|
:
|
|
|
|
<>
|
|
|
|
{!files.length ?
|
|
|
|
<p css={tw`text-sm text-neutral-400 text-center`}>
|
|
|
|
This directory seems to be empty.
|
|
|
|
</p>
|
|
|
|
:
|
|
|
|
<CSSTransition classNames={'fade'} timeout={150} appear in>
|
2020-07-11 22:37:59 +00:00
|
|
|
<div>
|
2020-07-11 23:47:13 +00:00
|
|
|
{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>
|
|
|
|
}
|
|
|
|
{
|
|
|
|
sortFiles(files.slice(0, 250)).map(file => (
|
2020-08-02 01:48:58 +00:00
|
|
|
<FileObjectRow key={file.key} file={file}/>
|
2020-07-11 23:47:13 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
<MassActionsBar/>
|
2020-07-11 22:37:59 +00:00
|
|
|
</div>
|
2020-07-11 05:10:51 +00:00
|
|
|
</CSSTransition>
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
}
|
2020-08-26 04:39:00 +00:00
|
|
|
</ServerContentBlock>
|
2019-07-28 03:23:51 +00:00
|
|
|
);
|
|
|
|
};
|