Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfixes(fe): fix toolbar tabs selected state #1588

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion odd-platform-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"react-infinite-scroll-component": "^6.1.0",
"react-multi-date-picker": "^4.4.1",
"react-redux": "^8.1.2",
"react-router-dom": "^6.20.1",
"react-router-dom": "^6.21.2",
"react-truncate-markup": "^5.1.2",
"recharts": "^2.10.2",
"styled-components": "^6.1.1",
Expand Down
22 changes: 11 additions & 11 deletions odd-platform-ui/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion odd-platform-ui/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const App: React.FC = () => {
<Route path={dataQualityPath()} element={<DataQuality />} />
<Route path={`${dataModellingPath()}/*`} element={<DataModeling />} />
<Route
path={`${lookupTablesPath()}`}
path={lookupTablesPath()}
element={
<WithPermissionsProvider
allowedPermissions={[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import type React from 'react';
import { useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import { useLocation, useResolvedPath } from 'react-router-dom';
import type { AppTabItem } from './AppTabs';

const useSetSelectedTab = (
tabs: AppTabItem[],
setSelectedTab: React.Dispatch<React.SetStateAction<number>>
) => {
const resolvedPath = useResolvedPath(useLocation().pathname);
useEffect(() => {
const foundIndex = tabs.findIndex(({ link }) => {
const { pathname } = resolvedPath;
return link ? pathname.includes(link) || link.includes(pathname) : false;
});
setSelectedTab(foundIndex);
}, [tabs, resolvedPath]);
const { pathname } = useResolvedPath(useLocation());

const findTabIndex = useCallback(
() =>
tabs.findIndex(({ link }) => {
if (link) return pathname.includes(link) || link.includes(pathname);
return false;
}),
[tabs, pathname]
);

useEffect(() => setSelectedTab(findTabIndex()), [tabs, pathname]);
};

export default useSetSelectedTab;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { type FC, useCallback, useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import React, { type FC, useCallback, useMemo, useState, useEffect } from 'react';
import { matchPath, useLocation, useNavigate, useResolvedPath } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useCreateSearch, useQueryParams } from 'lib/hooks';
import {
Expand All @@ -12,6 +12,7 @@ import AppTabs, { type AppTabItem } from 'components/shared/elements/AppTabs/App
import {
activityPath,
alertsPath,
dataEntitiesPath,
dataQualityPath,
directoryPath,
lookupTablesPath,
Expand All @@ -20,17 +21,17 @@ import {
searchPath,
termsSearchPath,
} from 'routes';
import useSetSelectedTab from '../../AppTabs/useSetSelectedTab';

const ToolbarTabs: FC = () => {
const dispatch = useAppDispatch();
const { t } = useTranslation();
const navigate = useNavigate();
const createSearch = useCreateSearch();
const { pathname } = useResolvedPath(useLocation());
const { defaultQueryString: activityQueryString } =
useQueryParams<ActivityQuery>(defaultActivityQuery);

const tabs = useMemo<AppTabItem[]>(
const tabs = useMemo<AppTabItem<string>[]>(
() => [
{
name: t('Catalog'),
Expand All @@ -39,49 +40,74 @@ const ToolbarTabs: FC = () => {
{
name: t('Directory'),
link: directoryPath(),
value: 'directory',
},
{
name: t('Data Quality'),
link: dataQualityPath(),
value: 'data-quality',
},
{
name: t('Data Modelling'),
link: queryExamplesPath(),
hint: t('BETA'),
hintType: 'secondary',
value: 'data-modelling',
},
{
name: t('Master Data'),
link: lookupTablesPath(),
value: 'master-data',
},
{
name: t('Management'),
link: managementPath(),
value: 'management',
},
{
name: t('Dictionary'),
link: termsSearchPath(),
value: 'dictionary',
},
{
name: t('Alerts'),
link: alertsPath('all'),
value: 'alerts',
},
{
name: t('Activity'),
link: activityPath(activityQueryString),
value: 'activity',
},
],
[activityQueryString, t]
);

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

useSetSelectedTab(tabs, setSelectedTab);
useEffect(() => {
if (matchPath('/', pathname)) {
setSelectedTab(-1);
return;
}

if (
matchPath(`${searchPath()}/*`, pathname) ||
matchPath(`${dataEntitiesPath()}/*`, pathname)
) {
setSelectedTab(0);
return;
}

tabs.forEach((tab, idx) => {
if (tab.value && pathname.includes(tab.value)) {
setSelectedTab(idx);
}
});
}, [pathname]);

const handleTabClick = useCallback(
(idx: number) => {
setSelectedTab(idx);

const initialParams = { query: '', pageSize: 30, filters: {} };

if (tabs[idx].name === t('Dictionary')) {
Expand Down
Loading