feat: Tabs selection persist when we refresh

This commit is contained in:
Palash gupta 2022-05-19 21:25:03 +05:30
parent 53528f1045
commit 9fd8d12cc0
No known key found for this signature in database
GPG Key ID: 8FD05AE6F9150AD6
5 changed files with 107 additions and 40 deletions

View File

@ -2,5 +2,8 @@
"general": "General",
"alert_channels": "Alert Channels",
"organization_settings": "Organization Settings",
"my_settings": "My Settings"
"my_settings": "My Settings",
"overview_metrics": "Overview Metrics",
"dbcall_metrics": "Database Calls",
"external_metrics": "External Calls"
}

View File

@ -2,5 +2,8 @@
"general": "General",
"alert_channels": "Alert Channels",
"organization_settings": "Organization Settings",
"my_settings": "My Settings"
"my_settings": "My Settings",
"overview_metrics": "Overview Metrics",
"dbcall_metrics": "Database Calls",
"external_metrics": "External Calls"
}

View File

@ -27,12 +27,19 @@ function RouteTab({
onChange={onChange}
destroyInactiveTabPane
activeKey={activeKey}
animated
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{routes.map(
({ Component, name }): JSX.Element => (
<TabPane tab={name} key={name}>
({ Component, name, route }): JSX.Element => (
<TabPane
tabKey={route}
animated
destroyInactiveTabPane
tab={name}
key={name}
>
<Component />
</TabPane>
),

View File

@ -1,54 +1,108 @@
import { Tabs } from 'antd';
import RouteTab from 'components/RouteTab';
import ROUTES from 'constants/routes';
import React from 'react';
import { generatePath, useParams } from 'react-router-dom';
import { useLocation } from 'react-use';
import { Widgets } from 'types/api/dashboard/getAll';
import ResourceAttributesFilter from './ResourceAttributesFilter';
import Application from './Tabs/Application';
import DBCall from './Tabs/DBCall';
import External from './Tabs/External';
import Overview from './Tabs/Overview';
const { TabPane } = Tabs;
const getWidget = (query: Widgets['query']): Widgets => {
return {
description: '',
id: '',
isStacked: false,
nullZeroValues: '',
opacity: '0',
panelTypes: 'TIME_SERIES',
query,
queryData: {
data: [],
error: false,
errorMessage: '',
loading: false,
},
timePreferance: 'GLOBAL_TIME',
title: '',
stepSize: 60,
};
};
function OverViewTab(): JSX.Element {
return <Overview getWidget={getWidget} />;
}
function DbCallTab(): JSX.Element {
return <DBCall getWidget={getWidget} />;
}
function ExternalTab(): JSX.Element {
return <External getWidget={getWidget} />;
}
function ServiceMetrics(): JSX.Element {
const getWidget = (query: Widgets['query']): Widgets => {
return {
description: '',
id: '',
isStacked: false,
nullZeroValues: '',
opacity: '0',
panelTypes: 'TIME_SERIES',
query,
queryData: {
data: [],
error: false,
errorMessage: '',
loading: false,
},
timePreferance: 'GLOBAL_TIME',
title: '',
stepSize: 60,
};
const { search } = useLocation();
const { servicename } = useParams<{ servicename: string }>();
const searchParams = new URLSearchParams(search);
const tab = searchParams.get('tab');
const overMetrics = 'Overview Metrics';
const dbCallMetrics = 'Database Calls';
const externalMetrics = 'External Calls';
const getActiveKey = (): string => {
if (tab === null) {
return overMetrics;
}
if (tab === dbCallMetrics) {
return dbCallMetrics;
}
if (tab === externalMetrics) {
return externalMetrics;
}
return overMetrics;
};
const activeKey = getActiveKey();
return (
<>
<ResourceAttributesFilter />
<Tabs defaultActiveKey="1">
<TabPane animated destroyInactiveTabPane tab="Application Metrics" key="1">
<Application getWidget={getWidget} />
</TabPane>
<TabPane animated destroyInactiveTabPane tab="External Calls" key="2">
<External getWidget={getWidget} />
</TabPane>
<TabPane animated destroyInactiveTabPane tab="Database Calls" key="3">
<DBCall getWidget={getWidget} />
</TabPane>
</Tabs>
<RouteTab
routes={[
{
Component: OverViewTab,
name: overMetrics,
route: `${generatePath(ROUTES.SERVICE_METRICS, {
servicename,
})}?tab=${overMetrics}`,
},
{
Component: DbCallTab,
name: dbCallMetrics,
route: `${generatePath(ROUTES.SERVICE_METRICS, {
servicename,
})}?tab=${dbCallMetrics}`,
},
{
Component: ExternalTab,
name: externalMetrics,
route: `${generatePath(ROUTES.SERVICE_METRICS, {
servicename,
})}?tab=${externalMetrics}`,
},
]}
activeKey={activeKey}
/>
</>
);
}
export default ServiceMetrics;
export default React.memo(ServiceMetrics);