QueryRows.tsx 5.1 KB

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