import React, { useEffect, useState } from 'react'; import Skeleton from 'react-loading-skeleton'; import { DataSourceApi, DataSourceOptionsType, DataSourceQueryType, QueryEditorProps } from '@grafana/data'; import { DataQuery, DataSourceJsonData } from '@grafana/schema'; import { isMigrationHandler, migrateQuery } from '../utils/migrationHandler'; /** * @alpha Experimental: QueryEditorWithMigration is a higher order component that wraps the QueryEditor component * and ensures that the query is migrated before being passed to the QueryEditor. */ export function QueryEditorWithMigration< DSType extends DataSourceApi, TQuery extends DataQuery = DataSourceQueryType, TOptions extends DataSourceJsonData = DataSourceOptionsType, >(QueryEditor: React.ComponentType>) { const WithExtra = (props: QueryEditorProps) => { const [migrated, setMigrated] = useState(false); const [query, setQuery] = useState(props.query); useEffect(() => { if (props.query && isMigrationHandler(props.datasource)) { migrateQuery(props.datasource, props.query).then((migrated) => { props.onChange(migrated); setQuery(migrated); setMigrated(true); }); } else { setMigrated(true); } }, []); // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { setQuery(props.query); }, [props.query]); if (!migrated) { return ; } return ; }; return WithExtra; }