header.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import isPropValid from '@emotion/is-prop-valid';
  2. import styled from '@emotion/styled';
  3. import Access from 'sentry/components/acl/access';
  4. import SnoozeAlert from 'sentry/components/alerts/snoozeAlert';
  5. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  6. import {Button} from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import IdBadge from 'sentry/components/idBadge';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import {IconCopy, IconEdit} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import type {Organization, Project} from 'sentry/types';
  13. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  14. import {getAlertRuleActionCategory} from 'sentry/views/alerts/rules/utils';
  15. import {isIssueAlert} from '../../../utils';
  16. type Props = {
  17. hasMetricRuleDetailsError: boolean;
  18. onSnooze: (nextState: {
  19. snooze: boolean;
  20. snoozeCreatedBy?: string;
  21. snoozeForEveryone?: boolean;
  22. }) => void;
  23. organization: Organization;
  24. project?: Project;
  25. rule?: MetricRule;
  26. };
  27. function DetailsHeader({
  28. hasMetricRuleDetailsError,
  29. rule,
  30. organization,
  31. project,
  32. onSnooze,
  33. }: Props) {
  34. const isRuleReady = !!rule && !hasMetricRuleDetailsError;
  35. const ruleTitle = rule && !hasMetricRuleDetailsError ? rule.name : '';
  36. const settingsLink =
  37. rule &&
  38. `/organizations/${organization.slug}/alerts/${
  39. isIssueAlert(rule) ? 'rules' : 'metric-rules'
  40. }/${project?.slug ?? rule?.projects?.[0]}/${rule.id}/`;
  41. const duplicateLink = {
  42. pathname: `/organizations/${organization.slug}/alerts/new/metric/`,
  43. query: {
  44. project: project?.slug,
  45. duplicateRuleId: rule?.id,
  46. createFromDuplicate: true,
  47. referrer: 'metric_rule_details',
  48. },
  49. };
  50. const isSnoozed = rule?.snooze ?? false;
  51. return (
  52. <Layout.Header>
  53. <Layout.HeaderContent>
  54. <Breadcrumbs
  55. crumbs={[
  56. {label: t('Alerts'), to: `/organizations/${organization.slug}/alerts/rules/`},
  57. {label: ruleTitle},
  58. ]}
  59. />
  60. <RuleTitle data-test-id="incident-rule-title" loading={!isRuleReady}>
  61. {project && (
  62. <IdBadge
  63. project={project}
  64. avatarSize={28}
  65. hideName
  66. avatarProps={{hasTooltip: true, tooltip: project.slug}}
  67. />
  68. )}
  69. {ruleTitle}
  70. </RuleTitle>
  71. </Layout.HeaderContent>
  72. <Layout.HeaderActions>
  73. <ButtonBar gap={1}>
  74. {rule && project && (
  75. <Access access={['alerts:write']}>
  76. {({hasAccess}) => (
  77. <SnoozeAlert
  78. isSnoozed={isSnoozed}
  79. onSnooze={onSnooze}
  80. ruleId={rule.id}
  81. projectSlug={project.slug}
  82. ruleActionCategory={getAlertRuleActionCategory(rule)}
  83. hasAccess={hasAccess}
  84. type="metric"
  85. />
  86. )}
  87. </Access>
  88. )}
  89. <Button size="sm" icon={<IconCopy />} to={duplicateLink}>
  90. {t('Duplicate')}
  91. </Button>
  92. <Button size="sm" icon={<IconEdit />} to={settingsLink}>
  93. {t('Edit Rule')}
  94. </Button>
  95. </ButtonBar>
  96. </Layout.HeaderActions>
  97. </Layout.Header>
  98. );
  99. }
  100. export default DetailsHeader;
  101. const RuleTitle = styled(Layout.Title, {
  102. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'loading',
  103. })<{loading: boolean}>`
  104. ${p => p.loading && 'opacity: 0'};
  105. `;