pageHeaderActions.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {useMemo} from 'react';
  2. import {Button} from 'sentry/components/button';
  3. import ButtonBar from 'sentry/components/buttonBar';
  4. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  5. import {IconBookmark, IconDashboard, IconEllipsis, IconSiren} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {isCustomMeasurement} from 'sentry/utils/metrics';
  8. import {MRIToField} from 'sentry/utils/metrics/mri';
  9. import {middleEllipsis} from 'sentry/utils/middleEllipsis';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import usePageFilters from 'sentry/utils/usePageFilters';
  12. import useRouter from 'sentry/utils/useRouter';
  13. import {useDDMContext} from 'sentry/views/ddm/context';
  14. import {getCreateAlert} from 'sentry/views/ddm/contextMenu';
  15. import {QuerySymbol} from 'sentry/views/ddm/querySymbol';
  16. import {useCreateDashboard} from 'sentry/views/ddm/useCreateDashboard';
  17. interface Props {
  18. addCustomMetric: (referrer: string) => void;
  19. showCustomMetricButton: boolean;
  20. }
  21. export function PageHeaderActions({showCustomMetricButton, addCustomMetric}: Props) {
  22. const router = useRouter();
  23. const organization = useOrganization();
  24. const {selection} = usePageFilters();
  25. const createDashboard = useCreateDashboard();
  26. const {isDefaultQuery, setDefaultQuery, widgets} = useDDMContext();
  27. const items = useMemo(
  28. () => [
  29. {
  30. leadingItems: [<IconDashboard key="icon" />],
  31. key: 'add-dashboard',
  32. label: t('Add to Dashboard'),
  33. onAction: createDashboard,
  34. },
  35. ],
  36. [createDashboard]
  37. );
  38. const alertItems = useMemo(
  39. () =>
  40. widgets.map((widget, index) => {
  41. const createAlert = getCreateAlert(organization, {
  42. datetime: selection.datetime,
  43. projects: selection.projects,
  44. environments: selection.environments,
  45. query: widget.query,
  46. mri: widget.mri,
  47. groupBy: widget.groupBy,
  48. op: widget.op,
  49. });
  50. return {
  51. leadingItems: [<QuerySymbol key="icon" index={index} />],
  52. key: `add-alert-${index}`,
  53. label: widget.mri
  54. ? middleEllipsis(MRIToField(widget.mri, widget.op!), 60, /\.|-|_/)
  55. : t('Select a metric to create an alert'),
  56. tooltip: isCustomMeasurement({mri: widget.mri})
  57. ? t('Custom measurements cannot be used to create alerts')
  58. : undefined,
  59. disabled: !createAlert,
  60. onAction: createAlert,
  61. };
  62. }),
  63. [
  64. organization,
  65. selection.datetime,
  66. selection.environments,
  67. selection.projects,
  68. widgets,
  69. ]
  70. );
  71. return (
  72. <ButtonBar gap={1}>
  73. {showCustomMetricButton && (
  74. <Button priority="primary" onClick={() => addCustomMetric('header')} size="sm">
  75. {t('Add Custom Metric')}
  76. </Button>
  77. )}
  78. <Button
  79. size="sm"
  80. icon={<IconBookmark isSolid={isDefaultQuery} />}
  81. onClick={() => setDefaultQuery(isDefaultQuery ? null : router.location.query)}
  82. >
  83. {isDefaultQuery ? t('Remove Default') : t('Save as default')}
  84. </Button>
  85. <DropdownMenu
  86. items={alertItems}
  87. triggerLabel={t('Create Alert')}
  88. triggerProps={{
  89. size: 'sm',
  90. showChevron: false,
  91. icon: <IconSiren direction="down" size="xs" />,
  92. }}
  93. position="bottom-end"
  94. />
  95. <DropdownMenu
  96. items={items}
  97. triggerProps={{
  98. 'aria-label': t('Page actions'),
  99. size: 'sm',
  100. showChevron: false,
  101. icon: <IconEllipsis direction="down" size="xs" />,
  102. }}
  103. position="bottom-end"
  104. />
  105. </ButtonBar>
  106. );
  107. }