Slightly less obtuse way of handling this little checkbox

This commit is contained in:
Dane Everitt 2020-12-08 20:34:31 -08:00
parent 60f170e919
commit 24417ac516
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 23 additions and 25 deletions

View file

@ -1,26 +1,20 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { ServerContext } from '@/state/server'; import { ServerContext } from '@/state/server';
import { NavLink, useRouteMatch } from 'react-router-dom'; import { NavLink } from 'react-router-dom';
import { cleanDirectoryPath } from '@/helpers'; import { cleanDirectoryPath } from '@/helpers';
import tw from 'twin.macro'; import tw from 'twin.macro';
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
interface Props { interface Props {
renderLeft?: JSX.Element;
withinFileEditor?: boolean; withinFileEditor?: boolean;
isNewFile?: boolean; isNewFile?: boolean;
} }
export default ({ withinFileEditor, isNewFile }: Props) => { export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => {
const [ file, setFile ] = useState<string | null>(null); const [ file, setFile ] = useState<string | null>(null);
const { params } = useRouteMatch<Record<string, string>>();
const id = ServerContext.useStoreState(state => state.server.data!.id); const id = ServerContext.useStoreState(state => state.server.data!.id);
const directory = ServerContext.useStoreState(state => state.files.directory); const directory = ServerContext.useStoreState(state => state.files.directory);
const { data: files } = useFileManagerSwr();
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
useEffect(() => { useEffect(() => {
const parts = cleanDirectoryPath(window.location.hash).split('/'); const parts = cleanDirectoryPath(window.location.hash).split('/');
@ -39,22 +33,9 @@ export default ({ withinFileEditor, isNewFile }: Props) => {
return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` }; return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` };
}); });
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
setSelectedFiles(e.currentTarget.checked ? (files?.map(file => file.name) || []) : []);
};
return ( return (
<div css={tw`flex flex-grow-0 items-center text-sm text-neutral-500 overflow-x-hidden`}> <div css={tw`flex flex-grow-0 items-center text-sm text-neutral-500 overflow-x-hidden`}>
{(files && files.length > 0 && !params?.action) ? {renderLeft || <div css={tw`w-12`}/>}
<FileActionCheckbox
type={'checkbox'}
css={tw`mx-4`}
checked={selectedFilesLength === (files ? files.length : -1)}
onChange={onSelectAllClick}
/>
:
<div css={tw`w-12`}/>
}
/<span css={tw`px-1 text-neutral-300`}>home</span>/ /<span css={tw`px-1 text-neutral-300`}>home</span>/
<NavLink <NavLink
to={`/server/${id}/files`} to={`/server/${id}/files`}

View file

@ -18,6 +18,7 @@ import UploadButton from '@/components/server/files/UploadButton';
import ServerContentBlock from '@/components/elements/ServerContentBlock'; import ServerContentBlock from '@/components/elements/ServerContentBlock';
import { useStoreActions } from '@/state/hooks'; import { useStoreActions } from '@/state/hooks';
import ErrorBoundary from '@/components/elements/ErrorBoundary'; import ErrorBoundary from '@/components/elements/ErrorBoundary';
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
const sortFiles = (files: FileObject[]): FileObject[] => { const sortFiles = (files: FileObject[]): FileObject[] => {
return files.sort((a, b) => a.name.localeCompare(b.name)) return files.sort((a, b) => a.name.localeCompare(b.name))
@ -31,7 +32,9 @@ export default () => {
const directory = ServerContext.useStoreState(state => state.files.directory); const directory = ServerContext.useStoreState(state => state.files.directory);
const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes); const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes);
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory); const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles); const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
useEffect(() => { useEffect(() => {
clearFlashes('files'); clearFlashes('files');
@ -43,6 +46,10 @@ export default () => {
mutate(); mutate();
}, [ directory ]); }, [ directory ]);
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
setSelectedFiles(e.currentTarget.checked ? (files?.map(file => file.name) || []) : []);
};
if (error) { if (error) {
return ( return (
<ServerError message={httpErrorToHuman(error)} onRetry={() => mutate()}/> <ServerError message={httpErrorToHuman(error)} onRetry={() => mutate()}/>
@ -53,9 +60,17 @@ export default () => {
<ServerContentBlock title={'File Manager'} showFlashKey={'files'}> <ServerContentBlock title={'File Manager'} showFlashKey={'files'}>
<div css={tw`flex flex-wrap-reverse md:flex-no-wrap justify-center mb-4`}> <div css={tw`flex flex-wrap-reverse md:flex-no-wrap justify-center mb-4`}>
<ErrorBoundary> <ErrorBoundary>
<FileManagerBreadcrumbs/> <FileManagerBreadcrumbs
renderLeft={
<FileActionCheckbox
type={'checkbox'}
css={tw`mx-4`}
checked={selectedFilesLength === (files ? files.length : -1)}
onChange={onSelectAllClick}
/>
}
/>
</ErrorBoundary> </ErrorBoundary>
<Can action={'file.create'}> <Can action={'file.create'}>
<ErrorBoundary> <ErrorBoundary>
<div css={tw`flex flex-shrink-0 flex-wrap-reverse md:flex-no-wrap justify-end mb-4 md:mb-0 ml-0 md:ml-auto`}> <div css={tw`flex flex-shrink-0 flex-wrap-reverse md:flex-no-wrap justify-end mb-4 md:mb-0 ml-0 md:ml-auto`}>

View file

@ -7,6 +7,8 @@ export default () => {
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid); const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const directory = ServerContext.useStoreState(state => state.files.directory); const directory = ServerContext.useStoreState(state => state.files.directory);
console.log('firing');
return useSWR<FileObject[]>( return useSWR<FileObject[]>(
`${uuid}:files:${directory}`, `${uuid}:files:${directory}`,
() => loadDirectory(uuid, cleanDirectoryPath(directory)), () => loadDirectory(uuid, cleanDirectoryPath(directory)),