Skip to content

Commit 21600f4

Browse files
committed
fix: move reducer to url
1 parent 1652e6c commit 21600f4

File tree

5 files changed

+37
-39
lines changed

5 files changed

+37
-39
lines changed

src/containers/Tenant/Diagnostics/TenantOverview/TenantStorage/TenantStorage.tsx

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import React from 'react';
22

33
import {Tab, TabList, TabProvider} from '@gravity-ui/uikit';
4-
import {useLocation} from 'react-router-dom';
54

65
import {InfoViewer} from '../../../../../components/InfoViewer/InfoViewer';
7-
import {InternalLink} from '../../../../../components/InternalLink';
86
import {LabelWithPopover} from '../../../../../components/LabelWithPopover';
97
import {ProgressViewer} from '../../../../../components/ProgressViewer/ProgressViewer';
10-
import {parseQuery} from '../../../../../routes';
118
import {TENANT_STORAGE_TABS_IDS} from '../../../../../store/reducers/tenant/constants';
129
import {cn} from '../../../../../utils/cn';
1310
import {formatStorageValues} from '../../../../../utils/dataFormatters/dataFormatters';
14-
import {useTypedSelector} from '../../../../../utils/hooks';
15-
import {TenantTabsGroups, getTenantPath} from '../../../TenantPages';
1611
import {TenantDashboard} from '../TenantDashboard/TenantDashboard';
1712
import i18n from '../i18n';
1813

1914
import {TopGroups} from './TopGroups';
2015
import {TopTables} from './TopTables';
2116
import {storageDashboardConfig} from './storageDashboardConfig';
17+
import {useTenantStorageQueryParams} from './useTenantStorageQueryParams';
2218

2319
import './TenantStorage.scss';
2420

@@ -42,13 +38,10 @@ interface TenantStorageProps {
4238
}
4339

4440
export function TenantStorage({tenantName, metrics}: TenantStorageProps) {
45-
const location = useLocation();
46-
const {storageTab = TENANT_STORAGE_TABS_IDS.tables} = useTypedSelector((state) => state.tenant);
41+
const {storageTab, handleStorageTabChange} = useTenantStorageQueryParams();
4742

4843
const {blobStorageUsed, tabletStorageUsed, blobStorageLimit, tabletStorageLimit} = metrics;
4944

50-
const queryParams = parseQuery(location);
51-
5245
const renderTabContent = () => {
5346
switch (storageTab) {
5447
case TENANT_STORAGE_TABS_IDS.tables:
@@ -110,15 +103,9 @@ export function TenantStorage({tenantName, metrics}: TenantStorageProps) {
110103
<TabProvider value={storageTab}>
111104
<TabList size="m">
112105
{storageTabs.map(({id, title}) => {
113-
const path = getTenantPath({
114-
...queryParams,
115-
[TenantTabsGroups.storageTab]: id,
116-
});
117106
return (
118-
<Tab key={id} value={id}>
119-
<InternalLink to={path} as="tab">
120-
{title}
121-
</InternalLink>
107+
<Tab key={id} value={id} onClick={() => handleStorageTabChange(id)}>
108+
{title}
122109
</Tab>
123110
);
124111
})}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {StringParam, useQueryParams} from 'use-query-params';
2+
3+
import {TENANT_STORAGE_TABS_IDS} from '../../../../../store/reducers/tenant/constants';
4+
import type {TenantStorageTab} from '../../../../../store/reducers/tenant/types';
5+
6+
export function useTenantStorageQueryParams() {
7+
const [queryParams, setQueryParams] = useQueryParams({
8+
storageTab: StringParam,
9+
});
10+
11+
// Parse and validate storageTab with fallback to tables
12+
const storageTab: TenantStorageTab = (() => {
13+
if (!queryParams.storageTab) {
14+
return TENANT_STORAGE_TABS_IDS.tables;
15+
}
16+
const validTabs = Object.values(TENANT_STORAGE_TABS_IDS) as string[];
17+
return validTabs.includes(queryParams.storageTab)
18+
? (queryParams.storageTab as TenantStorageTab)
19+
: TENANT_STORAGE_TABS_IDS.tables;
20+
})();
21+
22+
const handleStorageTabChange = (value: TenantStorageTab) => {
23+
setQueryParams({storageTab: value}, 'replaceIn');
24+
};
25+
26+
return {
27+
storageTab,
28+
handleStorageTabChange,
29+
};
30+
}

src/store/reducers/tenant/tenant.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import type {TTenantInfo} from '../../../types/api/tenant';
66
import {TENANT_INITIAL_PAGE_KEY} from '../../../utils/constants';
77
import {api} from '../api';
88

9-
import {TENANT_METRICS_TABS_IDS, TENANT_STORAGE_TABS_IDS} from './constants';
9+
import {TENANT_METRICS_TABS_IDS} from './constants';
1010
import {tenantPageSchema} from './types';
1111
import type {
1212
TenantDiagnosticsTab,
1313
TenantMetricsTab,
1414
TenantPage,
1515
TenantQueryTab,
1616
TenantState,
17-
TenantStorageTab,
1817
TenantSummaryTab,
1918
} from './types';
2019

@@ -25,7 +24,6 @@ const tenantPage = tenantPageSchema
2524
export const initialState: TenantState = {
2625
tenantPage,
2726
metricsTab: TENANT_METRICS_TABS_IDS.cpu,
28-
storageTab: TENANT_STORAGE_TABS_IDS.tables,
2927
};
3028

3129
const slice = createSlice({
@@ -50,24 +48,12 @@ const slice = createSlice({
5048
const isValidTab = action.payload && validTabs.includes(action.payload);
5149
state.metricsTab = isValidTab ? action.payload : TENANT_METRICS_TABS_IDS.cpu;
5250
},
53-
setStorageTab: (state, action: PayloadAction<TenantStorageTab>) => {
54-
// Ensure we always have a valid storage tab - fallback to tables if empty/invalid
55-
const validTabs = Object.values(TENANT_STORAGE_TABS_IDS) as TenantStorageTab[];
56-
const isValidTab = action.payload && validTabs.includes(action.payload);
57-
state.storageTab = isValidTab ? action.payload : TENANT_STORAGE_TABS_IDS.tables;
58-
},
5951
},
6052
});
6153

6254
export default slice.reducer;
63-
export const {
64-
setTenantPage,
65-
setQueryTab,
66-
setDiagnosticsTab,
67-
setSummaryTab,
68-
setMetricsTab,
69-
setStorageTab,
70-
} = slice.actions;
55+
export const {setTenantPage, setQueryTab, setDiagnosticsTab, setSummaryTab, setMetricsTab} =
56+
slice.actions;
7157

7258
export const tenantApi = api.injectEndpoints({
7359
endpoints: (builder) => ({

src/store/reducers/tenant/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ export interface TenantState {
3030
diagnosticsTab?: TenantDiagnosticsTab;
3131
summaryTab?: TenantSummaryTab;
3232
metricsTab: TenantMetricsTab;
33-
storageTab?: TenantStorageTab;
3433
}

src/store/state-url-mapping.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ export const paramSetup = {
5353
stateKey: 'tenant.metricsTab',
5454
initialState: initialTenantState.metricsTab,
5555
},
56-
storageTab: {
57-
stateKey: 'tenant.storageTab',
58-
initialState: initialTenantState.storageTab,
59-
},
6056
shardsMode: {
6157
stateKey: 'shardsWorkload.mode',
6258
},

0 commit comments

Comments
 (0)