Skip to content

Commit 0ee9127

Browse files
authored
fix(Tenant): always set tenantPage in the url (#859)
1 parent 0e038ec commit 0ee9127

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

src/containers/AsideNavigation/useNavigationMenuItems.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export function useNavigationMenuItems() {
2626
const location = useLocation();
2727
const history = useHistory();
2828

29-
const [initialTenantPage, setInitialTenantPage] = useSetting<string>(TENANT_INITIAL_PAGE_KEY);
30-
const {tenantPage = initialTenantPage} = useTypedSelector((state) => state.tenant);
29+
const [, setInitialTenantPage] = useSetting<string>(TENANT_INITIAL_PAGE_KEY);
30+
const {tenantPage} = useTypedSelector((state) => state.tenant);
3131

3232
const {pathname} = location;
3333
const queryParams = parseQuery(location);

src/containers/Tenant/ObjectGeneral/ObjectGeneral.tsx

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import {useThemeValue} from '@gravity-ui/uikit';
2-
import {useLocation} from 'react-router';
32

4-
import {parseQuery} from '../../../routes';
53
import {TENANT_PAGES_IDS} from '../../../store/reducers/tenant/constants';
64
import type {AdditionalNodesProps, AdditionalTenantsProps} from '../../../types/additionalProps';
75
import type {EPathType} from '../../../types/api/schema';
86
import {cn} from '../../../utils/cn';
9-
import {TENANT_INITIAL_PAGE_KEY} from '../../../utils/constants';
10-
import {useSetting} from '../../../utils/hooks';
7+
import {useTypedSelector} from '../../../utils/hooks';
118
import Diagnostics from '../Diagnostics/Diagnostics';
129
import {Query} from '../Query/Query';
1310

@@ -23,13 +20,9 @@ interface ObjectGeneralProps {
2320
}
2421

2522
function ObjectGeneral(props: ObjectGeneralProps) {
26-
const location = useLocation();
2723
const theme = useThemeValue();
2824

29-
const [initialPage] = useSetting<string>(TENANT_INITIAL_PAGE_KEY);
30-
31-
const queryParams = parseQuery(location);
32-
const {tenantPage = initialPage} = queryParams;
25+
const {tenantPage} = useTypedSelector((state) => state.tenant);
3326

3427
const renderTabContent = () => {
3528
const {type, additionalTenantProps, additionalNodesProps, tenantName} = props;

src/services/settings.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {parseJson} from '../utils/utils';
2323
export type SettingsObject = Record<string, unknown>;
2424

2525
/** User settings keys and their default values */
26-
export const DEFAULT_USER_SETTINGS: SettingsObject = {
26+
export const DEFAULT_USER_SETTINGS = {
2727
[THEME_KEY]: 'system',
2828
[LANGUAGE_KEY]: undefined,
2929
[INVERTED_DISKS_KEY]: false,
@@ -40,7 +40,7 @@ export const DEFAULT_USER_SETTINGS: SettingsObject = {
4040
[USE_CLUSTER_BALANCER_AS_BACKEND_KEY]: true,
4141
[ENABLE_AUTOCOMPLETE]: false,
4242
[AUTOCOMPLETE_ON_ENTER]: true,
43-
};
43+
} as const satisfies SettingsObject;
4444

4545
class SettingsManager {
4646
/**

src/store/reducers/tenant/tenant.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {createSlice} from '@reduxjs/toolkit';
22
import type {PayloadAction} from '@reduxjs/toolkit';
33

4+
import {DEFAULT_USER_SETTINGS, settingsManager} from '../../../services/settings';
5+
import {TENANT_INITIAL_PAGE_KEY} from '../../../utils/constants';
46
import {api} from '../api';
57

8+
import {tenantPageSchema} from './types';
69
import type {
710
TenantDiagnosticsTab,
811
TenantMetricsTab,
@@ -12,9 +15,17 @@ import type {
1215
TenantSummaryTab,
1316
} from './types';
1417

18+
const tenantPage = tenantPageSchema
19+
.catch(DEFAULT_USER_SETTINGS[TENANT_INITIAL_PAGE_KEY])
20+
.parse(settingsManager.readUserSettingsValue(TENANT_INITIAL_PAGE_KEY));
21+
22+
const initialState: TenantState = {
23+
tenantPage,
24+
};
25+
1526
const slice = createSlice({
1627
name: 'tenant',
17-
initialState: {} as TenantState,
28+
initialState,
1829
reducers: {
1930
setTenantPage: (state, action: PayloadAction<TenantPage>) => {
2031
state.tenantPage = action.payload;

src/store/reducers/tenant/types.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1+
import {z} from 'zod';
2+
13
import type {ValueOf} from '../../../types/common';
24

5+
import {TENANT_PAGES_IDS} from './constants';
36
import type {
47
TENANT_DIAGNOSTICS_TABS_IDS,
58
TENANT_METRICS_TABS_IDS,
6-
TENANT_PAGES_IDS,
79
TENANT_QUERY_TABS_ID,
810
TENANT_SUMMARY_TABS_IDS,
911
} from './constants';
1012

11-
export type TenantPage = ValueOf<typeof TENANT_PAGES_IDS>;
13+
export const tenantPageSchema = z.nativeEnum(TENANT_PAGES_IDS);
14+
export type TenantPage = z.infer<typeof tenantPageSchema>;
1215

1316
export type TenantQueryTab = ValueOf<typeof TENANT_QUERY_TABS_ID>;
1417
export type TenantDiagnosticsTab = ValueOf<typeof TENANT_DIAGNOSTICS_TABS_IDS>;
1518
export type TenantSummaryTab = ValueOf<typeof TENANT_SUMMARY_TABS_IDS>;
1619
export type TenantMetricsTab = ValueOf<typeof TENANT_METRICS_TABS_IDS>;
1720

1821
export interface TenantState {
19-
tenantPage?: TenantPage;
22+
tenantPage: TenantPage;
2023
queryTab?: TenantQueryTab;
2124
diagnosticsTab?: TenantDiagnosticsTab;
2225
summaryTab?: TenantSummaryTab;

0 commit comments

Comments
 (0)