import type { UiComponent , CollectConfigProps } from 'src/plugins/kibana_utils/public' ; import type { MigrateFunctionsObject } from 'src/plugins/kibana_utils/common' ; import { uiToReactComponent } from '../../../../../src/plugins/kibana_react/public' ; import type { UiActionsPresentable as Presentable , ActionMenuItemProps , } from '../../../../../src/plugins/ui_actions/public' ; import type { ActionFactoryDefinition } from './action_factory_definition' ; import type { Configurable } from '../../../../../src/plugins/kibana_utils/public' ; import type { BaseActionConfig , BaseActionFactoryContext , SerializedAction , SerializedEvent , } from './types' ; import type { ILicense , LicensingPluginStart , LicenseType } from '../../../licensing/public' ; import type { UiActionsActionDefinition as ActionDefinition } from '../../../../../src/plugins/ui_actions/public' ; import type { SavedObjectReference } from '../../../../../src/core/types' ; import type { PersistableState } from '../../../../../src/plugins/kibana_utils/common' ; export interface ActionFactoryDeps { readonly getLicense ? : ( ) => ILicense ; readonly getFeatureUsageStart ? : ( ) => LicensingPluginStart [ 'featureUsage' ] ; } export class ActionFactory < Config extends BaseActionConfig = BaseActionConfig , ExecutionContext extends object = object , FactoryContext extends BaseActionFactoryContext = BaseActionFactoryContext > implements Omit < Presentable < FactoryContext > , 'getHref' > , Configurable < Config , FactoryContext > , PersistableState < SerializedEvent > { public readonly id : string ; public readonly isBeta : boolean ; public readonly minimalLicense ? : LicenseType ; public readonly licenseFeatureName ? : string ; public readonly order : number ; public readonly MenuItem ? : UiComponent < ActionMenuItemProps < FactoryContext > > ; public readonly ReactMenuItem ? : React . FC < ActionMenuItemProps < FactoryContext > > ; public readonly CollectConfig : UiComponent < CollectConfigProps < Config , FactoryContext > > ; public readonly ReactCollectConfig : React . FC < CollectConfigProps < Config , FactoryContext > > ; public readonly createConfig : ( context : FactoryContext ) => Config ; public readonly isConfigValid : ( config : Config , context : FactoryContext ) => boolean ; public readonly migrations : MigrateFunctionsObject ; constructor ( protected readonly def , protected readonly deps ) { if ( def . minimalLicense && ! def . licenseFeatureName ) { throw new Error ( `ActionFactory [actionFactory.id = ${ def . id }] "licenseFeatureName" is required, if "minimalLicense" is provided` ) ; } this . id = this . def . id ; this . isBeta = this . def . isBeta ?? false ; this . minimalLicense = this . def . minimalLicense ; this . licenseFeatureName = this . def . licenseFeatureName ; this . order = this . def . order || 0 ; this . MenuItem = this . def . MenuItem ; this . ReactMenuItem = this . MenuItem ? uiToReactComponent ( this . MenuItem ) : undefined ; this . CollectConfig = this . def . CollectConfig ; this . ReactCollectConfig = uiToReactComponent ( this . CollectConfig ) ; this . createConfig = this . def . createConfig ; this . isConfigValid = this . def . isConfigValid ; this . migrations = this . def . migrations || { } ; } public getIconType ( context ) { if ( ! this . def . getIconType ) return undefined ; return this . def . getIconType ( context ) ; } public getDisplayName ( context ) { if ( ! this . def . getDisplayName ) return '' ; return this . def . getDisplayName ( context ) ; } public getDisplayNameTooltip ( context ) { return '' ; } public async isCompatible ( context ) { if ( ! this . def . isCompatible ) return true ; return await this . def . isCompatible ( context ) ; } /**
     * Does this action factory license requirements
     * compatible with current license?
     */ public isCompatibleLicense ( ) { if ( ! this . minimalLicense || ! this . deps . getLicense ) return true ; const license = this . deps . getLicense ( ) ; return license . isAvailable && license . isActive && license . hasAtLeast ( this . minimalLicense ) ; } public create ( serializedAction ) { const action = this . def . create ( serializedAction ) ; return { ... action , isCompatible : async ( context ) : Promise < boolean > => { if ( ! this . isCompatibleLicense ( ) ) return false ; if ( ! action . isCompatible ) return true ; return action . isCompatible ( context ) ; } , execute : async ( context ) : Promise < void > => { this . notifyFeatureUsage ( ) ; return action . execute ( context ) ; } , } ; } public supportedTriggers ( ) { return this . def . supportedTriggers ( ) ; } private notifyFeatureUsage ( ) { if ( ! this . minimalLicense || ! this . licenseFeatureName || ! this . deps . getFeatureUsageStart ) return ; this . deps . getFeatureUsageStart ( ) . notifyUsage ( this . licenseFeatureName ) . catch ( ( ) => { console . warn ( `ActionFactory [actionFactory.id = ${ this . def . id }] fail notify feature usage.` ) ; } ) ; } public telemetry ( state , telemetryData ) { return this . def . telemetry ? this . def . telemetry ( state , telemetryData ) : telemetryData ; } public extract ( state ) { return this . def . extract ? this . def . extract ( state ) : { state , references : [ ] } ; } public inject ( state , references ) { return this . def . inject ? this . def . inject ( state , references ) : state ; } }