useCreateDashboard.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {useMemo} from 'react';
  2. import {openCreateDashboardFromScratchpad} from 'sentry/actionCreators/modal';
  3. import {convertToDashboardWidget} from 'sentry/utils/metrics/dashboard';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import useRouter from 'sentry/utils/useRouter';
  7. import {useDDMContext} from 'sentry/views/ddm/context';
  8. export function useCreateDashboard() {
  9. const router = useRouter();
  10. const organization = useOrganization();
  11. const {widgets} = useDDMContext();
  12. const {selection} = usePageFilters();
  13. return useMemo(() => {
  14. return function () {
  15. const newDashboard = {
  16. title: 'Metrics Dashboard',
  17. description: '',
  18. widgets: widgets
  19. .filter(widget => !!widget.mri)
  20. .map(widget =>
  21. // @ts-expect-error TODO(ogi): fix this
  22. convertToDashboardWidget(widget, widget.displayType)
  23. )
  24. // Only import the first 30 widgets because of dashboard widget limit
  25. .slice(0, 30),
  26. projects: selection.projects,
  27. environment: selection.environments,
  28. start: selection.datetime.start as string,
  29. end: selection.datetime.end as string,
  30. period: selection.datetime.period as string,
  31. filters: {},
  32. utc: selection.datetime.utc ?? false,
  33. id: 'ddm-scratchpad',
  34. dateCreated: '',
  35. };
  36. openCreateDashboardFromScratchpad({newDashboard, router, organization});
  37. };
  38. }, [selection, widgets, organization, router]);
  39. }