Skip to content

Commit cb32885

Browse files
committed
Fix render performance and avoid re-rendering rows constantly
1 parent 325626e commit cb32885

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,15 @@ import { NavLink, useHistory, useRouteMatch } from 'react-router-dom';
1010
import tw from 'twin.macro';
1111
import isEqual from 'react-fast-compare';
1212
import styled from 'styled-components/macro';
13-
import Input from '@/components/elements/Input';
13+
import SelectFileCheckbox from '@/components/server/files/SelectFileCheckbox';
1414

1515
const Row = styled.div`
1616
${tw`flex bg-neutral-700 rounded-sm mb-px text-sm hover:text-neutral-100 cursor-pointer items-center no-underline hover:bg-neutral-600`};
1717
`;
1818

19-
const Checkbox = styled(Input)`
20-
&& {
21-
${tw`border-neutral-500`};
22-
23-
&:not(:checked) {
24-
${tw`hover:border-neutral-300`};
25-
}
26-
}
27-
`;
28-
2919
const FileObjectRow = ({ file }: { file: FileObject }) => {
3020
const directory = ServerContext.useStoreState(state => state.files.directory);
31-
const selectedFiles = ServerContext.useStoreState(state => state.files.selectedFiles);
3221
const setDirectory = ServerContext.useStoreActions(actions => actions.files.setDirectory);
33-
const setSelectedFiles = ServerContext.useStoreActions(actions => actions.files.setSelectedFiles);
3422

3523
const history = useHistory();
3624
const match = useRouteMatch();
@@ -57,21 +45,7 @@ const FileObjectRow = ({ file }: { file: FileObject }) => {
5745
window.dispatchEvent(new CustomEvent(`pterodactyl:files:ctx:${file.uuid}`, { detail: e.clientX }));
5846
}}
5947
>
60-
<label css={tw`flex-none p-4 absolute self-center z-30 cursor-pointer`}>
61-
<Checkbox
62-
name={'selectedFiles'}
63-
value={file.name}
64-
checked={selectedFiles.indexOf(file.name) >= 0}
65-
type={'checkbox'}
66-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
67-
if (e.currentTarget.checked) {
68-
setSelectedFiles(selectedFiles.filter(f => f !== file.name).concat(file.name));
69-
} else {
70-
setSelectedFiles(selectedFiles.filter(f => f !== file.name));
71-
}
72-
}}
73-
/>
74-
</label>
48+
<SelectFileCheckbox name={file.name}/>
7549
<NavLink
7650
to={`${match.url}/${file.isFile ? 'edit/' : ''}#${cleanDirectoryPath(`${directory}/${file.name}`)}`}
7751
css={tw`flex flex-1 text-neutral-300 no-underline p-3`}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import tw from 'twin.macro';
3+
import { ServerContext } from '@/state/server';
4+
import styled from 'styled-components/macro';
5+
import Input from '@/components/elements/Input';
6+
7+
const Checkbox = styled(Input)`
8+
&& {
9+
${tw`border-neutral-500`};
10+
11+
&:not(:checked) {
12+
${tw`hover:border-neutral-300`};
13+
}
14+
}
15+
`;
16+
17+
export default ({ name }: { name: string }) => {
18+
const isChecked = ServerContext.useStoreState(state => state.files.selectedFiles.indexOf(name) >= 0);
19+
const appendSelectedFile = ServerContext.useStoreActions(actions => actions.files.appendSelectedFile);
20+
const removeSelectedFile = ServerContext.useStoreActions(actions => actions.files.removeSelectedFile);
21+
22+
return (
23+
<label css={tw`flex-none p-4 absolute self-center z-30 cursor-pointer`}>
24+
<Checkbox
25+
name={'selectedFiles'}
26+
value={name}
27+
checked={isChecked}
28+
type={'checkbox'}
29+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
30+
if (e.currentTarget.checked) {
31+
appendSelectedFile(name);
32+
} else {
33+
removeSelectedFile(name);
34+
}
35+
}}
36+
/>
37+
</label>
38+
);
39+
};

resources/scripts/state/server/files.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export interface ServerFileStore {
77

88
setDirectory: Action<ServerFileStore, string>;
99
setSelectedFiles: Action<ServerFileStore, string[]>;
10+
appendSelectedFile: Action<ServerFileStore, string>;
11+
removeSelectedFile: Action<ServerFileStore, string>;
1012
}
1113

1214
const files: ServerFileStore = {
@@ -20,6 +22,14 @@ const files: ServerFileStore = {
2022
setSelectedFiles: action((state, payload) => {
2123
state.selectedFiles = payload;
2224
}),
25+
26+
appendSelectedFile: action((state, payload) => {
27+
state.selectedFiles = state.selectedFiles.filter(f => f !== payload).concat(payload);
28+
}),
29+
30+
removeSelectedFile: action((state, payload) => {
31+
state.selectedFiles = state.selectedFiles.filter(f => f !== payload);
32+
}),
2333
};
2434

2535
export default files;

0 commit comments

Comments
 (0)