QueryRows.tsx 4.2 KB

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