Skip to content

Commit

Permalink
Move KibanaEnvironmentContext to apm and infra
Browse files Browse the repository at this point in the history
  • Loading branch information
jennypavlova committed Jan 3, 2024
1 parent b3e0d7d commit 894d688
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 109 deletions.
25 changes: 6 additions & 19 deletions src/plugins/kibana_react/public/context/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
*/

import * as React from 'react';
import type {
KibanaEnvironment,
KibanaReactContext,
KibanaReactContextValue,
KibanaServices,
} from './types';
import { KibanaReactContext, KibanaReactContextValue, KibanaServices } from './types';
import { createReactOverlays } from '../overlays';
import { createNotifications } from '../notifications';

const { useMemo, useContext, createElement, createContext } = React;

const defaultContextValue = {
services: {},
kibanaEnvironment: {},
overlays: createReactOverlays({}),
notifications: createNotifications({}),
};
Expand All @@ -43,29 +37,22 @@ export const withKibana = <Props extends { kibana: KibanaReactContextValue<{}> }
};

export const createKibanaReactContext = <Services extends KibanaServices>(
services: Services,
kibanaEnvironment?: KibanaEnvironment
services: Services
): KibanaReactContext<Services> => {
const value: KibanaReactContextValue<Services> = {
services,
overlays: createReactOverlays(services),
notifications: createNotifications(services),
kibanaEnvironment: kibanaEnvironment ?? {},
};

const Provider: React.FC<{ services?: Services; kibanaEnvironment?: KibanaEnvironment }> = ({
const Provider: React.FC<{ services?: Services }> = ({
services: newServices = {},
kibanaEnvironment: newKibanaEnvironment = {},
children,
}) => {
const oldValue = useKibana();
const { value: newValue } = useMemo(
() =>
createKibanaReactContext(
{ ...services, ...oldValue.services, ...newServices },
{ ...kibanaEnvironment, ...oldValue.kibanaEnvironment, ...newKibanaEnvironment }
),
[services, oldValue, newServices, kibanaEnvironment, newKibanaEnvironment]
() => createKibanaReactContext({ ...services, ...oldValue.services, ...newServices }),
[services, oldValue, newServices]
);

const newProvider = createElement(context.Provider, {
Expand All @@ -83,4 +70,4 @@ export const createKibanaReactContext = <Services extends KibanaServices>(
};
};

export const { Provider: KibanaContextProvider } = createKibanaReactContext({}, {});
export const { Provider: KibanaContextProvider } = createKibanaReactContext({});
8 changes: 1 addition & 7 deletions src/plugins/kibana_react/public/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ import { KibanaReactOverlays } from '../overlays';
import { KibanaReactNotifications } from '../notifications';

export type KibanaServices = Partial<CoreStart>;
export interface KibanaEnvironment {
kibanaVersion?: string;
isCloudEnv?: boolean;
isServerlessEnv?: boolean;
}

export interface KibanaReactContextValue<Services extends KibanaServices> {
readonly services: Services;
readonly kibanaEnvironment: KibanaEnvironment;
readonly overlays: KibanaReactOverlays;
readonly notifications: KibanaReactNotifications;
}

export interface KibanaReactContext<T extends KibanaServices> {
value: KibanaReactContextValue<T>;
Provider: React.FC<{ services?: T; kibanaEnvironment?: KibanaEnvironment }>;
Provider: React.FC<{ services?: T }>;
Consumer: React.Consumer<KibanaReactContextValue<T>>;
}
88 changes: 44 additions & 44 deletions x-pack/plugins/apm/public/components/routing/app_root/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { RouteRenderer, RouterProvider } from '@kbn/typed-react-router-config';
import { euiDarkVars, euiLightVars } from '@kbn/ui-theme';
import React from 'react';
import { DefaultTheme, ThemeProvider } from 'styled-components';
import { useKibanaEnvironmentContextProvider } from '../../../context/kibana_environment_context/use_kibana_environment_context';
import { AnomalyDetectionJobsContextProvider } from '../../../context/anomaly_detection_jobs/anomaly_detection_jobs_context';
import {
ApmPluginContext,
Expand Down Expand Up @@ -55,6 +56,8 @@ export function ApmAppRoot({
apmServices: ApmServices;
}) {
const { appMountParameters, core } = apmPluginContextValue;
const KibanaEnvironmentContextProvider =
useKibanaEnvironmentContextProvider(pluginsStart);
const { history } = appMountParameters;
const i18nCore = core.i18n;

Expand All @@ -72,52 +75,49 @@ export function ApmAppRoot({
<ApmPluginContext.Provider value={apmPluginContextValue}>
<KibanaContextProvider
services={{ ...core, ...pluginsStart, storage, ...apmServices }}
kibanaEnvironment={{
kibanaVersion: pluginsStart.kibanaVersion,
isCloudEnv: pluginsStart.isCloudEnv,
isServerlessEnv: pluginsStart.isServerlessEnv,
}}
>
<i18nCore.Context>
<TimeRangeIdContextProvider>
<RouterProvider history={history} router={apmRouter as any}>
<ApmErrorBoundary>
<RedirectDependenciesToDependenciesInventory>
<RedirectWithDefaultEnvironment>
<RedirectWithDefaultDateRange>
<RedirectWithOffset>
<TrackPageview>
<UpdateExecutionContextOnRouteChange>
<BreadcrumbsContextProvider>
<UrlParamsProvider>
<LicenseProvider>
<AnomalyDetectionJobsContextProvider>
<InspectorContextProvider>
<ApmThemeProvider>
<MountApmHeaderActionMenu />
<KibanaEnvironmentContextProvider kibanaEnvironment={pluginsStart}>
<i18nCore.Context>
<TimeRangeIdContextProvider>
<RouterProvider history={history} router={apmRouter as any}>
<ApmErrorBoundary>
<RedirectDependenciesToDependenciesInventory>
<RedirectWithDefaultEnvironment>
<RedirectWithDefaultDateRange>
<RedirectWithOffset>
<TrackPageview>
<UpdateExecutionContextOnRouteChange>
<BreadcrumbsContextProvider>
<UrlParamsProvider>
<LicenseProvider>
<AnomalyDetectionJobsContextProvider>
<InspectorContextProvider>
<ApmThemeProvider>
<MountApmHeaderActionMenu />

<Route
component={
ScrollToTopOnPathChange
}
/>
<RouteRenderer />
</ApmThemeProvider>
</InspectorContextProvider>
</AnomalyDetectionJobsContextProvider>
</LicenseProvider>
</UrlParamsProvider>
</BreadcrumbsContextProvider>
</UpdateExecutionContextOnRouteChange>
</TrackPageview>
</RedirectWithOffset>
</RedirectWithDefaultDateRange>
</RedirectWithDefaultEnvironment>
</RedirectDependenciesToDependenciesInventory>
</ApmErrorBoundary>
</RouterProvider>
</TimeRangeIdContextProvider>
</i18nCore.Context>
<Route
component={
ScrollToTopOnPathChange
}
/>
<RouteRenderer />
</ApmThemeProvider>
</InspectorContextProvider>
</AnomalyDetectionJobsContextProvider>
</LicenseProvider>
</UrlParamsProvider>
</BreadcrumbsContextProvider>
</UpdateExecutionContextOnRouteChange>
</TrackPageview>
</RedirectWithOffset>
</RedirectWithDefaultDateRange>
</RedirectWithDefaultEnvironment>
</RedirectDependenciesToDependenciesInventory>
</ApmErrorBoundary>
</RouterProvider>
</TimeRangeIdContextProvider>
</i18nCore.Context>
</KibanaEnvironmentContextProvider>
</KibanaContextProvider>
</ApmPluginContext.Provider>
</RedirectAppLinks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { EuiFlexGroup, EuiFlexItem, EuiPageHeaderProps } from '@elastic/eui';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { ObservabilityPageTemplateProps } from '@kbn/observability-shared-plugin/public';
import type { KibanaPageTemplateProps } from '@kbn/shared-ux-page-kibana-template';
import React from 'react';
import React, { useContext } from 'react';
import { useLocation } from 'react-router-dom';
import { FeatureFeedbackButton } from '@kbn/observability-shared-plugin/public';
import { KibanaEnvironmentContext } from '../../../context/kibana_environment_context/kibana_environment_context';
import { getPathForFeedback } from '../../../utils/get_path_for_feedback';
import { EnvironmentsContextProvider } from '../../../context/environments_context/environments_context';
import { FETCH_STATUS, useFetcher } from '../../../hooks/use_fetcher';
Expand Down Expand Up @@ -58,7 +59,8 @@ export function ApmMainTemplate({
Pick<ObservabilityPageTemplateProps, 'pageSectionProps'>) {
const location = useLocation();

const { services, kibanaEnvironment } = useKibana<ApmPluginStartDeps>();
const { services } = useKibana<ApmPluginStartDeps>();
const kibanaEnvironment = useContext(KibanaEnvironmentContext);
const { http, docLinks, observabilityShared, application } = services;
const { kibanaVersion, isCloudEnv, isServerlessEnv } = kibanaEnvironment;
const basePath = http?.basePath.get();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 { createContext } from 'react';

export interface KibanaEnvContext {
kibanaVersion?: string;
isCloudEnv?: boolean;
isServerlessEnv?: boolean;
}

export const KibanaEnvironmentContext = createContext<KibanaEnvContext>({});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 React, { useMemo, createElement } from 'react';
import { ApmPluginStartDeps } from '../../plugin';
import {
KibanaEnvironmentContext,
type KibanaEnvContext,
} from './kibana_environment_context';

export const useKibanaEnvironmentContextProvider = (
plugins: ApmPluginStartDeps
) => {
const value = useMemo(
() => ({
kibanaVersion: plugins.kibanaVersion,
isCloudEnv: plugins.isCloudEnv,
isServerlessEnv: plugins.isServerlessEnv,
}),
[plugins]
);

const Provider: React.FC<{ kibanaEnvironment?: KibanaEnvContext }> = ({
kibanaEnvironment = {},
children,
}) => {
const newProvider = createElement(KibanaEnvironmentContext.Provider, {
value: { ...kibanaEnvironment, ...value },
children,
});

return newProvider;
};

return Provider;
};
15 changes: 11 additions & 4 deletions x-pack/plugins/infra/public/apps/common_providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import type { ObservabilityAIAssistantPluginStart } from '@kbn/observability-ai-
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { NavigationWarningPromptProvider } from '@kbn/observability-shared-plugin/public';
import { TriggersAndActionsUIPublicPluginStart } from '@kbn/triggers-actions-ui-plugin/public';
import { useKibanaContextForPluginProvider } from '../hooks/use_kibana';
import {
useKibanaContextForPluginProvider,
useKibanaEnvironmentContextProvider,
} from '../hooks/use_kibana';
import { InfraClientStartDeps, InfraClientStartExports } from '../types';
import { HeaderActionMenuProvider } from '../utils/header_action_menu_provider';
import { TriggersActionsProvider } from '../utils/triggers_actions_context';
Expand Down Expand Up @@ -70,11 +73,15 @@ export const CoreProviders: React.FC<CoreProvidersProps> = ({
pluginStart
);

const MyEnvContextForPluginProvider = useKibanaEnvironmentContextProvider(plugins);

return (
<KibanaContextProviderForPlugin services={{ ...core, ...plugins, ...pluginStart }}>
<core.i18n.Context>
<KibanaThemeProvider theme$={theme$}>{children}</KibanaThemeProvider>
</core.i18n.Context>
<MyEnvContextForPluginProvider kibanaEnvironment={{ ...plugins }}>
<core.i18n.Context>
<KibanaThemeProvider theme$={theme$}>{children}</KibanaThemeProvider>
</core.i18n.Context>
</MyEnvContextForPluginProvider>
</KibanaContextProviderForPlugin>
);
};
Expand Down
52 changes: 39 additions & 13 deletions x-pack/plugins/infra/public/hooks/use_kibana.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type { PropsOf } from '@elastic/eui';
import React, { useMemo } from 'react';
import React, { useMemo, createElement, createContext } from 'react';
import { CoreStart } from '@kbn/core/public';
import {
createKibanaReactContext,
Expand All @@ -17,23 +17,24 @@ import { InfraClientCoreSetup, InfraClientStartDeps, InfraClientStartExports } f

export type PluginKibanaContextValue = CoreStart & InfraClientStartDeps & InfraClientStartExports;

interface KibanaEnvContext {
kibanaVersion?: string;
isCloudEnv?: boolean;
isServerlessEnv?: boolean;
}

export const createKibanaContextForPlugin = (
core: CoreStart,
plugins: InfraClientStartDeps,
pluginStart: InfraClientStartExports
) =>
createKibanaReactContext<PluginKibanaContextValue>(
{
...core,
...plugins,
...pluginStart,
},
{
kibanaVersion: plugins.kibanaVersion,
isCloudEnv: plugins.isCloudEnv,
isServerlessEnv: plugins.isServerlessEnv,
}
);
createKibanaReactContext<PluginKibanaContextValue>({
...core,
...plugins,
...pluginStart,
});

export const KibanaEnvironmentContext = createContext<KibanaEnvContext>({});

export const useKibanaContextForPlugin =
useKibana as () => KibanaReactContextValue<PluginKibanaContextValue>;
Expand All @@ -50,6 +51,31 @@ export const useKibanaContextForPluginProvider = (
return Provider;
};

export const useKibanaEnvironmentContextProvider = (plugins: InfraClientStartDeps) => {
const value = useMemo(
() => ({
kibanaVersion: plugins.kibanaVersion,
isCloudEnv: plugins.isCloudEnv,
isServerlessEnv: plugins.isServerlessEnv,
}),
[plugins]
);

const Provider: React.FC<{ kibanaEnvironment?: KibanaEnvContext }> = ({
kibanaEnvironment = {},
children,
}) => {
const newProvider = createElement(KibanaEnvironmentContext.Provider, {
value: { ...kibanaEnvironment, ...value },
children,
});

return newProvider;
};

return Provider;
};

export const createLazyComponentWithKibanaContext = <T extends React.ComponentType<any>>(
coreSetup: InfraClientCoreSetup,
lazyComponentFactory: () => Promise<{ default: T }>
Expand Down
Loading

0 comments on commit 894d688

Please sign in to comment.