QueryRows.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { PureComponent } from 'react';
  2. import { QueryTransaction } 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[]) {
  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. class QueryRow extends PureComponent<any, {}> {
  14. onChangeQuery = (value, override?: boolean) => {
  15. const { index, onChangeQuery } = this.props;
  16. if (onChangeQuery) {
  17. onChangeQuery(value, index, override);
  18. }
  19. };
  20. onClickAddButton = () => {
  21. const { index, onAddQueryRow } = this.props;
  22. if (onAddQueryRow) {
  23. onAddQueryRow(index);
  24. }
  25. };
  26. onClickClearButton = () => {
  27. this.onChangeQuery('', true);
  28. };
  29. onClickHintFix = action => {
  30. const { index, onClickHintFix } = this.props;
  31. if (onClickHintFix) {
  32. onClickHintFix(action, index);
  33. }
  34. };
  35. onClickRemoveButton = () => {
  36. const { index, onRemoveQueryRow } = this.props;
  37. if (onRemoveQueryRow) {
  38. onRemoveQueryRow(index);
  39. }
  40. };
  41. onPressEnter = () => {
  42. const { onExecuteQuery } = this.props;
  43. if (onExecuteQuery) {
  44. onExecuteQuery();
  45. }
  46. };
  47. render() {
  48. const { history, query, request, supportsLogs, transactions } = this.props;
  49. const transactionWithError = transactions.find(t => t.error);
  50. const hint = getFirstHintFromTransactions(transactions);
  51. const queryError = transactionWithError ? transactionWithError.error : null;
  52. return (
  53. <div className="query-row">
  54. <div className="query-row-status">
  55. <QueryTransactions transactions={transactions} />
  56. </div>
  57. <div className="query-row-field">
  58. <QueryField
  59. error={queryError}
  60. hint={hint}
  61. initialQuery={query}
  62. history={history}
  63. onClickHintFix={this.onClickHintFix}
  64. onPressEnter={this.onPressEnter}
  65. onQueryChange={this.onChangeQuery}
  66. request={request}
  67. supportsLogs={supportsLogs}
  68. />
  69. </div>
  70. <div className="query-row-tools">
  71. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickClearButton}>
  72. <i className="fa fa-times" />
  73. </button>
  74. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickAddButton}>
  75. <i className="fa fa-plus" />
  76. </button>
  77. <button className="btn navbar-button navbar-button--tight" onClick={this.onClickRemoveButton}>
  78. <i className="fa fa-minus" />
  79. </button>
  80. </div>
  81. </div>
  82. );
  83. }
  84. }
  85. export default class QueryRows extends PureComponent<any, {}> {
  86. render() {
  87. const { className = '', queries, queryHints, transactions, ...handlers } = this.props;
  88. return (
  89. <div className={className}>
  90. {queries.map((q, index) => (
  91. <QueryRow
  92. key={q.key}
  93. index={index}
  94. query={q.query}
  95. transactions={transactions.filter(t => t.rowIndex === index)}
  96. {...handlers}
  97. />
  98. ))}
  99. </div>
  100. );
  101. }
  102. }