import React, { PureComponent } from 'react'; import { QueryTransaction, HistoryItem, QueryHint } from 'app/types/explore'; import DefaultQueryField from './QueryField'; import QueryTransactionStatus from './QueryTransactionStatus'; import { DataSource, DataQuery } from 'app/types'; function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint { const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0); if (transaction) { return transaction.hints[0]; } return undefined; } interface QueryRowEventHandlers { onAddQueryRow: (index: number) => void; onChangeQuery: (value: DataQuery, index: number, override?: boolean) => void; onClickHintFix: (action: object, index?: number) => void; onExecuteQuery: () => void; onRemoveQueryRow: (index: number) => void; } interface QueryRowCommonProps { className?: string; datasource: DataSource; history: HistoryItem[]; transactions: QueryTransaction[]; } type QueryRowProps = QueryRowCommonProps & QueryRowEventHandlers & { index: number; initialQuery: DataQuery; }; class QueryRow extends PureComponent { onChangeQuery = (value: DataQuery, override?: boolean) => { const { index, onChangeQuery } = this.props; if (onChangeQuery) { onChangeQuery(value, index, override); } }; onClickAddButton = () => { const { index, onAddQueryRow } = this.props; if (onAddQueryRow) { onAddQueryRow(index); } }; onClickClearButton = () => { this.onChangeQuery(null, true); }; onClickHintFix = action => { const { index, onClickHintFix } = this.props; if (onClickHintFix) { onClickHintFix(action, index); } }; onClickRemoveButton = () => { const { index, onRemoveQueryRow } = this.props; if (onRemoveQueryRow) { onRemoveQueryRow(index); } }; onPressEnter = () => { const { onExecuteQuery } = this.props; if (onExecuteQuery) { onExecuteQuery(); } }; render() { const { datasource, history, initialQuery, transactions } = this.props; const transactionWithError = transactions.find(t => t.error !== undefined); const hint = getFirstHintFromTransactions(transactions); const queryError = transactionWithError ? transactionWithError.error : null; const QueryField = datasource.pluginExports.ExploreQueryField || DefaultQueryField; return (
); } } type QueryRowsProps = QueryRowCommonProps & QueryRowEventHandlers & { initialQueries: DataQuery[]; }; export default class QueryRows extends PureComponent { render() { const { className = '', initialQueries, transactions, ...handlers } = this.props; return (
{initialQueries.map((query, index) => ( t.rowIndex === index)} {...handlers} /> ))}
); } }