Skip to content

Commit 24417ac

Browse files
committed
Slightly less obtuse way of handling this little checkbox
1 parent 60f170e commit 24417ac

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

resources/scripts/components/server/files/FileManagerBreadcrumbs.tsx

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import React, { useEffect, useState } from 'react';
22
import { ServerContext } from '@/state/server';
3-
import { NavLink, useRouteMatch } from 'react-router-dom';
3+
import { NavLink } from 'react-router-dom';
44
import { cleanDirectoryPath } from '@/helpers';
55
import tw from 'twin.macro';
6-
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
7-
import useFileManagerSwr from '@/plugins/useFileManagerSwr';
86

97
interface Props {
8+
renderLeft?: JSX.Element;
109
withinFileEditor?: boolean;
1110
isNewFile?: boolean;
1211
}
1312

14-
export default ({ withinFileEditor, isNewFile }: Props) => {
13+
export default ({ renderLeft, withinFileEditor, isNewFile }: Props) => {
1514
const [ file, setFile ] = useState<string | null>(null);
16-
const { params } = useRouteMatch<Record<string, string>>();
1715
const id = ServerContext.useStoreState(state => state.server.data!.id);
1816
const directory = ServerContext.useStoreState(state => state.files.directory);
1917

20-
const { data: files } = useFileManagerSwr();
21-
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
22-
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
23-
2418
useEffect(() => {
2519
const parts = cleanDirectoryPath(window.location.hash).split('/');
2620

@@ -39,22 +33,9 @@ export default ({ withinFileEditor, isNewFile }: Props) => {
3933
return { name: directory, path: `/${dirs.slice(0, index + 1).join('/')}` };
4034
});
4135

42-
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
43-
setSelectedFiles(e.currentTarget.checked ? (files?.map(file => file.name) || []) : []);
44-
};
45-
4636
return (
4737
<div css={tw`flex flex-grow-0 items-center text-sm text-neutral-500 overflow-x-hidden`}>
48-
{(files && files.length > 0 && !params?.action) ?
49-
<FileActionCheckbox
50-
type={'checkbox'}
51-
css={tw`mx-4`}
52-
checked={selectedFilesLength === (files ? files.length : -1)}
53-
onChange={onSelectAllClick}
54-
/>
55-
:
56-
<div css={tw`w-12`}/>
57-
}
38+
{renderLeft || <div css={tw`w-12`}/>}
5839
/<span css={tw`px-1 text-neutral-300`}>home</span>/
5940
<NavLink
6041
to={`/server/${id}/files`}

resources/scripts/components/server/files/FileManagerContainer.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import UploadButton from '@/components/server/files/UploadButton';
1818
import ServerContentBlock from '@/components/elements/ServerContentBlock';
1919
import { useStoreActions } from '@/state/hooks';
2020
import ErrorBoundary from '@/components/elements/ErrorBoundary';
21+
import { FileActionCheckbox } from '@/components/server/files/SelectFileCheckbox';
2122

2223
const sortFiles = (files: FileObject[]): FileObject[] => {
2324
return files.sort((a, b) => a.name.localeCompare(b.name))
@@ -31,7 +32,9 @@ export default () => {
3132
const directory = ServerContext.useStoreState(state => state.files.directory);
3233
const clearFlashes = useStoreActions(actions => actions.flashes.clearFlashes);
3334
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
35+
3436
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
37+
const selectedFilesLength = ServerContext.useStoreState(state => state.files.selectedFiles.length);
3538

3639
useEffect(() => {
3740
clearFlashes('files');
@@ -43,6 +46,10 @@ export default () => {
4346
mutate();
4447
}, [ directory ]);
4548

49+
const onSelectAllClick = (e: React.ChangeEvent<HTMLInputElement>) => {
50+
setSelectedFiles(e.currentTarget.checked ? (files?.map(file => file.name) || []) : []);
51+
};
52+
4653
if (error) {
4754
return (
4855
<ServerError message={httpErrorToHuman(error)} onRetry={() => mutate()}/>
@@ -53,9 +60,17 @@ export default () => {
5360
<ServerContentBlock title={'File Manager'} showFlashKey={'files'}>
5461
<div css={tw`flex flex-wrap-reverse md:flex-no-wrap justify-center mb-4`}>
5562
<ErrorBoundary>
56-
<FileManagerBreadcrumbs/>
63+
<FileManagerBreadcrumbs
64+
renderLeft={
65+
<FileActionCheckbox
66+
type={'checkbox'}
67+
css={tw`mx-4`}
68+
checked={selectedFilesLength === (files ? files.length : -1)}
69+
onChange={onSelectAllClick}
70+
/>
71+
}
72+
/>
5773
</ErrorBoundary>
58-
5974
<Can action={'file.create'}>
6075
<ErrorBoundary>
6176
<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`}>

resources/scripts/plugins/useFileManagerSwr.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export default () => {
77
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
88
const directory = ServerContext.useStoreState(state => state.files.directory);
99

10+
console.log('firing');
11+
1012
return useSWR<FileObject[]>(
1113
`${uuid}:files:${directory}`,
1214
() => loadDirectory(uuid, cleanDirectoryPath(directory)),

0 commit comments

Comments
 (0)