Skip to content

Commit

Permalink
CR change: extract to kibanaEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
jennypavlova committed Dec 21, 2023
1 parent f18945b commit aaadb14
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 27 deletions.
25 changes: 19 additions & 6 deletions src/plugins/kibana_react/public/context/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
*/

import * as React from 'react';
import { KibanaReactContext, KibanaReactContextValue, KibanaServices } from './types';
import type {
KibanaEnvironment,
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 @@ -37,22 +43,29 @@ export const withKibana = <Props extends { kibana: KibanaReactContextValue<{}> }
};

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

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

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

export const { Provider: KibanaContextProvider } = createKibanaReactContext({});
export const { Provider: KibanaContextProvider } = createKibanaReactContext({}, {});
8 changes: 7 additions & 1 deletion src/plugins/kibana_react/public/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ 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 }>;
Provider: React.FC<{ services?: T; kibanaEnvironment?: KibanaEnvironment }>;
Consumer: React.Consumer<KibanaReactContextValue<T>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ 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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,9 @@ export function ApmMainTemplate({
Pick<ObservabilityPageTemplateProps, 'pageSectionProps'>) {
const location = useLocation();

const { services } = useKibana<ApmPluginStartDeps>();
const {
http,
docLinks,
observabilityShared,
application,
kibanaVersion,
isCloudEnv,
isServerlessEnv,
} = services;
const { services, kibanaEnvironment } = useKibana<ApmPluginStartDeps>();
const { http, docLinks, observabilityShared, application } = services;
const { kibanaVersion, isCloudEnv, isServerlessEnv } = kibanaEnvironment;
const basePath = http?.basePath.get();
const { config } = useApmPluginContext();

Expand Down
18 changes: 12 additions & 6 deletions x-pack/plugins/infra/public/hooks/use_kibana.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ export const createKibanaContextForPlugin = (
plugins: InfraClientStartDeps,
pluginStart: InfraClientStartExports
) =>
createKibanaReactContext<PluginKibanaContextValue>({
...core,
...plugins,
...pluginStart,
});
createKibanaReactContext<PluginKibanaContextValue>(
{
...core,
...plugins,
...pluginStart,
},
{
kibanaVersion: plugins.kibanaVersion,
isCloudEnv: plugins.isCloudEnv,
isServerlessEnv: plugins.isServerlessEnv,
}
);

export const useKibanaContextForPlugin =
useKibana as () => KibanaReactContextValue<PluginKibanaContextValue>;
Expand All @@ -40,7 +47,6 @@ export const useKibanaContextForPluginProvider = (
() => createKibanaContextForPlugin(core, plugins, pluginStart),
[core, pluginStart, plugins]
);

return Provider;
};

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/infra/public/pages/metrics/hosts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const HOSTS_FEEDBACK_LINK =
export const HostsPage = () => {
const { isLoading, loadSourceFailureMessage, loadSource, source } = useSourceContext();
const {
services: { kibanaVersion, isCloudEnv, isServerlessEnv },
kibanaEnvironment: { kibanaVersion, isCloudEnv, isServerlessEnv },
} = useKibanaContextForPlugin();

useTrackPageview({ app: 'infra_metrics', path: 'hosts' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const SurveyKubernetes = () => {
const [isToastSeen, setIsToastSeen] = useLocalStorage(KUBERNETES_TOAST_STORAGE_KEY, false);
const markToastAsSeen = () => setIsToastSeen(true);
const {
services: { kibanaVersion, isCloudEnv, isServerlessEnv },
kibanaEnvironment: { kibanaVersion, isCloudEnv, isServerlessEnv },
} = useKibanaContextForPlugin();

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const INVENTORY_FEEDBACK_LINK = 'https://ela.st/survey-infra-inventory?usp=pp_ur
export const SurveySection = () => {
const { nodeType } = useWaffleOptionsContext();
const {
services: { kibanaVersion, isCloudEnv, isServerlessEnv },
kibanaEnvironment: { kibanaVersion, isCloudEnv, isServerlessEnv },
} = useKibanaContextForPlugin();

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const MetricsExplorerPage = ({ source, derivedIndexPattern }: MetricsExpl
const { currentView } = useMetricsExplorerViews();
const { source: sourceContext, metricIndicesExist } = useSourceContext();
const {
services: { kibanaVersion, isCloudEnv, isServerlessEnv },
kibanaEnvironment: { kibanaVersion, isCloudEnv, isServerlessEnv },
} = useKibanaContextForPlugin();

useTrackPageview({ app: 'infra_metrics', path: 'metrics_explorer' });
Expand Down

0 comments on commit aaadb14

Please sign in to comment.