issuesList.tsx 5.2 KB

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