issuesList.tsx 4.7 KB

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