Skip to content

Commit dd356af

Browse files
committed
fix: move to separate hook
1 parent b7d9a7e commit dd356af

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

src/containers/Storage/PaginatedStorageNodes.tsx

Lines changed: 5 additions & 35 deletions
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
}
@@ -252,20 +235,7 @@ function StorageNodesTableGroupContent({
252235
columns,
253236
initialEntitiesCount,
254237
}: StorageNodesTableGroupContentProps) {
255-
const [tableStyle, setTableStyle] = React.useState<React.CSSProperties | undefined>(undefined);
256-
257-
const handleDataFetched = React.useCallback(
258-
(data: PaginatedTableData<PreparedStorageNode>) => {
259-
if (data?.columnSettings && !tableStyle) {
260-
const {maxSlotsPerDisk, maxDisksPerNode} = data.columnSettings;
261-
setTableStyle({
262-
[MAX_SLOTS_CSS_VAR]: maxSlotsPerDisk,
263-
[MAX_DISKS_CSS_VAR]: maxDisksPerNode,
264-
} as React.CSSProperties);
265-
}
266-
},
267-
[tableStyle],
268-
);
238+
const {tableStyle, handleDataFetched} = useTableCSSVariables();
269239

270240
return (
271241
<PaginatedStorageNodesTable
@@ -283,7 +253,7 @@ function StorageNodesTableGroupContent({
283253
columns={columns}
284254
initialEntitiesCount={initialEntitiesCount}
285255
tableStyle={tableStyle}
286-
onDataFetched={handleDataFetched}
256+
onDataFetched={tableStyle ? undefined : handleDataFetched}
287257
/>
288258
);
289259
}

src/containers/Storage/utils/index.ts

Lines changed: 2 additions & 0 deletions
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';
Lines changed: 32 additions & 0 deletions
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)