Skip to content

Commit

Permalink
remove isConfigurable from the actionfactory
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed May 5, 2022
1 parent 397d08a commit be542fd
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React from 'react';
import { EuiLoadingSpinner } from '@elastic/eui';
import { ActionFactoryPicker as ActionFactoryPickerUi } from '../../../../components/action_factory_picker';
import { useDrilldownManager } from '../context';
import { ActionFactoryView } from '../action_factory_view';
Expand All @@ -14,17 +15,19 @@ export const ActionFactoryPicker: React.FC = ({}) => {
const drilldowns = useDrilldownManager();
const factory = drilldowns.useActionFactory();
const context = React.useMemo(() => drilldowns.getActionFactoryContext(), [drilldowns]);
const applicableFactories = drilldowns.deps.actionFactories.filter((actionFactory) =>
actionFactory.isConfigurable(context)
);
const compatibleFactories = drilldowns.useCompatibleActionFactories(context);

if (!!factory) {
return <ActionFactoryView factory={factory} context={context} />;
}

if (!compatibleFactories) {
return <EuiLoadingSpinner size="m" />;
}

return (
<ActionFactoryPickerUi
actionFactories={applicableFactories}
actionFactories={compatibleFactories}
context={context}
onSelect={(actionFactory) => {
drilldowns.setActionFactory(actionFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

import useObservable from 'react-use/lib/useObservable';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import type { SerializableRecord } from '@kbn/utility-types';
import { useMemo } from 'react';
import {
PublicDrilldownManagerProps,
DrilldownManagerDependencies,
Expand Down Expand Up @@ -255,6 +256,24 @@ export class DrilldownManagerState {
return context;
}

public getCompatibleActionFactories(
context: BaseActionFactoryContext
): Observable<ActionFactory[] | undefined> {
const compatibleActionFactories$ = new BehaviorSubject<undefined | ActionFactory[]>(undefined);
Promise.allSettled(
this.deps.actionFactories.map((factory) => factory.isCompatible(context))
).then((factoryCompatibility) => {
compatibleActionFactories$.next(
this.deps.actionFactories.filter((_factory, i) => {
const result = factoryCompatibility[i];
// treat failed isCompatible checks as non-compatible
return result.status === 'fulfilled' && result.value;
})
);
});
return compatibleActionFactories$.asObservable();
}

/**
* Get state object of the drilldown which is currently being created.
*/
Expand Down Expand Up @@ -478,4 +497,9 @@ export class DrilldownManagerState {
public readonly useActionFactory = () =>
useObservable(this.actionFactory$, this.actionFactory$.getValue());
public readonly useEvents = () => useObservable(this.events$, this.events$.getValue());
public readonly useCompatibleActionFactories = (context: BaseActionFactoryContext) =>
useObservable(
useMemo(() => this.getCompatibleActionFactories(context), [context]),
undefined
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export class ActionFactory<
public readonly createConfig: (context: FactoryContext) => Config;
public readonly isConfigValid: (config: Config, context: FactoryContext) => boolean;
public readonly migrations: MigrateFunctionsObject | GetMigrationFunctionObjectFn;
public readonly isConfigurable: (context: FactoryContext) => boolean;

constructor(
protected readonly def: ActionFactoryDefinition<Config, ExecutionContext, FactoryContext>,
Expand All @@ -78,7 +77,6 @@ export class ActionFactory<
this.ReactCollectConfig = uiToReactComponent(this.CollectConfig);
this.createConfig = this.def.createConfig;
this.isConfigValid = this.def.isConfigValid;
this.isConfigurable = this.def.isConfigurable || (() => true);
this.migrations = this.def.migrations || {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,5 @@ export interface ActionFactoryDefinition<
serializedAction: Omit<SerializedAction<Config>, 'factoryId'>
): ActionDefinition<ExecutionContext>;

/**
* Compatibility check during drilldown creation.
* Could be used to filter out a drilldown if it's not compatible with the current context.
*/
isConfigurable?(context: FactoryContext): boolean;

supportedTriggers(): string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ export class UiActionsServiceEnhancements
extract,
inject,
getIconType: () => euiIcon,
isCompatible: async () => true,
isConfigurable: (context) => !isConfigurable || isConfigurable(context),
isCompatible: async (context) => !isConfigurable || isConfigurable(context),
create: (serializedAction) => ({
id: '',
type: factoryId,
Expand Down

0 comments on commit be542fd

Please sign in to comment.