issuesList.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. isPending,
  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={isPending}
  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/`,
  83. query: {feedbackSlug: `${issue.project.slug}:${issue.id}`},
  84. }
  85. : {
  86. pathname: `/organizations/${organization.slug}/issues/${issue.id}/${
  87. eventId ? `events/${eventId}` : ''
  88. }`,
  89. query: {
  90. referrer: 'alert-rule-issue-list',
  91. ...(rule.environment ? {environment: rule.environment} : {}),
  92. },
  93. };
  94. return (
  95. <Fragment key={issue.id}>
  96. <TitleWrapper>
  97. <Link to={path}>{title}:</Link>
  98. <MessageWrapper>{message}</MessageWrapper>
  99. </TitleWrapper>
  100. <AlignRight>
  101. <Count value={count} />
  102. </AlignRight>
  103. <AlignRight>
  104. <Count value={issue.count} />
  105. </AlignRight>
  106. <div>
  107. <StyledDateTime
  108. date={getDynamicText({
  109. value: lastTriggered,
  110. fixed: 'Mar 16, 2020 9:10:13 AM UTC',
  111. })}
  112. year
  113. seconds
  114. timeZone
  115. />
  116. </div>
  117. </Fragment>
  118. );
  119. })}
  120. </StyledPanelTable>
  121. <PaginationWrapper>
  122. <StyledPagination pageLinks={getResponseHeader?.('Link')} size="xs" />
  123. </PaginationWrapper>
  124. </Fragment>
  125. );
  126. }
  127. export default AlertRuleIssuesList;
  128. const StyledPanelTable = styled(PanelTable)`
  129. grid-template-columns: 1fr 0.2fr 0.2fr 0.5fr;
  130. font-size: ${p => p.theme.fontSizeMedium};
  131. margin-bottom: ${space(1.5)};
  132. ${p =>
  133. !p.isEmpty &&
  134. css`
  135. & > div {
  136. padding: ${space(1)} ${space(2)};
  137. }
  138. `}
  139. `;
  140. const AlignRight = styled('div')`
  141. text-align: right;
  142. font-variant-numeric: tabular-nums;
  143. `;
  144. const StyledDateTime = styled(DateTime)`
  145. white-space: nowrap;
  146. color: ${p => p.theme.subText};
  147. `;
  148. const TitleWrapper = styled('div')`
  149. ${p => p.theme.overflowEllipsis};
  150. display: flex;
  151. gap: ${space(0.5)};
  152. min-width: 200px;
  153. `;
  154. const MessageWrapper = styled('span')`
  155. ${p => p.theme.overflowEllipsis};
  156. color: ${p => p.theme.textColor};
  157. `;
  158. const PaginationWrapper = styled('div')`
  159. display: flex;
  160. align-items: center;
  161. justify-content: flex-end;
  162. margin-bottom: ${space(2)};
  163. `;
  164. const StyledPagination = styled(Pagination)`
  165. margin-top: 0;
  166. `;