projectLatestAlerts.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import styled from '@emotion/styled';
  2. import {Location} from 'history';
  3. import pick from 'lodash/pick';
  4. import AlertBadge from 'sentry/components/alertBadge';
  5. import AsyncComponent from 'sentry/components/asyncComponent';
  6. import {SectionHeading} from 'sentry/components/charts/styles';
  7. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  8. import Link from 'sentry/components/links/link';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import TimeSince from 'sentry/components/timeSince';
  11. import {URL_PARAM} from 'sentry/constants/pageFilters';
  12. import {IconCheckmark, IconExclamation, IconFire, IconOpen} from 'sentry/icons';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {Organization} from 'sentry/types';
  16. import {Incident, IncidentStatus} from '../alerts/types';
  17. import MissingAlertsButtons from './missingFeatureButtons/missingAlertsButtons';
  18. import {SectionHeadingLink, SectionHeadingWrapper, SidebarSection} from './styles';
  19. import {didProjectOrEnvironmentChange} from './utils';
  20. const PLACEHOLDER_AND_EMPTY_HEIGHT = '172px';
  21. type Props = AsyncComponent['props'] & {
  22. isProjectStabilized: boolean;
  23. location: Location;
  24. organization: Organization;
  25. projectSlug: string;
  26. };
  27. type State = {
  28. resolvedAlerts: Incident[] | null;
  29. unresolvedAlerts: Incident[] | null;
  30. hasAlertRule?: boolean;
  31. } & AsyncComponent['state'];
  32. class ProjectLatestAlerts extends AsyncComponent<Props, State> {
  33. shouldComponentUpdate(nextProps: Props, nextState: State) {
  34. const {location, isProjectStabilized} = this.props;
  35. // TODO(project-detail): we temporarily removed refetching based on timeselector
  36. if (
  37. this.state !== nextState ||
  38. didProjectOrEnvironmentChange(location, nextProps.location) ||
  39. isProjectStabilized !== nextProps.isProjectStabilized
  40. ) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. componentDidUpdate(prevProps: Props) {
  46. const {location, isProjectStabilized} = this.props;
  47. if (
  48. didProjectOrEnvironmentChange(prevProps.location, location) ||
  49. prevProps.isProjectStabilized !== isProjectStabilized
  50. ) {
  51. this.remountComponent();
  52. }
  53. }
  54. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  55. const {location, organization, isProjectStabilized} = this.props;
  56. if (!isProjectStabilized) {
  57. return [];
  58. }
  59. const query = {
  60. ...pick(location.query, Object.values(URL_PARAM)),
  61. per_page: 3,
  62. };
  63. // we are listing 3 alerts total, first unresolved and then we fill with resolved
  64. return [
  65. [
  66. 'unresolvedAlerts',
  67. `/organizations/${organization.slug}/incidents/`,
  68. {query: {...query, status: 'open'}},
  69. ],
  70. [
  71. 'resolvedAlerts',
  72. `/organizations/${organization.slug}/incidents/`,
  73. {query: {...query, status: 'closed'}},
  74. ],
  75. ];
  76. }
  77. /**
  78. * If our alerts are empty, determine if we've configured alert rules (empty message differs then)
  79. */
  80. async onLoadAllEndpointsSuccess() {
  81. const {unresolvedAlerts, resolvedAlerts} = this.state;
  82. const {location, organization, isProjectStabilized} = this.props;
  83. if (!isProjectStabilized) {
  84. return;
  85. }
  86. if ([...(unresolvedAlerts ?? []), ...(resolvedAlerts ?? [])].length !== 0) {
  87. this.setState({hasAlertRule: true});
  88. return;
  89. }
  90. this.setState({loading: true});
  91. const alertRules = await this.api.requestPromise(
  92. `/organizations/${organization.slug}/alert-rules/`,
  93. {
  94. method: 'GET',
  95. query: {
  96. ...pick(location.query, [...Object.values(URL_PARAM)]),
  97. per_page: 1,
  98. },
  99. }
  100. );
  101. this.setState({hasAlertRule: alertRules.length > 0, loading: false});
  102. }
  103. get alertsLink() {
  104. const {organization} = this.props;
  105. // as this is a link to latest alerts, we want to only preserve project and environment
  106. return {
  107. pathname: `/organizations/${organization.slug}/alerts/`,
  108. query: {
  109. statsPeriod: undefined,
  110. start: undefined,
  111. end: undefined,
  112. utc: undefined,
  113. },
  114. };
  115. }
  116. renderAlertRow = (alert: Incident) => {
  117. const {organization} = this.props;
  118. const {status, id, identifier, title, dateClosed, dateStarted} = alert;
  119. const isResolved = status === IncidentStatus.CLOSED;
  120. const isWarning = status === IncidentStatus.WARNING;
  121. const Icon = isResolved ? IconCheckmark : isWarning ? IconExclamation : IconFire;
  122. const statusProps = {isResolved, isWarning};
  123. return (
  124. <AlertRowLink
  125. aria-label={title}
  126. to={`/organizations/${organization.slug}/alerts/${identifier}/`}
  127. key={id}
  128. >
  129. <AlertBadgeWrapper {...statusProps} icon={Icon}>
  130. <AlertBadge status={status} />
  131. </AlertBadgeWrapper>
  132. <AlertDetails>
  133. <AlertTitle>{title}</AlertTitle>
  134. <AlertDate {...statusProps}>
  135. {isResolved
  136. ? tct('Resolved [date]', {
  137. date: dateClosed ? <TimeSince date={dateClosed} /> : null,
  138. })
  139. : tct('Triggered [date]', {
  140. date: (
  141. <TimeSince
  142. date={dateStarted}
  143. tooltipUnderlineColor={getStatusColor(statusProps)}
  144. />
  145. ),
  146. })}
  147. </AlertDate>
  148. </AlertDetails>
  149. </AlertRowLink>
  150. );
  151. };
  152. renderInnerBody() {
  153. const {organization, projectSlug, isProjectStabilized} = this.props;
  154. const {loading, unresolvedAlerts, resolvedAlerts, hasAlertRule} = this.state;
  155. const alertsUnresolvedAndResolved = [
  156. ...(unresolvedAlerts ?? []),
  157. ...(resolvedAlerts ?? []),
  158. ];
  159. const checkingForAlertRules =
  160. alertsUnresolvedAndResolved.length === 0 && hasAlertRule === undefined;
  161. const showLoadingIndicator = loading || checkingForAlertRules || !isProjectStabilized;
  162. if (showLoadingIndicator) {
  163. return <Placeholder height={PLACEHOLDER_AND_EMPTY_HEIGHT} />;
  164. }
  165. if (!hasAlertRule) {
  166. return (
  167. <MissingAlertsButtons organization={organization} projectSlug={projectSlug} />
  168. );
  169. }
  170. if (alertsUnresolvedAndResolved.length === 0) {
  171. return (
  172. <StyledEmptyStateWarning small>{t('No alerts found')}</StyledEmptyStateWarning>
  173. );
  174. }
  175. return alertsUnresolvedAndResolved.slice(0, 3).map(this.renderAlertRow);
  176. }
  177. renderLoading() {
  178. return this.renderBody();
  179. }
  180. renderBody() {
  181. return (
  182. <SidebarSection>
  183. <SectionHeadingWrapper>
  184. <SectionHeading>{t('Latest Alerts')}</SectionHeading>
  185. <SectionHeadingLink to={this.alertsLink}>
  186. <IconOpen />
  187. </SectionHeadingLink>
  188. </SectionHeadingWrapper>
  189. <div>{this.renderInnerBody()}</div>
  190. </SidebarSection>
  191. );
  192. }
  193. }
  194. const AlertRowLink = styled(Link)`
  195. display: flex;
  196. align-items: center;
  197. height: 40px;
  198. margin-bottom: ${space(3)};
  199. margin-left: ${space(0.5)};
  200. &,
  201. &:hover,
  202. &:focus {
  203. color: inherit;
  204. }
  205. &:first-child {
  206. margin-top: ${space(1)};
  207. }
  208. `;
  209. type StatusColorProps = {
  210. isResolved: boolean;
  211. isWarning: boolean;
  212. };
  213. const getStatusColor = ({isResolved, isWarning}: StatusColorProps) =>
  214. isResolved ? 'successText' : isWarning ? 'warningText' : 'errorText';
  215. const AlertBadgeWrapper = styled('div')<{icon: React.ReactNode} & StatusColorProps>`
  216. display: flex;
  217. align-items: center;
  218. justify-content: center;
  219. flex-shrink: 0;
  220. /* icon warning needs to be treated differently to look visually centered */
  221. line-height: ${p => (p.icon === IconExclamation ? undefined : 1)};
  222. `;
  223. const AlertDetails = styled('div')`
  224. font-size: ${p => p.theme.fontSizeMedium};
  225. margin-left: ${space(1.5)};
  226. ${p => p.theme.overflowEllipsis}
  227. line-height: 1.35;
  228. `;
  229. const AlertTitle = styled('div')`
  230. font-weight: 400;
  231. overflow: hidden;
  232. text-overflow: ellipsis;
  233. `;
  234. const AlertDate = styled('span')<StatusColorProps>`
  235. color: ${p => p.theme[getStatusColor(p)]};
  236. `;
  237. const StyledEmptyStateWarning = styled(EmptyStateWarning)`
  238. height: ${PLACEHOLDER_AND_EMPTY_HEIGHT};
  239. justify-content: center;
  240. `;
  241. export default ProjectLatestAlerts;