Skip to content

Commit 2054e90

Browse files
authored
Removed code duplication and unset event listeners (#9262)
1 parent 9f4a467 commit 2054e90

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

cvat-ui/src/components/consensus-management-page/consensus-management-page.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,11 @@ import Result from 'antd/lib/result';
2121
import CVATLoadingSpinner from 'components/common/loading-spinner';
2222
import { ActionUnion, createAction } from 'utils/redux';
2323
import { fetchTask } from 'utils/fetch';
24+
import { getTabFromHash } from 'utils/location-utils';
2425
import ConsensusSettingsTab from './consensus-settings-tab';
2526

2627
const core = getCore();
2728

28-
function getTabFromHash(supportedTabs: string[]): string {
29-
const tab = window.location.hash.slice(1);
30-
return supportedTabs.includes(tab) ? tab : supportedTabs[0];
31-
}
32-
3329
enum TabName {
3430
settings = 'settings',
3531
}
@@ -107,8 +103,8 @@ const reducer = (state: State, action: ActionUnion<typeof reducerActions>): Stat
107103
return state;
108104
};
109105

106+
const supportedTabs = Object.values(TabName);
110107
function ConsensusManagementPage(): JSX.Element {
111-
const supportedTabs = Object.values(TabName);
112108
const [state, dispatch] = useReducer(reducer, {
113109
fetching: true,
114110
reportRefreshingStatus: null,
@@ -176,14 +172,13 @@ function ConsensusManagementPage(): JSX.Element {
176172
}, [requestedInstanceID]);
177173

178174
useEffect(() => {
179-
window.addEventListener('hashchange', () => {
180-
const hash = getTabFromHash(supportedTabs);
181-
setActiveTab(hash);
182-
});
175+
const onHashChange = () => setActiveTab(getTabFromHash(supportedTabs));
176+
window.addEventListener('hashchange', onHashChange);
177+
return () => window.removeEventListener('hashchange', onHashChange);
183178
}, []);
184179

185180
useEffect(() => {
186-
window.location.hash = activeTab;
181+
window.history.replaceState(null, '', `#${activeTab}`);
187182
}, [activeTab]);
188183

189184
const onTabKeyChange = useCallback((key: string): void => {

cvat-ui/src/components/quality-control/quality-control-page.tsx

+5-10
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ import {
2222
import CVATLoadingSpinner from 'components/common/loading-spinner';
2323
import GoBackButton from 'components/common/go-back-button';
2424
import { ActionUnion, createAction } from 'utils/redux';
25+
import { getTabFromHash } from 'utils/location-utils';
2526
import { fetchTask } from 'utils/fetch';
2627
import QualityOverviewTab from './task-quality/quality-overview-tab';
2728
import QualityManagementTab from './task-quality/quality-magement-tab';
2829
import QualitySettingsTab from './quality-settings-tab';
2930

3031
const core = getCore();
3132

32-
function getTabFromHash(supportedTabs: string[]): string {
33-
const tab = window.location.hash.slice(1);
34-
return supportedTabs.includes(tab) ? tab : supportedTabs[0];
35-
}
36-
3733
interface State {
3834
fetching: boolean;
3935
reportRefreshingStatus: string | null;
@@ -159,8 +155,8 @@ const reducer = (state: State, action: ActionUnion<typeof reducerActions>): Stat
159155
return state;
160156
};
161157

158+
const supportedTabs = ['overview', 'settings', 'management'];
162159
function QualityControlPage(): JSX.Element {
163-
const supportedTabs = ['overview', 'settings', 'management'];
164160
const [state, dispatch] = useReducer(reducer, {
165161
fetching: true,
166162
reportRefreshingStatus: null,
@@ -293,10 +289,9 @@ function QualityControlPage(): JSX.Element {
293289
}, [requestedInstanceID]);
294290

295291
useEffect(() => {
296-
window.addEventListener('hashchange', () => {
297-
const hash = getTabFromHash(supportedTabs);
298-
setActiveTab(hash);
299-
});
292+
const onHashChange = () => setActiveTab(getTabFromHash(supportedTabs));
293+
window.addEventListener('hashchange', onHashChange);
294+
return () => window.removeEventListener('hashchange', onHashChange);
300295
}, []);
301296

302297
useEffect(() => {

cvat-ui/src/utils/location-utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (C) CVAT.ai Corporation
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
export function getTabFromHash(supportedTabs: string[]): string {
6+
const tab = window.location.hash.slice(1);
7+
return supportedTabs.includes(tab) ? tab : supportedTabs[0];
8+
}

0 commit comments

Comments
 (0)