Skip to content

Commit 23e4856

Browse files
authored
feat: save storage type in user settings (#3160)
1 parent 689a547 commit 23e4856

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/containers/Storage/PaginatedStorage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {useIsUserAllowedToMakeChanges} from '../../utils/hooks/useIsUserAllowedT
33
import {PaginatedStorageGroups} from './PaginatedStorageGroups';
44
import {PaginatedStorageNodes} from './PaginatedStorageNodes';
55
import type {StorageViewContext} from './types';
6-
import {useStorageQueryParams} from './useStorageQueryParams';
6+
import {useSaveStorageType, useStorageQueryParams} from './useStorageQueryParams';
77
import {getStorageGroupsInitialEntitiesCount, getStorageNodesInitialEntitiesCount} from './utils';
88

99
export interface PaginatedStorageProps {
@@ -21,6 +21,7 @@ export interface PaginatedStorageProps {
2121

2222
export const PaginatedStorage = (props: PaginatedStorageProps) => {
2323
const {storageType} = useStorageQueryParams();
24+
useSaveStorageType();
2425
const isUserAllowedToMakeChanges = useIsUserAllowedToMakeChanges();
2526

2627
const isNodes = storageType === 'nodes';

src/containers/Storage/useStorageQueryParams.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React from 'react';
22

3-
import {StringParam, useQueryParams} from 'use-query-params';
3+
import {StringParam, useQueryParam, useQueryParams} from 'use-query-params';
44

5+
import {SETTING_KEYS} from '../../store/reducers/settings/constants';
6+
import {STORAGE_TYPES} from '../../store/reducers/storage/constants';
57
import type {StorageType, VisibleEntities} from '../../store/reducers/storage/types';
68
import {storageTypeSchema, visibleEntitiesSchema} from '../../store/reducers/storage/types';
9+
import {useSetting} from '../../utils/hooks';
710
import {NodesUptimeFilterValues, nodesUptimeFilterValuesSchema} from '../../utils/nodes';
811

912
import {storageGroupsGroupByParamSchema} from './PaginatedStorageGroupsTable/columns/constants';
@@ -22,6 +25,11 @@ export function useStorageQueryParams() {
2225
storageGroupsGroupBy: StringParam,
2326
});
2427

28+
const [_savedStorageType, setSavedStorageType] = useSetting<StorageType>(
29+
SETTING_KEYS.STORAGE_TYPE,
30+
STORAGE_TYPES.groups,
31+
);
32+
2533
const storageType = storageTypeSchema.parse(queryParams.type);
2634

2735
const visibleEntities = visibleEntitiesSchema.parse(queryParams.visible);
@@ -42,7 +50,7 @@ export function useStorageQueryParams() {
4250
patch[STORAGE_SEARCH_PARAM_BY_TYPE[storageType]] = queryParams.search;
4351
setQueryParams(patch, 'replaceIn');
4452
}
45-
}, [queryParams.search, storageType]);
53+
}, [queryParams.search, storageType, setQueryParams]);
4654

4755
const handleTextFilterGroupsChange = (value: string) => {
4856
setQueryParams({groupsSearch: value || undefined}, 'replaceIn');
@@ -56,9 +64,13 @@ export function useStorageQueryParams() {
5664
setQueryParams({visible: value}, 'replaceIn');
5765
};
5866

59-
const handleStorageTypeChange = (value: StorageType) => {
60-
setQueryParams({type: value}, 'replaceIn');
61-
};
67+
const handleStorageTypeChange = React.useCallback(
68+
(value: StorageType) => {
69+
setQueryParams({type: value}, 'replaceIn');
70+
setSavedStorageType(value);
71+
},
72+
[setQueryParams, setSavedStorageType],
73+
);
6274

6375
const handleUptimeFilterChange = (value: NodesUptimeFilterValues) => {
6476
setQueryParams({uptimeFilter: value}, 'replaceIn');
@@ -103,3 +115,22 @@ export function useStorageQueryParams() {
103115
handleShowAllNodes,
104116
};
105117
}
118+
119+
export function useSaveStorageType() {
120+
const [queryStorageType, setQueryStorageType] = useQueryParam('type', StringParam);
121+
const [savedStorageType] = useSetting<StorageType>(
122+
SETTING_KEYS.STORAGE_TYPE,
123+
STORAGE_TYPES.groups,
124+
);
125+
126+
const normalizedStorageType = React.useMemo(
127+
() => storageTypeSchema.parse(queryStorageType ?? savedStorageType),
128+
[queryStorageType, savedStorageType],
129+
);
130+
131+
React.useEffect(() => {
132+
if (normalizedStorageType !== queryStorageType) {
133+
setQueryStorageType(normalizedStorageType);
134+
}
135+
}, [normalizedStorageType, queryStorageType, setQueryStorageType]);
136+
}

src/store/reducers/settings/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {ValueOf} from '../../../types/common';
22
import {AclSyntax} from '../../../utils/constants';
33
import {Lang} from '../../../utils/i18n';
44
import {DEFAULT_QUERY_SETTINGS, QUERY_ACTIONS} from '../../../utils/query';
5+
import {STORAGE_TYPES} from '../storage/constants';
56
import {TENANT_PAGES_IDS} from '../tenant/constants';
67

78
import type {SettingOptions} from './types';
@@ -36,6 +37,7 @@ export const SETTING_KEYS = {
3637
QUERY_SETTINGS_BANNER_LAST_CLOSED: 'querySettingsBannerLastClosed',
3738
QUERY_EXECUTION_SETTINGS: 'queryExecutionSettings',
3839
ACL_SYNTAX: 'aclSyntax',
40+
STORAGE_TYPE: 'storageType',
3941
} as const;
4042

4143
export type SettingKey = ValueOf<typeof SETTING_KEYS>;
@@ -71,6 +73,7 @@ export const DEFAULT_USER_SETTINGS = {
7173
[SETTING_KEYS.QUERY_SETTINGS_BANNER_LAST_CLOSED]: undefined,
7274
[SETTING_KEYS.QUERY_EXECUTION_SETTINGS]: DEFAULT_QUERY_SETTINGS,
7375
[SETTING_KEYS.ACL_SYNTAX]: AclSyntax.YdbShort,
76+
[SETTING_KEYS.STORAGE_TYPE]: STORAGE_TYPES.groups,
7477
} as const satisfies Record<SettingKey, unknown>;
7578

7679
export const SETTINGS_OPTIONS: Record<string, SettingOptions> = {

0 commit comments

Comments
 (0)