useCreateDashboard.tsx 3.3 KB

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