diff --git a/public/app/plugins/datasource/dashboard/DashboardQueryEditor.tsx b/public/app/plugins/datasource/dashboard/DashboardQueryEditor.tsx index 752a6c4eb20..57805ea3d20 100644 --- a/public/app/plugins/datasource/dashboard/DashboardQueryEditor.tsx +++ b/public/app/plugins/datasource/dashboard/DashboardQueryEditor.tsx @@ -43,55 +43,61 @@ export class DashboardQueryEditor extends PureComponent { } async componentDidMount() { - this.componentDidUpdate(this.props); + await this.updateState(); } async componentDidUpdate(prevProps: Props) { const { panelData, queries } = this.props; - if (queries.length < 0) { - return; - } - - if (!prevProps || prevProps.panelData !== panelData) { - const query = queries[0] as DashboardQuery; - const defaultDS = await getDatasourceSrv().get(); - const dashboard = getDashboardSrv().getCurrent(); - const panel = dashboard.getPanelById(query.panelId ?? -124134); - - if (!panel) { - this.setState({ defaultDatasource: defaultDS.name }); - return; - } - - const mainDS = await getDatasourceSrv().get(panel.datasource); - const info: ResultInfo[] = []; - - for (const query of panel.targets) { - const ds = query.datasource ? await getDatasourceSrv().get(query.datasource) : mainDS; - const fmt = ds.getQueryDisplayText ? ds.getQueryDisplayText : getQueryDisplayText; - - const qData = filterPanelDataToQuery(panelData, query.refId); - const queryData = qData ? qData : panelData; - - info.push({ - refId: query.refId, - query: fmt(query), - img: ds.meta.info.logos.small, - data: queryData.series, - error: queryData.error, - }); - } - - this.setState({ defaultDatasource: defaultDS.name, results: info }); + if (prevProps.panelData !== panelData || prevProps.queries !== queries) { + await this.updateState(); } } + async updateState() { + const { panelData, queries } = this.props; + + const query = queries[0] as DashboardQuery; + const defaultDS = await getDatasourceSrv().get(); + const dashboard = getDashboardSrv().getCurrent(); + const panel = dashboard.getPanelById(query.panelId ?? -124134); + + if (!panel) { + this.setState({ defaultDatasource: defaultDS.name }); + return; + } + + const mainDS = await getDatasourceSrv().get(panel.datasource); + const info: ResultInfo[] = []; + + for (const query of panel.targets) { + const ds = query.datasource ? await getDatasourceSrv().get(query.datasource) : mainDS; + const fmt = ds.getQueryDisplayText ? ds.getQueryDisplayText : getQueryDisplayText; + + const qData = filterPanelDataToQuery(panelData, query.refId); + const queryData = qData ? qData : panelData; + + info.push({ + refId: query.refId, + query: fmt(query), + img: ds.meta.info.logos.small, + data: queryData.series, + error: queryData.error, + }); + } + + this.setState({ defaultDatasource: defaultDS.name, results: info }); + } + onPanelChanged = (id: number) => { - const { onChange } = this.props; const query = this.getQuery(); - query.panelId = id; - onChange([query]); + + this.props.onChange([ + { + ...query, + panelId: id, + } as DashboardQuery, + ]); }; renderQueryData(editURL: string) {