-
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.
replace not found page with a warning banner
- Loading branch information
Showing
5 changed files
with
100 additions
and
115 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
8 changes: 0 additions & 8 deletions
8
x-pack/plugins/ml/public/application/components/not_found_page/index.ts
This file was deleted.
Oops, something went wrong.
51 changes: 0 additions & 51 deletions
51
x-pack/plugins/ml/public/application/components/not_found_page/not_found_page.tsx
This file was deleted.
Oops, something went wrong.
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.
98 changes: 98 additions & 0 deletions
98
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,98 @@ | ||
/* | ||
* 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) { | ||
bannerId.current = overlays.banners.replace( | ||
bannerId.current, | ||
toMountPoint( | ||
<EuiCallOut | ||
color="warning" | ||
iconType="iInCircle" | ||
title={ | ||
<FormattedMessage | ||
id="xpack.ml.notFoundPage.title" | ||
defaultMessage="Page Not Found" | ||
/> | ||
} | ||
> | ||
<p> | ||
<FormattedMessage | ||
id="xpack.ml.notFoundPage.bannerText" | ||
defaultMessage="ML 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']; | ||
}; |