QueryRows.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React, { PureComponent } from 'react';
  2. import { QueryTransaction, HistoryItem, Query, QueryHint } from 'app/types/explore';
  3. import DefaultQueryField from './QueryField';
  4. import QueryTransactionStatus from './QueryTransactionStatus';
  5. import { DataSource } from 'app/types';
  6. function getFirstHintFromTransactions(transactions: QueryTransaction[]): QueryHint {
  7. const transaction = transactions.find(qt => qt.hints && qt.hints.length > 0);
  8. if (transaction) {
  9. return transaction.hints[0];
  10. }
  11. return undefined;
  12. }
  13. interface QueryRowEventHandlers {
  14. onAddQueryRow: (index: number) => void;
  15. onChangeQuery: (value: string, index: number, override?: boolean) => void;
  16. onClickHintFix: (action: object, index?: number) => void;
  17. onExecuteQuery: () => void;
  18. onRemoveQueryRow: (index: number) => void;
  19. }
  20. interface QueryRowCommonProps {
  21. className?: string;
  22. datasource: DataSource;
  23. history: HistoryItem[];
  24. transactions: QueryTransaction[];
  25. }
  26. type QueryRowProps = QueryRowCommonProps &
  27. QueryRowEventHandlers & {
  28. index: number;
  29. query: string;
  30. };
  31. class QueryRow extends PureComponent<QueryRowProps> {
  32. onChangeQuery = (value, override?: boolean) => {
  33. const { index, onChangeQuery } = this.props;
  34. if (onChangeQuery) {
  35. onChangeQuery(value, index, override);
  36. }
  37. };
  38. onClickAddButton = () => {
  39. const { index, onAddQueryRow } = this.props;
  40. if (onAddQueryRow) {
  41. onAddQueryRow(index);
  42. }
  43. };
  44. onClickClearButton = () => {
  45. this.onChangeQuery('', true);
  46. };
  47. onClickHintFix = action => {
  48. const { index, onClickHintFix } = this.props;
  49. if (onClickHintFix) {
  50. onClickHintFix(action, index);
  51. }
  52. };
  53. onClickRemoveButton = () => {
  54. const { index, onRemoveQueryRow } = this.props;
  55. if (onRemoveQueryRow) {
  56. onRemoveQueryRow(index);
  57. }
  58. };
  59. onPressEnter = () => {
  60. const { onExecuteQuery } = this.props;
  61. if (onExecuteQuery) {
  62. onExecuteQuery();
  63. }
  64. };
  65. render() {
  66. const { datasource, history, query, transactions } = this.props;
  67. const transactionWithError = transactions.find(t => t.error !== undefined);
  68. const hint = getFirstHintFromTransactions(transactions);
  69. const queryError = transactionWithError ? transactionWithError.error : null;
  70. const QueryField = datasource.pluginExports.ExploreQueryField || DefaultQueryField;
  71. return (
  72. <div className="query-row">
  73. <div className="query-row-status">
  74. <QueryTransactionStatus transactions={transactions} />
  75. </div>
  76. <div className="query-row-field">
  77. <QueryField
  78. datasource={datasource}
  79. error={queryError}
  80. hint={hint}
  81. initialQuery={query}
  82. history={history}
  83. onClickHintFix={this.onClickHintFix}
  84. onPressEnter={this.onPressEnter}
  85. onQueryChange={this.onChangeQuery}
  86. />
  87. </div>
  88. <div className="query-row-tools">
  89. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  90. <i className="fa fa-times" />
  91. </button>
  92. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  93. <i className="fa fa-plus" />
  94. </button>
  95. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  96. <i className="fa fa-minus" />
  97. </button>
  98. </div>
  99. </div>
  100. );
  101. }
  102. }
  103. type QueryRowsProps = QueryRowCommonProps &
  104. QueryRowEventHandlers & {
  105. queries: Query[];
  106. };
  107. export default class QueryRows extends PureComponent<QueryRowsProps> {
  108. render() {
  109. const { className = '', queries, transactions, ...handlers } = this.props;
  110. return (
  111. <div className={className}>
  112. {queries.map((q, index) => (
  113. <QueryRow
  114. key={q.key}
  115. index={index}
  116. query={q.query}
  117. transactions={transactions.filter(t => t.rowIndex === index)}
  118. {...handlers}
  119. />
  120. ))}
  121. </div>
  122. );
  123. }
  124. }