issuesList.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {Fragment} from 'react';
  2. import {css} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import type {DateTimeObject} from 'sentry/components/charts/utils';
  5. import Count from 'sentry/components/count';
  6. import DateTime from 'sentry/components/dateTime';
  7. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  8. import Link from 'sentry/components/links/link';
  9. import Pagination from 'sentry/components/pagination';
  10. import PanelTable from 'sentry/components/panels/panelTable';
  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 = DeprecatedAsyncComponent['props'] &
  24. DateTimeObject & {
  25. organization: Organization;
  26. project: Project;
  27. rule: IssueAlertRule;
  28. cursor?: string;
  29. };
  30. type State = DeprecatedAsyncComponent['state'] & {
  31. groupHistory: GroupHistory[] | null;
  32. };
  33. class AlertRuleIssuesList extends DeprecatedAsyncComponent<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<DeprecatedAsyncComponent['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: {
  105. referrer: 'alert-rule-issue-list',
  106. ...(rule.environment ? {environment: rule.environment} : {}),
  107. },
  108. }}
  109. >
  110. {title}:
  111. </Link>
  112. <MessageWrapper>{message}</MessageWrapper>
  113. </TitleWrapper>
  114. <AlignRight>
  115. <Count value={count} />
  116. </AlignRight>
  117. <AlignRight>
  118. <Count value={issue.count} />
  119. </AlignRight>
  120. <div>
  121. <StyledDateTime
  122. date={getDynamicText({
  123. value: lastTriggered,
  124. fixed: 'Mar 16, 2020 9:10:13 AM UTC',
  125. })}
  126. year
  127. seconds
  128. timeZone
  129. />
  130. </div>
  131. </Fragment>
  132. );
  133. })}
  134. </StyledPanelTable>
  135. <PaginationWrapper>
  136. <StyledPagination pageLinks={groupHistoryPageLinks} size="xs" />
  137. </PaginationWrapper>
  138. </Fragment>
  139. );
  140. }
  141. }
  142. export default AlertRuleIssuesList;
  143. const StyledPanelTable = styled(PanelTable)`
  144. grid-template-columns: 1fr 0.2fr 0.2fr 0.5fr;
  145. font-size: ${p => p.theme.fontSizeMedium};
  146. margin-bottom: ${space(1.5)};
  147. ${p =>
  148. !p.isEmpty &&
  149. css`
  150. & > div {
  151. padding: ${space(1)} ${space(2)};
  152. }
  153. `}
  154. `;
  155. const AlignRight = styled('div')`
  156. text-align: right;
  157. font-variant-numeric: tabular-nums;
  158. `;
  159. const StyledDateTime = styled(DateTime)`
  160. white-space: nowrap;
  161. color: ${p => p.theme.subText};
  162. `;
  163. const TitleWrapper = styled('div')`
  164. ${p => p.theme.overflowEllipsis};
  165. display: flex;
  166. gap: ${space(0.5)};
  167. min-width: 200px;
  168. `;
  169. const MessageWrapper = styled('span')`
  170. ${p => p.theme.overflowEllipsis};
  171. color: ${p => p.theme.textColor};
  172. `;
  173. const PaginationWrapper = styled('div')`
  174. display: flex;
  175. align-items: center;
  176. justify-content: flex-end;
  177. margin-bottom: ${space(2)};
  178. `;
  179. const StyledPagination = styled(Pagination)`
  180. margin-top: 0;
  181. `;