row.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import Access from 'sentry/components/acl/access';
  4. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  5. import TeamAvatar from 'sentry/components/avatar/teamAvatar';
  6. import Tag from 'sentry/components/badge/tag';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import DropdownAutoComplete from 'sentry/components/dropdownAutoComplete';
  9. import type {ItemsBeforeFilter} from 'sentry/components/dropdownAutoComplete/types';
  10. import DropdownBubble from 'sentry/components/dropdownBubble';
  11. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  12. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  13. import ErrorBoundary from 'sentry/components/errorBoundary';
  14. import IdBadge from 'sentry/components/idBadge';
  15. import ExternalLink from 'sentry/components/links/externalLink';
  16. import Link from 'sentry/components/links/link';
  17. import LoadingIndicator from 'sentry/components/loadingIndicator';
  18. import TextOverflow from 'sentry/components/textOverflow';
  19. import {Tooltip} from 'sentry/components/tooltip';
  20. import {IconChevron, IconEllipsis, IconUser} from 'sentry/icons';
  21. import {t, tct} from 'sentry/locale';
  22. import {space} from 'sentry/styles/space';
  23. import {MonitorType} from 'sentry/types/alerts';
  24. import type {Actor} from 'sentry/types/core';
  25. import type {Project} from 'sentry/types/project';
  26. import {useUserTeams} from 'sentry/utils/useUserTeams';
  27. import ActivatedMetricAlertRuleStatus from 'sentry/views/alerts/list/rules/activatedMetricAlertRuleStatus';
  28. import AlertLastIncidentActivationInfo from 'sentry/views/alerts/list/rules/alertLastIncidentActivationInfo';
  29. import AlertRuleStatus from 'sentry/views/alerts/list/rules/alertRuleStatus';
  30. import CombinedAlertBadge from 'sentry/views/alerts/list/rules/combinedAlertBadge';
  31. import {getActor} from 'sentry/views/alerts/list/rules/utils';
  32. import {
  33. UptimeMonitorMode,
  34. UptimeMonitorStatus,
  35. } from 'sentry/views/alerts/rules/uptime/types';
  36. import type {CombinedAlerts} from '../../types';
  37. import {CombinedAlertType} from '../../types';
  38. import {isIssueAlert} from '../../utils';
  39. type Props = {
  40. hasEditAccess: boolean;
  41. onDelete: (projectId: string, rule: CombinedAlerts) => void;
  42. onOwnerChange: (projectId: string, rule: CombinedAlerts, ownerValue: string) => void;
  43. orgId: string;
  44. projects: Project[];
  45. projectsLoaded: boolean;
  46. rule: CombinedAlerts;
  47. };
  48. function RuleListRow({
  49. rule,
  50. projectsLoaded,
  51. projects,
  52. orgId,
  53. onDelete,
  54. onOwnerChange,
  55. hasEditAccess,
  56. }: Props) {
  57. const {teams: userTeams} = useUserTeams();
  58. const [assignee, setAssignee] = useState<string>('');
  59. const isActivatedAlertRule =
  60. rule.type === CombinedAlertType.METRIC && rule.monitorType === MonitorType.ACTIVATED;
  61. const isUptime = rule.type === CombinedAlertType.UPTIME;
  62. const slug = isUptime ? rule.projectSlug : rule.projects[0];
  63. const editKey = {
  64. [CombinedAlertType.ISSUE]: 'rules',
  65. [CombinedAlertType.METRIC]: 'metric-rules',
  66. [CombinedAlertType.UPTIME]: 'uptime-rules',
  67. } satisfies Record<CombinedAlertType, string>;
  68. const editLink = `/organizations/${orgId}/alerts/${editKey[rule.type]}/${slug}/${rule.id}/`;
  69. const mutateKey = {
  70. [CombinedAlertType.ISSUE]: 'issue',
  71. [CombinedAlertType.METRIC]: 'metric',
  72. [CombinedAlertType.UPTIME]: 'uptime',
  73. } satisfies Record<CombinedAlertType, string>;
  74. const duplicateLink = {
  75. pathname: `/organizations/${orgId}/alerts/new/${mutateKey[rule.type]}/`,
  76. query: {
  77. project: slug,
  78. duplicateRuleId: rule.id,
  79. createFromDuplicate: true,
  80. referrer: 'alert_stream',
  81. },
  82. };
  83. const ownerActor = getActor(rule);
  84. const canEdit = ownerActor?.id
  85. ? userTeams.some(team => team.id === ownerActor.id)
  86. : true;
  87. const activeActions = {
  88. [CombinedAlertType.ISSUE]: ['edit', 'duplicate', 'delete'],
  89. [CombinedAlertType.METRIC]: ['edit', 'duplicate', 'delete'],
  90. [CombinedAlertType.UPTIME]: ['edit', 'delete'],
  91. };
  92. const actions: MenuItemProps[] = [
  93. {
  94. key: 'edit',
  95. label: t('Edit'),
  96. to: editLink,
  97. hidden: !activeActions[rule.type].includes('edit'),
  98. },
  99. {
  100. key: 'duplicate',
  101. label: t('Duplicate'),
  102. to: duplicateLink,
  103. hidden: !activeActions[rule.type].includes('duplicate'),
  104. },
  105. {
  106. key: 'delete',
  107. label: t('Delete'),
  108. hidden: !activeActions[rule.type].includes('delete'),
  109. priority: 'danger',
  110. onAction: () => {
  111. openConfirmModal({
  112. onConfirm: () => onDelete(slug, rule),
  113. header: <h5>{t('Delete Alert Rule?')}</h5>,
  114. message: t(
  115. 'Are you sure you want to delete "%s"? You won\'t be able to view the history of this alert once it\'s deleted.',
  116. rule.name
  117. ),
  118. confirmText: t('Delete Rule'),
  119. priority: 'danger',
  120. });
  121. },
  122. },
  123. ];
  124. function handleOwnerChange({value}: {value: string}) {
  125. const ownerValue = value && `team:${value}`;
  126. setAssignee(ownerValue);
  127. onOwnerChange(slug, rule, ownerValue);
  128. }
  129. const unassignedOption: ItemsBeforeFilter[number] = {
  130. value: '',
  131. label: (
  132. <MenuItemWrapper>
  133. <PaddedIconUser size="lg" />
  134. <Label>{t('Unassigned')}</Label>
  135. </MenuItemWrapper>
  136. ),
  137. searchKey: 'unassigned',
  138. actor: '',
  139. disabled: false,
  140. };
  141. const project = projects.find(p => p.slug === slug);
  142. const filteredProjectTeams = (project?.teams ?? []).filter(projTeam => {
  143. return userTeams.some(team => team.id === projTeam.id);
  144. });
  145. const dropdownTeams = filteredProjectTeams
  146. .map<ItemsBeforeFilter[number]>((team, idx) => ({
  147. value: team.id,
  148. searchKey: team.slug,
  149. label: (
  150. <MenuItemWrapper data-test-id="assignee-option" key={idx}>
  151. <IconContainer>
  152. <TeamAvatar team={team} size={24} />
  153. </IconContainer>
  154. <Label>#{team.slug}</Label>
  155. </MenuItemWrapper>
  156. ),
  157. }))
  158. .concat(unassignedOption);
  159. const teamId = assignee?.split(':')[1];
  160. const teamName = filteredProjectTeams.find(team => team.id === teamId);
  161. const assigneeTeamActor = assignee && {
  162. type: 'team' as Actor['type'],
  163. id: teamId,
  164. name: '',
  165. };
  166. const avatarElement = assigneeTeamActor ? (
  167. <ActorAvatar
  168. actor={assigneeTeamActor}
  169. className="avatar"
  170. size={24}
  171. tooltipOptions={{overlayStyle: {textAlign: 'left'}}}
  172. tooltip={tct('Assigned to [name]', {name: teamName && `#${teamName.name}`})}
  173. />
  174. ) : (
  175. <Tooltip isHoverable skipWrapper title={t('Unassigned')}>
  176. <PaddedIconUser size="lg" color="gray400" />
  177. </Tooltip>
  178. );
  179. const hasUptimeAutoconfigureBadge =
  180. rule.type === CombinedAlertType.UPTIME &&
  181. [UptimeMonitorMode.AUTO_DETECTED_ACTIVE, UptimeMonitorMode.MANUAL].includes(
  182. rule.mode
  183. );
  184. const titleBadge = hasUptimeAutoconfigureBadge ? (
  185. <Tag
  186. type="info"
  187. tooltipProps={{isHoverable: true}}
  188. tooltipText={tct(
  189. 'This Uptime Monitoring alert was auto-detected. [learnMore: Learn more].',
  190. {
  191. learnMore: (
  192. <ExternalLink href="https://docs.sentry.io/product/alerts/uptime-monitoring/" />
  193. ),
  194. }
  195. )}
  196. >
  197. {t('Auto Detected')}
  198. </Tag>
  199. ) : null;
  200. return (
  201. <ErrorBoundary>
  202. <AlertNameWrapper isIssueAlert={isIssueAlert(rule)}>
  203. <AlertNameAndStatus>
  204. <AlertName>
  205. <Link
  206. to={
  207. rule.type === CombinedAlertType.ISSUE
  208. ? `/organizations/${orgId}/alerts/rules/${rule.projects[0]}/${rule.id}/details/`
  209. : rule.type === CombinedAlertType.METRIC
  210. ? `/organizations/${orgId}/alerts/rules/details/${rule.id}/`
  211. : `/organizations/${orgId}/alerts/rules/uptime/${rule.projectSlug}/${rule.id}/details/`
  212. }
  213. >
  214. {rule.name} {titleBadge}
  215. </Link>
  216. </AlertName>
  217. <AlertIncidentDate>
  218. <AlertLastIncidentActivationInfo rule={rule} />
  219. </AlertIncidentDate>
  220. </AlertNameAndStatus>
  221. </AlertNameWrapper>
  222. <FlexCenter>
  223. <FlexCenter>
  224. <CombinedAlertBadge rule={rule} />
  225. </FlexCenter>
  226. <MarginLeft>
  227. {isActivatedAlertRule ? (
  228. <ActivatedMetricAlertRuleStatus rule={rule} />
  229. ) : isUptime ? (
  230. rule.status === UptimeMonitorStatus.FAILED ? (
  231. t('Down')
  232. ) : (
  233. t('Up')
  234. )
  235. ) : (
  236. <AlertRuleStatus rule={rule} />
  237. )}
  238. </MarginLeft>
  239. </FlexCenter>
  240. <FlexCenter>
  241. <ProjectBadgeContainer>
  242. <ProjectBadge
  243. avatarSize={18}
  244. project={projectsLoaded && project ? project : {slug}}
  245. />
  246. </ProjectBadgeContainer>
  247. </FlexCenter>
  248. <FlexCenter>
  249. {ownerActor ? (
  250. <ActorAvatar actor={ownerActor} size={24} />
  251. ) : (
  252. <AssigneeWrapper>
  253. {!projectsLoaded && <StyledLoadingIndicator mini />}
  254. {projectsLoaded && (
  255. <DropdownAutoComplete
  256. data-test-id="alert-row-assignee"
  257. maxHeight={400}
  258. onOpen={e => {
  259. e?.stopPropagation();
  260. }}
  261. items={dropdownTeams}
  262. alignMenu="right"
  263. onSelect={handleOwnerChange}
  264. itemSize="small"
  265. searchPlaceholder={t('Filter teams')}
  266. disableLabelPadding
  267. emptyHidesInput
  268. disabled={!hasEditAccess}
  269. >
  270. {({getActorProps, isOpen}) => (
  271. <DropdownButton {...getActorProps({})}>
  272. {avatarElement}
  273. {hasEditAccess && (
  274. <StyledChevron direction={isOpen ? 'up' : 'down'} size="xs" />
  275. )}
  276. </DropdownButton>
  277. )}
  278. </DropdownAutoComplete>
  279. )}
  280. </AssigneeWrapper>
  281. )}
  282. </FlexCenter>
  283. <ActionsColumn>
  284. <Access access={['alerts:write']}>
  285. {({hasAccess}) => (
  286. <DropdownMenu
  287. items={actions}
  288. position="bottom-end"
  289. triggerProps={{
  290. 'aria-label': t('Actions'),
  291. size: 'xs',
  292. icon: <IconEllipsis />,
  293. showChevron: false,
  294. }}
  295. disabledKeys={hasAccess && canEdit ? [] : ['delete']}
  296. />
  297. )}
  298. </Access>
  299. </ActionsColumn>
  300. </ErrorBoundary>
  301. );
  302. }
  303. // TODO: see static/app/components/profiling/flex.tsx and utilize the FlexContainer styled component
  304. const FlexCenter = styled('div')`
  305. display: flex;
  306. align-items: center;
  307. `;
  308. const AlertNameWrapper = styled('div')<{isIssueAlert?: boolean}>`
  309. ${p => p.theme.overflowEllipsis}
  310. display: flex;
  311. align-items: center;
  312. gap: ${space(2)};
  313. ${p => p.isIssueAlert && `padding: ${space(3)} ${space(2)}; line-height: 2.4;`}
  314. `;
  315. const AlertNameAndStatus = styled('div')`
  316. ${p => p.theme.overflowEllipsis}
  317. line-height: 1.35;
  318. `;
  319. const AlertName = styled('div')`
  320. ${p => p.theme.overflowEllipsis}
  321. font-size: ${p => p.theme.fontSizeLarge};
  322. `;
  323. const AlertIncidentDate = styled('div')`
  324. color: ${p => p.theme.gray300};
  325. `;
  326. const ProjectBadgeContainer = styled('div')`
  327. width: 100%;
  328. `;
  329. const ProjectBadge = styled(IdBadge)`
  330. flex-shrink: 0;
  331. `;
  332. const ActionsColumn = styled('div')`
  333. display: flex;
  334. align-items: center;
  335. justify-content: center;
  336. padding: ${space(1)};
  337. `;
  338. const AssigneeWrapper = styled('div')`
  339. display: flex;
  340. justify-content: flex-end;
  341. /* manually align menu underneath dropdown caret */
  342. ${DropdownBubble} {
  343. right: -14px;
  344. }
  345. `;
  346. const DropdownButton = styled('div')`
  347. display: flex;
  348. align-items: center;
  349. font-size: 20px;
  350. `;
  351. const StyledChevron = styled(IconChevron)`
  352. margin-left: ${space(1)};
  353. `;
  354. const PaddedIconUser = styled(IconUser)`
  355. padding: ${space(0.25)};
  356. `;
  357. const IconContainer = styled('div')`
  358. display: flex;
  359. align-items: center;
  360. justify-content: center;
  361. width: ${p => p.theme.iconSizes.lg};
  362. height: ${p => p.theme.iconSizes.lg};
  363. flex-shrink: 0;
  364. `;
  365. const MenuItemWrapper = styled('div')`
  366. display: flex;
  367. align-items: center;
  368. font-size: ${p => p.theme.fontSizeSmall};
  369. `;
  370. const Label = styled(TextOverflow)`
  371. margin-left: ${space(0.75)};
  372. `;
  373. const MarginLeft = styled('div')`
  374. margin-left: ${space(1)};
  375. `;
  376. const StyledLoadingIndicator = styled(LoadingIndicator)`
  377. height: 24px;
  378. margin: 0;
  379. margin-right: ${space(1.5)};
  380. `;
  381. export default RuleListRow;