11import React from 'react' ;
22
3+ import { ArrayParam , StringParam , useQueryParams , withDefault } from 'use-query-params' ;
4+
35import { AccessDenied } from '../../components/Errors/403' ;
46import { ResponseError } from '../../components/Errors/ResponseError' ;
57import { TableWithControlsLayout } from '../../components/TableWithControlsLayout/TableWithControlsLayout' ;
@@ -10,20 +12,9 @@ import {
1012 filterGroups ,
1113 filterNodes ,
1214 getUsageFilterOptions ,
13- selectGroupsSortParams ,
14- selectNodesSortParams ,
1515} 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' ;
2718import type {
2819 StorageSortParams ,
2920 StorageType ,
@@ -35,39 +26,69 @@ import {
3526 useNodesRequestParams ,
3627 useStorageRequestParams ,
3728 useTableSort ,
38- useTypedDispatch ,
3929 useTypedSelector ,
4030} from '../../utils/hooks' ;
41- import { NodesUptimeFilterValues } from '../../utils/nodes' ;
31+ import { NodesUptimeFilterValues , nodesUptimeFilterValuesSchema } from '../../utils/nodes' ;
4232
4333import { StorageControls } from './StorageControls/StorageControls' ;
4434import { StorageGroups } from './StorageGroups/StorageGroups' ;
4535import { StorageNodes } from './StorageNodes/StorageNodes' ;
4636import { b } from './shared' ;
37+ import { defaultSortNode , getDefaultSortGroup } from './utils' ;
4738
4839import './Storage.scss' ;
4940
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+
5058interface StorageProps {
5159 additionalNodesProps ?: AdditionalNodesProps ;
5260 tenant ?: string ;
5361 nodeId ?: string ;
5462}
5563
5664export const Storage = ( { additionalNodesProps, tenant, nodeId} : StorageProps ) => {
57- const dispatch = useTypedDispatch ( ) ;
58-
5965 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 ;
6778
6879 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 ) ;
7192
7293 // Do not display Nodes table for Node page (NodeId present)
7394 const isNodePage = nodeId !== undefined ;
@@ -120,38 +141,31 @@ export const Storage = ({additionalNodesProps, tenant, nodeId}: StorageProps) =>
120141
121142 const usageFilterOptions = React . useMemo ( ( ) => getUsageFilterOptions ( groups ) , [ groups ] ) ;
122143
123- React . useEffect ( ( ) => {
124- return ( ) => {
125- // Clean data on component unmount
126- dispatch ( setInitialState ( ) ) ;
127- } ;
128- } , [ dispatch ] ) ;
129-
130144 const [ nodesSort , handleNodesSort ] = useTableSort ( nodesSortParams , ( params ) =>
131- dispatch ( setNodesSortParams ( params as NodesSortParams ) ) ,
145+ setNodeSort ( params as NodesSortParams ) ,
132146 ) ;
133147 const [ groupsSort , handleGroupsSort ] = useTableSort ( groupsSortParams , ( params ) =>
134- dispatch ( setGroupsSortParams ( params as StorageSortParams ) ) ,
148+ setGroupSort ( params as StorageSortParams ) ,
135149 ) ;
136150
137151 const handleUsageFilterChange = ( value : string [ ] ) => {
138- dispatch ( setUsageFilter ( value ) ) ;
152+ setQueryParams ( { usageFilter : value . length ? value : undefined } , 'replaceIn' ) ;
139153 } ;
140154
141155 const handleTextFilterChange = ( value : string ) => {
142- dispatch ( setStorageTextFilter ( value ) ) ;
156+ setQueryParams ( { search : value || undefined } , 'replaceIn' ) ;
143157 } ;
144158
145159 const handleGroupVisibilityChange = ( value : VisibleEntities ) => {
146- dispatch ( setVisibleEntities ( value ) ) ;
160+ setQueryParams ( { visible : value } , 'replaceIn' ) ;
147161 } ;
148162
149163 const handleStorageTypeChange = ( value : StorageType ) => {
150- dispatch ( setStorageType ( value ) ) ;
164+ setQueryParams ( { type : value } , 'replaceIn' ) ;
151165 } ;
152166
153167 const handleUptimeFilterChange = ( value : NodesUptimeFilterValues ) => {
154- dispatch ( setUptimeFilter ( value ) ) ;
168+ setQueryParams ( { uptimeFilter : value } , 'replaceIn' ) ;
155169 } ;
156170
157171 const handleShowAllNodes = ( ) => {
0 commit comments