Skip to content

Commit

Permalink
make it possible to filter out drilldowns from list based on context
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed May 3, 2022
1 parent 7225c50 commit 1854a21
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { EuiFormRow, EuiSwitch } from '@elastic/eui';
import { DiscoverSetup } from '@kbn/discover-plugin/public';
import { ApplyGlobalFilterActionContext } from '@kbn/unified-search-plugin/public';
import { i18n } from '@kbn/i18n';
import { execute, isCompatible } from './open_in_discover_helpers';
import { execute, isCompatible, isLensEmbeddable } from './open_in_discover_helpers';

interface EmbeddableQueryInput extends EmbeddableInput {
query?: Query;
Expand Down Expand Up @@ -54,12 +54,12 @@ export interface ActionFactoryContext extends BaseActionFactoryContext {
}
export type CollectConfigProps = CollectConfigPropsBase<Config, ActionFactoryContext>;

const URL_DRILLDOWN = 'OPEN_IN_DISCOVER_DRILLDOWN';
const OPEN_IN_DISCOVER_DRILLDOWN = 'OPEN_IN_DISCOVER_DRILLDOWN';

export class OpenInDiscoverDrilldown
implements Drilldown<Config, ActionContext, ActionFactoryContext>
{
public readonly id = URL_DRILLDOWN;
public readonly id = OPEN_IN_DISCOVER_DRILLDOWN;

constructor(private readonly deps: UrlDrilldownDeps) {}

Expand Down Expand Up @@ -117,6 +117,10 @@ export class OpenInDiscoverDrilldown
});
};

public readonly isConfigurable = (context: ActionFactoryContext) => {
return this.deps.hasDiscoverAccess() && isLensEmbeddable(context.embeddable as IEmbeddable);
};

public readonly execute = async (config: Config, context: ActionContext) => {
const { restOfFilters: filters, timeRange: timeRange } = extractTimeRange(
context.filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface Context {
discover: Pick<DiscoverSetup, 'locator'>;
}

function isLensEmbeddable(embeddable: IEmbeddable): embeddable is Embeddable {
export function isLensEmbeddable(embeddable: IEmbeddable): embeddable is Embeddable {
return embeddable.type === DOC_TYPE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export interface DrilldownDefinition<
*/
isConfigValid: ActionFactoryDefinition<Config, ExecutionContext, FactoryContext>['isConfigValid'];

/**
* 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;

/**
* Name of EUI icon to display when showing this drilldown to user.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ 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)
);

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

return (
<ActionFactoryPickerUi
actionFactories={drilldowns.deps.actionFactories}
actionFactories={applicableFactories}
context={context}
onSelect={(actionFactory) => {
drilldowns.setActionFactory(actionFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ 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 @@ -77,6 +78,7 @@ 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,5 +63,11 @@ 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 @@ -116,6 +116,7 @@ export class UiActionsServiceEnhancements
licenseFeatureName,
supportedTriggers,
isCompatible,
isConfigurable,
telemetry,
extract,
inject,
Expand All @@ -136,6 +137,7 @@ export class UiActionsServiceEnhancements
inject,
getIconType: () => euiIcon,
isCompatible: async () => true,
isConfigurable: (context) => !isConfigurable || isConfigurable(context),
create: (serializedAction) => ({
id: '',
type: factoryId,
Expand Down

0 comments on commit 1854a21

Please sign in to comment.