useCreateDashboard.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {useMemo} from 'react';
  2. import {openCreateDashboardFromScratchpad} from 'sentry/actionCreators/modal';
  3. import {convertToDashboardWidget} from 'sentry/utils/metrics/dashboard';
  4. import {MetricQueryType, type MetricQueryWidgetParams} 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 {useDDMContext} from 'sentry/views/ddm/context';
  9. export function useCreateDashboard() {
  10. const router = useRouter();
  11. const organization = useOrganization();
  12. const {widgets, isMultiChartMode} = useDDMContext();
  13. const {selection} = usePageFilters();
  14. const dashboardWidgets = useMemo(() => {
  15. // TODO(aknaus): Remove filtering once dashboard supports metrics formulas
  16. const supportedWidgets = widgets.filter(
  17. widget => widget.type === MetricQueryType.QUERY
  18. ) as MetricQueryWidgetParams[];
  19. if (isMultiChartMode) {
  20. return [convertToDashboardWidget(supportedWidgets, widgets[0].displayType)];
  21. }
  22. return supportedWidgets.map(widget =>
  23. convertToDashboardWidget([widget], widget.displayType)
  24. );
  25. }, [widgets, isMultiChartMode]);
  26. return useMemo(() => {
  27. return function () {
  28. const newDashboard = {
  29. title: 'Metrics Dashboard',
  30. description: '',
  31. widgets: dashboardWidgets.slice(0, 30),
  32. projects: selection.projects,
  33. environment: selection.environments,
  34. start: selection.datetime.start as string,
  35. end: selection.datetime.end as string,
  36. period: selection.datetime.period as string,
  37. filters: {},
  38. utc: selection.datetime.utc ?? false,
  39. id: 'ddm-scratchpad',
  40. dateCreated: '',
  41. };
  42. openCreateDashboardFromScratchpad({newDashboard, router, organization});
  43. };
  44. }, [selection, organization, router, dashboardWidgets]);
  45. }