Skip to content

Commit 6e1cd32

Browse files
committed
fix: move to separate hook
1 parent 18f32f3 commit 6e1cd32

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

src/containers/Storage/PaginatedStorageNodes.tsx

+5-35
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ import React from 'react';
33
import {ResponseError} from '../../components/Errors/ResponseError';
44
import {LoaderWrapper} from '../../components/LoaderWrapper/LoaderWrapper';
55
import type {RenderControls} from '../../components/PaginatedTable';
6-
import type {PaginatedTableData} from '../../components/PaginatedTable/types';
76
import {TableWithControlsLayout} from '../../components/TableWithControlsLayout/TableWithControlsLayout';
87
import {
98
useCapabilitiesLoaded,
109
useViewerNodesHandlerHasGrouping,
1110
} from '../../store/reducers/capabilities/hooks';
1211
import {storageApi} from '../../store/reducers/storage/storage';
13-
import type {PreparedStorageNode} from '../../store/reducers/storage/types';
1412
import type {NodesGroupByField} from '../../types/api/nodes';
1513
import {useAutoRefreshInterval} from '../../utils/hooks';
1614
import {useAdditionalNodesProps} from '../../utils/hooks/useAdditionalNodesProps';
@@ -27,6 +25,7 @@ import i18n from './i18n';
2725
import {b, renderPaginatedTableErrorMessage} from './shared';
2826
import type {StorageViewContext} from './types';
2927
import {useStorageQueryParams} from './useStorageQueryParams';
28+
import {useTableCSSVariables} from './utils';
3029

3130
import './Storage.scss';
3231

@@ -59,9 +58,6 @@ export const PaginatedStorageNodes = (props: PaginatedStorageProps) => {
5958
return <LoaderWrapper loading={!capabilitiesLoaded}>{renderContent()}</LoaderWrapper>;
6059
};
6160

62-
const MAX_SLOTS_CSS_VAR = '--maximum-slots';
63-
const MAX_DISKS_CSS_VAR = '--maximum-disks';
64-
6561
function StorageNodesComponent({
6662
database,
6763
nodeId,
@@ -80,20 +76,7 @@ function StorageNodesComponent({
8076
viewContext,
8177
});
8278

83-
const [tableStyle, setTableStyle] = React.useState<React.CSSProperties | undefined>(undefined);
84-
85-
const handleDataFetched = React.useCallback(
86-
(data: PaginatedTableData<PreparedStorageNode>) => {
87-
if (data?.columnSettings && !tableStyle) {
88-
const {maxSlotsPerDisk, maxDisksPerNode} = data.columnSettings;
89-
setTableStyle({
90-
[MAX_SLOTS_CSS_VAR]: maxSlotsPerDisk,
91-
[MAX_DISKS_CSS_VAR]: maxDisksPerNode,
92-
} as React.CSSProperties);
93-
}
94-
},
95-
[tableStyle],
96-
);
79+
const {tableStyle, handleDataFetched} = useTableCSSVariables();
9780

9881
const renderControls: RenderControls = ({totalEntities, foundEntities, inited}) => {
9982
return (
@@ -124,7 +107,7 @@ function StorageNodesComponent({
124107
columns={columnsToShow}
125108
initialEntitiesCount={initialEntitiesCount}
126109
tableStyle={tableStyle}
127-
onDataFetched={handleDataFetched}
110+
onDataFetched={tableStyle ? undefined : handleDataFetched}
128111
/>
129112
);
130113
}
@@ -248,20 +231,7 @@ function StorageNodesTableGroupContent({
248231
columns,
249232
initialEntitiesCount,
250233
}: StorageNodesTableGroupContentProps) {
251-
const [tableStyle, setTableStyle] = React.useState<React.CSSProperties | undefined>(undefined);
252-
253-
const handleDataFetched = React.useCallback(
254-
(data: PaginatedTableData<PreparedStorageNode>) => {
255-
if (data?.columnSettings && !tableStyle) {
256-
const {maxSlotsPerDisk, maxDisksPerNode} = data.columnSettings;
257-
setTableStyle({
258-
[MAX_SLOTS_CSS_VAR]: maxSlotsPerDisk,
259-
[MAX_DISKS_CSS_VAR]: maxDisksPerNode,
260-
} as React.CSSProperties);
261-
}
262-
},
263-
[tableStyle],
264-
);
234+
const {tableStyle, handleDataFetched} = useTableCSSVariables();
265235

266236
return (
267237
<PaginatedStorageNodesTable
@@ -279,7 +249,7 @@ function StorageNodesTableGroupContent({
279249
columns={columns}
280250
initialEntitiesCount={initialEntitiesCount}
281251
tableStyle={tableStyle}
282-
onDataFetched={handleDataFetched}
252+
onDataFetched={tableStyle ? undefined : handleDataFetched}
283253
/>
284254
);
285255
}

src/containers/Storage/utils/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,5 @@ export function useVDisksWithDCMargins(vDisks: PreparedVDisk[] = [], erasure?: E
114114
return disksWithMargins;
115115
}, [erasure, vDisks, nodesMap]);
116116
}
117+
118+
export {useTableCSSVariables} from './useTableCSSVariables';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
3+
import type {PaginatedTableData} from '../../../components/PaginatedTable/types';
4+
import type {PreparedStorageNode} from '../../../store/reducers/storage/types';
5+
6+
// Constants moved from PaginatedStorageNodes.tsx
7+
const MAX_SLOTS_CSS_VAR = '--maximum-slots';
8+
const MAX_DISKS_CSS_VAR = '--maximum-disks';
9+
10+
/**
11+
* Hook to manage table style based on column settings
12+
*
13+
* @returns An object with tableStyle and handleDataFetched
14+
*/
15+
export function useTableCSSVariables() {
16+
const [tableStyle, setTableStyle] = React.useState<React.CSSProperties | undefined>(undefined);
17+
18+
const handleDataFetched = React.useCallback((data: PaginatedTableData<PreparedStorageNode>) => {
19+
if (data?.columnSettings) {
20+
const {maxSlotsPerDisk, maxDisksPerNode} = data.columnSettings;
21+
setTableStyle({
22+
[MAX_SLOTS_CSS_VAR]: maxSlotsPerDisk,
23+
[MAX_DISKS_CSS_VAR]: maxDisksPerNode,
24+
} as React.CSSProperties);
25+
}
26+
}, []);
27+
28+
return {
29+
tableStyle,
30+
handleDataFetched,
31+
};
32+
}

0 commit comments

Comments
 (0)