From 90a82ef52137f66ae91673134df1b0be38b9a1ac Mon Sep 17 00:00:00 2001 From: Milton Hultgren Date: Mon, 3 Oct 2022 13:18:13 +0200 Subject: [PATCH] [Infra] Make nav react to Hosts view enabled flag changing (#140996) --- x-pack/plugins/infra/public/plugin.ts | 48 +++++++++++++++++++++------ 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/x-pack/plugins/infra/public/plugin.ts b/x-pack/plugins/infra/public/plugin.ts index 6eb6663b10eeb..8797fabb01739 100644 --- a/x-pack/plugins/infra/public/plugin.ts +++ b/x-pack/plugins/infra/public/plugin.ts @@ -5,12 +5,17 @@ * 2.0. */ +import { + AppMountParameters, + AppUpdater, + CoreStart, + DEFAULT_APP_CATEGORIES, + PluginInitializerContext, +} from '@kbn/core/public'; import { i18n } from '@kbn/i18n'; -import { AppMountParameters, PluginInitializerContext } from '@kbn/core/public'; -import { from } from 'rxjs'; -import { map } from 'rxjs/operators'; -import { DEFAULT_APP_CATEGORIES } from '@kbn/core/public'; import { enableInfrastructureHostsView } from '@kbn/observability-plugin/public'; +import { BehaviorSubject, combineLatest, from } from 'rxjs'; +import { map } from 'rxjs/operators'; import { defaultLogViewsStaticConfig } from '../common/log_views'; import { InfraPublicConfig } from '../common/plugin_config_types'; import { createInventoryMetricRuleType } from './alerting/inventory'; @@ -38,6 +43,7 @@ import { getLogsHasDataFetcher, getLogsOverviewDataFetcher } from './utils/logs_ export class Plugin implements InfraClientPluginClass { public config: InfraPublicConfig; private logViews: LogViewsService; + private readonly appUpdater$ = new BehaviorSubject(() => ({})); constructor(context: PluginInitializerContext) { this.config = context.config.get(); @@ -74,6 +80,11 @@ export class Plugin implements InfraClientPluginClass { fetchData: createMetricsFetchData(core.getStartServices), }); + const startDep$AndHostViewFlag$ = combineLatest([ + from(core.getStartServices()), + core.uiSettings.get$(enableInfrastructureHostsView), + ]); + /** !! Need to be kept in sync with the deepLinks in x-pack/plugins/infra/public/plugin.ts */ const infraEntries = [ { label: 'Inventory', app: 'metrics', path: '/inventory' }, @@ -81,12 +92,15 @@ export class Plugin implements InfraClientPluginClass { ]; const hostInfraEntry = { label: 'Hosts', app: 'metrics', path: '/hosts' }; pluginsSetup.observability.navigation.registerSections( - from(core.getStartServices()).pipe( + startDep$AndHostViewFlag$.pipe( map( ([ - { - application: { capabilities }, - }, + [ + { + application: { capabilities }, + }, + ], + isInfrastructureHostsViewEnabled, ]) => [ ...(capabilities.logs.show ? [ @@ -106,7 +120,7 @@ export class Plugin implements InfraClientPluginClass { { label: 'Infrastructure', sortKey: 300, - entries: core.uiSettings.get(enableInfrastructureHostsView) + entries: isInfrastructureHostsViewEnabled ? [hostInfraEntry, ...infraEntries] : infraEntries, }, @@ -210,8 +224,9 @@ export class Plugin implements InfraClientPluginClass { order: 8200, appRoute: '/app/metrics', category: DEFAULT_APP_CATEGORIES.observability, + updater$: this.appUpdater$, // !! Need to be kept in sync with the routes in x-pack/plugins/infra/public/pages/metrics/index.tsx - deepLinks: core.uiSettings.get(enableInfrastructureHostsView) + deepLinks: core.uiSettings.get(enableInfrastructureHostsView) ? [hostInfraDeepLink, ...infraDeepLinks] : infraDeepLinks, mount: async (params: AppMountParameters) => { @@ -223,6 +238,19 @@ export class Plugin implements InfraClientPluginClass { }, }); + startDep$AndHostViewFlag$.subscribe( + ([_startServices, isInfrastructureHostsViewEnabled]: [ + [CoreStart, InfraClientStartDeps, InfraClientStartExports], + boolean + ]) => { + this.appUpdater$.next(() => ({ + deepLinks: isInfrastructureHostsViewEnabled + ? [hostInfraDeepLink, ...infraDeepLinks] + : infraDeepLinks, + })); + } + ); + /* This exists purely to facilitate URL redirects from the old App ID ("infra"), to our new App IDs ("metrics" and "logs"). With version 8.0.0 we can remove this. */ core.application.register({