sidebar.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  4. import {SectionHeading} from 'sentry/components/charts/styles';
  5. import {KeyValueTable, KeyValueTableRow} from 'sentry/components/keyValueTable';
  6. import TimeSince from 'sentry/components/timeSince';
  7. import {IconChevron} from 'sentry/icons';
  8. import {t, tct} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {IssueAlertRule} from 'sentry/types/alerts';
  11. import type {Actor} from 'sentry/types/core';
  12. import type {Member, Team} from 'sentry/types/organization';
  13. import {useApiQuery} from 'sentry/utils/queryClient';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import {TextAction, TextCondition} from './textRule';
  16. type Props = {
  17. projectSlug: string;
  18. rule: IssueAlertRule;
  19. teams: Team[];
  20. };
  21. function Conditions({rule, teams, projectSlug}: Props) {
  22. const organization = useOrganization();
  23. const {data: memberList} = useApiQuery<Member[]>(
  24. [`/organizations/${organization.slug}/users/`, {query: {projectSlug}}],
  25. {staleTime: 60000}
  26. );
  27. return (
  28. <ConditionsContainer>
  29. <Step>
  30. <StepContainer>
  31. <ChevronContainer>
  32. <IconChevron color="gray200" isCircled direction="right" size="sm" />
  33. </ChevronContainer>
  34. <StepContent>
  35. <StepLead>
  36. {tct('[when:When] an event is captured [selector]', {
  37. when: <Badge />,
  38. selector: rule.conditions.length ? t('and %s...', rule.actionMatch) : '',
  39. })}
  40. </StepLead>
  41. {rule.conditions.map((condition, idx) => (
  42. <ConditionsBadge key={idx}>
  43. <TextCondition condition={condition} />
  44. </ConditionsBadge>
  45. ))}
  46. </StepContent>
  47. </StepContainer>
  48. </Step>
  49. {rule.filters.length ? (
  50. <Step>
  51. <StepContainer>
  52. <ChevronContainer>
  53. <IconChevron color="gray200" isCircled direction="right" size="sm" />
  54. </ChevronContainer>
  55. <StepContent>
  56. <StepLead>
  57. {tct('[if:If] [selector] of these filters match', {
  58. if: <Badge />,
  59. selector: rule.filterMatch,
  60. })}
  61. </StepLead>
  62. {rule.filters.map((filter, idx) => (
  63. <ConditionsBadge key={idx}>
  64. {filter.time ? filter.name + '(s)' : filter.name}
  65. </ConditionsBadge>
  66. ))}
  67. </StepContent>
  68. </StepContainer>
  69. </Step>
  70. ) : null}
  71. <Step>
  72. <StepContainer>
  73. <ChevronContainer>
  74. <IconChevron isCircled color="gray200" direction="right" size="sm" />
  75. </ChevronContainer>
  76. <div>
  77. <StepLead>
  78. {tct('[then:Then] perform these actions', {
  79. then: <Badge />,
  80. })}
  81. </StepLead>
  82. {rule.actions.length ? (
  83. rule.actions.map((action, idx) => {
  84. return (
  85. <ConditionsBadge key={idx}>
  86. <TextAction
  87. action={action}
  88. memberList={memberList ?? []}
  89. teams={teams}
  90. />
  91. </ConditionsBadge>
  92. );
  93. })
  94. ) : (
  95. <ConditionsBadge>{t('Do nothing')}</ConditionsBadge>
  96. )}
  97. </div>
  98. </StepContainer>
  99. </Step>
  100. </ConditionsContainer>
  101. );
  102. }
  103. function Sidebar({rule, teams, projectSlug}: Props) {
  104. const ownerId = rule.owner?.split(':')[1];
  105. const teamActor = ownerId && {type: 'team' as Actor['type'], id: ownerId, name: ''};
  106. return (
  107. <Fragment>
  108. <StatusContainer>
  109. <SectionHeading>{t('Last Triggered')}</SectionHeading>
  110. <Status>
  111. {rule.lastTriggered ? (
  112. <TimeSince date={rule.lastTriggered} />
  113. ) : (
  114. t('No alerts triggered')
  115. )}
  116. </Status>
  117. </StatusContainer>
  118. <SectionHeading>{t('Alert Conditions')}</SectionHeading>
  119. <Conditions rule={rule} teams={teams} projectSlug={projectSlug} />
  120. <SectionHeading>{t('Alert Rule Details')}</SectionHeading>
  121. <KeyValueTable>
  122. <KeyValueTableRow
  123. keyName={t('Environment')}
  124. value={<OverflowTableValue>{rule.environment ?? '-'}</OverflowTableValue>}
  125. />
  126. {rule.dateCreated && (
  127. <KeyValueTableRow
  128. keyName={t('Date created')}
  129. value={<TimeSince date={rule.dateCreated} suffix={t('ago')} />}
  130. />
  131. )}
  132. {rule.createdBy && (
  133. <KeyValueTableRow
  134. keyName={t('Created by')}
  135. value={<OverflowTableValue>{rule.createdBy.name ?? '-'}</OverflowTableValue>}
  136. />
  137. )}
  138. <KeyValueTableRow
  139. keyName={t('Team')}
  140. value={
  141. teamActor ? <ActorAvatar actor={teamActor} size={24} /> : t('Unassigned')
  142. }
  143. />
  144. </KeyValueTable>
  145. </Fragment>
  146. );
  147. }
  148. export default Sidebar;
  149. const Status = styled('div')`
  150. position: relative;
  151. display: grid;
  152. grid-template-columns: auto auto auto;
  153. gap: ${space(0.5)};
  154. font-size: ${p => p.theme.fontSizeLarge};
  155. `;
  156. const StatusContainer = styled('div')`
  157. margin-bottom: ${space(2)};
  158. h4 {
  159. margin-top: 0;
  160. }
  161. `;
  162. const ConditionsContainer = styled('div')`
  163. margin-bottom: ${space(2)};
  164. `;
  165. const Step = styled('div')`
  166. position: relative;
  167. margin-top: ${space(4)};
  168. :first-child {
  169. margin-top: ${space(1)};
  170. }
  171. `;
  172. const StepContainer = styled('div')`
  173. display: flex;
  174. align-items: flex-start;
  175. flex-grow: 1;
  176. `;
  177. const StepContent = styled('div')`
  178. &::before {
  179. content: '';
  180. position: absolute;
  181. height: 100%;
  182. top: 28px;
  183. left: ${space(0.75)};
  184. border-right: 1px ${p => p.theme.gray200} dashed;
  185. }
  186. `;
  187. const StepLead = styled('div')`
  188. margin-bottom: ${space(0.5)};
  189. font-size: ${p => p.theme.fontSizeMedium};
  190. font-weight: ${p => p.theme.fontWeightNormal};
  191. `;
  192. const ChevronContainer = styled('div')`
  193. display: flex;
  194. align-items: center;
  195. padding: ${space(0.5)} ${space(1)} ${space(0.5)} 0;
  196. `;
  197. const Badge = styled('span')`
  198. display: inline-block;
  199. background-color: ${p => p.theme.purple300};
  200. padding: 0 ${space(0.75)};
  201. border-radius: ${p => p.theme.borderRadius};
  202. color: ${p => p.theme.white};
  203. text-transform: uppercase;
  204. text-align: center;
  205. font-size: ${p => p.theme.fontSizeSmall};
  206. font-weight: ${p => p.theme.fontWeightNormal};
  207. line-height: 1.5;
  208. `;
  209. const ConditionsBadge = styled('span')`
  210. display: block;
  211. background-color: ${p => p.theme.surface200};
  212. padding: 0 ${space(0.75)};
  213. border-radius: ${p => p.theme.borderRadius};
  214. color: ${p => p.theme.textColor};
  215. font-size: ${p => p.theme.fontSizeSmall};
  216. margin-bottom: ${space(1)};
  217. width: fit-content;
  218. font-weight: ${p => p.theme.fontWeightNormal};
  219. `;
  220. const OverflowTableValue = styled('div')`
  221. ${p => p.theme.overflowEllipsis}
  222. `;