issuesList.tsx 5.0 KB

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