useVisualizes.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {useCallback, useMemo} from 'react';
  2. import type {Location} from 'history';
  3. import {defined} from 'sentry/utils';
  4. import {parseFunction} from 'sentry/utils/discover/fields';
  5. import {
  6. ALLOWED_EXPLORE_VISUALIZE_AGGREGATES,
  7. ALLOWED_EXPLORE_VISUALIZE_FIELDS,
  8. } from 'sentry/utils/fields';
  9. import {decodeList} from 'sentry/utils/queryString';
  10. import {useLocation} from 'sentry/utils/useLocation';
  11. import {useNavigate} from 'sentry/utils/useNavigate';
  12. import {ChartType} from 'sentry/views/insights/common/components/chart';
  13. type BaseVisualize = {
  14. chartType: ChartType;
  15. yAxes: string[];
  16. };
  17. export type Visualize = BaseVisualize & {
  18. label: string;
  19. };
  20. interface Options {
  21. location: Location;
  22. navigate: ReturnType<typeof useNavigate>;
  23. }
  24. export const DEFAULT_VISUALIZATION = `${ALLOWED_EXPLORE_VISUALIZE_AGGREGATES[0]}(${ALLOWED_EXPLORE_VISUALIZE_FIELDS[0]})`;
  25. export function useVisualizes(): [Visualize[], (visualizes: BaseVisualize[]) => void] {
  26. const location = useLocation();
  27. const navigate = useNavigate();
  28. const options = {location, navigate};
  29. return useVisualizesImpl(options);
  30. }
  31. function useVisualizesImpl({
  32. location,
  33. navigate,
  34. }: Options): [Visualize[], (visualizes: BaseVisualize[]) => void] {
  35. const visualizes: Visualize[] = useMemo(() => {
  36. const rawVisualizes = decodeList(location.query.visualize);
  37. const result: Visualize[] = rawVisualizes
  38. .map(parseVisualizes)
  39. .filter(defined)
  40. .filter(parsed => parsed.yAxes.length > 0)
  41. .map((parsed, i) => {
  42. return {
  43. chartType: parsed.chartType,
  44. yAxes: parsed.yAxes,
  45. label: String.fromCharCode(65 + i), // starts from 'A'
  46. };
  47. });
  48. return result.length
  49. ? result
  50. : [{chartType: ChartType.LINE, label: 'A', yAxes: [DEFAULT_VISUALIZATION]}];
  51. }, [location.query.visualize]);
  52. const setVisualizes = useCallback(
  53. (newVisualizes: BaseVisualize[]) => {
  54. const stringified: string[] = [];
  55. for (const visualize of newVisualizes) {
  56. // ignore the label from visualize because it'll determined later
  57. stringified.push(
  58. JSON.stringify({
  59. chartType: visualize.chartType,
  60. yAxes: visualize.yAxes,
  61. })
  62. );
  63. }
  64. navigate({
  65. ...location,
  66. query: {
  67. ...location.query,
  68. visualize: stringified,
  69. },
  70. });
  71. },
  72. [location, navigate]
  73. );
  74. return [visualizes, setVisualizes];
  75. }
  76. function parseVisualizes(raw: string): BaseVisualize | null {
  77. try {
  78. const parsed = JSON.parse(raw);
  79. if (!defined(parsed) || !Array.isArray(parsed.yAxes)) {
  80. return null;
  81. }
  82. const yAxes = parsed.yAxes.filter(parseFunction);
  83. if (yAxes.length <= 0) {
  84. return null;
  85. }
  86. let chartType = Number(parsed.chartType);
  87. if (isNaN(chartType) || !Object.values(ChartType).includes(chartType)) {
  88. chartType = ChartType.LINE;
  89. }
  90. return {yAxes, chartType};
  91. } catch (error) {
  92. return null;
  93. }
  94. }