useVisualizeFields.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 = {
  11. yAxes: string[];
  12. };
  13. export function useVisualizeFields({yAxes}: Props) {
  14. const numberTags = useSpanTags('number');
  15. const parsedYAxes: ParsedFunction[] = useMemo(() => {
  16. return yAxes.map(parseFunction).filter(defined);
  17. }, [yAxes]);
  18. const fieldOptions: Array<SelectOption<string>> = useMemo(() => {
  19. const unknownOptions = parsedYAxes
  20. .flatMap(entry => {
  21. return entry.arguments;
  22. })
  23. .filter(option => {
  24. return !numberTags.hasOwnProperty(option);
  25. });
  26. const options = [
  27. ...unknownOptions.map(option => ({
  28. label: prettifyTagKey(option),
  29. value: option,
  30. textValue: option,
  31. })),
  32. ...Object.values(numberTags).map(tag => {
  33. return {
  34. label: tag.name,
  35. value: tag.key,
  36. textValue: tag.name,
  37. };
  38. }),
  39. ];
  40. options.sort((a, b) => {
  41. if (a.label < b.label) {
  42. return -1;
  43. }
  44. if (a.label > b.label) {
  45. return 1;
  46. }
  47. return 0;
  48. });
  49. return options;
  50. }, [numberTags, parsedYAxes]);
  51. return fieldOptions;
  52. }