Skip to content

Commit 4aa6df4

Browse files
committed
fix: better code
1 parent 8c96333 commit 4aa6df4

File tree

9 files changed

+87
-47
lines changed

9 files changed

+87
-47
lines changed
Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,109 @@
11
import React from 'react';
22

33
import {TableWithControlsLayout} from '../TableWithControlsLayout/TableWithControlsLayout';
4-
import type {TableProps} from '../TableWithControlsLayout/TableWithControlsLayout';
4+
import type {TableWrapperProps} from '../TableWithControlsLayout/TableWithControlsLayout';
55

66
import {PaginatedTableProvider, usePaginatedTableState} from './PaginatedTableContext';
77
import type {PaginatedTableState} from './types';
88

99
export interface PaginatedTableWithLayoutProps {
1010
controls?: React.ReactNode;
1111
table: React.ReactNode;
12-
tableProps?: TableProps;
12+
tableWrapperProps?: TableWrapperProps;
1313
error?: React.ReactNode;
1414
initialState?: Partial<PaginatedTableState>;
1515
fullHeight?: boolean;
1616
noBatching?: boolean;
1717
}
1818

19-
// Internal component that has access to the paginated table context
20-
const TableWithAutoScrolling = ({
19+
/**
20+
* Hook to enhance table wrapper props with sort-dependent scroll behavior
21+
*/
22+
const useTableWrapperPropsWithSortDependency = (
23+
tableWrapperProps?: TableWrapperProps,
24+
): TableWrapperProps => {
25+
const {tableState} = usePaginatedTableState();
26+
const {sortParams} = tableState;
27+
28+
return React.useMemo(() => {
29+
const existingScrollDependencies = tableWrapperProps?.scrollDependencies || [];
30+
31+
return {
32+
...tableWrapperProps,
33+
scrollDependencies: [...existingScrollDependencies, sortParams],
34+
};
35+
}, [tableWrapperProps, sortParams]);
36+
};
37+
38+
/**
39+
* Internal component that wraps the table with sort-dependent scrolling behavior.
40+
* This component has access to the paginated table context and automatically
41+
* scrolls to top when sort parameters change.
42+
*/
43+
const TableWrapper = ({
2144
table,
22-
tableProps,
45+
tableWrapperProps,
2346
}: {
2447
table: React.ReactNode;
25-
tableProps?: TableProps;
48+
tableWrapperProps?: TableWrapperProps;
2649
}) => {
27-
const {tableState} = usePaginatedTableState();
28-
const {sortParams} = tableState;
29-
30-
// Enhance tableProps to include sortParams in scrollDependencies
31-
const enhancedTableProps = React.useMemo(
32-
() => ({
33-
...tableProps,
34-
scrollDependencies: [...(tableProps?.scrollDependencies || []), sortParams],
35-
}),
36-
[tableProps, sortParams],
37-
);
50+
const enhancedTableWrapperProps = useTableWrapperPropsWithSortDependency(tableWrapperProps);
3851

3952
return (
40-
<TableWithControlsLayout.Table {...enhancedTableProps}>
53+
<TableWithControlsLayout.Table {...enhancedTableWrapperProps}>
4154
{table}
4255
</TableWithControlsLayout.Table>
4356
);
4457
};
4558

59+
/**
60+
* Renders the controls section if controls are provided
61+
*/
62+
const ControlsSection = ({controls}: {controls?: React.ReactNode}) => {
63+
if (!controls) {
64+
return null;
65+
}
66+
67+
return <TableWithControlsLayout.Controls>{controls}</TableWithControlsLayout.Controls>;
68+
};
69+
70+
/**
71+
* Renders the error section if an error is provided
72+
*/
73+
const ErrorSection = ({error}: {error?: React.ReactNode}) => {
74+
if (!error) {
75+
return null;
76+
}
77+
78+
return <React.Fragment>{error}</React.Fragment>;
79+
};
80+
81+
/**
82+
* A complete table layout component that provides pagination context and
83+
* integrates with TableWithControlsLayout for consistent styling and behavior.
84+
*
85+
* Features:
86+
* - Provides pagination context to child components
87+
* - Automatic scroll-to-top on sort changes
88+
* - Optional controls and error display
89+
* - Configurable height behavior
90+
*/
4691
export const PaginatedTableWithLayout = ({
4792
controls,
4893
table,
49-
tableProps,
94+
tableWrapperProps,
5095
error,
5196
initialState,
5297
noBatching,
5398
fullHeight = true,
54-
}: PaginatedTableWithLayoutProps) => (
55-
<PaginatedTableProvider initialState={initialState} noBatching={noBatching}>
56-
<TableWithControlsLayout fullHeight={fullHeight}>
57-
{controls ? (
58-
<TableWithControlsLayout.Controls>{controls}</TableWithControlsLayout.Controls>
59-
) : null}
60-
{error}
61-
<TableWithAutoScrolling table={table} tableProps={tableProps} />
62-
</TableWithControlsLayout>
63-
</PaginatedTableProvider>
64-
);
99+
}: PaginatedTableWithLayoutProps) => {
100+
return (
101+
<PaginatedTableProvider initialState={initialState} noBatching={noBatching}>
102+
<TableWithControlsLayout fullHeight={fullHeight}>
103+
<ControlsSection controls={controls} />
104+
<ErrorSection error={error} />
105+
<TableWrapper table={table} tableWrapperProps={tableWrapperProps} />
106+
</TableWithControlsLayout>
107+
</PaginatedTableProvider>
108+
);
109+
};

