diff --git a/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/slo_alerts_embeddable_factory.tsx b/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/slo_alerts_embeddable_factory.tsx index 7472c43253454b..737761f9098a3a 100644 --- a/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/slo_alerts_embeddable_factory.tsx +++ b/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/slo_alerts_embeddable_factory.tsx @@ -21,8 +21,10 @@ import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { createBrowserHistory } from 'history'; import { Storage } from '@kbn/kibana-utils-plugin/public'; +import type { StartServicesAccessor } from '@kbn/core-lifecycle-browser'; import { SLO_ALERTS_EMBEDDABLE_ID } from './constants'; -import { SloEmbeddableDeps, SloAlertsEmbeddableState, SloAlertsApi } from './types'; +import { SloAlertsEmbeddableState, SloAlertsApi } from './types'; +import { SloPublicPluginsStart, SloPublicStart } from '../../../types'; import { SloAlertsWrapper } from './slo_alerts_wrapper'; const history = createBrowserHistory(); const queryClient = new QueryClient(); @@ -32,13 +34,33 @@ export const getAlertsPanelTitle = () => defaultMessage: 'SLO Alerts', }); -export function getAlertsEmbeddableFactory(deps: SloEmbeddableDeps, kibanaVersion: string) { +export function getAlertsEmbeddableFactory( + getStartServices: StartServicesAccessor, + kibanaVersion: string +) { const factory: ReactEmbeddableFactory = { type: SLO_ALERTS_EMBEDDABLE_ID, deserializeState: (state) => { return state.rawState as SloAlertsEmbeddableState; }, buildEmbeddable: async (state, buildApi, uuid, parentApi) => { + const [coreStart, pluginStart] = await getStartServices(); + const deps = { ...coreStart, ...pluginStart }; + async function onEdit() { + try { + const { openSloConfiguration } = await import('./slo_alerts_open_configuration'); + + const result = await openSloConfiguration( + coreStart, + pluginStart, + api.getSloAlertsConfig() + ); + api.updateSloAlertsConfig(result); + } catch (e) { + return Promise.reject(); + } + } + const { titlesApi, titleComparators, serializeTitles } = initializeTitles(state); const defaultTitle$ = new BehaviorSubject(getAlertsPanelTitle()); const slos$ = new BehaviorSubject(state.slos); @@ -48,6 +70,14 @@ export function getAlertsEmbeddableFactory(deps: SloEmbeddableDeps, kibanaVersio { ...titlesApi, defaultPanelTitle: defaultTitle$, + getTypeDisplayName: () => + i18n.translate('xpack.slo.editSloAlertswEmbeddable.typeDisplayName', { + defaultMessage: 'configuration', + }), + isEditingEnabled: () => true, + onEdit: async () => { + onEdit(); + }, serializeState: () => { return { rawState: { @@ -112,6 +142,7 @@ export function getAlertsEmbeddableFactory(deps: SloEmbeddableDeps, kibanaVersio void; reloadSubject: Subject; showAllGroupByInstances?: boolean; + onEdit: () => void; } export function SloAlertsWrapper({ @@ -39,6 +36,7 @@ export function SloAlertsWrapper({ onRenderComplete, reloadSubject, showAllGroupByInstances, + onEdit, }: Props) { const { application: { navigateToUrl }, @@ -102,11 +100,7 @@ export function SloAlertsWrapper({ { - const trigger = deps.uiActions.getTrigger(CONTEXT_MENU_TRIGGER); - deps.uiActions.getAction(EDIT_SLO_ALERTS_ACTION).execute({ - trigger, - embeddable, - } as ActionExecutionContext); + onEdit(); }} data-test-subj="o11ySloAlertsWrapperSlOsIncludedLink" > diff --git a/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/types.ts b/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/types.ts index 8b660442ca7b66..7682c3f55bedfe 100644 --- a/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/types.ts +++ b/x-pack/plugins/observability_solution/slo/public/embeddable/slo/alerts/types.ts @@ -24,6 +24,7 @@ import { PublishesWritablePanelTitle, PublishesPanelTitle, EmbeddableApiContext, + HasEditCapabilities, } from '@kbn/presentation-publishing'; export interface SloItem { @@ -45,7 +46,8 @@ export type SloAlertsEmbeddableState = SerializedTitles & EmbeddableSloProps; export type SloAlertsApi = DefaultEmbeddableApi & PublishesWritablePanelTitle & PublishesPanelTitle & - HasSloAlertsConfig; + HasSloAlertsConfig & + HasEditCapabilities; export interface HasSloAlertsConfig { getSloAlertsConfig: () => EmbeddableSloProps; diff --git a/x-pack/plugins/observability_solution/slo/public/plugin.ts b/x-pack/plugins/observability_solution/slo/public/plugin.ts index c996a0ba395608..0ee7c4b51c0cd2 100644 --- a/x-pack/plugins/observability_solution/slo/public/plugin.ts +++ b/x-pack/plugins/observability_solution/slo/public/plugin.ts @@ -125,7 +125,7 @@ export class SloPlugin './embeddable/slo/alerts/slo_alerts_embeddable_factory' ); - return getAlertsEmbeddableFactory(deps, kibanaVersion); + return getAlertsEmbeddableFactory(coreSetup.getStartServices, kibanaVersion); } ); diff --git a/x-pack/plugins/observability_solution/slo/public/ui_actions/edit_slo_alerts_panel.tsx b/x-pack/plugins/observability_solution/slo/public/ui_actions/edit_slo_alerts_panel.tsx deleted file mode 100644 index ce9b4d196ffb5b..00000000000000 --- a/x-pack/plugins/observability_solution/slo/public/ui_actions/edit_slo_alerts_panel.tsx +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 { i18n } from '@kbn/i18n'; -import { ViewMode } from '@kbn/embeddable-plugin/common'; -import type { CoreSetup } from '@kbn/core/public'; -import { - apiCanAccessViewMode, - apiHasType, - apiIsOfType, - EmbeddableApiContext, - getInheritedViewMode, - CanAccessViewMode, - HasType, -} from '@kbn/presentation-publishing'; -import { type UiActionsActionDefinition } from '@kbn/ui-actions-plugin/public'; -import { SLO_ALERTS_EMBEDDABLE_ID } from '../embeddable/slo/alerts/constants'; -import { SloPublicPluginsStart, SloPublicStart } from '..'; -import { - HasSloAlertsConfig, - SloAlertsEmbeddableActionContext, -} from '../embeddable/slo/alerts/types'; -export const EDIT_SLO_ALERTS_ACTION = 'editSloAlertsPanelAction'; -type EditSloAlertsPanelApi = CanAccessViewMode & HasType & HasSloAlertsConfig; -const isEditSloAlertsPanelApi = (api: unknown): api is EditSloAlertsPanelApi => - Boolean( - apiHasType(api) && - apiIsOfType(api, SLO_ALERTS_EMBEDDABLE_ID) && - apiCanAccessViewMode(api) && - getInheritedViewMode(api) === ViewMode.EDIT - ); - -export function createEditSloAlertsPanelAction( - getStartServices: CoreSetup['getStartServices'] -): UiActionsActionDefinition { - return { - id: EDIT_SLO_ALERTS_ACTION, - type: EDIT_SLO_ALERTS_ACTION, - getIconType(): string { - return 'pencil'; - }, - getDisplayName: () => - i18n.translate('xpack.slo.actions.editSloAlertsEmbeddableTitle', { - defaultMessage: 'Edit configuration', - }), - async execute({ embeddable }) { - if (!embeddable) { - throw new Error('Not possible to execute an action without the embeddable context'); - } - - const [coreStart, pluginStart] = await getStartServices(); - - try { - const { openSloConfiguration } = await import( - '../embeddable/slo/alerts/slo_alerts_open_configuration' - ); - - const result = await openSloConfiguration( - coreStart, - pluginStart, - embeddable.getSloAlertsConfig() - ); - embeddable.updateSloAlertsConfig(result); - } catch (e) { - return Promise.reject(); - } - }, - isCompatible: async ({ embeddable }: EmbeddableApiContext) => - isEditSloAlertsPanelApi(embeddable), - }; -} diff --git a/x-pack/plugins/observability_solution/slo/public/ui_actions/index.ts b/x-pack/plugins/observability_solution/slo/public/ui_actions/index.ts index 5e5b7d9357b844..61c1569f1a9d7c 100644 --- a/x-pack/plugins/observability_solution/slo/public/ui_actions/index.ts +++ b/x-pack/plugins/observability_solution/slo/public/ui_actions/index.ts @@ -6,9 +6,7 @@ */ import type { UiActionsSetup } from '@kbn/ui-actions-plugin/public'; -import { CONTEXT_MENU_TRIGGER } from '@kbn/embeddable-plugin/public'; import type { CoreSetup } from '@kbn/core/public'; -import { createEditSloAlertsPanelAction } from './edit_slo_alerts_panel'; import { createOverviewPanelAction } from './create_overview_panel_action'; import { createAddErrorBudgetPanelAction } from './create_error_budget_action'; import { createAddAlertsPanelAction } from './create_alerts_panel_action'; @@ -19,13 +17,11 @@ export function registerSloUiActions( core: CoreSetup ) { // Initialize actions - const editSloAlertsPanelAction = createEditSloAlertsPanelAction(core.getStartServices); const addOverviewPanelAction = createOverviewPanelAction(core.getStartServices); const addErrorBudgetPanelAction = createAddErrorBudgetPanelAction(core.getStartServices); const addAlertsPanelAction = createAddAlertsPanelAction(core.getStartServices); // Assign triggers - uiActions.addTriggerAction(CONTEXT_MENU_TRIGGER, editSloAlertsPanelAction); uiActions.addTriggerAction('ADD_PANEL_TRIGGER', addOverviewPanelAction); uiActions.addTriggerAction('ADD_PANEL_TRIGGER', addErrorBudgetPanelAction); uiActions.addTriggerAction('ADD_PANEL_TRIGGER', addAlertsPanelAction);