QueryRows.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. />
  106. )}
  107. </div>
  108. <div className="query-row-tools">
  109. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  110. <i className="fa fa-times" />
  111. </button>
  112. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  113. <i className="fa fa-plus" />
  114. </button>
  115. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  116. <i className="fa fa-minus" />
  117. </button>
  118. </div>
  119. </div>
  120. );
  121. }
  122. }
  123. type QueryRowsProps = QueryRowCommonProps &
  124. QueryRowEventHandlers & {
  125. initialQueries: DataQuery[];
  126. };
  127. export default class QueryRows extends PureComponent<QueryRowsProps> {
  128. render() {
  129. const { className = '', initialQueries, transactions, ...handlers } = this.props;
  130. return (
  131. <div className={className}>
  132. {initialQueries.map((query, index) => (
  133. <QueryRow
  134. key={query.key}
  135. index={index}
  136. initialQuery={query}
  137. transactions={transactions.filter(t => t.rowIndex === index)}
  138. {...handlers}
  139. />
  140. ))}
  141. </div>
  142. );
  143. }
  144. }