QueryRows.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, { PureComponent } from 'react';
  2. import { QueryTransaction, HistoryItem, Query, QueryHint } from 'app/types/explore';
  3. // TODO make this datasource-plugin-dependent
  4. import QueryField from './PromQueryField';
  5. import QueryTransactions from './QueryTransactions';
  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: 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 QueryRow 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 { 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. return (
  73. <div className="query-row">
  74. <div className="query-row-status">
  75. <QueryTransactions transactions={transactions} />
  76. </div>
  77. <div className="query-row-field">
  78. <QueryField
  79. datasource={datasource}
  80. error={queryError}
  81. hint={hint}
  82. initialQuery={query}
  83. history={history}
  84. onClickHintFix={this.onClickHintFix}
  85. onPressEnter={this.onPressEnter}
  86. onQueryChange={this.onChangeQuery}
  87. supportsLogs={supportsLogs}
  88. />
  89. </div>
  90. <div className="query-row-tools">
  91. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  92. <i className="fa fa-times" />
  93. </button>
  94. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  95. <i className="fa fa-plus" />
  96. </button>
  97. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  98. <i className="fa fa-minus" />
  99. </button>
  100. </div>
  101. </div>
  102. );
  103. }
  104. }
  105. type QueryRowsProps = QueryRowCommonProps &
  106. QueryRowEventHandlers & {
  107. queries: Query[];
  108. };
  109. export default class QueryRows extends PureComponent<QueryRowsProps> {
  110. render() {
  111. const { className = '', queries, transactions, ...handlers } = this.props;
  112. return (
  113. <div className={className}>
  114. {queries.map((q, index) => (
  115. <QueryRow
  116. key={q.key}
  117. index={index}
  118. query={q.query}
  119. transactions={transactions.filter(t => t.rowIndex === index)}
  120. {...handlers}
  121. />
  122. ))}
  123. </div>
  124. );
  125. }
  126. }