-
Notifications
You must be signed in to change notification settings - Fork 8.5k
[ML] Fix regression with some links not opening in new tab #80785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
65e86d4
257835a
b8052fe
36cc49a
9e68a05
66e775a
b5418b6
0064254
ac8ffcc
df14a3a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| import React, { ReactChild } from 'react'; | ||
| import { useMlKibana, useNavigateToPath } from '../../contexts/kibana'; | ||
|
|
||
| function isModifiedEvent(event: React.MouseEvent<HTMLAnchorElement, MouseEvent>) { | ||
| return !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey); | ||
| } | ||
|
|
||
| export const MlHref = ({ | ||
|
||
| href, | ||
| target, | ||
| onClick, | ||
| children, | ||
| ...rest | ||
|
||
| }: { | ||
| href: string; | ||
| target?: string; | ||
| onClick?: Function; | ||
| children?: ReactChild; | ||
| }) => { | ||
| const { | ||
| services: { | ||
| http: { basePath }, | ||
| }, | ||
| } = useMlKibana(); | ||
|
|
||
| const navigateToPath = useNavigateToPath(); | ||
| return ( | ||
| <a | ||
darnautov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| target={target} | ||
| href={basePath.prepend(`/app/ml/${href}`)} | ||
|
||
| onClick={(event) => { | ||
| try { | ||
| if (onClick) onClick(event); | ||
| } catch (ex) { | ||
| event.preventDefault(); | ||
| throw ex; | ||
| } | ||
|
|
||
| if ( | ||
| !event.defaultPrevented && // onClick prevented default | ||
| event.button === 0 && // ignore everything but left clicks | ||
| (!target || target === '_self') && // let browser handle "target=_blank" etc. | ||
| !isModifiedEvent(event) // ignore clicks with modifier keys | ||
| ) { | ||
| event.preventDefault(); | ||
| navigateToPath(href); | ||
| return false; | ||
| } | ||
| return true; | ||
| }} | ||
|
||
| {...rest} | ||
| > | ||
| {children} | ||
| </a> | ||
| ); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,15 +4,15 @@ | |
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| import React, { FC, useState, useEffect, useCallback } from 'react'; | ||
| import React, { FC, useState, useEffect } from 'react'; | ||
| import moment from 'moment'; | ||
| import { FormattedMessage } from '@kbn/i18n/react'; | ||
| import { EuiFlexGroup, EuiFlexItem, EuiCard, EuiIcon } from '@elastic/eui'; | ||
| import { ml } from '../../../../services/ml_api_service'; | ||
| import { isFullLicense } from '../../../../license'; | ||
| import { checkPermission } from '../../../../capabilities/check_capabilities'; | ||
| import { mlNodesAvailable } from '../../../../ml_nodes_check/check_ml_nodes'; | ||
| import { useMlKibana, useMlUrlGenerator, useNavigateToPath } from '../../../../contexts/kibana'; | ||
| import { useMlKibana, useMlUrlGenerator } from '../../../../contexts/kibana'; | ||
| import { ML_PAGES } from '../../../../../../common/constants/ml_url_generator'; | ||
| import { MlCommonGlobalState } from '../../../../../../common/types/ml_url_generator'; | ||
| import { | ||
|
|
@@ -45,12 +45,16 @@ export const ResultsLinks: FC<Props> = ({ | |
| const [globalState, setGlobalState] = useState<MlCommonGlobalState | undefined>(); | ||
|
|
||
| const [discoverLink, setDiscoverLink] = useState(''); | ||
| const [indexManagementLink, setIndexManagementLink] = useState(''); | ||
| const [indexPatternManagementLink, setIndexPatternManagementLink] = useState(''); | ||
| const [dataVisualizerLink, setDataVisualizerLink] = useState(''); | ||
| const [createJobsSelectTypePage, setCreateJobsSelectTypePage] = useState(''); | ||
|
|
||
| const mlUrlGenerator = useMlUrlGenerator(); | ||
| const navigateToPath = useNavigateToPath(); | ||
|
|
||
| const { | ||
| services: { | ||
| application: { navigateToApp, navigateToUrl }, | ||
| application: { getUrlForApp }, | ||
| share: { | ||
| urlGenerators: { getUrlGenerator }, | ||
| }, | ||
|
|
@@ -84,35 +88,52 @@ export const ResultsLinks: FC<Props> = ({ | |
| setDiscoverLink(discoverUrl); | ||
| } | ||
| }; | ||
|
|
||
| const getDataVisualizerLink = async (): Promise<void> => { | ||
| const _dataVisualizerLink = await mlUrlGenerator.createUrl({ | ||
| page: ML_PAGES.DATA_VISUALIZER_INDEX_VIEWER, | ||
| pageState: { | ||
| index: indexPatternId, | ||
| globalState, | ||
| }, | ||
| }); | ||
| if (!unmounted) { | ||
| setDataVisualizerLink(_dataVisualizerLink); | ||
| } | ||
| }; | ||
| const getADCreateJobsSelectTypePage = async (): Promise<void> => { | ||
| const _createJobsSelectTypePage = await mlUrlGenerator.createUrl({ | ||
| page: ML_PAGES.ANOMALY_DETECTION_CREATE_JOB_SELECT_TYPE, | ||
| pageState: { | ||
| index: indexPatternId, | ||
| globalState, | ||
| }, | ||
| }); | ||
| if (!unmounted) { | ||
| setCreateJobsSelectTypePage(_createJobsSelectTypePage); | ||
| } | ||
| }; | ||
|
|
||
| getDiscoverUrl(); | ||
| getDataVisualizerLink(); | ||
| getADCreateJobsSelectTypePage(); | ||
|
|
||
| if (!unmounted) { | ||
| setIndexManagementLink( | ||
| getUrlForApp('management', { path: '/data/index_management/indices' }) | ||
| ); | ||
| setIndexPatternManagementLink( | ||
| getUrlForApp('management', { | ||
| path: `/kibana/indexPatterns${createIndexPattern ? `/patterns/${indexPatternId}` : ''}`, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| return () => { | ||
| unmounted = true; | ||
| }; | ||
| }, [indexPatternId, getUrlGenerator]); | ||
|
|
||
| const openInDataVisualizer = useCallback(async () => { | ||
| const path = await mlUrlGenerator.createUrl({ | ||
| page: ML_PAGES.DATA_VISUALIZER_INDEX_VIEWER, | ||
| pageState: { | ||
| index: indexPatternId, | ||
| globalState, | ||
| }, | ||
| }); | ||
| await navigateToPath(path); | ||
| }, [indexPatternId, globalState]); | ||
|
|
||
| const redirectToADCreateJobsSelectTypePage = useCallback(async () => { | ||
| const path = await mlUrlGenerator.createUrl({ | ||
| page: ML_PAGES.ANOMALY_DETECTION_CREATE_JOB_SELECT_TYPE, | ||
| pageState: { | ||
| index: indexPatternId, | ||
| globalState, | ||
| }, | ||
| }); | ||
| await navigateToPath(path); | ||
| }, [indexPatternId, globalState]); | ||
|
|
||
| useEffect(() => { | ||
| setShowCreateJobLink(checkPermission('canCreateJob') && mlNodesAvailable()); | ||
| updateTimeValues(); | ||
|
|
@@ -148,23 +169,6 @@ export const ResultsLinks: FC<Props> = ({ | |
| } | ||
| } | ||
|
|
||
| function openInDiscover(e: React.MouseEvent<HTMLButtonElement>) { | ||
| e.preventDefault(); | ||
| navigateToUrl(discoverLink); | ||
| } | ||
|
|
||
| function openIndexManagement(e: React.MouseEvent<HTMLButtonElement>) { | ||
| e.preventDefault(); | ||
| navigateToApp('management', { path: '/data/index_management/indices' }); | ||
| } | ||
|
|
||
| function openIndexPatternManagement(e: React.MouseEvent<HTMLButtonElement>) { | ||
| e.preventDefault(); | ||
| navigateToApp('management', { | ||
| path: `/kibana/indexPatterns${createIndexPattern ? `/patterns/${indexPatternId}` : ''}`, | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <EuiFlexGroup gutterSize="l"> | ||
| {createIndexPattern && discoverLink && ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need to add a check for all the other links as well?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated here 0064254 |
||
|
|
@@ -178,7 +182,7 @@ export const ResultsLinks: FC<Props> = ({ | |
| /> | ||
| } | ||
| description="" | ||
| onClick={openInDiscover} | ||
| href={discoverLink} | ||
| /> | ||
| </EuiFlexItem> | ||
| )} | ||
|
|
@@ -197,7 +201,7 @@ export const ResultsLinks: FC<Props> = ({ | |
| /> | ||
| } | ||
| description="" | ||
| onClick={redirectToADCreateJobsSelectTypePage} | ||
| href={createJobsSelectTypePage} | ||
| /> | ||
| </EuiFlexItem> | ||
| )} | ||
|
|
@@ -213,7 +217,7 @@ export const ResultsLinks: FC<Props> = ({ | |
| /> | ||
| } | ||
| description="" | ||
| onClick={openInDataVisualizer} | ||
| href={dataVisualizerLink} | ||
| /> | ||
| </EuiFlexItem> | ||
| )} | ||
|
|
@@ -228,7 +232,7 @@ export const ResultsLinks: FC<Props> = ({ | |
| /> | ||
| } | ||
| description="" | ||
| onClick={openIndexManagement} | ||
| href={indexManagementLink} | ||
| /> | ||
| </EuiFlexItem> | ||
|
|
||
|
|
@@ -242,7 +246,7 @@ export const ResultsLinks: FC<Props> = ({ | |
| /> | ||
| } | ||
| description="" | ||
| onClick={openIndexPatternManagement} | ||
| href={indexPatternManagementLink} | ||
| /> | ||
| </EuiFlexItem> | ||
| <EuiFlexItem> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we use
MlHrefinstead ofwindow.open?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll keep this one here for now for 7.10.0.