From 43767044cf2761f3ae3b01b5a2ef6a8802fe8916 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Thu, 8 Jun 2023 09:26:21 -0600 Subject: [PATCH] [maps] pass dataFilters to ISource.getImmutableProperties (#159255) https://github.com/elastic/kibana/pull/159161 is adding adding hyperlink to anomaly explorer for job from anomaly layer in maps. A requirement from this request is to be able to include timerange and query state in the hyperlink. This PR updates ISource.getImmutableProperties to take dataFilters parameter so hyperlink can be updated with latest query state. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com> --- .../maps/public/classes/layers/layer.tsx | 8 +-- .../layers/layer_group/layer_group.tsx | 6 +- .../mvt_single_layer_vector_source.test.tsx | 2 +- .../maps/public/classes/sources/source.ts | 3 +- .../edit_layer_panel.test.tsx.snap | 38 ++--------- .../edit_layer_panel.test.tsx | 3 - .../edit_layer_panel/edit_layer_panel.tsx | 52 +------------- .../edit_layer_panel/source_details/index.ts | 20 ++++++ .../source_details/source_details.tsx | 68 +++++++++++++++++++ 9 files changed, 103 insertions(+), 97 deletions(-) create mode 100644 x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/index.ts create mode 100644 x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/source_details.tsx diff --git a/x-pack/plugins/maps/public/classes/layers/layer.tsx b/x-pack/plugins/maps/public/classes/layers/layer.tsx index cda93f701fe85..8e17da1ce4a96 100644 --- a/x-pack/plugins/maps/public/classes/layers/layer.tsx +++ b/x-pack/plugins/maps/public/classes/layers/layer.tsx @@ -33,7 +33,7 @@ import { Timeslice, StyleMetaDescriptor, } from '../../../common/descriptor_types'; -import { ImmutableSourceProperty, ISource, SourceEditorArgs } from '../sources/source'; +import { ISource, SourceEditorArgs } from '../sources/source'; import { DataRequestContext } from '../../actions'; import { IStyle } from '../styles/style'; import { LICENSED_FEATURES } from '../../licensed_features'; @@ -66,7 +66,6 @@ export interface ILayer { getStyle(): IStyle; getStyleForEditing(): IStyle; getCurrentStyle(): IStyle; - getImmutableSourceProperties(): Promise; renderSourceSettingsEditor(sourceEditorArgs: SourceEditorArgs): ReactElement | null; isLayerLoading(): boolean; isFilteredByGlobalTime(): Promise; @@ -338,11 +337,6 @@ export class AbstractLayer implements ILayer { return this._descriptor.query ? this._descriptor.query : null; } - async getImmutableSourceProperties(): Promise { - const source = this.getSource(); - return await source.getImmutableProperties(); - } - renderSourceSettingsEditor(sourceEditorArgs: SourceEditorArgs) { return this.getSourceForEditing().renderSourceSettingsEditor(sourceEditorArgs); } diff --git a/x-pack/plugins/maps/public/classes/layers/layer_group/layer_group.tsx b/x-pack/plugins/maps/public/classes/layers/layer_group/layer_group.tsx index add3713832813..d7fb1d9dbb17f 100644 --- a/x-pack/plugins/maps/public/classes/layers/layer_group/layer_group.tsx +++ b/x-pack/plugins/maps/public/classes/layers/layer_group/layer_group.tsx @@ -25,7 +25,7 @@ import { StyleDescriptor, StyleMetaDescriptor, } from '../../../../common/descriptor_types'; -import { ImmutableSourceProperty, ISource, SourceEditorArgs } from '../../sources/source'; +import { ISource, SourceEditorArgs } from '../../sources/source'; import { type DataRequestContext } from '../../../actions'; import { getLayersExtent } from '../../../actions/get_layers_extent'; import { ILayer, LayerIcon } from '../layer'; @@ -263,10 +263,6 @@ export class LayerGroup implements ILayer { return null; } - async getImmutableSourceProperties(): Promise { - return []; - } - renderSourceSettingsEditor(sourceEditorArgs: SourceEditorArgs) { return null; } diff --git a/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx b/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx index 68c94351a3c04..c21be5b0bfdb0 100644 --- a/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx +++ b/x-pack/plugins/maps/public/classes/sources/mvt_single_layer_vector_source/mvt_single_layer_vector_source.test.tsx @@ -77,7 +77,7 @@ describe('getTooltipProperties', () => { }); }); -describe('getImmutableSourceProperties', () => { +describe('getImmutableProperties', () => { it('should only show immutable props', async () => { const source = new MVTSingleLayerVectorSource(descriptor); const properties = await source.getImmutableProperties(); diff --git a/x-pack/plugins/maps/public/classes/sources/source.ts b/x-pack/plugins/maps/public/classes/sources/source.ts index b6e261dd3d690..5ce8dd6118d38 100644 --- a/x-pack/plugins/maps/public/classes/sources/source.ts +++ b/x-pack/plugins/maps/public/classes/sources/source.ts @@ -14,6 +14,7 @@ import { FieldFormatter, LAYER_TYPE, MAX_ZOOM, MIN_ZOOM } from '../../../common/ import { AbstractSourceDescriptor, Attribution, + DataFilters, DataRequestMeta, StyleDescriptor, Timeslice, @@ -49,7 +50,7 @@ export interface ISource { isFilterByMapBounds(): boolean; isQueryAware(): boolean; isTimeAware(): Promise; - getImmutableProperties(): Promise; + getImmutableProperties(dataFilters: DataFilters): Promise; getAttributionProvider(): (() => Promise) | null; isESSource(): boolean; renderSourceSettingsEditor(sourceEditorArgs: SourceEditorArgs): ReactElement | null; diff --git a/x-pack/plugins/maps/public/connected_components/edit_layer_panel/__snapshots__/edit_layer_panel.test.tsx.snap b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/__snapshots__/edit_layer_panel.test.tsx.snap index 3cbeac5d9a3e5..40951b4f846ed 100644 --- a/x-pack/plugins/maps/public/connected_components/edit_layer_panel/__snapshots__/edit_layer_panel.test.tsx.snap +++ b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/__snapshots__/edit_layer_panel.test.tsx.snap @@ -49,35 +49,13 @@ exports[`EditLayerPanel is rendered 1`] = ` -
- - - -

- - source prop1 - - - - you get one chance to set me - -

-
-
-
+
{ return 'layer 1'; }, - getImmutableSourceProperties: () => { - return [{ label: 'source prop1', value: 'you get one chance to set me' }]; - }, canShowTooltip: () => { return true; }, diff --git a/x-pack/plugins/maps/public/connected_components/edit_layer_panel/edit_layer_panel.tsx b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/edit_layer_panel.tsx index ae8e02b4cef3e..ee7d40412da1a 100644 --- a/x-pack/plugins/maps/public/connected_components/edit_layer_panel/edit_layer_panel.tsx +++ b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/edit_layer_panel.tsx @@ -17,9 +17,6 @@ import { EuiFlyoutHeader, EuiFlyoutFooter, EuiSpacer, - EuiAccordion, - EuiText, - EuiLink, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; @@ -33,10 +30,11 @@ import { StyleDescriptor, VectorLayerDescriptor } from '../../../common/descript import { getData, getCore } from '../../kibana_services'; import { ILayer } from '../../classes/layers/layer'; import { isVectorLayer, IVectorLayer } from '../../classes/layers/vector_layer'; -import { ImmutableSourceProperty, OnSourceChangeArgs } from '../../classes/sources/source'; +import { OnSourceChangeArgs } from '../../classes/sources/source'; import { IField } from '../../classes/fields/field'; import { isLayerGroup } from '../../classes/layers/layer_group'; import { isSpatialJoin } from '../../classes/joins/is_spatial_join'; +import { SourceDetails } from './source_details'; const localStorage = new Storage(window.localStorage); @@ -48,7 +46,6 @@ export interface Props { interface State { displayName: string; - immutableSourceProps: ImmutableSourceProperty[]; leftJoinFields: JoinField[]; supportsFitToBounds: boolean; } @@ -57,7 +54,6 @@ export class EditLayerPanel extends Component { private _isMounted = false; state: State = { displayName: '', - immutableSourceProps: [], leftJoinFields: [], supportsFitToBounds: false, }; @@ -65,7 +61,6 @@ export class EditLayerPanel extends Component { componentDidMount() { this._isMounted = true; this._loadDisplayName(); - this._loadImmutableSourceProperties(); this._loadLeftJoinFields(); this._loadSupportsFitToBounds(); } @@ -96,17 +91,6 @@ export class EditLayerPanel extends Component { } }; - _loadImmutableSourceProperties = async () => { - if (!this.props.selectedLayer || isLayerGroup(this.props.selectedLayer)) { - return; - } - - const immutableSourceProps = await this.props.selectedLayer.getImmutableSourceProperties(); - if (this._isMounted) { - this.setState({ immutableSourceProps }); - } - }; - async _loadLeftJoinFields() { if (!this.props.selectedLayer || !isVectorLayer(this.props.selectedLayer)) { return; @@ -205,37 +189,7 @@ export class EditLayerPanel extends Component { _renderSourceDetails() { return !this.props.selectedLayer || isLayerGroup(this.props.selectedLayer) ? null : ( -
- - - - {this.state.immutableSourceProps.map( - ({ label, value, link }: ImmutableSourceProperty) => { - function renderValue() { - if (link) { - return ( - - {value} - - ); - } - return {value}; - } - return ( -

- {label} {renderValue()} -

- ); - } - )} -
-
-
+ ); } diff --git a/x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/index.ts b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/index.ts new file mode 100644 index 0000000000000..06a2dd61432a2 --- /dev/null +++ b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/index.ts @@ -0,0 +1,20 @@ +/* + * 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 { connect } from 'react-redux'; +import { SourceDetails } from './source_details'; +import { getDataFilters } from '../../../selectors/map_selectors'; +import { MapStoreState } from '../../../reducers/store'; + +function mapStateToProps(state: MapStoreState) { + return { + dataFilters: getDataFilters(state), + }; +} + +const connected = connect(mapStateToProps, {})(SourceDetails); +export { connected as SourceDetails }; diff --git a/x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/source_details.tsx b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/source_details.tsx new file mode 100644 index 0000000000000..f02d1fb8b5170 --- /dev/null +++ b/x-pack/plugins/maps/public/connected_components/edit_layer_panel/source_details/source_details.tsx @@ -0,0 +1,68 @@ +/* + * 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, { useEffect, useState } from 'react'; +import { EuiAccordion, EuiLink, EuiSkeletonText, EuiSpacer, EuiText } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; +import { DataFilters } from '../../../../common/descriptor_types'; +import { ImmutableSourceProperty, ISource } from '../../../classes/sources/source'; + +export interface Props { + source: ISource; + dataFilters: DataFilters; +} + +export function SourceDetails(props: Props) { + const [isLoading, setIsLoading] = useState(false); + const [sourceProperties, setSourceProperties] = useState([]); + + useEffect(() => { + let ignore = false; + setIsLoading(true); + props.source.getImmutableProperties(props.dataFilters).then((nextSourceProperties) => { + if (!ignore) { + setSourceProperties(nextSourceProperties); + setIsLoading(false); + } + }); + + return () => { + ignore = true; + }; + }, [props.dataFilters, props.source]); + + return ( +
+ + + + + {sourceProperties.map(({ label, value, link }: ImmutableSourceProperty) => { + return ( +

+ {label}{' '} + {link ? ( + + {value} + + ) : ( + {value} + )} +

+ ); + })} +
+
+
+
+ ); +}