useCreateDashboard.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 (scratchpad?: {name: string}) {
  15. const newDashboard = {
  16. title: scratchpad?.name || 'DDM 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. projects: selection.projects,
  25. environment: selection.environments,
  26. start: selection.datetime.start as string,
  27. end: selection.datetime.end as string,
  28. period: selection.datetime.period as string,
  29. filters: {},
  30. utc: selection.datetime.utc ?? false,
  31. id: 'ddm-scratchpad',
  32. dateCreated: '',
  33. };
  34. openCreateDashboardFromScratchpad({newDashboard, router, organization});
  35. };
  36. }, [selection, widgets, organization, router]);
  37. }