Skip to content

Commit 83bdaaf

Browse files
committed
fix: small refactoring
1 parent 7ee36e2 commit 83bdaaf

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface PaginatedTableProps<T, F> {
3232
columns: Column<T>[];
3333
getRowClassName?: GetRowClassName<T>;
3434
rowHeight?: number;
35-
parentRef?: React.RefObject<HTMLElement> | null;
35+
parentRef?: React.RefObject<HTMLElement>;
3636
initialSortParams?: SortParams;
3737
onColumnsResize?: HandleTableColumnsResize;
3838
renderControls?: RenderControls;

src/components/PaginatedTable/useScrollBasedChunks.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,65 @@ import React from 'react';
22

33
import {throttle} from 'lodash';
44

5+
import {getArray} from '../../utils';
6+
57
interface UseScrollBasedChunksProps {
6-
containerRef: React.RefObject<HTMLElement> | null;
8+
containerRef: React.RefObject<HTMLElement>;
79
totalItems: number;
810
itemHeight: number;
911
chunkSize: number;
1012
}
1113

12-
const THROTTLING_TIMEOUT = 100;
14+
const THROTTLE_DELAY = 100;
1315

1416
export const useScrollBasedChunks = ({
1517
containerRef,
1618
totalItems,
1719
itemHeight,
1820
chunkSize,
19-
}: UseScrollBasedChunksProps) => {
21+
}: UseScrollBasedChunksProps): number[] => {
2022
const [activeChunks, setActiveChunks] = React.useState<number[]>([0]);
2123

22-
const handleScroll = React.useCallback(() => {
23-
if (!containerRef?.current) {
24+
const calculateActiveChunks = React.useCallback(() => {
25+
const container = containerRef.current;
26+
if (!container) {
2427
return;
2528
}
2629

27-
const {scrollTop, clientHeight} = containerRef.current;
28-
30+
const {scrollTop, clientHeight} = container;
2931
const visibleStartIndex = Math.floor(scrollTop / itemHeight);
3032
const visibleEndIndex = Math.min(
3133
Math.ceil((scrollTop + clientHeight) / itemHeight),
32-
Math.max(totalItems - 1, 0),
34+
totalItems - 1,
3335
);
3436

3537
const startChunk = Math.floor(visibleStartIndex / chunkSize);
3638
const endChunk = Math.floor(visibleEndIndex / chunkSize);
3739

38-
const newActiveChunks = Array.from(
39-
{length: endChunk - startChunk + 1},
40-
(_, index) => startChunk + index,
40+
const newActiveChunks = getArray(endChunk - startChunk + 1).map(
41+
(index) => startChunk + index,
4142
);
4243

4344
setActiveChunks(newActiveChunks);
4445
}, [chunkSize, containerRef, itemHeight, totalItems]);
4546

47+
const throttledCalculateActiveChunks = React.useMemo(
48+
() => throttle(calculateActiveChunks, THROTTLE_DELAY),
49+
[calculateActiveChunks],
50+
);
51+
4652
React.useEffect(() => {
47-
const containerElement = containerRef?.current;
48-
if (!containerElement) {
53+
const container = containerRef.current;
54+
if (!container) {
4955
return undefined;
5056
}
5157

52-
const throttledHandleScroll = throttle(handleScroll, THROTTLING_TIMEOUT);
53-
containerElement.addEventListener('scroll', throttledHandleScroll);
54-
58+
container.addEventListener('scroll', throttledCalculateActiveChunks);
5559
return () => {
56-
containerElement.removeEventListener('scroll', throttledHandleScroll);
60+
container.removeEventListener('scroll', throttledCalculateActiveChunks);
61+
throttledCalculateActiveChunks.cancel();
5762
};
58-
}, [containerRef, handleScroll]);
63+
}, [containerRef, throttledCalculateActiveChunks]);
5964

6065
return activeChunks;
6166
};

src/containers/Nodes/PaginatedNodes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const b = cn('ydb-nodes');
4444
interface NodesProps {
4545
path?: string;
4646
database?: string;
47-
parentRef?: React.RefObject<HTMLElement> | null;
47+
parentRef?: React.RefObject<HTMLElement>;
4848
additionalNodesProps?: AdditionalNodesProps;
4949
}
5050

src/containers/Storage/PaginatedStorage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface PaginatedStorageProps {
1212

1313
viewContext: StorageViewContext;
1414

15-
parentRef?: React.RefObject<HTMLElement> | null;
15+
parentRef?: React.RefObject<HTMLElement>;
1616

1717
initialEntitiesCount?: number;
1818
}

src/containers/Storage/StorageGroups/PaginatedStorageGroupsTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface PaginatedStorageGroupsTableProps {
3232
visibleEntities: VisibleEntities;
3333
onShowAll: VoidFunction;
3434

35-
parentRef?: React.RefObject<HTMLElement> | null;
35+
parentRef?: React.RefObject<HTMLElement>;
3636
renderControls?: RenderControls;
3737
renderErrorMessage: RenderErrorMessage;
3838
initialEntitiesCount?: number;

src/containers/Storage/StorageNodes/PaginatedStorageNodesTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface PaginatedStorageNodesTableProps {
2929
nodesUptimeFilter: NodesUptimeFilterValues;
3030
onShowAll: VoidFunction;
3131

32-
parentRef?: React.RefObject<HTMLElement> | null;
32+
parentRef?: React.RefObject<HTMLElement>;
3333
renderControls?: RenderControls;
3434
renderErrorMessage: RenderErrorMessage;
3535
initialEntitiesCount?: number;

src/containers/Storage/StorageWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface StorageWrapperProps {
1111
pDiskId?: string | number;
1212
groupId?: string | number;
1313
vDiskSlotId?: string | number;
14-
parentRef?: React.RefObject<HTMLElement> | null;
14+
parentRef?: React.RefObject<HTMLElement>;
1515
}
1616

1717
export const StorageWrapper = ({parentRef, ...props}: StorageWrapperProps) => {

0 commit comments

Comments
 (0)