QueryEditor.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { QueryEditorProps } from '@grafana/data';
  4. import { FileDropzone, InlineField, InlineFieldRow, QueryField, RadioButtonGroup, useTheme2 } from '@grafana/ui';
  5. import { JaegerDatasource } from '../datasource';
  6. import { JaegerQuery, JaegerQueryType } from '../types';
  7. import { SearchForm } from './SearchForm';
  8. type Props = QueryEditorProps<JaegerDatasource, JaegerQuery>;
  9. export function QueryEditor({ datasource, query, onChange, onRunQuery }: Props) {
  10. const theme = useTheme2();
  11. const onChangeQuery = (value: string) => {
  12. const nextQuery: JaegerQuery = { ...query, query: value };
  13. onChange(nextQuery);
  14. };
  15. const renderEditorBody = () => {
  16. switch (query.queryType) {
  17. case 'search':
  18. return <SearchForm datasource={datasource} query={query} onChange={onChange} />;
  19. case 'upload':
  20. return (
  21. <div className={css({ padding: theme.spacing(2) })}>
  22. <FileDropzone
  23. options={{ multiple: false }}
  24. onLoad={(result) => {
  25. datasource.uploadedJson = result;
  26. onRunQuery();
  27. }}
  28. />
  29. </div>
  30. );
  31. default:
  32. return (
  33. <InlineFieldRow>
  34. <InlineField label="Trace ID" labelWidth={14} grow>
  35. <QueryField
  36. query={query.query}
  37. onChange={onChangeQuery}
  38. onRunQuery={onRunQuery}
  39. onBlur={() => {}}
  40. placeholder={'Enter a Trace ID (run with Shift+Enter)'}
  41. portalOrigin="jaeger"
  42. />
  43. </InlineField>
  44. </InlineFieldRow>
  45. );
  46. }
  47. };
  48. return (
  49. <>
  50. <div className={css({ width: '100%' })}>
  51. <InlineFieldRow>
  52. <InlineField label="Query type">
  53. <RadioButtonGroup<JaegerQueryType>
  54. options={[
  55. { value: 'search', label: 'Search' },
  56. { value: undefined, label: 'TraceID' },
  57. { value: 'upload', label: 'JSON file' },
  58. ]}
  59. value={query.queryType}
  60. onChange={(v) =>
  61. onChange({
  62. ...query,
  63. queryType: v,
  64. })
  65. }
  66. size="md"
  67. />
  68. </InlineField>
  69. </InlineFieldRow>
  70. {renderEditorBody()}
  71. </div>
  72. </>
  73. );
  74. }