projectLatestAlerts.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. to={`/organizations/${organization.slug}/alerts/${identifier}/`}
  126. key={id}
  127. >
  128. <AlertBadgeWrapper {...statusProps} icon={Icon}>
  129. <AlertBadge status={status} hideText />
  130. </AlertBadgeWrapper>
  131. <AlertDetails>
  132. <AlertTitle>{title}</AlertTitle>
  133. <AlertDate {...statusProps}>
  134. {isResolved
  135. ? tct('Resolved [date]', {
  136. date: dateClosed ? <TimeSince date={dateClosed} /> : null,
  137. })
  138. : tct('Triggered [date]', {
  139. date: (
  140. <TimeSince
  141. date={dateStarted}
  142. tooltipUnderlineColor={getStatusColor(statusProps)}
  143. />
  144. ),
  145. })}
  146. </AlertDate>
  147. </AlertDetails>
  148. </AlertRowLink>
  149. );
  150. };
  151. renderInnerBody() {
  152. const {organization, projectSlug, isProjectStabilized} = this.props;
  153. const {loading, unresolvedAlerts, resolvedAlerts, hasAlertRule} = this.state;
  154. const alertsUnresolvedAndResolved = [
  155. ...(unresolvedAlerts ?? []),
  156. ...(resolvedAlerts ?? []),
  157. ];
  158. const checkingForAlertRules =
  159. alertsUnresolvedAndResolved.length === 0 && hasAlertRule === undefined;
  160. const showLoadingIndicator = loading || checkingForAlertRules || !isProjectStabilized;
  161. if (showLoadingIndicator) {
  162. return <Placeholder height={PLACEHOLDER_AND_EMPTY_HEIGHT} />;
  163. }
  164. if (!hasAlertRule) {
  165. return (
  166. <MissingAlertsButtons organization={organization} projectSlug={projectSlug} />
  167. );
  168. }
  169. if (alertsUnresolvedAndResolved.length === 0) {
  170. return (
  171. <StyledEmptyStateWarning small>{t('No alerts found')}</StyledEmptyStateWarning>
  172. );
  173. }
  174. return alertsUnresolvedAndResolved.slice(0, 3).map(this.renderAlertRow);
  175. }
  176. renderLoading() {
  177. return this.renderBody();
  178. }
  179. renderBody() {
  180. return (
  181. <SidebarSection>
  182. <SectionHeadingWrapper>
  183. <SectionHeading>{t('Latest Alerts')}</SectionHeading>
  184. <SectionHeadingLink to={this.alertsLink}>
  185. <IconOpen />
  186. </SectionHeadingLink>
  187. </SectionHeadingWrapper>
  188. <div>{this.renderInnerBody()}</div>
  189. </SidebarSection>
  190. );
  191. }
  192. }
  193. const AlertRowLink = styled(Link)`
  194. display: flex;
  195. align-items: center;
  196. height: 40px;
  197. margin-bottom: ${space(3)};
  198. margin-left: ${space(0.5)};
  199. &,
  200. &:hover,
  201. &:focus {
  202. color: inherit;
  203. }
  204. &:first-child {
  205. margin-top: ${space(1)};
  206. }
  207. `;
  208. type StatusColorProps = {
  209. isResolved: boolean;
  210. isWarning: boolean;
  211. };
  212. const getStatusColor = ({isResolved, isWarning}: StatusColorProps) =>
  213. isResolved ? 'green300' : isWarning ? 'yellow300' : 'red300';
  214. const AlertBadgeWrapper = styled('div')<{icon: React.ReactNode} & StatusColorProps>`
  215. display: flex;
  216. align-items: center;
  217. justify-content: center;
  218. flex-shrink: 0;
  219. /* icon warning needs to be treated differently to look visually centered */
  220. line-height: ${p => (p.icon === IconExclamation ? undefined : 1)};
  221. `;
  222. const AlertDetails = styled('div')`
  223. font-size: ${p => p.theme.fontSizeMedium};
  224. margin-left: ${space(1.5)};
  225. ${p => p.theme.overflowEllipsis}
  226. line-height: 1.35;
  227. `;
  228. const AlertTitle = styled('div')`
  229. font-weight: 400;
  230. overflow: hidden;
  231. text-overflow: ellipsis;
  232. `;
  233. const AlertDate = styled('span')<StatusColorProps>`
  234. color: ${p => p.theme[getStatusColor(p)]};
  235. `;
  236. const StyledEmptyStateWarning = styled(EmptyStateWarning)`
  237. height: ${PLACEHOLDER_AND_EMPTY_HEIGHT};
  238. justify-content: center;
  239. `;
  240. export default ProjectLatestAlerts;