toolbarVisualize.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import {CompactSelect, type SelectOption} from 'sentry/components/compactSelect';
  4. import {t} from 'sentry/locale';
  5. import {parseFunction} from 'sentry/utils/discover/fields';
  6. import type {SpanIndexedField} from 'sentry/views/insights/types';
  7. import {
  8. ALLOWED_VISUALIZE_AGGREGATES,
  9. ALLOWED_VISUALIZE_FIELDS,
  10. } from '../hooks/useVisualize';
  11. import {ToolbarHeading, ToolbarSection} from './styles';
  12. interface ToolbarVisualizeProps {
  13. setVisualize: (visualize: string) => void;
  14. visualize: string;
  15. }
  16. export function ToolbarVisualize({visualize, setVisualize}: ToolbarVisualizeProps) {
  17. const parsedVisualize = useMemo(() => {
  18. return parseFunction(visualize);
  19. }, [visualize]);
  20. const fieldOptions: SelectOption<SpanIndexedField>[] = ALLOWED_VISUALIZE_FIELDS.map(
  21. field => {
  22. return {
  23. label: field,
  24. value: field,
  25. };
  26. }
  27. );
  28. const aggregateOptions: SelectOption<string>[] = ALLOWED_VISUALIZE_AGGREGATES.map(
  29. aggregate => {
  30. return {
  31. label: aggregate,
  32. value: aggregate,
  33. };
  34. }
  35. );
  36. return (
  37. <ToolbarSection data-test-id="section-visualize">
  38. <ToolbarHeading>{t('Visualize')}</ToolbarHeading>
  39. <ToolbarContent>
  40. <CompactSelect
  41. size="md"
  42. options={fieldOptions}
  43. value={parsedVisualize?.arguments[0]}
  44. onChange={newField =>
  45. setVisualize(`${parsedVisualize?.name}(${newField.value})`)
  46. }
  47. />
  48. <CompactSelect
  49. size="md"
  50. options={aggregateOptions}
  51. value={parsedVisualize?.name}
  52. onChange={newAggregate =>
  53. setVisualize(`${newAggregate.value}(${parsedVisualize?.arguments[0]})`)
  54. }
  55. />
  56. </ToolbarContent>
  57. </ToolbarSection>
  58. );
  59. }
  60. const ToolbarContent = styled('div')`
  61. display: flex;
  62. `;