Skip to content

Commit 37e02c4

Browse files
committed
fix: some performance optimizations
1 parent 8f9019b commit 37e02c4

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,20 @@ export const PaginatedTable = <T, F>({
7676
chunkSize: limit,
7777
});
7878

79-
const handleDataFetched = React.useCallback((total: number, found: number) => {
80-
setTotalEntities(total);
81-
setFoundEntities(found);
82-
setIsInitialLoad(false);
83-
}, []);
79+
const handleDataFetched = React.useCallback(
80+
(total: number, found: number) => {
81+
if (total !== totalEntities) {
82+
setTotalEntities(total);
83+
}
84+
85+
if (found !== foundEntities) {
86+
setFoundEntities(found);
87+
}
88+
89+
setIsInitialLoad(false);
90+
},
91+
[foundEntities, totalEntities],
92+
);
8493

8594
// reset table on filters change
8695
React.useLayoutEffect(() => {
@@ -123,7 +132,7 @@ export const PaginatedTable = <T, F>({
123132
getRowClassName={getRowClassName}
124133
renderErrorMessage={renderErrorMessage}
125134
onDataFetched={handleDataFetched}
126-
isActive={activeChunks.includes(value)}
135+
isActive={activeChunks[value]}
127136
/>
128137
));
129138
};

src/components/PaginatedTable/useScrollBasedChunks.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import React from 'react';
22

33
import {isEqual, throttle} from 'lodash';
44

5-
import {getArray} from '../../utils';
6-
75
interface UseScrollBasedChunksProps {
86
parentRef?: React.RefObject<HTMLElement>;
97
tableRef: React.RefObject<HTMLElement>;
@@ -21,10 +19,8 @@ export const useScrollBasedChunks = ({
2119
totalItems,
2220
rowHeight,
2321
chunkSize,
24-
}: UseScrollBasedChunksProps): number[] => {
25-
const [activeChunks, setActiveChunks] = React.useState<number[]>(
26-
getArray(1 + CHUNKS_AHEAD_COUNT).map((index) => index),
27-
);
22+
}: UseScrollBasedChunksProps): boolean[] => {
23+
const [activeChunks, setActiveChunks] = React.useState<boolean[]>([true, true]);
2824

2925
const calculateActiveChunks = React.useCallback(() => {
3026
const container = parentRef?.current;
@@ -40,12 +36,16 @@ export const useScrollBasedChunks = ({
4036
totalItems - 1,
4137
);
4238

43-
const startChunk = Math.floor(visibleStartIndex / chunkSize);
44-
const endChunk = Math.floor(visibleEndIndex / chunkSize);
45-
46-
const newActiveChunks = getArray(endChunk - startChunk + 1 + CHUNKS_AHEAD_COUNT).map(
47-
(index) => startChunk + index,
39+
const startChunk = Math.max(
40+
Math.floor(visibleStartIndex / chunkSize) - CHUNKS_AHEAD_COUNT,
41+
0,
4842
);
43+
const endChunk = Math.floor(visibleEndIndex / chunkSize) + CHUNKS_AHEAD_COUNT;
44+
45+
const newActiveChunks = [];
46+
for (let i = startChunk; i <= endChunk; i++) {
47+
newActiveChunks[i] = true;
48+
}
4949

5050
if (!isEqual(activeChunks, newActiveChunks)) {
5151
setActiveChunks(newActiveChunks);

0 commit comments

Comments
 (0)