ElasticsearchQueryField.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import _ from 'lodash';
  2. import React from 'react';
  3. // @ts-ignore
  4. import PluginPrism from 'slate-prism';
  5. // @ts-ignore
  6. import Prism from 'prismjs';
  7. // dom also includes Element polyfills
  8. import QueryField from 'app/features/explore/QueryField';
  9. import { ExploreQueryFieldProps } from '@grafana/ui';
  10. import { ElasticDatasource } from '../datasource';
  11. import { ElasticsearchOptions, ElasticsearchQuery } from '../types';
  12. interface Props extends ExploreQueryFieldProps<ElasticDatasource, ElasticsearchQuery, ElasticsearchOptions> {}
  13. interface State {
  14. syntaxLoaded: boolean;
  15. }
  16. class ElasticsearchQueryField extends React.PureComponent<Props, State> {
  17. plugins: any[];
  18. constructor(props: Props, context: React.Context<any>) {
  19. super(props, context);
  20. this.plugins = [
  21. PluginPrism({
  22. onlyIn: (node: any) => node.type === 'code_block',
  23. getSyntax: (node: any) => 'lucene',
  24. }),
  25. ];
  26. this.state = {
  27. syntaxLoaded: false,
  28. };
  29. }
  30. componentDidMount() {
  31. this.onChangeQuery('', true);
  32. }
  33. componentWillUnmount() {}
  34. componentDidUpdate(prevProps: Props) {
  35. // if query changed from the outside (i.e. cleared via explore toolbar)
  36. if (!this.props.query.isLogsQuery) {
  37. this.onChangeQuery('', true);
  38. }
  39. }
  40. onChangeQuery = (value: string, override?: boolean) => {
  41. // Send text change to parent
  42. const { query, onChange, onRunQuery } = this.props;
  43. if (onChange) {
  44. const nextQuery: ElasticsearchQuery = { ...query, query: value, isLogsQuery: true };
  45. onChange(nextQuery);
  46. if (override && onRunQuery) {
  47. onRunQuery();
  48. }
  49. }
  50. };
  51. render() {
  52. const { queryResponse, query } = this.props;
  53. const { syntaxLoaded } = this.state;
  54. return (
  55. <>
  56. <div className="gf-form-inline gf-form-inline--nowrap">
  57. <div className="gf-form gf-form--grow flex-shrink-1">
  58. <QueryField
  59. additionalPlugins={this.plugins}
  60. initialQuery={query.query}
  61. onChange={this.onChangeQuery}
  62. onRunQuery={this.props.onRunQuery}
  63. placeholder="Enter a Lucene query"
  64. portalOrigin="elasticsearch"
  65. syntaxLoaded={syntaxLoaded}
  66. />
  67. </div>
  68. </div>
  69. {queryResponse && queryResponse.error ? (
  70. <div className="prom-query-field-info text-error">{queryResponse.error.message}</div>
  71. ) : null}
  72. </>
  73. );
  74. }
  75. }
  76. export default ElasticsearchQueryField;