Skip to content

Commit

Permalink
[APM] Keep selected tab in url to avoid changing when refreshing (#14…
Browse files Browse the repository at this point in the history
  • Loading branch information
MiriamAparicio authored Nov 16, 2022
1 parent f49746a commit 768d5b8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
* 2.0.
*/

import { EuiTabbedContent, EuiLoadingSpinner } from '@elastic/eui';
import { EuiLoadingSpinner, EuiTabs, EuiTab } from '@elastic/eui';
import React from 'react';
import { useHistory } from 'react-router-dom';
import { useApmServiceContext } from '../../../../context/apm_service/use_apm_service_context';
import { useApmParams } from '../../../../hooks/use_apm_params';
import { useTimeRange } from '../../../../hooks/use_time_range';
import { FETCH_STATUS, useFetcher } from '../../../../hooks/use_fetcher';
import { EmptyPrompt } from './empty_prompt';
import { FailurePrompt } from './failure_prompt';
import { useTabs } from './use_tabs';
import { push } from '../../../shared/links/url_helpers';

const INITIAL_STATE = {
containerIds: [],
Expand All @@ -23,8 +25,9 @@ const INITIAL_STATE = {

export function InfraTabs() {
const { serviceName } = useApmServiceContext();
const history = useHistory();
const {
query: { environment, kuery, rangeFrom, rangeTo },
query: { environment, kuery, rangeFrom, rangeTo, detailTab },
} = useApmParams('/services/{serviceName}/infrastructure');
const { start, end } = useTimeRange({ rangeFrom, rangeTo });

Expand Down Expand Up @@ -89,13 +92,30 @@ export function InfraTabs() {
);
}

const currentTab = tabs.find(({ id }) => id === detailTab) ?? tabs[0];

return (
<>
<EuiTabbedContent
tabs={tabs}
initialSelectedTab={tabs[0]}
autoFocus="selected"
/>
<EuiTabs>
{tabs.map(({ id, name }) => {
return (
<EuiTab
onClick={() => {
push(history, {
query: {
detailTab: id,
},
});
}}
isSelected={currentTab.id === id}
id={id}
>
{name}
</EuiTab>
);
})}
</EuiTabs>
{currentTab.content}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import { useKibana } from '@kbn/kibana-react-plugin/public';
import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import React from 'react';
import { EuiSpacer } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { ApmPluginStartDeps } from '../../../../plugin';

type Tab = NonNullable<EuiTabbedContentProps['tabs']>[0] & {
id: 'containers' | 'pods' | 'hosts';
hidden?: boolean;
};

export enum InfraTab {
containers = 'containers',
pods = 'pods',
hosts = 'hosts',
}

export function useTabs({
containerIds,
podNames,
Expand Down Expand Up @@ -102,20 +109,26 @@ export function useTabs({

const tabs: Tab[] = [
{
id: 'containers',
name: 'Containers',
id: InfraTab.containers,
name: i18n.translate('xpack.apm.views.infra.tabs.containers', {
defaultMessage: 'Containers',
}),
content: containerMetricsTable,
hidden: containerIds && containerIds.length <= 0,
},
{
id: 'pods',
name: 'Pods',
id: InfraTab.pods,
name: i18n.translate('xpack.apm.views.infra.tabs.pods', {
defaultMessage: 'Pods',
}),
content: podMetricsTable,
hidden: podNames && podNames.length <= 0,
},
{
id: 'hosts',
name: 'Hosts',
id: InfraTab.hosts,
name: i18n.translate('xpack.apm.views.infra.tabs.hosts', {
defaultMessage: 'Hosts',
}),
content: hostMetricsTable,
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { AlertsOverview } from '../../app/alerts_overview';
import { ErrorGroupDetails } from '../../app/error_group_details';
import { ErrorGroupOverview } from '../../app/error_group_overview';
import { InfraOverview } from '../../app/infra_overview';
import { InfraTab } from '../../app/infra_overview/infra_tabs/use_tabs';
import { Metrics } from '../../app/metrics';
import { MetricsDetails } from '../../app/metrics_details';
import { ServiceDependencies } from '../../app/service_dependencies';
Expand Down Expand Up @@ -320,18 +321,27 @@ export const serviceDetail = {
showKueryBar: false,
},
}),
'/services/{serviceName}/infrastructure': page({
tab: 'infrastructure',
title: i18n.translate('xpack.apm.views.infra.title', {
defaultMessage: 'Infrastructure',
'/services/{serviceName}/infrastructure': {
...page({
tab: 'infrastructure',
title: i18n.translate('xpack.apm.views.infra.title', {
defaultMessage: 'Infrastructure',
}),
element: <InfraOverview />,
searchBarOptions: {
showKueryBar: false,
},
}),
element: <InfraOverview />,
searchBarOptions: {
showKueryBar: false,
showTimeComparison: false,
showTransactionTypeSelector: false,
},
}),
params: t.partial({
query: t.partial({
detailTab: t.union([
t.literal(InfraTab.containers),
t.literal(InfraTab.pods),
t.literal(InfraTab.hosts),
]),
}),
}),
},
'/services/{serviceName}/alerts': page({
tab: 'alerts',
title: i18n.translate('xpack.apm.views.alerts.title', {
Expand Down

0 comments on commit 768d5b8

Please sign in to comment.