Skip to content

Commit

Permalink
[7.4] [Infra + Logs UI] Handle absence of the spaces plugin (#… (elas…
Browse files Browse the repository at this point in the history
…tic#46345)

Backports the following commits to 7.4:
 - [Infra + Logs UI] Handle absence of the spaces plugin (elastic#46241)
  • Loading branch information
weltenwort authored Sep 23, 2019
1 parent 22743ff commit 0c241e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<LogAnalysisJobs.Provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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 { npSetup } from 'ui/new_platform';

/**
* This hook provides access to the "injected variables" provided by the
* platform. While it doesn't need to be a hook right now, it has been written
* as one in order to keep the API stable despite the upcoming injection
* through the context after the new platform migration.
*/
export const useKibanaInjectedVar = (name: string, defaultValue?: unknown) => {
const injectedMetadata = npSetup.core.injectedMetadata;

return injectedMetadata.getInjectedVar(name, defaultValue);
};
23 changes: 23 additions & 0 deletions x-pack/legacy/plugins/infra/public/utils/use_kibana_space_id.ts
Original file line number Diff line number Diff line change
@@ -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,
}),
});
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 0c241e1

Please sign in to comment.