Skip to content

Commit f9b9200

Browse files
committed
fix: better code
1 parent 468fd8d commit f9b9200

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,62 +1,107 @@
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
}
1717

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

3851
return (
39-
<TableWithControlsLayout.Table {...enhancedTableProps}>
52+
<TableWithControlsLayout.Table {...enhancedTableWrapperProps}>
4053
{table}
4154
</TableWithControlsLayout.Table>
4255
);
4356
};
4457

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

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
/>
352351
</DrawerWrapper>
353352
)

0 commit comments

Comments
 (0)