projectLatestAlerts.tsx 8.1 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 overflowEllipsis from 'sentry/styles/overflowEllipsis';
  15. import space from 'sentry/styles/space';
  16. import {Organization} from 'sentry/types';
  17. import {Incident, IncidentStatus} from '../alerts/types';
  18. import MissingAlertsButtons from './missingFeatureButtons/missingAlertsButtons';
  19. import {SectionHeadingLink, SectionHeadingWrapper, SidebarSection} from './styles';
  20. import {didProjectOrEnvironmentChange} from './utils';
  21. const PLACEHOLDER_AND_EMPTY_HEIGHT = '172px';
  22. type Props = AsyncComponent['props'] & {
  23. isProjectStabilized: boolean;
  24. location: Location;
  25. organization: Organization;
  26. projectSlug: string;
  27. };
  28. type State = {
  29. resolvedAlerts: Incident[] | null;
  30. unresolvedAlerts: Incident[] | null;
  31. hasAlertRule?: boolean;
  32. } & AsyncComponent['state'];
  33. class ProjectLatestAlerts extends AsyncComponent<Props, State> {
  34. shouldComponentUpdate(nextProps: Props, nextState: State) {
  35. const {location, isProjectStabilized} = this.props;
  36. // TODO(project-detail): we temporarily removed refetching based on timeselector
  37. if (
  38. this.state !== nextState ||
  39. didProjectOrEnvironmentChange(location, nextProps.location) ||
  40. isProjectStabilized !== nextProps.isProjectStabilized
  41. ) {
  42. return true;
  43. }
  44. return false;
  45. }
  46. componentDidUpdate(prevProps: Props) {
  47. const {location, isProjectStabilized} = this.props;
  48. if (
  49. didProjectOrEnvironmentChange(prevProps.location, location) ||
  50. prevProps.isProjectStabilized !== isProjectStabilized
  51. ) {
  52. this.remountComponent();
  53. }
  54. }
  55. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  56. const {location, organization, isProjectStabilized} = this.props;
  57. if (!isProjectStabilized) {
  58. return [];
  59. }
  60. const query = {
  61. ...pick(location.query, Object.values(URL_PARAM)),
  62. per_page: 3,
  63. };
  64. // we are listing 3 alerts total, first unresolved and then we fill with resolved
  65. return [
  66. [
  67. 'unresolvedAlerts',
  68. `/organizations/${organization.slug}/incidents/`,
  69. {query: {...query, status: 'open'}},
  70. ],
  71. [
  72. 'resolvedAlerts',
  73. `/organizations/${organization.slug}/incidents/`,
  74. {query: {...query, status: 'closed'}},
  75. ],
  76. ];
  77. }
  78. /**
  79. * If our alerts are empty, determine if we've configured alert rules (empty message differs then)
  80. */
  81. async onLoadAllEndpointsSuccess() {
  82. const {unresolvedAlerts, resolvedAlerts} = this.state;
  83. const {location, organization, isProjectStabilized} = this.props;
  84. if (!isProjectStabilized) {
  85. return;
  86. }
  87. if ([...(unresolvedAlerts ?? []), ...(resolvedAlerts ?? [])].length !== 0) {
  88. this.setState({hasAlertRule: true});
  89. return;
  90. }
  91. this.setState({loading: true});
  92. const alertRules = await this.api.requestPromise(
  93. `/organizations/${organization.slug}/alert-rules/`,
  94. {
  95. method: 'GET',
  96. query: {
  97. ...pick(location.query, [...Object.values(URL_PARAM)]),
  98. per_page: 1,
  99. },
  100. }
  101. );
  102. this.setState({hasAlertRule: alertRules.length > 0, loading: false});
  103. }
  104. get alertsLink() {
  105. const {organization} = this.props;
  106. // as this is a link to latest alerts, we want to only preserve project and environment
  107. return {
  108. pathname: `/organizations/${organization.slug}/alerts/`,
  109. query: {
  110. statsPeriod: undefined,
  111. start: undefined,
  112. end: undefined,
  113. utc: undefined,
  114. },
  115. };
  116. }
  117. renderAlertRow = (alert: Incident) => {
  118. const {organization} = this.props;
  119. const {status, id, identifier, title, dateClosed, dateStarted} = alert;
  120. const isResolved = status === IncidentStatus.CLOSED;
  121. const isWarning = status === IncidentStatus.WARNING;
  122. const Icon = isResolved ? IconCheckmark : isWarning ? IconExclamation : IconFire;
  123. const statusProps = {isResolved, isWarning};
  124. return (
  125. <AlertRowLink
  126. to={`/organizations/${organization.slug}/alerts/${identifier}/`}
  127. key={id}
  128. >
  129. <AlertBadgeWrapper {...statusProps} icon={Icon}>
  130. <AlertBadge status={status} hideText />
  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 ? 'green300' : isWarning ? 'yellow300' : 'red300';
  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. ${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;