Skip to content

Commit

Permalink
Alternative plugin context usage
Browse files Browse the repository at this point in the history
  • Loading branch information
CoenWarmer committed Aug 30, 2023
1 parent df7e130 commit 1fe31fa
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 62 deletions.
43 changes: 27 additions & 16 deletions x-pack/plugins/observability_ai_assistant/public/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import React, { useMemo } from 'react';
import type { Observable } from 'rxjs';
import { KibanaThemeProvider } from '@kbn/react-kibana-context-theme';
import { ObservabilityAIAssistantProvider } from './context/observability_ai_assistant_provider';
import { ObservabilityAiAssistantContext } from './context/observability_ai_assistant_plugins/observability_ai_assistant_plugin_context';
import { LicenseProvider } from './context/license/license_context';
import { observabilityAIAssistantRouter } from './routes/config';
import type {
ObservabilityAIAssistantPluginStartDependencies,
Expand All @@ -39,22 +41,31 @@ export function Application({
return (
<EuiErrorBoundary>
<KibanaThemeProvider theme={theme}>
<KibanaContextProvider
services={{
...coreStart,
...pluginsStart,
}}
>
<RedirectAppLinks coreStart={coreStart}>
<coreStart.i18n.Context>
<ObservabilityAIAssistantProvider value={service}>
<RouterProvider history={history} router={observabilityAIAssistantRouter as any}>
<RouteRenderer />
</RouterProvider>
</ObservabilityAIAssistantProvider>
</coreStart.i18n.Context>
</RedirectAppLinks>
</KibanaContextProvider>
<ObservabilityAiAssistantContext.Provider value={{ start: { ...pluginsStart } }}>
<KibanaContextProvider
services={{
...coreStart,
plugins: {
start: pluginsStart,
},
}}
>
<RedirectAppLinks coreStart={coreStart}>
<coreStart.i18n.Context>
<ObservabilityAIAssistantProvider value={service}>
<LicenseProvider>
<RouterProvider
history={history}
router={observabilityAIAssistantRouter as any}
>
<RouteRenderer />
</RouterProvider>
</LicenseProvider>
</ObservabilityAIAssistantProvider>
</coreStart.i18n.Context>
</RedirectAppLinks>
</KibanaContextProvider>
</ObservabilityAiAssistantContext.Provider>
</KibanaThemeProvider>
</EuiErrorBoundary>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { Message } from '../../../common/types';
import type { UseGenAIConnectorsResult } from '../../hooks/use_genai_connectors';
import type { UseKnowledgeBaseResult } from '../../hooks/use_knowledge_base';
import { useTimeline } from '../../hooks/use_timeline';
import { useLicense } from '../../hooks/use_license';
import { useLicenseContext } from '../../context/license/use_license_context';
import { useObservabilityAIAssistantChatService } from '../../hooks/use_observability_ai_assistant_chat_service';
import { MissingCredentialsCallout } from '../missing_credentials_callout';
import { ExperimentalFeatureBanner } from './experimental_feature_banner';
Expand Down Expand Up @@ -74,8 +74,8 @@ export function ChatBody({
onChatComplete: (messages: Message[]) => void;
onSaveTitle: (title: string) => void;
}) {
const { hasAtLeast } = useLicense();
const hasCorrectLicense = hasAtLeast('enterprise');
const license = useLicenseContext();
const hasCorrectLicense = license?.hasAtLeast('enterprise');

const chatService = useObservabilityAIAssistantChatService();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function ExperimentalFeatureBanner() {
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton fill color="warning" href="https://ela.st/obs-ai-assistant" target="_blank">
<EuiButton color="warning" href="https://ela.st/obs-ai-assistant" target="_blank">
{i18n.translate(
'xpack.observabilityAiAssistant.experimentalFunctionBanner.feedbackButton',
{ defaultMessage: 'Give feedback' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ const pageSectionContentClassName = css`

export function ObservabilityAIAssistantPageTemplate({ children }: { children: React.ReactNode }) {
const {
services: { observabilityShared },
services: {
plugins: {
start: { observabilityShared },
},
},
} = useKibana();

const PageTemplate = observabilityShared.navigation.PageTemplate;
Expand Down
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
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import useObservable from 'react-use/lib/useObservable';
import { ILicense } from '@kbn/licensing-plugin/public';
import { useObservabilityAiAssistantPluginContext } from '../observability_ai_assistant_plugins/use_observability_ai_assistant_plugin_context';

export const LicenseContext = React.createContext<ILicense | undefined>(undefined);

export function LicenseProvider({ children }: { children: React.ReactChild }) {
const {
start: { licensing },
} = useObservabilityAiAssistantPluginContext();
const license = useObservable(licensing.license$);

// render rest of application and pass down license via context
return <LicenseContext.Provider value={license} children={children} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 { useContext } from 'react';
import { LicenseContext } from './license_context';

export function useLicenseContext() {
return useContext(LicenseContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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';
import { ObservabilityAIAssistantPluginStartDependencies } from '../../types';

export interface ObservabilityAiAssistantPluginContextValue {
start: ObservabilityAIAssistantPluginStartDependencies;
}

export const ObservabilityAiAssistantContext = createContext(
{} as ObservabilityAiAssistantPluginContextValue
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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 { useContext } from 'react';
import { ObservabilityAiAssistantContext } from './observability_ai_assistant_plugin_context';

export function useObservabilityAiAssistantPluginContext() {
return useContext(ObservabilityAiAssistantContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ function Lens({
end: string;
}) {
const {
services: { lens, dataViews },
services: {
plugins: {
start: { lens, dataViews },
},
},
} = useKibana();

const formulaAsync = useAsync(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { CoreStart } from '@kbn/core/public';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { ObservabilityAIAssistantPluginStartDependencies } from '../types';

export type StartServices<TAdditionalServices> = CoreStart &
ObservabilityAIAssistantPluginStartDependencies &
TAdditionalServices & {};
export type StartServices<TAdditionalServices> = CoreStart & {
plugins: { start: ObservabilityAIAssistantPluginStartDependencies };
} & TAdditionalServices & {};
const useTypedKibana = <AdditionalServices extends object = {}>() =>
useKibana<StartServices<AdditionalServices>>();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* 2.0.
*/

import { useKibana } from './use_kibana';
import { useObservabilityAiAssistantPluginContext } from '../context/observability_ai_assistant_plugins/use_observability_ai_assistant_plugin_context';

const LICENSE_MANAGEMENT_LOCATOR = 'LICENSE_MANAGEMENT_LOCATOR';

export const useLicenseManagementLocator = () => {
const { share } = useKibana().services;
const {
start: { share },
} = useObservabilityAiAssistantPluginContext();

const locator = share.url.locators.get(LICENSE_MANAGEMENT_LOCATOR);

Expand Down

0 comments on commit 1fe31fa

Please sign in to comment.