From aaadb14d60cd9da76ea4a523148061201324a36e Mon Sep 17 00:00:00 2001 From: Jenny Date: Thu, 21 Dec 2023 16:33:35 +0100 Subject: [PATCH] CR change: extract to kibanaEnvironment --- .../kibana_react/public/context/context.tsx | 25 ++++++++++++++----- .../kibana_react/public/context/types.ts | 8 +++++- .../components/routing/app_root/index.tsx | 5 ++++ .../routing/templates/apm_main_template.tsx | 13 +++------- .../plugins/infra/public/hooks/use_kibana.tsx | 18 ++++++++----- .../public/pages/metrics/hosts/index.tsx | 2 +- .../components/survey_kubernetes.tsx | 2 +- .../components/survey_section.tsx | 2 +- .../pages/metrics/metrics_explorer/index.tsx | 2 +- 9 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/plugins/kibana_react/public/context/context.tsx b/src/plugins/kibana_react/public/context/context.tsx index 8c32bdab69b31..fd81bb34e9861 100644 --- a/src/plugins/kibana_react/public/context/context.tsx +++ b/src/plugins/kibana_react/public/context/context.tsx @@ -7,7 +7,12 @@ */ 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'; @@ -15,6 +20,7 @@ const { useMemo, useContext, createElement, createContext } = React; const defaultContextValue = { services: {}, + kibanaEnvironment: {}, overlays: createReactOverlays({}), notifications: createNotifications({}), }; @@ -37,22 +43,29 @@ export const withKibana = } }; export const createKibanaReactContext = ( - services: Services + services: Services, + kibanaEnvironment?: KibanaEnvironment ): KibanaReactContext => { const value: KibanaReactContextValue = { 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, { @@ -70,4 +83,4 @@ export const createKibanaReactContext = ( }; }; -export const { Provider: KibanaContextProvider } = createKibanaReactContext({}); +export const { Provider: KibanaContextProvider } = createKibanaReactContext({}, {}); diff --git a/src/plugins/kibana_react/public/context/types.ts b/src/plugins/kibana_react/public/context/types.ts index 4962267974d79..7307611defe59 100644 --- a/src/plugins/kibana_react/public/context/types.ts +++ b/src/plugins/kibana_react/public/context/types.ts @@ -12,15 +12,21 @@ import { KibanaReactOverlays } from '../overlays'; import { KibanaReactNotifications } from '../notifications'; export type KibanaServices = Partial; +export interface KibanaEnvironment { + kibanaVersion?: string; + isCloudEnv?: boolean; + isServerlessEnv?: boolean; +} export interface KibanaReactContextValue { readonly services: Services; + readonly kibanaEnvironment: KibanaEnvironment; readonly overlays: KibanaReactOverlays; readonly notifications: KibanaReactNotifications; } export interface KibanaReactContext { value: KibanaReactContextValue; - Provider: React.FC<{ services?: T }>; + Provider: React.FC<{ services?: T; kibanaEnvironment?: KibanaEnvironment }>; Consumer: React.Consumer>; } diff --git a/x-pack/plugins/apm/public/components/routing/app_root/index.tsx b/x-pack/plugins/apm/public/components/routing/app_root/index.tsx index 85df672363ce1..b4ff66d12629d 100644 --- a/x-pack/plugins/apm/public/components/routing/app_root/index.tsx +++ b/x-pack/plugins/apm/public/components/routing/app_root/index.tsx @@ -72,6 +72,11 @@ export function ApmAppRoot({ diff --git a/x-pack/plugins/apm/public/components/routing/templates/apm_main_template.tsx b/x-pack/plugins/apm/public/components/routing/templates/apm_main_template.tsx index e46ede5ca96e6..a9ad169aa23a6 100644 --- a/x-pack/plugins/apm/public/components/routing/templates/apm_main_template.tsx +++ b/x-pack/plugins/apm/public/components/routing/templates/apm_main_template.tsx @@ -58,16 +58,9 @@ export function ApmMainTemplate({ Pick) { const location = useLocation(); - const { services } = useKibana(); - const { - http, - docLinks, - observabilityShared, - application, - kibanaVersion, - isCloudEnv, - isServerlessEnv, - } = services; + const { services, kibanaEnvironment } = useKibana(); + const { http, docLinks, observabilityShared, application } = services; + const { kibanaVersion, isCloudEnv, isServerlessEnv } = kibanaEnvironment; const basePath = http?.basePath.get(); const { config } = useApmPluginContext(); diff --git a/x-pack/plugins/infra/public/hooks/use_kibana.tsx b/x-pack/plugins/infra/public/hooks/use_kibana.tsx index 0055d01a23bec..f1b83f6a229f5 100644 --- a/x-pack/plugins/infra/public/hooks/use_kibana.tsx +++ b/x-pack/plugins/infra/public/hooks/use_kibana.tsx @@ -22,11 +22,18 @@ export const createKibanaContextForPlugin = ( plugins: InfraClientStartDeps, pluginStart: InfraClientStartExports ) => - createKibanaReactContext({ - ...core, - ...plugins, - ...pluginStart, - }); + createKibanaReactContext( + { + ...core, + ...plugins, + ...pluginStart, + }, + { + kibanaVersion: plugins.kibanaVersion, + isCloudEnv: plugins.isCloudEnv, + isServerlessEnv: plugins.isServerlessEnv, + } + ); export const useKibanaContextForPlugin = useKibana as () => KibanaReactContextValue; @@ -40,7 +47,6 @@ export const useKibanaContextForPluginProvider = ( () => createKibanaContextForPlugin(core, plugins, pluginStart), [core, pluginStart, plugins] ); - return Provider; }; diff --git a/x-pack/plugins/infra/public/pages/metrics/hosts/index.tsx b/x-pack/plugins/infra/public/pages/metrics/hosts/index.tsx index f679712edab02..726f501c6be71 100644 --- a/x-pack/plugins/infra/public/pages/metrics/hosts/index.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/hosts/index.tsx @@ -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' }); diff --git a/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_kubernetes.tsx b/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_kubernetes.tsx index 6172e01a483e3..3f0448942f738 100644 --- a/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_kubernetes.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_kubernetes.tsx @@ -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 ( diff --git a/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_section.tsx b/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_section.tsx index 96f3a68cad800..840522efa1722 100644 --- a/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_section.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/inventory_view/components/survey_section.tsx @@ -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 ( diff --git a/x-pack/plugins/infra/public/pages/metrics/metrics_explorer/index.tsx b/x-pack/plugins/infra/public/pages/metrics/metrics_explorer/index.tsx index fbd64757ed97d..061768e2a8042 100644 --- a/x-pack/plugins/infra/public/pages/metrics/metrics_explorer/index.tsx +++ b/x-pack/plugins/infra/public/pages/metrics/metrics_explorer/index.tsx @@ -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' });