Skip to content

Commit 606d63f

Browse files
committed
fix: some optimizations
1 parent d49fa3c commit 606d63f

File tree

7 files changed

+75
-8
lines changed

7 files changed

+75
-8
lines changed

src/components/PaginatedTable/PaginatedTable.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,6 @@
181181
}
182182

183183
&__row-skeleton::after {
184-
animation: none !important;
184+
display: none !important;
185185
}
186186
}

src/components/PaginatedTable/PaginatedTable.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import type {
1717
SortParams,
1818
} from './types';
1919
import {useScrollBasedChunks} from './useScrollBasedChunks';
20+
import {useViewportChunkSize} from './useViewportChunkSize';
2021

2122
import './PaginatedTable.scss';
2223

2324
export interface PaginatedTableProps<T, F> {
24-
limit: number;
25+
limit: number | 'viewport';
2526
initialEntitiesCount?: number;
2627
fetchData: FetchData<T, F>;
2728
filters?: F;
@@ -39,7 +40,7 @@ export interface PaginatedTableProps<T, F> {
3940
}
4041

4142
export const PaginatedTable = <T, F>({
42-
limit: chunkSize,
43+
limit,
4344
initialEntitiesCount,
4445
fetchData,
4546
filters,
@@ -65,6 +66,13 @@ export const PaginatedTable = <T, F>({
6566

6667
const tableRef = React.useRef<HTMLDivElement>(null);
6768

69+
// Use the custom hook for viewport-based chunk size calculation
70+
const chunkSize = useViewportChunkSize({
71+
limit,
72+
parentRef,
73+
rowHeight,
74+
});
75+
6876
const activeChunks = useScrollBasedChunks({
6977
parentRef,
7078
tableRef,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
3+
interface UseViewportChunkSizeParams {
4+
limit: number | 'viewport';
5+
parentRef: React.RefObject<HTMLElement>;
6+
rowHeight: number;
7+
defaultChunkSize?: number;
8+
}
9+
10+
/**
11+
* Hook that calculates the number of rows that can fit in the viewport
12+
* Returns calculated chunk size based on viewport height or the provided limit if it's a number
13+
*/
14+
export const useViewportChunkSize = ({
15+
limit,
16+
parentRef,
17+
rowHeight,
18+
defaultChunkSize = 20,
19+
}: UseViewportChunkSizeParams): number => {
20+
// State to store calculated chunk size for viewport mode
21+
const [calculatedChunkSize, setCalculatedChunkSize] = React.useState(
22+
typeof limit === 'number' ? limit : defaultChunkSize,
23+
);
24+
25+
// Calculate rows that fit in viewport and update when container size changes
26+
React.useEffect(() => {
27+
if (limit !== 'viewport' || !parentRef.current) {
28+
if (typeof limit === 'number') {
29+
setCalculatedChunkSize(limit);
30+
}
31+
return undefined;
32+
}
33+
34+
// Store a reference to the current element
35+
const currentElement = parentRef.current;
36+
37+
const calculateVisibleRows = () => {
38+
const viewportHeight = currentElement.clientHeight;
39+
const visibleRows = Math.floor(viewportHeight / rowHeight);
40+
setCalculatedChunkSize(Math.max(visibleRows, 1));
41+
};
42+
43+
// Calculate initially
44+
calculateVisibleRows();
45+
46+
// Set up ResizeObserver to recalculate on parent container size changes
47+
const resizeObserver = new ResizeObserver(calculateVisibleRows);
48+
resizeObserver.observe(currentElement);
49+
50+
return () => {
51+
// Use the stored reference in the cleanup
52+
resizeObserver.unobserve(currentElement);
53+
resizeObserver.disconnect();
54+
};
55+
}, [limit, parentRef, rowHeight]);
56+
57+
// Return the calculated or provided chunk size
58+
return typeof limit === 'number' ? limit : calculatedChunkSize;
59+
};

src/containers/Nodes/NodesTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function NodesTable({
8383
parentRef={parentRef}
8484
columns={columns}
8585
fetchData={getNodes}
86-
limit={50}
86+
limit="viewport"
8787
initialEntitiesCount={initialEntitiesCount}
8888
renderControls={renderControls}
8989
renderErrorMessage={renderPaginatedTableErrorMessage}

src/containers/Storage/StorageGroups/PaginatedStorageGroupsTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const PaginatedStorageGroupsTable = ({
101101
parentRef={parentRef}
102102
columns={columns}
103103
fetchData={fetchData}
104-
limit={50}
104+
limit="viewport"
105105
initialEntitiesCount={initialEntitiesCount}
106106
renderControls={renderControls}
107107
renderErrorMessage={renderErrorMessage}

src/containers/Storage/StorageNodes/PaginatedStorageNodesTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const PaginatedStorageNodesTable = ({
104104
columns={columns}
105105
fetchData={getStorageNodes}
106106
rowHeight={51}
107-
limit={50}
107+
limit="viewport"
108108
initialEntitiesCount={initialEntitiesCount}
109109
renderControls={renderControls}
110110
renderErrorMessage={renderErrorMessage}

src/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const getArray = (arrayLength: number) => {
2-
return [...Array(arrayLength).keys()];
1+
export const getArray = (arrayLength: number): number[] => {
2+
return Array.from({length: arrayLength}, (_, i) => i);
33
};
44

55
export function valueIsDefined<T>(value: T | null | undefined): value is T {

0 commit comments

Comments
 (0)