Skip to content

Commit 8866d3c

Browse files
bugfixes(fe): fix toolbar tabs selected state (#1588)
1 parent 8ea9d90 commit 8866d3c

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

odd-platform-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"react-infinite-scroll-component": "^6.1.0",
8181
"react-multi-date-picker": "^4.4.1",
8282
"react-redux": "^8.1.2",
83-
"react-router-dom": "^6.20.1",
83+
"react-router-dom": "^6.21.2",
8484
"react-truncate-markup": "^5.1.2",
8585
"recharts": "^2.10.2",
8686
"styled-components": "^6.1.1",

odd-platform-ui/pnpm-lock.yaml

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

odd-platform-ui/src/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const App: React.FC = () => {
7373
<Route path={dataQualityPath()} element={<DataQuality />} />
7474
<Route path={`${dataModellingPath()}/*`} element={<DataModeling />} />
7575
<Route
76-
path={`${lookupTablesPath()}`}
76+
path={lookupTablesPath()}
7777
element={
7878
<WithPermissionsProvider
7979
allowedPermissions={[
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import type React from 'react';
2-
import { useEffect } from 'react';
2+
import { useCallback, useEffect } from 'react';
33
import { useLocation, useResolvedPath } from 'react-router-dom';
44
import type { AppTabItem } from './AppTabs';
55

66
const useSetSelectedTab = (
77
tabs: AppTabItem[],
88
setSelectedTab: React.Dispatch<React.SetStateAction<number>>
99
) => {
10-
const resolvedPath = useResolvedPath(useLocation().pathname);
11-
useEffect(() => {
12-
const foundIndex = tabs.findIndex(({ link }) => {
13-
const { pathname } = resolvedPath;
14-
return link ? pathname.includes(link) || link.includes(pathname) : false;
15-
});
16-
setSelectedTab(foundIndex);
17-
}, [tabs, resolvedPath]);
10+
const { pathname } = useResolvedPath(useLocation());
11+
12+
const findTabIndex = useCallback(
13+
() =>
14+
tabs.findIndex(({ link }) => {
15+
if (link) return pathname.includes(link) || link.includes(pathname);
16+
return false;
17+
}),
18+
[tabs, pathname]
19+
);
20+
21+
useEffect(() => setSelectedTab(findTabIndex()), [tabs, pathname]);
1822
};
1923

2024
export default useSetSelectedTab;

odd-platform-ui/src/components/shared/elements/AppToolbar/ToolbarTabs/ToolbarTabs.tsx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { type FC, useCallback, useMemo, useState } from 'react';
2-
import { useNavigate } from 'react-router-dom';
1+
import React, { type FC, useCallback, useMemo, useState, useEffect } from 'react';
2+
import { matchPath, useLocation, useNavigate, useResolvedPath } from 'react-router-dom';
33
import { useTranslation } from 'react-i18next';
44
import { useCreateSearch, useQueryParams } from 'lib/hooks';
55
import {
@@ -12,6 +12,7 @@ import AppTabs, { type AppTabItem } from 'components/shared/elements/AppTabs/App
1212
import {
1313
activityPath,
1414
alertsPath,
15+
dataEntitiesPath,
1516
dataQualityPath,
1617
directoryPath,
1718
lookupTablesPath,
@@ -20,17 +21,17 @@ import {
2021
searchPath,
2122
termsSearchPath,
2223
} from 'routes';
23-
import useSetSelectedTab from '../../AppTabs/useSetSelectedTab';
2424

2525
const ToolbarTabs: FC = () => {
2626
const dispatch = useAppDispatch();
2727
const { t } = useTranslation();
2828
const navigate = useNavigate();
2929
const createSearch = useCreateSearch();
30+
const { pathname } = useResolvedPath(useLocation());
3031
const { defaultQueryString: activityQueryString } =
3132
useQueryParams<ActivityQuery>(defaultActivityQuery);
3233

33-
const tabs = useMemo<AppTabItem[]>(
34+
const tabs = useMemo<AppTabItem<string>[]>(
3435
() => [
3536
{
3637
name: t('Catalog'),
@@ -39,49 +40,74 @@ const ToolbarTabs: FC = () => {
3940
{
4041
name: t('Directory'),
4142
link: directoryPath(),
43+
value: 'directory',
4244
},
4345
{
4446
name: t('Data Quality'),
4547
link: dataQualityPath(),
48+
value: 'data-quality',
4649
},
4750
{
4851
name: t('Data Modelling'),
4952
link: queryExamplesPath(),
5053
hint: t('BETA'),
5154
hintType: 'secondary',
55+
value: 'data-modelling',
5256
},
5357
{
5458
name: t('Master Data'),
5559
link: lookupTablesPath(),
60+
value: 'master-data',
5661
},
5762
{
5863
name: t('Management'),
5964
link: managementPath(),
65+
value: 'management',
6066
},
6167
{
6268
name: t('Dictionary'),
6369
link: termsSearchPath(),
70+
value: 'dictionary',
6471
},
6572
{
6673
name: t('Alerts'),
6774
link: alertsPath('all'),
75+
value: 'alerts',
6876
},
6977
{
7078
name: t('Activity'),
7179
link: activityPath(activityQueryString),
80+
value: 'activity',
7281
},
7382
],
7483
[activityQueryString, t]
7584
);
7685

7786
const [selectedTab, setSelectedTab] = useState(-1);
7887

79-
useSetSelectedTab(tabs, setSelectedTab);
88+
useEffect(() => {
89+
if (matchPath('/', pathname)) {
90+
setSelectedTab(-1);
91+
return;
92+
}
93+
94+
if (
95+
matchPath(`${searchPath()}/*`, pathname) ||
96+
matchPath(`${dataEntitiesPath()}/*`, pathname)
97+
) {
98+
setSelectedTab(0);
99+
return;
100+
}
101+
102+
tabs.forEach((tab, idx) => {
103+
if (tab.value && pathname.includes(tab.value)) {
104+
setSelectedTab(idx);
105+
}
106+
});
107+
}, [pathname]);
80108

81109
const handleTabClick = useCallback(
82110
(idx: number) => {
83-
setSelectedTab(idx);
84-
85111
const initialParams = { query: '', pageSize: 30, filters: {} };
86112

87113
if (tabs[idx].name === t('Dictionary')) {

0 commit comments

Comments
 (0)