useVisualizeFields.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {useMemo} from 'react';
  2. import type {SelectOption} from 'sentry/components/compactSelect';
  3. import {defined} from 'sentry/utils';
  4. import {
  5. type ParsedFunction,
  6. parseFunction,
  7. prettifyTagKey,
  8. } from 'sentry/utils/discover/fields';
  9. import {useSpanTags} from 'sentry/views/explore/contexts/spanTagsContext';
  10. type Props = {yAxes: string[]};
  11. export function useVisualizeFields({yAxes}: Props) {
  12. const {tags: numberTags} = useSpanTags('number');
  13. const parsedYAxes: ParsedFunction[] = useMemo(() => {
  14. return yAxes.map(parseFunction).filter(defined);
  15. }, [yAxes]);
  16. const fieldOptions: Array<SelectOption<string>> = useMemo(() => {
  17. const unknownOptions = parsedYAxes
  18. .flatMap(entry => {
  19. return entry.arguments;
  20. })
  21. .filter(option => {
  22. return !numberTags.hasOwnProperty(option);
  23. });
  24. const options = [
  25. ...unknownOptions.map(option => ({
  26. label: prettifyTagKey(option),
  27. value: option,
  28. textValue: option,
  29. })),
  30. ...Object.values(numberTags).map(tag => {
  31. return {label: tag.name, value: tag.key, textValue: tag.name};
  32. }),
  33. ];
  34. options.sort((a, b) => {
  35. if (a.label < b.label) {
  36. return -1;
  37. }
  38. if (a.label > b.label) {
  39. return 1;
  40. }
  41. return 0;
  42. });
  43. return options;
  44. }, [numberTags, parsedYAxes]);
  45. return fieldOptions;
  46. }