useCreateDashboard.tsx 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {useMemo} from 'react';
  2. import {openCreateDashboardFromScratchpad} from 'sentry/actionCreators/modal';
  3. import {convertToDashboardWidget} from 'sentry/utils/metrics/dashboard';
  4. import {
  5. isMetricsEquationWidget,
  6. MetricExpressionType,
  7. type MetricsWidget,
  8. } from 'sentry/utils/metrics/types';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import useRouter from 'sentry/utils/useRouter';
  12. import type {Widget} from 'sentry/views/dashboards/types';
  13. import type {useFormulaDependencies} from 'sentry/views/metrics/utils/useFormulaDependencies';
  14. export function useCreateDashboard(
  15. widgets: MetricsWidget[],
  16. formulaDependencies: ReturnType<typeof useFormulaDependencies>,
  17. isMultiChartMode: boolean
  18. ) {
  19. const router = useRouter();
  20. const organization = useOrganization();
  21. const {selection} = usePageFilters();
  22. const dashboardWidgets = useMemo(() => {
  23. if (!isMultiChartMode) {
  24. const queryIdsInArray = new Set<number>();
  25. const widgetsWithDependencies = widgets.reduce<MetricsWidget[]>((acc, widget) => {
  26. if (isMetricsEquationWidget(widget)) {
  27. const {dependencies, isError} = formulaDependencies[widget.id];
  28. if (isError) {
  29. return acc;
  30. }
  31. // Only add dependencies that are not already in the list of widgets
  32. const filteredDependencies: MetricsWidget[] = [];
  33. dependencies.forEach(dependency => {
  34. if (!queryIdsInArray.has(dependency.id)) {
  35. filteredDependencies.push({...dependency, isHidden: true});
  36. queryIdsInArray.add(dependency.id);
  37. }
  38. });
  39. return [...filteredDependencies, ...acc, widget];
  40. }
  41. if (queryIdsInArray.has(widget.id)) {
  42. return acc;
  43. }
  44. queryIdsInArray.add(widget.id);
  45. return [...acc, widget];
  46. }, []);
  47. return [convertToDashboardWidget(widgetsWithDependencies, widgets[0].displayType)];
  48. }
  49. return widgets
  50. .map(widget => {
  51. if (widget.type !== MetricExpressionType.EQUATION) {
  52. return convertToDashboardWidget([widget], widget.displayType);
  53. }
  54. const {dependencies, isError} = formulaDependencies[widget.id];
  55. if (isError) {
  56. return null;
  57. }
  58. return convertToDashboardWidget(
  59. [...dependencies.map(query => ({...query, isHidden: true})), widget],
  60. widget.displayType
  61. );
  62. })
  63. .filter((widget): widget is Widget => widget !== null);
  64. }, [isMultiChartMode, widgets, formulaDependencies]);
  65. return useMemo(() => {
  66. return function () {
  67. const newDashboard = {
  68. title: 'Metrics Dashboard',
  69. description: '',
  70. widgets: dashboardWidgets.slice(0, 30),
  71. projects: selection.projects,
  72. environment: selection.environments,
  73. start: selection.datetime.start as string,
  74. end: selection.datetime.end as string,
  75. period: selection.datetime.period as string,
  76. filters: {},
  77. utc: selection.datetime.utc ?? false,
  78. id: 'ddm-scratchpad',
  79. dateCreated: '',
  80. };
  81. openCreateDashboardFromScratchpad({newDashboard, router, organization});
  82. };
  83. }, [selection, organization, router, dashboardWidgets]);
  84. }