1
1
import React from 'react' ;
2
2
3
+ import { ArrayParam , StringParam , useQueryParams , withDefault } from 'use-query-params' ;
4
+
3
5
import { AccessDenied } from '../../components/Errors/403' ;
4
6
import { ResponseError } from '../../components/Errors/ResponseError' ;
5
7
import { TableWithControlsLayout } from '../../components/TableWithControlsLayout/TableWithControlsLayout' ;
@@ -10,20 +12,9 @@ import {
10
12
filterGroups ,
11
13
filterNodes ,
12
14
getUsageFilterOptions ,
13
- selectGroupsSortParams ,
14
- selectNodesSortParams ,
15
15
} from '../../store/reducers/storage/selectors' ;
16
- import {
17
- setGroupsSortParams ,
18
- setInitialState ,
19
- setNodesSortParams ,
20
- setStorageTextFilter ,
21
- setStorageType ,
22
- setUptimeFilter ,
23
- setUsageFilter ,
24
- setVisibleEntities ,
25
- storageApi ,
26
- } from '../../store/reducers/storage/storage' ;
16
+ import { storageApi } from '../../store/reducers/storage/storage' ;
17
+ import { storageTypeSchema , visibleEntitiesSchema } from '../../store/reducers/storage/types' ;
27
18
import type {
28
19
StorageSortParams ,
29
20
StorageType ,
@@ -35,39 +26,69 @@ import {
35
26
useNodesRequestParams ,
36
27
useStorageRequestParams ,
37
28
useTableSort ,
38
- useTypedDispatch ,
39
29
useTypedSelector ,
40
30
} from '../../utils/hooks' ;
41
- import { NodesUptimeFilterValues } from '../../utils/nodes' ;
31
+ import { NodesUptimeFilterValues , nodesUptimeFilterValuesSchema } from '../../utils/nodes' ;
42
32
43
33
import { StorageControls } from './StorageControls/StorageControls' ;
44
34
import { StorageGroups } from './StorageGroups/StorageGroups' ;
45
35
import { StorageNodes } from './StorageNodes/StorageNodes' ;
46
36
import { b } from './shared' ;
37
+ import { defaultSortNode , getDefaultSortGroup } from './utils' ;
47
38
48
39
import './Storage.scss' ;
49
40
41
+ const UsageFilterParam = withDefault (
42
+ {
43
+ encode : ArrayParam . encode ,
44
+ decode : ( input ) => {
45
+ if ( input === null || input === undefined ) {
46
+ return input ;
47
+ }
48
+
49
+ if ( ! Array . isArray ( input ) ) {
50
+ return input ? [ input ] : [ ] ;
51
+ }
52
+ return input . filter ( Boolean ) as string [ ] ;
53
+ } ,
54
+ } ,
55
+ [ ] ,
56
+ ) ;
57
+
50
58
interface StorageProps {
51
59
additionalNodesProps ?: AdditionalNodesProps ;
52
60
tenant ?: string ;
53
61
nodeId ?: string ;
54
62
}
55
63
56
64
export const Storage = ( { additionalNodesProps, tenant, nodeId} : StorageProps ) => {
57
- const dispatch = useTypedDispatch ( ) ;
58
-
59
65
const { autorefresh} = useTypedSelector ( ( state ) => state . schema ) ;
60
- const {
61
- type,
62
- visible : visibleEntities ,
63
- filter,
64
- usageFilter,
65
- uptimeFilter,
66
- } = useTypedSelector ( ( state ) => state . storage ) ;
66
+ const [ queryParams , setQueryParams ] = useQueryParams ( {
67
+ type : StringParam ,
68
+ visible : StringParam ,
69
+ search : StringParam ,
70
+ uptimeFilter : StringParam ,
71
+ usageFilter : UsageFilterParam ,
72
+ } ) ;
73
+ const type = storageTypeSchema . parse ( queryParams . type ) ;
74
+ const visibleEntities = visibleEntitiesSchema . parse ( queryParams . visible ) ;
75
+ const filter = queryParams . search ?? '' ;
76
+ const uptimeFilter = nodesUptimeFilterValuesSchema . parse ( queryParams . uptimeFilter ) ;
77
+ const usageFilter = queryParams . usageFilter ;
67
78
68
79
const nodesMap = useTypedSelector ( selectNodesMap ) ;
69
- const nodesSortParams = useTypedSelector ( selectNodesSortParams ) ;
70
- const groupsSortParams = useTypedSelector ( selectGroupsSortParams ) ;
80
+
81
+ const [ nodeSort , setNodeSort ] = React . useState < NodesSortParams > ( {
82
+ sortOrder : undefined ,
83
+ sortValue : undefined ,
84
+ } ) ;
85
+ const nodesSortParams = nodeSort . sortValue ? nodeSort : defaultSortNode ;
86
+
87
+ const [ groupSort , setGroupSort ] = React . useState < StorageSortParams > ( {
88
+ sortOrder : undefined ,
89
+ sortValue : undefined ,
90
+ } ) ;
91
+ const groupsSortParams = groupSort . sortOrder ? groupSort : getDefaultSortGroup ( visibleEntities ) ;
71
92
72
93
// Do not display Nodes table for Node page (NodeId present)
73
94
const isNodePage = nodeId !== undefined ;
@@ -120,38 +141,31 @@ export const Storage = ({additionalNodesProps, tenant, nodeId}: StorageProps) =>
120
141
121
142
const usageFilterOptions = React . useMemo ( ( ) => getUsageFilterOptions ( groups ) , [ groups ] ) ;
122
143
123
- React . useEffect ( ( ) => {
124
- return ( ) => {
125
- // Clean data on component unmount
126
- dispatch ( setInitialState ( ) ) ;
127
- } ;
128
- } , [ dispatch ] ) ;
129
-
130
144
const [ nodesSort , handleNodesSort ] = useTableSort ( nodesSortParams , ( params ) =>
131
- dispatch ( setNodesSortParams ( params as NodesSortParams ) ) ,
145
+ setNodeSort ( params as NodesSortParams ) ,
132
146
) ;
133
147
const [ groupsSort , handleGroupsSort ] = useTableSort ( groupsSortParams , ( params ) =>
134
- dispatch ( setGroupsSortParams ( params as StorageSortParams ) ) ,
148
+ setGroupSort ( params as StorageSortParams ) ,
135
149
) ;
136
150
137
151
const handleUsageFilterChange = ( value : string [ ] ) => {
138
- dispatch ( setUsageFilter ( value ) ) ;
152
+ setQueryParams ( { usageFilter : value . length ? value : undefined } , 'replaceIn' ) ;
139
153
} ;
140
154
141
155
const handleTextFilterChange = ( value : string ) => {
142
- dispatch ( setStorageTextFilter ( value ) ) ;
156
+ setQueryParams ( { search : value || undefined } , 'replaceIn' ) ;
143
157
} ;
144
158
145
159
const handleGroupVisibilityChange = ( value : VisibleEntities ) => {
146
- dispatch ( setVisibleEntities ( value ) ) ;
160
+ setQueryParams ( { visible : value } , 'replaceIn' ) ;
147
161
} ;
148
162
149
163
const handleStorageTypeChange = ( value : StorageType ) => {
150
- dispatch ( setStorageType ( value ) ) ;
164
+ setQueryParams ( { type : value } , 'replaceIn' ) ;
151
165
} ;
152
166
153
167
const handleUptimeFilterChange = ( value : NodesUptimeFilterValues ) => {
154
- dispatch ( setUptimeFilter ( value ) ) ;
168
+ setQueryParams ( { uptimeFilter : value } , 'replaceIn' ) ;
155
169
} ;
156
170
157
171
const handleShowAllNodes = ( ) => {
0 commit comments