issuesList.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import AsyncComponent from 'sentry/components/asyncComponent';
  5. import type {DateTimeObject} from 'sentry/components/charts/utils';
  6. import Count from 'sentry/components/count';
  7. import DateTime from 'sentry/components/dateTime';
  8. import Link from 'sentry/components/links/link';
  9. import Pagination from 'sentry/components/pagination';
  10. import {PanelTable} from 'sentry/components/panels';
  11. import {t} from 'sentry/locale';
  12. import space from 'sentry/styles/space';
  13. import {Group, Organization, Project} from 'sentry/types';
  14. import {IssueAlertRule} from 'sentry/types/alerts';
  15. import {getMessage, getTitle} from 'sentry/utils/events';
  16. import getDynamicText from 'sentry/utils/getDynamicText';
  17. type GroupHistory = {
  18. count: number;
  19. eventId: string;
  20. group: Group;
  21. lastTriggered: string;
  22. };
  23. type Props = AsyncComponent['props'] &
  24. DateTimeObject & {
  25. organization: Organization;
  26. project: Project;
  27. rule: IssueAlertRule;
  28. cursor?: string;
  29. };
  30. type State = AsyncComponent['state'] & {
  31. groupHistory: GroupHistory[] | null;
  32. };
  33. class AlertRuleIssuesList extends AsyncComponent<Props, State> {
  34. shouldRenderBadRequests = true;
  35. componentDidUpdate(prevProps: Props) {
  36. const {project, organization, start, end, period, utc, cursor} = this.props;
  37. if (
  38. prevProps.start !== start ||
  39. prevProps.end !== end ||
  40. prevProps.period !== period ||
  41. prevProps.utc !== utc ||
  42. prevProps.organization.id !== organization.id ||
  43. prevProps.project.id !== project.id ||
  44. prevProps.cursor !== cursor
  45. ) {
  46. this.remountComponent();
  47. }
  48. }
  49. getDefaultState(): State {
  50. return {
  51. ...super.getDefaultState(),
  52. groupHistory: null,
  53. };
  54. }
  55. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  56. const {project, rule, organization, period, start, end, utc, cursor} = this.props;
  57. return [
  58. [
  59. 'groupHistory',
  60. `/projects/${organization.slug}/${project.slug}/rules/${rule.id}/group-history/`,
  61. {
  62. query: {
  63. per_page: 10,
  64. ...(period && {statsPeriod: period}),
  65. start,
  66. end,
  67. utc,
  68. cursor,
  69. },
  70. },
  71. ],
  72. ];
  73. }
  74. renderLoading() {
  75. return this.renderBody();
  76. }
  77. renderBody() {
  78. const {organization, rule} = this.props;
  79. const {loading, groupHistory, groupHistoryPageLinks} = this.state;
  80. return (
  81. <Fragment>
  82. <StyledPanelTable
  83. isLoading={loading}
  84. isEmpty={groupHistory?.length === 0}
  85. emptyMessage={t('No issues exist for the current query.')}
  86. headers={[
  87. t('Issue'),
  88. <AlignRight key="alerts">{t('Alerts')}</AlignRight>,
  89. <AlignRight key="events">{t('Events')}</AlignRight>,
  90. t('Last Triggered'),
  91. ]}
  92. >
  93. {groupHistory?.map(({group: issue, count, lastTriggered, eventId}) => {
  94. const message = getMessage(issue);
  95. const {title} = getTitle(issue);
  96. return (
  97. <Fragment key={issue.id}>
  98. <TitleWrapper>
  99. <Link
  100. to={{
  101. pathname:
  102. `/organizations/${organization.slug}/issues/${issue.id}/` +
  103. (eventId ? `events/${eventId}` : ''),
  104. query: rule.environment ? {environment: rule.environment} : {},
  105. }}
  106. >
  107. {title}:
  108. </Link>
  109. <MessageWrapper>{message}</MessageWrapper>
  110. </TitleWrapper>
  111. <AlignRight>
  112. <Count value={count} />
  113. </AlignRight>
  114. <AlignRight>
  115. <Count value={issue.count} />
  116. </AlignRight>
  117. <div>
  118. <StyledDateTime
  119. date={getDynamicText({
  120. value: lastTriggered,
  121. fixed: 'Mar 16, 2020 9:10:13 AM UTC',
  122. })}
  123. year
  124. seconds
  125. timeZone
  126. />
  127. </div>
  128. </Fragment>
  129. );
  130. })}
  131. </StyledPanelTable>
  132. <PaginationWrapper>
  133. <StyledPagination pageLinks={groupHistoryPageLinks} size="xs" />
  134. </PaginationWrapper>
  135. </Fragment>
  136. );
  137. }
  138. }
  139. export default AlertRuleIssuesList;
  140. const StyledPanelTable = styled(PanelTable)`
  141. grid-template-columns: 1fr 0.2fr 0.2fr 0.5fr;
  142. font-size: ${p => p.theme.fontSizeMedium};
  143. margin-bottom: ${space(1.5)};
  144. ${p =>
  145. !p.isEmpty &&
  146. css`
  147. & > div {
  148. padding: ${space(1)} ${space(2)};
  149. }
  150. `}
  151. `;
  152. const AlignRight = styled('div')`
  153. text-align: right;
  154. font-variant-numeric: tabular-nums;
  155. `;
  156. const StyledDateTime = styled(DateTime)`
  157. white-space: nowrap;
  158. color: ${p => p.theme.subText};
  159. `;
  160. const TitleWrapper = styled('div')`
  161. ${p => p.theme.overflowEllipsis};
  162. display: flex;
  163. gap: ${space(0.5)};
  164. min-width: 200px;
  165. `;
  166. const MessageWrapper = styled('span')`
  167. ${p => p.theme.overflowEllipsis};
  168. color: ${p => p.theme.textColor};
  169. `;
  170. const PaginationWrapper = styled('div')`
  171. display: flex;
  172. align-items: center;
  173. justify-content: flex-end;
  174. margin-bottom: ${space(2)};
  175. `;
  176. const StyledPagination = styled(Pagination)`
  177. margin-top: 0;
  178. `;