StreamingClientEditor.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React, { ChangeEvent } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { InlineField, InlineFieldRow, Input, Select } from '@grafana/ui';
  4. import { EditorProps } from '../QueryEditor';
  5. import { StreamingQuery } from '../types';
  6. const streamingClientFields = [
  7. { label: 'Speed (ms)', id: 'speed', placeholder: 'value', min: 10, step: 10 },
  8. { label: 'Spread', id: 'spread', placeholder: 'value', min: 0.5, step: 0.1 },
  9. { label: 'Noise', id: 'noise', placeholder: 'value', min: 0, step: 0.1 },
  10. { label: 'Bands', id: 'bands', placeholder: 'bands', min: 0, step: 1 },
  11. ];
  12. const types = [
  13. { value: 'signal', label: 'Signal' },
  14. { value: 'logs', label: 'Logs' },
  15. { value: 'fetch', label: 'Fetch' },
  16. ];
  17. export const StreamingClientEditor = ({ onChange, query }: EditorProps) => {
  18. const onSelectChange = ({ value }: SelectableValue) => {
  19. onChange({ target: { name: 'type', value } });
  20. };
  21. // Convert values to numbers before saving
  22. const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
  23. const { name, value } = e.target;
  24. onChange({ target: { name, value: Number(value) } });
  25. };
  26. return (
  27. <InlineFieldRow>
  28. <InlineField label="Type" labelWidth={14}>
  29. <Select width={32} onChange={onSelectChange} defaultValue={types[0]} options={types} />
  30. </InlineField>
  31. {query?.stream?.type === 'signal' &&
  32. streamingClientFields.map(({ label, id, min, step, placeholder }) => {
  33. return (
  34. <InlineField label={label} labelWidth={14} key={id}>
  35. <Input
  36. width={32}
  37. type="number"
  38. id={`stream.${id}-${query.refId}`}
  39. name={id}
  40. min={min}
  41. step={step}
  42. value={query.stream?.[id as keyof StreamingQuery]}
  43. placeholder={placeholder}
  44. onChange={onInputChange}
  45. />
  46. </InlineField>
  47. );
  48. })}
  49. {query?.stream?.type === 'fetch' && (
  50. <InlineField label="URL" labelWidth={14} grow>
  51. <Input
  52. type="text"
  53. name="url"
  54. id={`stream.url-${query.refId}`}
  55. value={query?.stream?.url}
  56. placeholder="Fetch URL"
  57. onChange={onChange}
  58. />
  59. </InlineField>
  60. )}
  61. </InlineFieldRow>
  62. );
  63. };