src/components/TableWithControlsLayout/TableWithControlsLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface TableWithControlsLayoutItemProps {
1818
fullHeight?: boolean;
1919
}
2020

21-
export interface TableProps extends Omit<TableWithControlsLayoutItemProps, 'children'> {
21+
export interface TableWrapperProps extends Omit<TableWithControlsLayoutItemProps, 'children'> {
2222
loading?: boolean;
2323
scrollContainerRef?: React.RefObject<HTMLElement>;
2424
scrollDependencies?: any[];
@@ -61,7 +61,7 @@ TableWithControlsLayout.Table = function Table({
6161
scrollContainerRef,
6262
scrollDependencies = [],
6363
onSort,
64-
}: TableProps) {
64+
}: TableWrapperProps) {
6565
// Create an internal ref for the table container
6666
const tableContainerRef = React.useRef<HTMLDivElement>(null);
6767

src/containers/Nodes/PaginatedNodes/GroupedNodesComponent.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const NodeGroup = React.memo(function NodeGroup({
7272
scrollContainerRef={scrollContainerRef}
7373
/>
7474
}
75-
tableProps={{
75+
tableWrapperProps={{
7676
scrollContainerRef: scrollContainerRef,
7777
}}
7878
/>
@@ -192,13 +192,12 @@ export function GroupedNodesComponent({
192192
}
193193
error={error ? <ResponseError error={error} /> : null}
194194
table={renderGroups()}
195-
tableProps={{
195+
tableWrapperProps={{
196196
scrollContainerRef,
197197
scrollDependencies: [searchValue, groupByParam, tableGroups, peerRoleFilter],
198198
loading: isLoading,
199199
className: b('groups-wrapper'),
200200
}}
201-
fullHeight
202201
/>
203202
);
204203
}

src/containers/Nodes/PaginatedNodes/NodesComponent.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ export function NodesComponent({
7272
scrollContainerRef={scrollContainerRef}
7373
/>
7474
}
75-
tableProps={{
75+
tableWrapperProps={{
7676
scrollContainerRef,
7777
scrollDependencies: [searchValue, problemFilter, uptimeFilter, peerRoleFilter],
7878
}}
79-
fullHeight
8079
/>
8180
);
8281
}

src/containers/Storage/PaginatedStorageGroups/GroupedStorageGroupsComponent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const StorageGroupGroup = React.memo(function StorageGroupGroup({
7878
initialEntitiesCount={count}
7979
/>
8080
}
81-
tableProps={{
81+
tableWrapperProps={{
8282
scrollContainerRef: scrollContainerRef,
8383
}}
8484
/>
@@ -181,7 +181,7 @@ export function GroupedStorageGroupsComponent({
181181
error={error ? <ResponseError error={error} /> : null}
182182
table={renderGroups()}
183183
initialState={initialState}
184-
tableProps={{
184+
tableWrapperProps={{
185185
scrollContainerRef,
186186
scrollDependencies: [searchValue, storageGroupsGroupByParam, tableGroups],
187187
loading: isLoading,

src/containers/Storage/PaginatedStorageGroups/StorageGroupsComponent.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ export function StorageGroupsComponent({
5151
initialEntitiesCount={initialEntitiesCount}
5252
/>
5353
}
54-
tableProps={{
54+
tableWrapperProps={{
5555
scrollContainerRef,
5656
scrollDependencies: [searchValue, visibleEntities],
5757
}}
58-
fullHeight
5958
/>
6059
);
6160
}

src/containers/Storage/PaginatedStorageNodes/GroupedStorageNodesComponent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const StorageNodeGroup = React.memo(function StorageNodeGroup({
8080
onDataFetched={onDataFetched}
8181
/>
8282
}
83-
tableProps={{
83+
tableWrapperProps={{
8484
scrollContainerRef: scrollContainerRef,
8585
}}
8686
/>
@@ -182,7 +182,7 @@ export function GroupedStorageNodesComponent({
182182
error={error ? <ResponseError error={error} /> : null}
183183
table={renderGroups()}
184184
initialState={initialState}
185-
tableProps={{
185+
tableWrapperProps={{
186186
scrollContainerRef,
187187
scrollDependencies: [searchValue, storageNodesGroupByParam, tableGroups],
188188
loading: isLoading,

src/containers/Storage/PaginatedStorageNodes/StorageNodesComponent.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ export function StorageNodesComponent({
5656
onDataFetched={handleDataFetched}
5757
/>
5858
}
59-
tableProps={{
59+
tableWrapperProps={{
6060
scrollContainerRef,
6161
scrollDependencies: [searchValue, visibleEntities, nodesUptimeFilter],
6262
}}
63-
fullHeight
6463
/>
6564
);
6665
}

src/containers/Tenant/Diagnostics/TopicData/TopicData.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,10 @@ export function TopicData({scrollContainerRef, path, database}: TopicDataProps)
343343
}}
344344
/>
345345
}
346-
tableProps={{
346+
tableWrapperProps={{
347347
scrollContainerRef,
348348
scrollDependencies: [baseOffset, baseEndOffset, tableFilters],
349349
}}
350-
fullHeight
351350
noBatching
352351
/>
353352
</DrawerWrapper>

0 commit comments

Comments
 (0)