From c0a14cd41bfa2d7d1c8dc8753a3a0260b2d0c95e Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Fri, 27 Mar 2020 16:12:02 +0100 Subject: [PATCH] Navlink url tracker: Reset navlink on failed redirect (#61460) --- .../kibana/public/discover/kibana_services.ts | 5 +++++ .../public/discover/np_ready/angular/discover.js | 4 ++++ .../core_plugins/kibana/public/discover/plugin.ts | 10 ++++++++-- .../kibana/public/visualize/np_ready/legacy_app.js | 3 +++ .../public/history/redirect_when_missing.tsx | 8 ++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts b/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts index e1ff274911284..2ceb06f325a9e 100644 --- a/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts +++ b/src/legacy/core_plugins/kibana/public/discover/kibana_services.ts @@ -47,10 +47,15 @@ export function setServices(newServices: any) { services = newServices; } +export const [getUrlTracker, setUrlTracker] = createGetterSetter<{ + setTrackedUrl: (url: string) => void; +}>('urlTracker'); + // EXPORT legacy static dependencies, should be migrated when available in a new version; export { angular }; export { wrapInI18nContext } from 'ui/i18n'; import { search } from '../../../../../plugins/data/public'; +import { createGetterSetter } from '../../../../../plugins/kibana_utils/common'; export const { getRequestInspectorStats, getResponseInspectorStats, tabifyAggResponse } = search; export { unhashUrl, diff --git a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js index bcf97a21dcde9..630ba22d4f3a6 100644 --- a/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/np_ready/angular/discover.js @@ -44,6 +44,7 @@ import { getRequestInspectorStats, getResponseInspectorStats, getServices, + getUrlTracker, unhashUrl, subscribeWithScope, tabifyAggResponse, @@ -160,6 +161,9 @@ app.config($routeProvider => { '/management/kibana/objects/savedSearches/' + $route.current.params.id, }, toastNotifications, + onBeforeRedirect() { + getUrlTracker().setTrackedUrl('/discover'); + }, }) ), }); diff --git a/src/legacy/core_plugins/kibana/public/discover/plugin.ts b/src/legacy/core_plugins/kibana/public/discover/plugin.ts index 661c1c052caba..fcac7aa74f54a 100644 --- a/src/legacy/core_plugins/kibana/public/discover/plugin.ts +++ b/src/legacy/core_plugins/kibana/public/discover/plugin.ts @@ -31,7 +31,7 @@ import { registerFeature } from './np_ready/register_feature'; import './kibana_services'; import { EmbeddableStart, EmbeddableSetup } from '../../../../../plugins/embeddable/public'; import { getInnerAngularModule, getInnerAngularModuleEmbeddable } from './get_inner_angular'; -import { setAngularModule, setServices } from './kibana_services'; +import { setAngularModule, setServices, setUrlTracker } from './kibana_services'; import { NavigationPublicPluginStart as NavigationStart } from '../../../../../plugins/navigation/public'; import { ChartsPluginStart } from '../../../../../plugins/charts/public'; import { buildServices } from './build_services'; @@ -92,7 +92,12 @@ export class DiscoverPlugin implements Plugin { public initializeServices?: () => Promise<{ core: CoreStart; plugins: DiscoverStartPlugins }>; setup(core: CoreSetup, plugins: DiscoverSetupPlugins) { - const { appMounted, appUnMounted, stop: stopUrlTracker } = createKbnUrlTracker({ + const { + appMounted, + appUnMounted, + stop: stopUrlTracker, + setActiveUrl: setTrackedUrl, + } = createKbnUrlTracker({ baseUrl: core.http.basePath.prepend('/app/kibana'), defaultSubUrl: '#/discover', storageKey: 'lastUrl:discover', @@ -113,6 +118,7 @@ export class DiscoverPlugin implements Plugin { }, ], }); + setUrlTracker({ setTrackedUrl }); this.stopUrlTracking = () => { stopUrlTracker(); }; diff --git a/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js b/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js index b0b1ae31a02a5..d1bf4411cac2a 100644 --- a/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js +++ b/src/legacy/core_plugins/kibana/public/visualize/np_ready/legacy_app.js @@ -204,6 +204,9 @@ export function initVisualizeApp(app, deps) { '/management/kibana/objects/savedVisualizations/' + $route.current.params.id, }, toastNotifications, + onBeforeRedirect() { + deps.setActiveUrl(VisualizeConstants.LANDING_PAGE_PATH); + }, }) ); }, diff --git a/src/plugins/kibana_utils/public/history/redirect_when_missing.tsx b/src/plugins/kibana_utils/public/history/redirect_when_missing.tsx index cbdeef6fbe96c..8a4cc88999bfe 100644 --- a/src/plugins/kibana_utils/public/history/redirect_when_missing.tsx +++ b/src/plugins/kibana_utils/public/history/redirect_when_missing.tsx @@ -37,6 +37,7 @@ export function redirectWhenMissing({ history, mapping, toastNotifications, + onBeforeRedirect, }: { history: History; /** @@ -48,6 +49,10 @@ export function redirectWhenMissing({ * Toast notifications service to show toasts in error cases. */ toastNotifications: ToastsSetup; + /** + * Optional callback invoked directly before a redirect is triggered + */ + onBeforeRedirect?: (error: SavedObjectNotFound) => void; }) { let localMappingObject: Mapping; @@ -75,6 +80,9 @@ export function redirectWhenMissing({ text: toMountPoint({error.message}), }); + if (onBeforeRedirect) { + onBeforeRedirect(error); + } history.replace(url); }; }