issuesList.tsx 5.3 KB

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