From 9f93a81bee8d2b541373016d963d739fb539e6f5 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Fri, 1 Oct 2021 11:06:11 -0700 Subject: [PATCH] Geomap: use panel context editor state (#39920) --- public/app/plugins/panel/geomap/GeomapPanel.tsx | 11 ++++++++--- .../app/plugins/panel/geomap/editor/MapViewEditor.tsx | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/public/app/plugins/panel/geomap/GeomapPanel.tsx b/public/app/plugins/panel/geomap/GeomapPanel.tsx index 0dafcad1391..eecd123ce2f 100644 --- a/public/app/plugins/panel/geomap/GeomapPanel.tsx +++ b/public/app/plugins/panel/geomap/GeomapPanel.tsx @@ -25,7 +25,7 @@ import { centerPointRegistry, MapCenterID } from './view'; import { fromLonLat, toLonLat } from 'ol/proj'; import { Coordinate } from 'ol/coordinate'; import { css } from '@emotion/css'; -import { Portal, stylesFactory, VizTooltipContainer } from '@grafana/ui'; +import { PanelContext, PanelContextRoot, Portal, stylesFactory, VizTooltipContainer } from '@grafana/ui'; import { GeomapOverlay, OverlayProps } from './GeomapOverlay'; import { DebugOverlay } from './components/DebugOverlay'; import { getGlobalStyles } from './globalStyles'; @@ -41,7 +41,6 @@ interface MapLayerState { // Allows multiple panels to share the same view instance let sharedView: View | undefined = undefined; -export let lastGeomapPanelInstance: GeomapPanel | undefined = undefined; type Props = PanelProps; interface State extends OverlayProps { @@ -49,6 +48,9 @@ interface State extends OverlayProps { } export class GeomapPanel extends Component { + static contextType = PanelContextRoot; + panelContext: PanelContext = {} as PanelContext; + globalCSS = getGlobalStyles(config.theme2); counter = 0; @@ -66,7 +68,10 @@ export class GeomapPanel extends Component { } componentDidMount() { - lastGeomapPanelInstance = this; + this.panelContext = this.context as PanelContext; + if (this.panelContext.onInstanceStateChange) { + this.panelContext.onInstanceStateChange(this); + } } shouldComponentUpdate(nextProps: Props) { diff --git a/public/app/plugins/panel/geomap/editor/MapViewEditor.tsx b/public/app/plugins/panel/geomap/editor/MapViewEditor.tsx index 121df8c21c9..4da235e68cb 100644 --- a/public/app/plugins/panel/geomap/editor/MapViewEditor.tsx +++ b/public/app/plugins/panel/geomap/editor/MapViewEditor.tsx @@ -4,10 +4,10 @@ import { Button, InlineField, InlineFieldRow, Select, VerticalGroup } from '@gra import { GeomapPanelOptions, MapViewConfig } from '../types'; import { centerPointRegistry, MapCenterID } from '../view'; import { NumberInput } from 'app/features/dimensions/editors/NumberInput'; -import { lastGeomapPanelInstance } from '../GeomapPanel'; import { toLonLat } from 'ol/proj'; +import { GeomapPanel } from '../GeomapPanel'; -export const MapViewEditor: FC> = ({ +export const MapViewEditor: FC> = ({ value, onChange, context, @@ -25,7 +25,7 @@ export const MapViewEditor: FC { - const map = lastGeomapPanelInstance?.map; + const map = context.instanceState?.map; if (map) { const view = map.getView(); const coords = view.getCenter(); @@ -40,7 +40,7 @@ export const MapViewEditor: FC) => {