-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Fixes handling of unrecognised URLs (#133157)
* not found page * replace not found page with a warning banner * add check for non-empty pathname * functional test * disabled anomaly detection section * update message
- Loading branch information
Showing
6 changed files
with
144 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
x-pack/plugins/ml/public/application/routing/use_active_route.ts
This file was deleted.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
x-pack/plugins/ml/public/application/routing/use_active_route.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useLocation, useRouteMatch } from 'react-router-dom'; | ||
import { keyBy } from 'lodash'; | ||
import React, { useEffect, useMemo, useRef } from 'react'; | ||
import { toMountPoint, useExecutionContext } from '@kbn/kibana-react-plugin/public'; | ||
import { EuiCallOut } from '@elastic/eui'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { useMlKibana } from '../contexts/kibana'; | ||
import type { MlRoute } from './router'; | ||
|
||
/** | ||
* Provides an active route of the ML app. | ||
* @param routesList | ||
*/ | ||
export const useActiveRoute = (routesList: MlRoute[]): MlRoute => { | ||
const { pathname } = useLocation(); | ||
|
||
const { | ||
services: { executionContext, overlays, theme }, | ||
} = useMlKibana(); | ||
|
||
/** | ||
* Temp fix for routes with params. | ||
*/ | ||
const editCalendarMatch = useRouteMatch('/settings/calendars_list/edit_calendar/:calendarId'); | ||
const editFilterMatch = useRouteMatch('/settings/filter_lists/edit_filter_list/:filterId'); | ||
|
||
const routesMap = useMemo(() => keyBy(routesList, 'path'), []); | ||
|
||
const activeRoute = useMemo(() => { | ||
if (editCalendarMatch) { | ||
return routesMap[editCalendarMatch.path]; | ||
} | ||
if (editFilterMatch) { | ||
return routesMap[editFilterMatch.path]; | ||
} | ||
// Remove trailing slash from the pathname | ||
const pathnameKey = pathname.replace(/\/$/, ''); | ||
return routesMap[pathnameKey]; | ||
}, [pathname]); | ||
|
||
const bannerId = useRef<string | undefined>(); | ||
|
||
useEffect( | ||
function handleNotFoundRoute() { | ||
if (!activeRoute && !!pathname) { | ||
bannerId.current = overlays.banners.replace( | ||
bannerId.current, | ||
toMountPoint( | ||
<EuiCallOut | ||
color="warning" | ||
iconType="iInCircle" | ||
title={ | ||
<FormattedMessage | ||
id="xpack.ml.notFoundPage.title" | ||
defaultMessage="Page Not Found" | ||
/> | ||
} | ||
data-test-subj={'mlPageNotFoundBanner'} | ||
> | ||
<p data-test-subj={'mlPageNotFoundBannerText'}> | ||
<FormattedMessage | ||
id="xpack.ml.notFoundPage.bannerText" | ||
defaultMessage="The Machine Learning application doesn't recognize this route: {route}. You've been redirected to the Overview page." | ||
values={{ | ||
route: pathname, | ||
}} | ||
/> | ||
</p> | ||
</EuiCallOut>, | ||
{ theme$: theme.theme$ } | ||
) | ||
); | ||
|
||
// hide the message after the user has had a chance to acknowledge it -- so it doesn't permanently stick around | ||
setTimeout(() => { | ||
if (bannerId.current) { | ||
overlays.banners.remove(bannerId.current); | ||
} | ||
}, 15000); | ||
} | ||
}, | ||
[activeRoute, overlays, theme, pathname] | ||
); | ||
|
||
useExecutionContext(executionContext, { | ||
name: 'Machine Learning', | ||
type: 'application', | ||
page: activeRoute?.path ?? '/overview', | ||
}); | ||
|
||
return activeRoute ?? routesMap['/overview']; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters