Skip to content

Commit 565dd25

Browse files
Kerry350elasticmachineweltenwort
authored
[Logs / Metrics UI] Link handling / stop page reloads (#58478)
* Add a hook for seamlessly handling onClick and href props of links, buttons etc Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com>
1 parent f1272b5 commit 565dd25

File tree

23 files changed

+801
-370
lines changed

23 files changed

+801
-370
lines changed

x-pack/plugins/infra/public/components/logging/log_analysis_results/analyze_in_ml_button.tsx

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,40 @@ import { EuiButton } from '@elastic/eui';
88
import { FormattedMessage } from '@kbn/i18n/react';
99
import React from 'react';
1010
import { encode } from 'rison-node';
11-
import url from 'url';
12-
import { stringify } from 'query-string';
13-
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';
1411
import { TimeRange } from '../../../../common/http_api/shared/time_range';
15-
import { url as urlUtils } from '../../../../../../../src/plugins/kibana_utils/public';
12+
import { useLinkProps, LinkDescriptor } from '../../../hooks/use_link_props';
1613

1714
export const AnalyzeInMlButton: React.FunctionComponent<{
1815
jobId: string;
1916
partition?: string;
2017
timeRange: TimeRange;
2118
}> = ({ jobId, partition, timeRange }) => {
22-
const prependBasePath = useKibana().services.http?.basePath?.prepend;
23-
if (!prependBasePath) {
24-
return null;
25-
}
26-
const pathname = prependBasePath('/app/ml');
19+
const linkProps = useLinkProps(
20+
typeof partition === 'string'
21+
? getPartitionSpecificSingleMetricViewerLinkDescriptor(jobId, partition, timeRange)
22+
: getOverallAnomalyExplorerLinkDescriptor(jobId, timeRange)
23+
);
2724
const buttonLabel = (
2825
<FormattedMessage
2926
id="xpack.infra.logs.analysis.analyzeInMlButtonLabel"
3027
defaultMessage="Analyze in ML"
3128
/>
3229
);
3330
return typeof partition === 'string' ? (
34-
<EuiButton
35-
fill={false}
36-
size="s"
37-
href={getPartitionSpecificSingleMetricViewerLink(pathname, jobId, partition, timeRange)}
38-
>
31+
<EuiButton fill={false} size="s" {...linkProps}>
3932
{buttonLabel}
4033
</EuiButton>
4134
) : (
42-
<EuiButton
43-
fill={true}
44-
size="s"
45-
href={getOverallAnomalyExplorerLink(pathname, jobId, timeRange)}
46-
>
35+
<EuiButton fill={true} size="s" {...linkProps}>
4736
{buttonLabel}
4837
</EuiButton>
4938
);
5039
};
5140

52-
const getOverallAnomalyExplorerLink = (pathname: string, jobId: string, timeRange: TimeRange) => {
41+
const getOverallAnomalyExplorerLinkDescriptor = (
42+
jobId: string,
43+
timeRange: TimeRange
44+
): LinkDescriptor => {
5345
const { from, to } = convertTimeRangeToParams(timeRange);
5446

5547
const _g = encode({
@@ -62,20 +54,18 @@ const getOverallAnomalyExplorerLink = (pathname: string, jobId: string, timeRang
6254
},
6355
});
6456

65-
const hash = `/explorer?${stringify(urlUtils.encodeQuery({ _g }), { encode: false })}`;
66-
67-
return url.format({
68-
pathname,
69-
hash,
70-
});
57+
return {
58+
app: 'ml',
59+
hash: '/explorer',
60+
search: { _g },
61+
};
7162
};
7263

73-
const getPartitionSpecificSingleMetricViewerLink = (
74-
pathname: string,
64+
const getPartitionSpecificSingleMetricViewerLinkDescriptor = (
7565
jobId: string,
7666
partition: string,
7767
timeRange: TimeRange
78-
) => {
68+
): LinkDescriptor => {
7969
const { from, to } = convertTimeRangeToParams(timeRange);
8070

8171
const _g = encode({
@@ -95,15 +85,11 @@ const getPartitionSpecificSingleMetricViewerLink = (
9585
},
9686
});
9787

98-
const hash = `/timeseriesexplorer?${stringify(urlUtils.encodeQuery({ _g, _a }), {
99-
sort: false,
100-
encode: false,
101-
})}`;
102-
103-
return url.format({
104-
pathname,
105-
hash,
106-
});
88+
return {
89+
app: 'ml',
90+
hash: '/timeseriesexplorer',
91+
search: { _g, _a },
92+
};
10793
};
10894

10995
const convertTimeRangeToParams = (timeRange: TimeRange): { from: string; to: string } => {

x-pack/plugins/infra/public/components/logging/log_analysis_setup/user_management_link.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
import { EuiButton, EuiButtonProps } from '@elastic/eui';
88
import { FormattedMessage } from '@kbn/i18n/react';
99
import React from 'react';
10+
import { useLinkProps } from '../../../hooks/use_link_props';
1011

11-
export const UserManagementLink: React.FunctionComponent<EuiButtonProps> = props => (
12-
<EuiButton href="kibana#/management/security/users" color="primary" fill {...props}>
13-
<FormattedMessage
14-
id="xpack.infra.logs.analysis.userManagementButtonLabel"
15-
defaultMessage="Manage users"
16-
/>
17-
</EuiButton>
18-
);
12+
export const UserManagementLink: React.FunctionComponent<EuiButtonProps> = props => {
13+
const linkProps = useLinkProps({
14+
app: 'kibana',
15+
hash: '/management/security/users',
16+
});
17+
return (
18+
<EuiButton color="primary" fill {...linkProps} {...props}>
19+
<FormattedMessage
20+
id="xpack.infra.logs.analysis.userManagementButtonLabel"
21+
defaultMessage="Manage users"
22+
/>
23+
</EuiButton>
24+
);
25+
};

0 commit comments

Comments
 (0)