Skip to content

Commit 3d91165

Browse files
authored
[APM] Remove useLocation and some minor route improvements (#76343)
* [APM] Remove useLocation and some minor route improvements * Replace `useLocation` and our `LocationContext` with `useLocation` from React Router. We can do this since we're now using the platform history, added in #76287. * Pass in `RouteComponentProps` where appropriate to routes to use `history` and `location`. This is in the service of #51963, but doesn't do anything with `useUrlParams` or any of the other changes specified in that issue.
1 parent 98e6475 commit 3d91165

File tree

34 files changed

+196
-160
lines changed

34 files changed

+196
-160
lines changed

x-pack/plugins/apm/public/application/application.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { createCallApmApi } from '../services/rest/createCallApmApi';
1414
import { renderApp } from './';
1515
import { disableConsoleWarning } from '../utils/testHelpers';
1616

17+
jest.mock('../services/rest/index_pattern', () => ({
18+
createStaticIndexPattern: () => Promise.resolve(undefined),
19+
}));
20+
1721
describe('renderApp', () => {
1822
let mockConsole: jest.SpyInstance;
1923

x-pack/plugins/apm/public/application/index.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import { ScrollToTopOnPathChange } from '../components/app/Main/ScrollToTopOnPat
2525
import { ApmPluginContext } from '../context/ApmPluginContext';
2626
import { LicenseProvider } from '../context/LicenseContext';
2727
import { LoadingIndicatorProvider } from '../context/LoadingIndicatorContext';
28-
import { LocationProvider } from '../context/LocationContext';
2928
import { UrlParamsProvider } from '../context/UrlParamsContext';
3029
import { useBreadcrumbs } from '../hooks/use_breadcrumbs';
3130
import { ApmPluginSetupDeps } from '../plugin';
@@ -99,15 +98,13 @@ export function ApmAppRoot({
9998
<KibanaContextProvider services={{ ...core, ...plugins }}>
10099
<i18nCore.Context>
101100
<Router history={history}>
102-
<LocationProvider>
103-
<UrlParamsProvider>
104-
<LoadingIndicatorProvider>
105-
<LicenseProvider>
106-
<App />
107-
</LicenseProvider>
108-
</LoadingIndicatorProvider>
109-
</UrlParamsProvider>
110-
</LocationProvider>
101+
<UrlParamsProvider>
102+
<LoadingIndicatorProvider>
103+
<LicenseProvider>
104+
<App />
105+
</LicenseProvider>
106+
</LoadingIndicatorProvider>
107+
</UrlParamsProvider>
111108
</Router>
112109
</i18nCore.Context>
113110
</KibanaContextProvider>

x-pack/plugins/apm/public/components/app/ErrorGroupOverview/List/__test__/List.test.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { MockUrlParamsContextProvider } from '../../../../../context/UrlParamsCo
1111
import { mockMoment, toJson } from '../../../../../utils/testHelpers';
1212
import { ErrorGroupList } from '../index';
1313
import props from './props.json';
14+
import { MemoryRouter } from 'react-router-dom';
1415

1516
jest.mock('@elastic/eui/lib/services/accessibility/html_id_generator', () => {
1617
return {
@@ -26,9 +27,11 @@ describe('ErrorGroupOverview -> List', () => {
2627
it('should render empty state', () => {
2728
const storeState = {};
2829
const wrapper = mount(
29-
<MockUrlParamsContextProvider>
30-
<ErrorGroupList items={[]} serviceName="opbeans-python" />
31-
</MockUrlParamsContextProvider>,
30+
<MemoryRouter>
31+
<MockUrlParamsContextProvider>
32+
<ErrorGroupList items={[]} serviceName="opbeans-python" />
33+
</MockUrlParamsContextProvider>
34+
</MemoryRouter>,
3235
storeState
3336
);
3437

@@ -37,11 +40,13 @@ describe('ErrorGroupOverview -> List', () => {
3740

3841
it('should render with data', () => {
3942
const wrapper = mount(
40-
<MockApmPluginContextWrapper>
41-
<MockUrlParamsContextProvider>
42-
<ErrorGroupList items={props.items} serviceName="opbeans-python" />
43-
</MockUrlParamsContextProvider>
44-
</MockApmPluginContextWrapper>
43+
<MemoryRouter>
44+
<MockApmPluginContextWrapper>
45+
<MockUrlParamsContextProvider>
46+
<ErrorGroupList items={props.items} serviceName="opbeans-python" />
47+
</MockUrlParamsContextProvider>
48+
</MockApmPluginContextWrapper>
49+
</MemoryRouter>
4550
);
4651

4752
expect(toJson(wrapper)).toMatchSnapshot();

x-pack/plugins/apm/public/components/app/Main/route_config/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,33 @@ function ServiceDetailsTransactions(
113113
return <ServiceDetails {...props} tab="transactions" />;
114114
}
115115

116-
function SettingsAgentConfiguration() {
116+
function SettingsAgentConfiguration(props: RouteComponentProps<{}>) {
117117
return (
118-
<Settings>
118+
<Settings {...props}>
119119
<AgentConfigurations />
120120
</Settings>
121121
);
122122
}
123123

124-
function SettingsAnomalyDetection() {
124+
function SettingsAnomalyDetection(props: RouteComponentProps<{}>) {
125125
return (
126-
<Settings>
126+
<Settings {...props}>
127127
<AnomalyDetection />
128128
</Settings>
129129
);
130130
}
131131

132-
function SettingsApmIndices() {
132+
function SettingsApmIndices(props: RouteComponentProps<{}>) {
133133
return (
134-
<Settings>
134+
<Settings {...props}>
135135
<ApmIndices />
136136
</Settings>
137137
);
138138
}
139139

140-
function SettingsCustomizeUI() {
140+
function SettingsCustomizeUI(props: RouteComponentProps<{}>) {
141141
return (
142-
<Settings>
142+
<Settings {...props}>
143143
<CustomizeUI />
144144
</Settings>
145145
);

x-pack/plugins/apm/public/components/app/Main/route_config/route_handlers/agent_configuration.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import { AgentConfigurationCreateEdit } from '../../../Settings/AgentConfigurati
1313

1414
type EditAgentConfigurationRouteHandler = RouteComponentProps<{}>;
1515

16-
export function EditAgentConfigurationRouteHandler({
17-
history,
18-
}: EditAgentConfigurationRouteHandler) {
19-
const { search } = history.location;
16+
export function EditAgentConfigurationRouteHandler(
17+
props: EditAgentConfigurationRouteHandler
18+
) {
19+
const { search } = props.history.location;
2020

2121
// typescript complains because `pageStop` does not exist in `APMQueryParams`
2222
// Going forward we should move away from globally declared query params and this is a first step
@@ -34,7 +34,7 @@ export function EditAgentConfigurationRouteHandler({
3434
);
3535

3636
return (
37-
<Settings>
37+
<Settings {...props}>
3838
<AgentConfigurationCreateEdit
3939
pageStep={pageStep || 'choose-settings-step'}
4040
existingConfigResult={res}
@@ -45,17 +45,17 @@ export function EditAgentConfigurationRouteHandler({
4545

4646
type CreateAgentConfigurationRouteHandlerProps = RouteComponentProps<{}>;
4747

48-
export function CreateAgentConfigurationRouteHandler({
49-
history,
50-
}: CreateAgentConfigurationRouteHandlerProps) {
51-
const { search } = history.location;
48+
export function CreateAgentConfigurationRouteHandler(
49+
props: CreateAgentConfigurationRouteHandlerProps
50+
) {
51+
const { search } = props.history.location;
5252

5353
// Ignoring here because we specifically DO NOT want to add the query params to the global route handler
5454
// @ts-expect-error
5555
const { pageStep } = toQuery(search);
5656

5757
return (
58-
<Settings>
58+
<Settings {...props}>
5959
<AgentConfigurationCreateEdit
6060
pageStep={pageStep || 'choose-service-step'}
6161
/>

x-pack/plugins/apm/public/components/app/ServiceOverview/__test__/ServiceOverview.test.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66

77
import { render, wait, waitForElement } from '@testing-library/react';
88
import { CoreStart } from 'kibana/public';
9+
import { merge } from 'lodash';
910
import React, { FunctionComponent, ReactChild } from 'react';
11+
import { MemoryRouter } from 'react-router-dom';
1012
import { createKibanaReactContext } from 'src/plugins/kibana_react/public';
11-
import { merge } from 'lodash';
1213
import { ServiceOverview } from '..';
14+
import { EuiThemeProvider } from '../../../../../../observability/public';
1315
import { ApmPluginContextValue } from '../../../../context/ApmPluginContext';
1416
import {
1517
mockApmPluginContextValue,
1618
MockApmPluginContextWrapper,
1719
} from '../../../../context/ApmPluginContext/MockApmPluginContext';
20+
import * as useAnomalyDetectionJobs from '../../../../hooks/useAnomalyDetectionJobs';
1821
import { FETCH_STATUS } from '../../../../hooks/useFetcher';
1922
import * as useLocalUIFilters from '../../../../hooks/useLocalUIFilters';
2023
import * as urlParamsHooks from '../../../../hooks/useUrlParams';
21-
import * as useAnomalyDetectionJobs from '../../../../hooks/useAnomalyDetectionJobs';
2224
import { SessionStorageMock } from '../../../../services/__test__/SessionStorageMock';
23-
import { EuiThemeProvider } from '../../../../../../../legacy/common/eui_styled_components';
2425

2526
const KibanaReactContext = createKibanaReactContext({
2627
usageCollection: { reportUiStats: () => {} },
@@ -44,13 +45,15 @@ function wrapper({ children }: { children: ReactChild }) {
4445
}) as unknown) as ApmPluginContextValue;
4546

4647
return (
47-
<KibanaReactContext.Provider>
48+
<MemoryRouter>
4849
<EuiThemeProvider>
49-
<MockApmPluginContextWrapper value={mockPluginContext as any}>
50-
{children}
51-
</MockApmPluginContextWrapper>
50+
<KibanaReactContext.Provider>
51+
<MockApmPluginContextWrapper value={mockPluginContext}>
52+
{children}
53+
</MockApmPluginContextWrapper>
54+
</KibanaReactContext.Provider>
5255
</EuiThemeProvider>
53-
</KibanaReactContext.Provider>
56+
</MemoryRouter>
5457
);
5558
}
5659

x-pack/plugins/apm/public/components/app/Settings/AgentConfigurations/List/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import {
1515
import { i18n } from '@kbn/i18n';
1616
import { isEmpty } from 'lodash';
1717
import React, { useState } from 'react';
18+
import { useLocation } from 'react-router-dom';
1819
import { getOptionLabel } from '../../../../../../common/agent_configuration/all_option';
1920
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
2021
import { AgentConfigurationListAPIResponse } from '../../../../../../server/lib/settings/agent_configuration/list_configurations';
2122
import { useApmPluginContext } from '../../../../../hooks/useApmPluginContext';
2223
import { FETCH_STATUS } from '../../../../../hooks/useFetcher';
23-
import { useLocation } from '../../../../../hooks/useLocation';
2424
import { useTheme } from '../../../../../hooks/useTheme';
2525
import { px, units } from '../../../../../style/variables';
2626
import {

x-pack/plugins/apm/public/components/app/Settings/AgentConfigurations/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import {
1515
import { i18n } from '@kbn/i18n';
1616
import { isEmpty } from 'lodash';
1717
import React from 'react';
18+
import { useLocation } from 'react-router-dom';
1819
import { useTrackPageview } from '../../../../../../observability/public';
1920
import { useApmPluginContext } from '../../../../hooks/useApmPluginContext';
2021
import { useFetcher } from '../../../../hooks/useFetcher';
21-
import { useLocation } from '../../../../hooks/useLocation';
2222
import { createAgentConfigurationHref } from '../../../shared/Links/apm/agentConfigurationLinks';
2323
import { AgentConfigurationList } from './List';
2424

x-pack/plugins/apm/public/components/app/Settings/Settings.test.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,27 @@ import { render } from '@testing-library/react';
88
import { MockApmPluginContextWrapper } from '../../../context/ApmPluginContext/MockApmPluginContext';
99
import React, { ReactNode } from 'react';
1010
import { Settings } from './';
11-
import { LocationContext } from '../../../context/LocationContext';
1211
import { createMemoryHistory } from 'history';
12+
import { MemoryRouter, RouteComponentProps } from 'react-router-dom';
13+
14+
const { location } = createMemoryHistory();
1315

1416
function Wrapper({ children }: { children?: ReactNode }) {
15-
const { location } = createMemoryHistory();
1617
return (
17-
<LocationContext.Provider value={location}>
18+
<MemoryRouter>
1819
<MockApmPluginContextWrapper>{children}</MockApmPluginContextWrapper>
19-
</LocationContext.Provider>
20+
</MemoryRouter>
2021
);
2122
}
2223

2324
describe('Settings', () => {
2425
it('renders', async () => {
26+
const routerProps = ({
27+
location,
28+
} as unknown) as RouteComponentProps<{}>;
2529
expect(() =>
2630
render(
27-
<Settings>
31+
<Settings {...routerProps}>
2832
<div />
2933
</Settings>,
3034
{ wrapper: Wrapper }

x-pack/plugins/apm/public/components/app/Settings/index.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { ReactNode } from 'react';
8-
import { i18n } from '@kbn/i18n';
97
import {
108
EuiButtonEmpty,
119
EuiPage,
12-
EuiSideNav,
13-
EuiPageSideBar,
1410
EuiPageBody,
11+
EuiPageSideBar,
12+
EuiSideNav,
1513
} from '@elastic/eui';
16-
import { HomeLink } from '../../shared/Links/apm/HomeLink';
17-
import { useLocation } from '../../../hooks/useLocation';
18-
import { getAPMHref } from '../../shared/Links/apm/APMLink';
14+
import { i18n } from '@kbn/i18n';
15+
import React, { ReactNode } from 'react';
16+
import { RouteComponentProps } from 'react-router-dom';
1917
import { useApmPluginContext } from '../../../hooks/useApmPluginContext';
18+
import { getAPMHref } from '../../shared/Links/apm/APMLink';
19+
import { HomeLink } from '../../shared/Links/apm/HomeLink';
20+
21+
interface SettingsProps extends RouteComponentProps<{}> {
22+
children: ReactNode;
23+
}
2024

21-
export function Settings(props: { children: ReactNode }) {
25+
export function Settings({ children, location }: SettingsProps) {
2226
const { core } = useApmPluginContext();
2327
const { basePath } = core.http;
2428
const canAccessML = !!core.application.capabilities.ml?.canAccessML;
25-
const { search, pathname } = useLocation();
29+
const { search, pathname } = location;
2630

2731
function getSettingsHref(path: string) {
2832
return getAPMHref({ basePath, path: `/settings${path}`, search });
@@ -94,7 +98,7 @@ export function Settings(props: { children: ReactNode }) {
9498
]}
9599
/>
96100
</EuiPageSideBar>
97-
<EuiPageBody>{props.children}</EuiPageBody>
101+
<EuiPageBody>{children}</EuiPageBody>
98102
</EuiPage>
99103
</>
100104
);

0 commit comments

Comments
 (0)