// Libraries import React, { PureComponent } from 'react'; // Types import { InputDatasource, describeDataFrame } from './InputDatasource'; import { InputQuery, InputOptions } from './types'; import { FormLabel, Select, QueryEditorProps, SelectOptionItem, DataFrame, TableInputCSV, toCSV } from '@grafana/ui'; type Props = QueryEditorProps; const options = [ { value: 'panel', label: 'Panel', description: 'Save data in the panel configuration.' }, { value: 'shared', label: 'Shared', description: 'Save data in the shared datasource object.' }, ]; interface State { text: string; } export class InputQueryEditor extends PureComponent { state = { text: '', }; onComponentDidMount() { const { query } = this.props; const text = query.data ? toCSV(query.data) : ''; this.setState({ text }); } onSourceChange = (item: SelectOptionItem) => { const { datasource, query, onChange, onRunQuery } = this.props; let data: DataFrame[] | undefined = undefined; if (item.value === 'panel') { if (query.data) { return; } data = [...datasource.data]; if (!data) { data = [ { fields: [], rows: [], }, ]; } this.setState({ text: toCSV(data) }); } onChange({ ...query, data }); onRunQuery(); }; onSeriesParsed = (data: DataFrame[], text: string) => { const { query, onChange, onRunQuery } = this.props; this.setState({ text }); if (!data) { data = [ { fields: [], rows: [], }, ]; } onChange({ ...query, data }); onRunQuery(); }; render() { const { datasource, query } = this.props; const { id, name } = datasource; const { text } = this.state; const selected = query.data ? options[0] : options[1]; return (
Data