From 0c241e1be0c7c8fa369998a490ef7db6d69abf7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20St=C3=BCrmer?= Date: Mon, 23 Sep 2019 23:53:06 +0200 Subject: [PATCH] =?UTF-8?q?[7.4]=20[Infra=20+=20Logs=20UI]=20Handle=20abse?= =?UTF-8?q?nce=20of=20the=20spaces=20plugin=20(#=E2=80=A6=20(#46345)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backports the following commits to 7.4: - [Infra + Logs UI] Handle absence of the spaces plugin (#46241) --- .../pages/logs/analysis/page_providers.tsx | 4 ++-- .../public/utils/use_kibana_injected_var.ts | 19 +++++++++++++++ .../infra/public/utils/use_kibana_space_id.ts | 23 +++++++++++++++++++ .../framework/kibana_framework_adapter.ts | 8 ++++++- 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 x-pack/legacy/plugins/infra/public/utils/use_kibana_injected_var.ts create mode 100644 x-pack/legacy/plugins/infra/public/utils/use_kibana_space_id.ts diff --git a/x-pack/legacy/plugins/infra/public/pages/logs/analysis/page_providers.tsx b/x-pack/legacy/plugins/infra/public/pages/logs/analysis/page_providers.tsx index 0d48c6d379aac..fba32f6cbd6d0 100644 --- a/x-pack/legacy/plugins/infra/public/pages/logs/analysis/page_providers.tsx +++ b/x-pack/legacy/plugins/infra/public/pages/logs/analysis/page_providers.tsx @@ -5,14 +5,14 @@ */ import React, { useContext } from 'react'; -import chrome from 'ui/chrome'; import { LogAnalysisJobs } from '../../../containers/logs/log_analysis'; import { Source } from '../../../containers/source'; +import { useKibanaSpaceId } from '../../../utils/use_kibana_space_id'; export const AnalysisPageProviders: React.FunctionComponent = ({ children }) => { const { sourceId, source } = useContext(Source.Context); - const spaceId = chrome.getInjected('activeSpace').space.id; + const spaceId = useKibanaSpaceId(); return ( { + const injectedMetadata = npSetup.core.injectedMetadata; + + return injectedMetadata.getInjectedVar(name, defaultValue); +}; diff --git a/x-pack/legacy/plugins/infra/public/utils/use_kibana_space_id.ts b/x-pack/legacy/plugins/infra/public/utils/use_kibana_space_id.ts new file mode 100644 index 0000000000000..cf956b349b606 --- /dev/null +++ b/x-pack/legacy/plugins/infra/public/utils/use_kibana_space_id.ts @@ -0,0 +1,23 @@ +/* + * 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 * as rt from 'io-ts'; + +import { useKibanaInjectedVar } from './use_kibana_injected_var'; + +export const useKibanaSpaceId = (): string => { + const activeSpace = useKibanaInjectedVar('activeSpace'); + + return activeSpaceRT + .decode(activeSpace) + .fold(() => 'default', decodedActiveSpace => decodedActiveSpace.space.id); +}; + +const activeSpaceRT = rt.type({ + space: rt.type({ + id: rt.string, + }), +}); diff --git a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts index ed5751a438a90..e824d70ced86d 100644 --- a/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts +++ b/x-pack/legacy/plugins/infra/server/lib/adapters/framework/kibana_framework_adapter.ts @@ -153,7 +153,13 @@ export class InfraKibanaBackendFrameworkAdapter implements InfraBackendFramework } public getSpaceId(request: InfraFrameworkRequest): string { - return this.server.plugins.spaces.getSpaceId(request[internalInfraFrameworkRequest]); + const spacesPlugin = this.server.plugins.spaces; + + if (spacesPlugin && typeof spacesPlugin.getSpaceId === 'function') { + return spacesPlugin.getSpaceId(request[internalInfraFrameworkRequest]); + } else { + return 'default'; + } } public getSavedObjectsService() {