chartContextMenu.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {DropdownMenu, type MenuItemProps} from 'sentry/components/dropdownMenu';
  2. import {IconEllipsis} from 'sentry/icons';
  3. import {t} from 'sentry/locale';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import useProjects from 'sentry/utils/useProjects';
  7. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  8. import {useAddToDashboard} from 'sentry/views/explore/hooks/useAddToDashboard';
  9. import {getAlertsUrl} from 'sentry/views/insights/common/utils/getAlertsUrl';
  10. function ChartContextMenu({
  11. visualizeIndex,
  12. visualizeYAxes,
  13. query,
  14. interval,
  15. }: {
  16. interval: string;
  17. query: string;
  18. visualizeIndex: number;
  19. visualizeYAxes: string[];
  20. }) {
  21. const {addToDashboard} = useAddToDashboard();
  22. const organization = useOrganization();
  23. const {projects} = useProjects();
  24. const pageFilters = usePageFilters();
  25. const project =
  26. projects.length === 1
  27. ? projects[0]
  28. : projects.find(p => p.id === `${pageFilters.selection.projects[0]}`);
  29. const singleProject =
  30. (pageFilters.selection.projects.length === 1 || projects.length === 1) && project;
  31. const alertsUrls = singleProject
  32. ? visualizeYAxes.map(yAxis => ({
  33. key: yAxis,
  34. label: yAxis,
  35. to: getAlertsUrl({
  36. project,
  37. query,
  38. pageFilters: pageFilters.selection,
  39. aggregate: yAxis,
  40. orgSlug: organization.slug,
  41. dataset: Dataset.EVENTS_ANALYTICS_PLATFORM,
  42. interval,
  43. }),
  44. }))
  45. : undefined;
  46. const items: MenuItemProps[] = [];
  47. if (organization.features.includes('alerts-eap')) {
  48. items.push({
  49. key: 'create-alert',
  50. label: t('Create an alert for'),
  51. children: alertsUrls ?? [],
  52. tooltip: !singleProject
  53. ? t('Cannot create an alert when multiple projects are selected')
  54. : undefined,
  55. disabled: !alertsUrls || alertsUrls.length === 0 || !singleProject,
  56. isSubmenu: true,
  57. });
  58. }
  59. if (organization.features.includes('dashboards-eap')) {
  60. items.push({
  61. key: 'add-to-dashboard',
  62. label: t('Add to Dashboard'),
  63. onAction: () => addToDashboard(visualizeIndex),
  64. });
  65. }
  66. if (items.length === 0) {
  67. return null;
  68. }
  69. return (
  70. <DropdownMenu
  71. triggerProps={{
  72. size: 'sm',
  73. borderless: true,
  74. showChevron: false,
  75. icon: <IconEllipsis />,
  76. }}
  77. position="bottom-end"
  78. items={items}
  79. />
  80. );
  81. }
  82. export default ChartContextMenu;