forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Make open in discover drilldown work (elastic#131237)
* make open in discover drilldown work * cleanup and tests * fix test * fix icon * fix type * fix open in new tab * fix open in new tab * fix test * make it possible to filter out drilldowns from list based on context * review comments * remove isConfigurable from the actionfactory Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Stratoula Kalafateli <[email protected]>
- Loading branch information
Showing
12 changed files
with
328 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/lens/public/trigger_actions/open_in_discover_drilldown.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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 React, { FormEvent } from 'react'; | ||
import { IEmbeddable, EmbeddableInput } from '@kbn/embeddable-plugin/public'; | ||
import { DiscoverSetup } from '@kbn/discover-plugin/public'; | ||
import { execute, isCompatible } from './open_in_discover_helpers'; | ||
import { mount } from 'enzyme'; | ||
import { Filter } from '@kbn/es-query'; | ||
import { | ||
ActionFactoryContext, | ||
CollectConfigProps, | ||
OpenInDiscoverDrilldown, | ||
} from './open_in_discover_drilldown'; | ||
|
||
jest.mock('./open_in_discover_helpers', () => ({ | ||
isCompatible: jest.fn(() => true), | ||
execute: jest.fn(), | ||
})); | ||
|
||
describe('open in discover drilldown', () => { | ||
let drilldown: OpenInDiscoverDrilldown; | ||
beforeEach(() => { | ||
drilldown = new OpenInDiscoverDrilldown({ | ||
discover: {} as DiscoverSetup, | ||
hasDiscoverAccess: () => true, | ||
}); | ||
}); | ||
it('provides UI to edit config', () => { | ||
const Component = (drilldown as unknown as { ReactCollectConfig: React.FC<CollectConfigProps> }) | ||
.ReactCollectConfig; | ||
const setConfig = jest.fn(); | ||
const instance = mount( | ||
<Component | ||
config={{ openInNewTab: false }} | ||
onConfig={setConfig} | ||
context={{} as ActionFactoryContext} | ||
/> | ||
); | ||
instance.find('EuiSwitch').prop('onChange')!({} as unknown as FormEvent<{}>); | ||
expect(setConfig).toHaveBeenCalledWith({ openInNewTab: true }); | ||
}); | ||
it('calls through to isCompatible helper', () => { | ||
const filters: Filter[] = [{ meta: { disabled: false } }]; | ||
drilldown.isCompatible( | ||
{ openInNewTab: true }, | ||
{ embeddable: { type: 'lens' } as IEmbeddable<EmbeddableInput>, filters } | ||
); | ||
expect(isCompatible).toHaveBeenCalledWith(expect.objectContaining({ filters })); | ||
}); | ||
it('calls through to execute helper', () => { | ||
const filters: Filter[] = [{ meta: { disabled: false } }]; | ||
drilldown.execute( | ||
{ openInNewTab: true }, | ||
{ embeddable: { type: 'lens' } as IEmbeddable<EmbeddableInput>, filters } | ||
); | ||
expect(execute).toHaveBeenCalledWith( | ||
expect.objectContaining({ filters, openInSameTab: false }) | ||
); | ||
}); | ||
}); |
139 changes: 139 additions & 0 deletions
139
x-pack/plugins/lens/public/trigger_actions/open_in_discover_drilldown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { IEmbeddable, EmbeddableInput } from '@kbn/embeddable-plugin/public'; | ||
import { | ||
Query, | ||
Filter, | ||
TimeRange, | ||
extractTimeRange, | ||
APPLY_FILTER_TRIGGER, | ||
} from '@kbn/data-plugin/public'; | ||
import { CollectConfigProps as CollectConfigPropsBase } from '@kbn/kibana-utils-plugin/public'; | ||
import { reactToUiComponent } from '@kbn/kibana-react-plugin/public'; | ||
import { | ||
UiActionsEnhancedDrilldownDefinition as Drilldown, | ||
UiActionsEnhancedBaseActionFactoryContext as BaseActionFactoryContext, | ||
} from '@kbn/ui-actions-enhanced-plugin/public'; | ||
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, isLensEmbeddable } from './open_in_discover_helpers'; | ||
|
||
interface EmbeddableQueryInput extends EmbeddableInput { | ||
query?: Query; | ||
filters?: Filter[]; | ||
timeRange?: TimeRange; | ||
} | ||
|
||
/** @internal */ | ||
export type EmbeddableWithQueryInput = IEmbeddable<EmbeddableQueryInput>; | ||
|
||
interface UrlDrilldownDeps { | ||
discover: Pick<DiscoverSetup, 'locator'>; | ||
hasDiscoverAccess: () => boolean; | ||
} | ||
|
||
export type ActionContext = ApplyGlobalFilterActionContext; | ||
|
||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||
export type Config = { | ||
openInNewTab: boolean; | ||
}; | ||
|
||
export type OpenInDiscoverTrigger = typeof APPLY_FILTER_TRIGGER; | ||
|
||
export interface ActionFactoryContext extends BaseActionFactoryContext { | ||
embeddable?: EmbeddableWithQueryInput; | ||
} | ||
export type CollectConfigProps = CollectConfigPropsBase<Config, ActionFactoryContext>; | ||
|
||
const OPEN_IN_DISCOVER_DRILLDOWN = 'OPEN_IN_DISCOVER_DRILLDOWN'; | ||
|
||
export class OpenInDiscoverDrilldown | ||
implements Drilldown<Config, ActionContext, ActionFactoryContext> | ||
{ | ||
public readonly id = OPEN_IN_DISCOVER_DRILLDOWN; | ||
|
||
constructor(private readonly deps: UrlDrilldownDeps) {} | ||
|
||
public readonly order = 8; | ||
|
||
public readonly getDisplayName = () => | ||
i18n.translate('xpack.lens.app.exploreDataInDiscoverDrilldown', { | ||
defaultMessage: 'Open in Discover', | ||
}); | ||
|
||
public readonly euiIcon = 'discoverApp'; | ||
|
||
supportedTriggers(): OpenInDiscoverTrigger[] { | ||
return [APPLY_FILTER_TRIGGER]; | ||
} | ||
|
||
private readonly ReactCollectConfig: React.FC<CollectConfigProps> = ({ | ||
config, | ||
onConfig, | ||
context, | ||
}) => { | ||
return ( | ||
<EuiFormRow hasChildLabel={false}> | ||
<EuiSwitch | ||
id="openInNewTab" | ||
name="openInNewTab" | ||
label={i18n.translate('xpack.lens.app.exploreDataInDiscoverDrilldown.newTabConfig', { | ||
defaultMessage: 'Open in new tab', | ||
})} | ||
checked={config.openInNewTab} | ||
onChange={() => onConfig({ ...config, openInNewTab: !config.openInNewTab })} | ||
data-test-subj="openInDiscoverDrilldownOpenInNewTab" | ||
/> | ||
</EuiFormRow> | ||
); | ||
}; | ||
|
||
public readonly CollectConfig = reactToUiComponent(this.ReactCollectConfig); | ||
|
||
public readonly createConfig = () => ({ | ||
openInNewTab: true, | ||
}); | ||
|
||
public readonly isConfigValid = (config: Config): config is Config => { | ||
return true; | ||
}; | ||
|
||
public readonly isCompatible = async (config: Config, context: ActionContext) => { | ||
return isCompatible({ | ||
discover: this.deps.discover, | ||
hasDiscoverAccess: this.deps.hasDiscoverAccess(), | ||
...context, | ||
embeddable: context.embeddable as IEmbeddable, | ||
...config, | ||
}); | ||
}; | ||
|
||
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, | ||
context.timeFieldName | ||
); | ||
execute({ | ||
discover: this.deps.discover, | ||
hasDiscoverAccess: this.deps.hasDiscoverAccess(), | ||
...context, | ||
embeddable: context.embeddable as IEmbeddable, | ||
openInSameTab: !config.openInNewTab, | ||
filters, | ||
timeRange, | ||
}); | ||
}; | ||
} |
Oops, something went wrong.