row.tsx 11 KB

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