import { CoreSetup , Plugin , CoreStart } from 'src/core/public' ; import { AlertNavigationRegistry , AlertNavigationHandler } from './alert_navigation_registry' ; import { loadAlert , loadAlertType } from './alert_api' ; import { Alert , AlertNavigation } from '../common' ; export interface PluginSetupContract { param applicationId param ruleType param handler throws registerNavigation : ( applicationId : string , ruleType : string , handler : AlertNavigationHandler ) => void ; param applicationId param handler registerDefaultNavigation : ( applicationId : string , handler : AlertNavigationHandler ) => void ; } export interface PluginStartContract { getNavigation : ( alertId : Alert [ 'id' ] ) => Promise < AlertNavigation | undefined > ; } export class AlertingPublicPlugin implements Plugin < PluginSetupContract , PluginStartContract > { private alertNavigationRegistry ? : AlertNavigationRegistry ; public setup ( core ) { this . alertNavigationRegistry = new AlertNavigationRegistry ( ) ; const registerNavigation = async ( applicationId , ruleTypeId , handler ) => { this . alertNavigationRegistry ! . register ( applicationId , ruleTypeId , handler ) ; } ; const registerDefaultNavigation = async ( applicationId , handler ) => this . alertNavigationRegistry ! . registerDefault ( applicationId , handler ) ; return { registerNavigation , registerDefaultNavigation , } ; } public start ( core ) { return { getNavigation : async ( alertId ) => { const alert = await loadAlert ( { http : core . http , alertId } ) ; const alertType = await loadAlertType ( { http : core . http , id : alert . alertTypeId } ) ; if ( ! alertType ) { console . log ( `Unable to get navigation for alert type "${ alert . alertTypeId }" because it is not registered on the server side.` ) ; return ; } if ( this . alertNavigationRegistry ! . has ( alert . consumer , alertType ) ) { const navigationHandler = this . alertNavigationRegistry ! . get ( alert . consumer , alertType ) ; const state = navigationHandler ( alert ) ; return typeof state === 'string' ? { path : state } : { state } ; } } , } ; } }