|
1 | 1 | import React from 'react';
|
2 | 2 |
|
3 |
| -import {ASCENDING} from '@gravity-ui/react-data-table/build/esm/lib/constants'; |
4 |
| - |
5 |
| -import {AccessDenied} from '../../components/Errors/403'; |
6 |
| -import {isAccessError} from '../../components/Errors/PageError/PageError'; |
7 | 3 | import {ResponseError} from '../../components/Errors/ResponseError';
|
8 |
| -import {Illustration} from '../../components/Illustration'; |
9 |
| -import {ResizeableDataTable} from '../../components/ResizeableDataTable/ResizeableDataTable'; |
| 4 | +import {LoaderWrapper} from '../../components/LoaderWrapper/LoaderWrapper'; |
| 5 | +import type {Column, RenderControls} from '../../components/PaginatedTable'; |
10 | 6 | import {TableWithControlsLayout} from '../../components/TableWithControlsLayout/TableWithControlsLayout';
|
11 |
| -import {NODES_COLUMNS_WIDTH_LS_KEY} from '../../components/nodesColumns/constants'; |
| 7 | +import {NODES_COLUMNS_TITLES} from '../../components/nodesColumns/constants'; |
| 8 | +import type {NodesColumnId} from '../../components/nodesColumns/constants'; |
| 9 | +import { |
| 10 | + useCapabilitiesLoaded, |
| 11 | + useViewerNodesHandlerHasGrouping, |
| 12 | +} from '../../store/reducers/capabilities/hooks'; |
12 | 13 | import {nodesApi} from '../../store/reducers/nodes/nodes';
|
13 |
| -import {filterNodes} from '../../store/reducers/nodes/selectors'; |
14 |
| -import type {NodesSortParams} from '../../store/reducers/nodes/types'; |
| 14 | +import type {NodesPreparedEntity} from '../../store/reducers/nodes/types'; |
15 | 15 | import {useProblemFilter} from '../../store/reducers/settings/hooks';
|
16 | 16 | import type {AdditionalNodesProps} from '../../types/additionalProps';
|
17 |
| -import {DEFAULT_TABLE_SETTINGS} from '../../utils/constants'; |
18 |
| -import {useAutoRefreshInterval, useTableSort} from '../../utils/hooks'; |
| 17 | +import type {NodesGroupByField} from '../../types/api/nodes'; |
| 18 | +import {useAutoRefreshInterval} from '../../utils/hooks'; |
| 19 | +import {useSelectedColumns} from '../../utils/hooks/useSelectedColumns'; |
19 | 20 | import {NodesUptimeFilterValues} from '../../utils/nodes';
|
| 21 | +import {TableGroup} from '../Storage/TableGroup/TableGroup'; |
| 22 | +import {useExpandedGroups} from '../Storage/TableGroup/useExpandedTableGroups'; |
20 | 23 |
|
21 | 24 | import {NodesControls} from './NodesControls/NodesControls';
|
22 |
| -import {useNodesSelectedColumns} from './columns/hooks'; |
| 25 | +import {NodesTable} from './NodesTable'; |
| 26 | +import {getNodesColumns} from './columns/columns'; |
| 27 | +import { |
| 28 | + ALL_NODES_GROUP_BY_PARAMS, |
| 29 | + DEFAULT_NODES_COLUMNS, |
| 30 | + NODES_TABLE_SELECTED_COLUMNS_LS_KEY, |
| 31 | + REQUIRED_NODES_COLUMNS, |
| 32 | +} from './columns/constants'; |
23 | 33 | import i18n from './i18n';
|
24 |
| -import {getRowClassName} from './shared'; |
| 34 | +import {b} from './shared'; |
25 | 35 | import {useNodesPageQueryParams} from './useNodesPageQueryParams';
|
26 | 36 |
|
27 | 37 | import './Nodes.scss';
|
28 | 38 |
|
29 |
| -interface NodesProps { |
| 39 | +export interface NodesProps { |
30 | 40 | path?: string;
|
31 | 41 | database?: string;
|
| 42 | + parentRef: React.RefObject<HTMLElement>; |
32 | 43 | additionalNodesProps?: AdditionalNodesProps;
|
| 44 | + |
| 45 | + columns?: Column<NodesPreparedEntity>[]; |
| 46 | + defaultColumnsIds?: NodesColumnId[]; |
| 47 | + requiredColumnsIds?: NodesColumnId[]; |
| 48 | + selectedColumnsKey?: string; |
| 49 | + groupByParams?: NodesGroupByField[]; |
33 | 50 | }
|
34 | 51 |
|
35 |
| -export const Nodes = ({path, database, additionalNodesProps = {}}: NodesProps) => { |
36 |
| - const {searchValue, uptimeFilter} = useNodesPageQueryParams(undefined); |
37 |
| - const {problemFilter} = useProblemFilter(); |
| 52 | +export function Nodes({ |
| 53 | + path, |
| 54 | + database, |
| 55 | + parentRef, |
| 56 | + additionalNodesProps, |
| 57 | + columns = getNodesColumns({database, getNodeRef: additionalNodesProps?.getNodeRef}), |
| 58 | + defaultColumnsIds = DEFAULT_NODES_COLUMNS, |
| 59 | + requiredColumnsIds = REQUIRED_NODES_COLUMNS, |
| 60 | + selectedColumnsKey = NODES_TABLE_SELECTED_COLUMNS_LS_KEY, |
| 61 | + groupByParams = ALL_NODES_GROUP_BY_PARAMS, |
| 62 | +}: NodesProps) { |
| 63 | + const {uptimeFilter, groupByParam, handleUptimeFilterChange} = |
| 64 | + useNodesPageQueryParams(groupByParams); |
| 65 | + const {problemFilter, handleProblemFilterChange} = useProblemFilter(); |
38 | 66 |
|
39 |
| - const [autoRefreshInterval] = useAutoRefreshInterval(); |
| 67 | + const capabilitiesLoaded = useCapabilitiesLoaded(); |
| 68 | + const viewerNodesHandlerHasGrouping = useViewerNodesHandlerHasGrouping(); |
40 | 69 |
|
41 |
| - const {columnsToShow, columnsToSelect, setColumns} = useNodesSelectedColumns({ |
42 |
| - getNodeRef: additionalNodesProps.getNodeRef, |
43 |
| - database, |
44 |
| - }); |
| 70 | + // Other filters do not fit with grouping |
| 71 | + // Reset them if grouping available |
| 72 | + React.useEffect(() => { |
| 73 | + if ( |
| 74 | + viewerNodesHandlerHasGrouping && |
| 75 | + (problemFilter !== 'All' || uptimeFilter !== NodesUptimeFilterValues.All) |
| 76 | + ) { |
| 77 | + handleProblemFilterChange('All'); |
| 78 | + handleUptimeFilterChange(NodesUptimeFilterValues.All); |
| 79 | + } |
| 80 | + }, [ |
| 81 | + handleProblemFilterChange, |
| 82 | + handleUptimeFilterChange, |
| 83 | + problemFilter, |
| 84 | + uptimeFilter, |
| 85 | + viewerNodesHandlerHasGrouping, |
| 86 | + ]); |
45 | 87 |
|
46 |
| - const { |
47 |
| - currentData: data, |
48 |
| - isLoading, |
49 |
| - error, |
50 |
| - } = nodesApi.useGetNodesQuery({path, database}, {pollingInterval: autoRefreshInterval}); |
| 88 | + const renderContent = () => { |
| 89 | + if (viewerNodesHandlerHasGrouping && groupByParam) { |
| 90 | + return ( |
| 91 | + <GroupedNodesComponent |
| 92 | + path={path} |
| 93 | + database={database} |
| 94 | + parentRef={parentRef} |
| 95 | + columns={columns} |
| 96 | + defaultColumnsIds={defaultColumnsIds} |
| 97 | + requiredColumnsIds={requiredColumnsIds} |
| 98 | + selectedColumnsKey={selectedColumnsKey} |
| 99 | + groupByParams={groupByParams} |
| 100 | + /> |
| 101 | + ); |
| 102 | + } |
51 | 103 |
|
52 |
| - const [sortValue, setSortValue] = React.useState<NodesSortParams>({ |
53 |
| - sortValue: 'NodeId', |
54 |
| - sortOrder: ASCENDING, |
55 |
| - }); |
56 |
| - const [sort, handleSort] = useTableSort(sortValue, (sortParams) => { |
57 |
| - setSortValue(sortParams as NodesSortParams); |
58 |
| - }); |
| 104 | + return ( |
| 105 | + <NodesComponent |
| 106 | + path={path} |
| 107 | + database={database} |
| 108 | + parentRef={parentRef} |
| 109 | + columns={columns} |
| 110 | + defaultColumnsIds={defaultColumnsIds} |
| 111 | + requiredColumnsIds={requiredColumnsIds} |
| 112 | + selectedColumnsKey={selectedColumnsKey} |
| 113 | + groupByParams={groupByParams} |
| 114 | + /> |
| 115 | + ); |
| 116 | + }; |
59 | 117 |
|
60 |
| - const nodes = React.useMemo(() => { |
61 |
| - return filterNodes(data?.Nodes, {searchValue, uptimeFilter, problemFilter}); |
62 |
| - }, [data, searchValue, uptimeFilter, problemFilter]); |
| 118 | + return <LoaderWrapper loading={!capabilitiesLoaded}>{renderContent()}</LoaderWrapper>; |
| 119 | +} |
63 | 120 |
|
64 |
| - const totalNodes = data?.TotalNodes || 0; |
| 121 | +interface NodesComponentProps { |
| 122 | + path?: string; |
| 123 | + database?: string; |
| 124 | + parentRef: React.RefObject<HTMLElement>; |
65 | 125 |
|
66 |
| - const renderControls = () => { |
| 126 | + columns: Column<NodesPreparedEntity>[]; |
| 127 | + defaultColumnsIds: NodesColumnId[]; |
| 128 | + requiredColumnsIds: NodesColumnId[]; |
| 129 | + selectedColumnsKey: string; |
| 130 | + groupByParams: NodesGroupByField[]; |
| 131 | +} |
| 132 | + |
| 133 | +function NodesComponent({ |
| 134 | + path, |
| 135 | + database, |
| 136 | + parentRef, |
| 137 | + columns, |
| 138 | + defaultColumnsIds, |
| 139 | + requiredColumnsIds, |
| 140 | + selectedColumnsKey, |
| 141 | + groupByParams, |
| 142 | +}: NodesComponentProps) { |
| 143 | + const {searchValue, uptimeFilter} = useNodesPageQueryParams(groupByParams); |
| 144 | + const {problemFilter} = useProblemFilter(); |
| 145 | + const viewerNodesHandlerHasGrouping = useViewerNodesHandlerHasGrouping(); |
| 146 | + |
| 147 | + const {columnsToShow, columnsToSelect, setColumns} = useSelectedColumns( |
| 148 | + columns, |
| 149 | + selectedColumnsKey, |
| 150 | + NODES_COLUMNS_TITLES, |
| 151 | + defaultColumnsIds, |
| 152 | + requiredColumnsIds, |
| 153 | + ); |
| 154 | + |
| 155 | + const renderControls: RenderControls = ({totalEntities, foundEntities, inited}) => { |
67 | 156 | return (
|
68 | 157 | <NodesControls
|
| 158 | + withGroupBySelect={viewerNodesHandlerHasGrouping} |
| 159 | + groupByParams={groupByParams} |
69 | 160 | columnsToSelect={columnsToSelect}
|
70 | 161 | handleSelectedColumnsUpdate={setColumns}
|
71 |
| - entitiesCountCurrent={nodes.length} |
72 |
| - entitiesCountTotal={totalNodes} |
73 |
| - entitiesLoading={isLoading} |
74 |
| - groupByParams={undefined} |
| 162 | + entitiesCountCurrent={foundEntities} |
| 163 | + entitiesCountTotal={totalEntities} |
| 164 | + entitiesLoading={!inited} |
75 | 165 | />
|
76 | 166 | );
|
77 | 167 | };
|
78 | 168 |
|
79 |
| - const renderTable = () => { |
80 |
| - if (nodes.length === 0) { |
81 |
| - if (problemFilter !== 'All' || uptimeFilter !== NodesUptimeFilterValues.All) { |
82 |
| - return <Illustration name="thumbsUp" width="200" />; |
83 |
| - } |
84 |
| - } |
| 169 | + return ( |
| 170 | + <NodesTable |
| 171 | + path={path} |
| 172 | + database={database} |
| 173 | + searchValue={searchValue} |
| 174 | + problemFilter={problemFilter} |
| 175 | + uptimeFilter={uptimeFilter} |
| 176 | + columns={columnsToShow} |
| 177 | + parentRef={parentRef} |
| 178 | + renderControls={renderControls} |
| 179 | + /> |
| 180 | + ); |
| 181 | +} |
| 182 | + |
| 183 | +function GroupedNodesComponent({ |
| 184 | + path, |
| 185 | + database, |
| 186 | + parentRef, |
| 187 | + columns, |
| 188 | + defaultColumnsIds, |
| 189 | + requiredColumnsIds, |
| 190 | + selectedColumnsKey, |
| 191 | + groupByParams, |
| 192 | +}: NodesComponentProps) { |
| 193 | + const {searchValue, groupByParam} = useNodesPageQueryParams(groupByParams); |
| 194 | + const [autoRefreshInterval] = useAutoRefreshInterval(); |
| 195 | + |
| 196 | + const {columnsToShow, columnsToSelect, setColumns} = useSelectedColumns( |
| 197 | + columns, |
| 198 | + selectedColumnsKey, |
| 199 | + NODES_COLUMNS_TITLES, |
| 200 | + defaultColumnsIds, |
| 201 | + requiredColumnsIds, |
| 202 | + ); |
| 203 | + |
| 204 | + const {currentData, isFetching, error} = nodesApi.useGetNodesQuery( |
| 205 | + { |
| 206 | + path, |
| 207 | + database, |
| 208 | + filter: searchValue, |
| 209 | + group: groupByParam, |
| 210 | + limit: 0, |
| 211 | + }, |
| 212 | + { |
| 213 | + pollingInterval: autoRefreshInterval, |
| 214 | + }, |
| 215 | + ); |
| 216 | + |
| 217 | + const isLoading = currentData === undefined && isFetching; |
| 218 | + const { |
| 219 | + NodeGroups: tableGroups, |
| 220 | + FoundNodes: found = 0, |
| 221 | + TotalNodes: total = 0, |
| 222 | + } = currentData || {}; |
85 | 223 |
|
| 224 | + const {expandedGroups, setIsGroupExpanded} = useExpandedGroups(tableGroups); |
| 225 | + |
| 226 | + const renderControls = () => { |
86 | 227 | return (
|
87 |
| - <ResizeableDataTable |
88 |
| - columnsWidthLSKey={NODES_COLUMNS_WIDTH_LS_KEY} |
89 |
| - data={nodes || []} |
90 |
| - columns={columnsToShow} |
91 |
| - settings={DEFAULT_TABLE_SETTINGS} |
92 |
| - sortOrder={sort} |
93 |
| - onSort={handleSort} |
94 |
| - emptyDataMessage={i18n('empty.default')} |
95 |
| - rowClassName={getRowClassName} |
| 228 | + <NodesControls |
| 229 | + withGroupBySelect |
| 230 | + groupByParams={groupByParams} |
| 231 | + columnsToSelect={columnsToSelect} |
| 232 | + handleSelectedColumnsUpdate={setColumns} |
| 233 | + entitiesCountCurrent={found} |
| 234 | + entitiesCountTotal={total} |
| 235 | + entitiesLoading={isLoading} |
96 | 236 | />
|
97 | 237 | );
|
98 | 238 | };
|
99 | 239 |
|
100 |
| - if (isAccessError(error)) { |
101 |
| - return <AccessDenied />; |
102 |
| - } |
| 240 | + const renderGroups = () => { |
| 241 | + if (tableGroups?.length) { |
| 242 | + return tableGroups.map(({name, count}) => { |
| 243 | + const isExpanded = expandedGroups[name]; |
| 244 | + |
| 245 | + return ( |
| 246 | + <TableGroup |
| 247 | + key={name} |
| 248 | + title={name} |
| 249 | + count={count} |
| 250 | + entityName={i18n('nodes')} |
| 251 | + expanded={isExpanded} |
| 252 | + onIsExpandedChange={setIsGroupExpanded} |
| 253 | + > |
| 254 | + <NodesTable |
| 255 | + path={path} |
| 256 | + database={database} |
| 257 | + searchValue={searchValue} |
| 258 | + problemFilter={'All'} |
| 259 | + uptimeFilter={NodesUptimeFilterValues.All} |
| 260 | + filterGroup={name} |
| 261 | + filterGroupBy={groupByParam} |
| 262 | + initialEntitiesCount={count} |
| 263 | + columns={columnsToShow} |
| 264 | + parentRef={parentRef} |
| 265 | + /> |
| 266 | + </TableGroup> |
| 267 | + ); |
| 268 | + }); |
| 269 | + } |
| 270 | + |
| 271 | + return i18n('no-nodes-groups'); |
| 272 | + }; |
103 | 273 |
|
104 | 274 | return (
|
105 | 275 | <TableWithControlsLayout>
|
106 | 276 | <TableWithControlsLayout.Controls>{renderControls()}</TableWithControlsLayout.Controls>
|
107 | 277 | {error ? <ResponseError error={error} /> : null}
|
108 |
| - <TableWithControlsLayout.Table loading={isLoading}> |
109 |
| - {data ? renderTable() : null} |
| 278 | + <TableWithControlsLayout.Table loading={isLoading} className={b('groups-wrapper')}> |
| 279 | + {renderGroups()} |
110 | 280 | </TableWithControlsLayout.Table>
|
111 | 281 | </TableWithControlsLayout>
|
112 | 282 | );
|
113 |
| -}; |
| 283 | +} |
0 commit comments