Skip to content

Commit baba3c2

Browse files
authored
[APM] Fixes rare cases where KibanaLink is loaded outside of React context (#24705) (#24738)
* Fixes rare cases where KibanaLink will be loaded outside of React context and requires no redux connect dependency * Fixes tests for updated Kibana link component * Removes obsolete snapshot
1 parent e7d97d0 commit baba3c2

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

x-pack/plugins/apm/public/components/app/ErrorGroupOverview/Watcher/WatcherFlyOut.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333

3434
import { XPACK_DOCS } from '../../../../utils/documentation/xpack';
3535

36-
import { KibanaLink } from '../../../../utils/url';
36+
import { UnconnectedKibanaLink } from '../../../../utils/url';
3737
import { createErrorGroupWatch } from './createErrorGroupWatch';
3838
import chrome from 'ui/chrome';
3939

@@ -187,12 +187,13 @@ export default class WatcherFlyout extends Component {
187187
<p>
188188
The watch is now ready and will send error reports for{' '}
189189
{this.props.serviceName}.{' '}
190-
<KibanaLink
190+
<UnconnectedKibanaLink
191+
location={this.props.location}
191192
pathname={'/app/kibana'}
192193
hash={`/management/elasticsearch/watcher/watches/watch/${id}`}
193194
>
194195
View watch.
195-
</KibanaLink>
196+
</UnconnectedKibanaLink>
196197
</p>
197198
)
198199
});
@@ -419,6 +420,7 @@ export default class WatcherFlyout extends Component {
419420
}
420421

421422
WatcherFlyout.propTypes = {
423+
location: PropTypes.object.isRequired,
422424
isOpen: PropTypes.bool.isRequired,
423425
serviceName: PropTypes.string,
424426
onClose: PropTypes.func.isRequired

x-pack/plugins/apm/public/components/app/ErrorGroupOverview/view.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ErrorGroupOverview extends Component {
5353
/>
5454

5555
<WatcherFlyout
56+
location={location}
5657
serviceName={serviceName}
5758
isOpen={this.state.isFlyoutOpen}
5859
onClose={this.onCloseFlyout}
Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`KibanaLinkComponent should render correct markup 1`] = `
3+
exports[`RelativeLinkComponent should render correct markup 1`] = `
44
<a
5-
className="euiLink euiLink--primary"
6-
href="myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:'context.service.name:myServiceName AND error.grouping_key:myGroupId'),sort:('@timestamp':desc))&_g="
5+
className="euiLink euiLink--primary "
6+
href="/opbeans-node/errors?foo=bar&foo2=bar2"
7+
onClick={[Function]}
78
>
89
Go to Discover
910
</a>
1011
`;
1112

12-
exports[`RelativeLinkComponent should render correct markup 1`] = `
13+
exports[`UnconnectedKibanaLink should render correct markup 1`] = `
1314
<a
14-
className="euiLink euiLink--primary "
15-
href="/opbeans-node/errors?foo=bar&foo2=bar2"
16-
onClick={[Function]}
15+
className="euiLink euiLink--primary"
16+
href="myBasePath/app/kibana#/discover?_a=(interval:auto,query:(language:lucene,query:'context.service.name:myServiceName AND error.grouping_key:myGroupId'),sort:('@timestamp':desc))&_g="
1717
>
1818
Go to Discover
1919
</a>
2020
`;
2121

2222
exports[`ViewMLJob should render component 1`] = `
23-
<Connect(KibanaLinkComponent)
23+
<Component
2424
hash="/timeseriesexplorer"
25+
location={
26+
Object {
27+
"search": "",
28+
}
29+
}
2530
pathname="/app/ml"
2631
query={
2732
Object {
@@ -37,5 +42,5 @@ exports[`ViewMLJob should render component 1`] = `
3742
}
3843
>
3944
View Job
40-
</Connect(KibanaLinkComponent)>
45+
</Component>
4146
`;

x-pack/plugins/apm/public/utils/__test__/url.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import createHistory from 'history/createMemoryHistory';
1111
import {
1212
toQuery,
1313
fromQuery,
14-
KibanaLinkComponent,
14+
UnconnectedKibanaLink,
1515
RelativeLinkComponent,
1616
encodeKibanaSearchParams,
1717
decodeKibanaSearchParams,
@@ -182,7 +182,7 @@ describe('RelativeLinkComponent', () => {
182182
});
183183
});
184184

185-
describe('KibanaLinkComponent', () => {
185+
describe('UnconnectedKibanaLink', () => {
186186
let wrapper;
187187

188188
beforeEach(() => {
@@ -198,14 +198,14 @@ describe('KibanaLinkComponent', () => {
198198
};
199199

200200
wrapper = mount(
201-
<KibanaLinkComponent
201+
<UnconnectedKibanaLink
202202
location={{ search: '' }}
203203
pathname={'/app/kibana'}
204204
hash={'/discover'}
205205
query={discoverQuery}
206206
>
207207
Go to Discover
208-
</KibanaLinkComponent>
208+
</UnconnectedKibanaLink>
209209
);
210210
});
211211

x-pack/plugins/apm/public/utils/url.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export function ViewMLJob({
4444
};
4545

4646
return (
47-
<KibanaLink
47+
<UnconnectedKibanaLink
48+
location={location}
4849
pathname={pathname}
4950
hash={hash}
5051
query={query}
@@ -164,13 +165,19 @@ export interface KibanaLinkArgs {
164165
className?: string;
165166
}
166167

167-
export function KibanaLinkComponent({
168+
/**
169+
* NOTE: Use this component directly if you have to use a link that is
170+
* going to be rendered outside of React, e.g. in the Kibana global toast loader.
171+
*
172+
* You must remember to pass in location in that case.
173+
*/
174+
export const UnconnectedKibanaLink: React.SFC<KibanaLinkArgs> = ({
168175
location,
169176
pathname,
170177
hash,
171178
query = {},
172179
...props
173-
}: KibanaLinkArgs) {
180+
}) => {
174181
// Preserve current _g and _a
175182
const currentQuery = toQuery(location.search);
176183
const nextQuery = {
@@ -186,14 +193,14 @@ export function KibanaLinkComponent({
186193
});
187194

188195
return <EuiLink {...props} href={href} />;
189-
}
196+
};
190197

191198
const withLocation = connect(
192199
({ location }: { location: any }) => ({ location }),
193200
{}
194201
);
195202
export const RelativeLink = withLocation(RelativeLinkComponent);
196-
export const KibanaLink = withLocation(KibanaLinkComponent);
203+
export const KibanaLink = withLocation(UnconnectedKibanaLink);
197204

198205
// This is downright horrible 😭 💔
199206
// Angular decodes encoded url tokens like "%2F" to "/" which causes the route to change.

0 commit comments

Comments
 (0)