useCreateDashboard.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {useMemo} from 'react';
  2. import {openCreateDashboardFromScratchpad} from 'sentry/actionCreators/modal';
  3. import {convertToDashboardWidget} from 'sentry/utils/metrics/dashboard';
  4. import {MetricQueryType} 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} = useDDMContext();
  13. const {selection} = usePageFilters();
  14. return useMemo(() => {
  15. return function () {
  16. const newDashboard = {
  17. title: 'Metrics Dashboard',
  18. description: '',
  19. widgets: widgets
  20. // TODO(aknaus): Remove filtering once dashboard supports metrics formulas
  21. .filter(widget => widget.type === MetricQueryType.QUERY && !!widget.mri)
  22. .map(widget =>
  23. // @ts-expect-error TODO(ogi): fix this
  24. convertToDashboardWidget(widget, widget.displayType)
  25. )
  26. // Only import the first 30 widgets because of dashboard widget limit
  27. .slice(0, 30),
  28. projects: selection.projects,
  29. environment: selection.environments,
  30. start: selection.datetime.start as string,
  31. end: selection.datetime.end as string,
  32. period: selection.datetime.period as string,
  33. filters: {},
  34. utc: selection.datetime.utc ?? false,
  35. id: 'ddm-scratchpad',
  36. dateCreated: '',
  37. };
  38. openCreateDashboardFromScratchpad({newDashboard, router, organization});
  39. };
  40. }, [selection, widgets, organization, router]);
  41. }