row.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. rule.mode === UptimeMonitorMode.AUTO_DETECTED_ACTIVE;
  182. const titleBadge = hasUptimeAutoconfigureBadge ? (
  183. <Tag
  184. type="info"
  185. tooltipProps={{isHoverable: true}}
  186. tooltipText={tct(
  187. 'This Uptime Monitoring alert was auto-detected. [learnMore: Learn more].',
  188. {
  189. learnMore: (
  190. <ExternalLink href="https://docs.sentry.io/product/alerts/uptime-monitoring/" />
  191. ),
  192. }
  193. )}
  194. >
  195. {t('Auto Detected')}
  196. </Tag>
  197. ) : null;
  198. return (
  199. <ErrorBoundary>
  200. <AlertNameWrapper isIssueAlert={isIssueAlert(rule)}>
  201. <AlertNameAndStatus>
  202. <AlertName>
  203. <Link
  204. to={
  205. rule.type === CombinedAlertType.ISSUE
  206. ? `/organizations/${orgId}/alerts/rules/${rule.projects[0]}/${rule.id}/details/`
  207. : rule.type === CombinedAlertType.METRIC
  208. ? `/organizations/${orgId}/alerts/rules/details/${rule.id}/`
  209. : `/organizations/${orgId}/alerts/rules/uptime/${rule.projectSlug}/${rule.id}/details/`
  210. }
  211. >
  212. {rule.name} {titleBadge}
  213. </Link>
  214. </AlertName>
  215. <AlertIncidentDate>
  216. <AlertLastIncidentActivationInfo rule={rule} />
  217. </AlertIncidentDate>
  218. </AlertNameAndStatus>
  219. </AlertNameWrapper>
  220. <FlexCenter>
  221. <FlexCenter>
  222. <CombinedAlertBadge rule={rule} />
  223. </FlexCenter>
  224. <MarginLeft>
  225. {isActivatedAlertRule ? (
  226. <ActivatedMetricAlertRuleStatus rule={rule} />
  227. ) : isUptime ? (
  228. rule.status === UptimeMonitorStatus.FAILED ? (
  229. t('Down')
  230. ) : (
  231. t('Up')
  232. )
  233. ) : (
  234. <AlertRuleStatus rule={rule} />
  235. )}
  236. </MarginLeft>
  237. </FlexCenter>
  238. <FlexCenter>
  239. <ProjectBadgeContainer>
  240. <ProjectBadge
  241. avatarSize={18}
  242. project={projectsLoaded && project ? project : {slug}}
  243. />
  244. </ProjectBadgeContainer>
  245. </FlexCenter>
  246. <FlexCenter>
  247. {ownerActor ? (
  248. <ActorAvatar actor={ownerActor} size={24} />
  249. ) : (
  250. <AssigneeWrapper>
  251. {!projectsLoaded && <StyledLoadingIndicator mini />}
  252. {projectsLoaded && (
  253. <DropdownAutoComplete
  254. data-test-id="alert-row-assignee"
  255. maxHeight={400}
  256. onOpen={e => {
  257. e?.stopPropagation();
  258. }}
  259. items={dropdownTeams}
  260. alignMenu="right"
  261. onSelect={handleOwnerChange}
  262. itemSize="small"
  263. searchPlaceholder={t('Filter teams')}
  264. disableLabelPadding
  265. emptyHidesInput
  266. disabled={!hasEditAccess}
  267. >
  268. {({getActorProps, isOpen}) => (
  269. <DropdownButton {...getActorProps({})}>
  270. {avatarElement}
  271. {hasEditAccess && (
  272. <StyledChevron direction={isOpen ? 'up' : 'down'} size="xs" />
  273. )}
  274. </DropdownButton>
  275. )}
  276. </DropdownAutoComplete>
  277. )}
  278. </AssigneeWrapper>
  279. )}
  280. </FlexCenter>
  281. <ActionsColumn>
  282. <Access access={['alerts:write']}>
  283. {({hasAccess}) => (
  284. <DropdownMenu
  285. items={actions}
  286. position="bottom-end"
  287. triggerProps={{
  288. 'aria-label': t('Actions'),
  289. size: 'xs',
  290. icon: <IconEllipsis />,
  291. showChevron: false,
  292. }}
  293. disabledKeys={hasAccess && canEdit ? [] : ['delete']}
  294. />
  295. )}
  296. </Access>
  297. </ActionsColumn>
  298. </ErrorBoundary>
  299. );
  300. }
  301. // TODO: see static/app/components/profiling/flex.tsx and utilize the FlexContainer styled component
  302. const FlexCenter = styled('div')`
  303. display: flex;
  304. align-items: center;
  305. `;
  306. const AlertNameWrapper = styled('div')<{isIssueAlert?: boolean}>`
  307. ${p => p.theme.overflowEllipsis}
  308. display: flex;
  309. align-items: center;
  310. gap: ${space(2)};
  311. ${p => p.isIssueAlert && `padding: ${space(3)} ${space(2)}; line-height: 2.4;`}
  312. `;
  313. const AlertNameAndStatus = styled('div')`
  314. ${p => p.theme.overflowEllipsis}
  315. line-height: 1.35;
  316. `;
  317. const AlertName = styled('div')`
  318. ${p => p.theme.overflowEllipsis}
  319. font-size: ${p => p.theme.fontSizeLarge};
  320. `;
  321. const AlertIncidentDate = styled('div')`
  322. color: ${p => p.theme.gray300};
  323. `;
  324. const ProjectBadgeContainer = styled('div')`
  325. width: 100%;
  326. `;
  327. const ProjectBadge = styled(IdBadge)`
  328. flex-shrink: 0;
  329. `;
  330. const ActionsColumn = styled('div')`
  331. display: flex;
  332. align-items: center;
  333. justify-content: center;
  334. padding: ${space(1)};
  335. `;
  336. const AssigneeWrapper = styled('div')`
  337. display: flex;
  338. justify-content: flex-end;
  339. /* manually align menu underneath dropdown caret */
  340. ${DropdownBubble} {
  341. right: -14px;
  342. }
  343. `;
  344. const DropdownButton = styled('div')`
  345. display: flex;
  346. align-items: center;
  347. font-size: 20px;
  348. `;
  349. const StyledChevron = styled(IconChevron)`
  350. margin-left: ${space(1)};
  351. `;
  352. const PaddedIconUser = styled(IconUser)`
  353. padding: ${space(0.25)};
  354. `;
  355. const IconContainer = styled('div')`
  356. display: flex;
  357. align-items: center;
  358. justify-content: center;
  359. width: ${p => p.theme.iconSizes.lg};
  360. height: ${p => p.theme.iconSizes.lg};
  361. flex-shrink: 0;
  362. `;
  363. const MenuItemWrapper = styled('div')`
  364. display: flex;
  365. align-items: center;
  366. font-size: ${p => p.theme.fontSizeSmall};
  367. `;
  368. const Label = styled(TextOverflow)`
  369. margin-left: ${space(0.75)};
  370. `;
  371. const MarginLeft = styled('div')`
  372. margin-left: ${space(1)};
  373. `;
  374. const StyledLoadingIndicator = styled(LoadingIndicator)`
  375. height: 24px;
  376. margin: 0;
  377. margin-right: ${space(1.5)};
  378. `;
  379. export default RuleListRow;