issuesList.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <LoadingError message={error?.responseJSON?.detail ?? t('default message')} />;
  56. }
  57. return (
  58. <Fragment>
  59. <StyledPanelTable
  60. isLoading={isLoading}
  61. isEmpty={groupHistory?.length === 0}
  62. emptyMessage={t('No issues exist for the current query.')}
  63. headers={[
  64. t('Issue'),
  65. <AlignRight key="alerts">{t('Alerts')}</AlignRight>,
  66. <AlignRight key="events">{t('Events')}</AlignRight>,
  67. t('Last Triggered'),
  68. ]}
  69. >
  70. {groupHistory?.map(({group: issue, count, lastTriggered, eventId}) => {
  71. const message = getMessage(issue);
  72. const {title} = getTitle(issue);
  73. return (
  74. <Fragment key={issue.id}>
  75. <TitleWrapper>
  76. <Link
  77. to={{
  78. pathname: `/organizations/${organization.slug}/issues/${issue.id}/${
  79. eventId ? `events/${eventId}` : ''
  80. }`,
  81. query: {
  82. referrer: 'alert-rule-issue-list',
  83. ...(rule.environment ? {environment: rule.environment} : {}),
  84. },
  85. }}
  86. >
  87. {title}:
  88. </Link>
  89. <MessageWrapper>{message}</MessageWrapper>
  90. </TitleWrapper>
  91. <AlignRight>
  92. <Count value={count} />
  93. </AlignRight>
  94. <AlignRight>
  95. <Count value={issue.count} />
  96. </AlignRight>
  97. <div>
  98. <StyledDateTime
  99. date={getDynamicText({
  100. value: lastTriggered,
  101. fixed: 'Mar 16, 2020 9:10:13 AM UTC',
  102. })}
  103. year
  104. seconds
  105. timeZone
  106. />
  107. </div>
  108. </Fragment>
  109. );
  110. })}
  111. </StyledPanelTable>
  112. <PaginationWrapper>
  113. <StyledPagination pageLinks={getResponseHeader?.('Link')} size="xs" />
  114. </PaginationWrapper>
  115. </Fragment>
  116. );
  117. }
  118. export default AlertRuleIssuesList;
  119. const StyledPanelTable = styled(PanelTable)`
  120. grid-template-columns: 1fr 0.2fr 0.2fr 0.5fr;
  121. font-size: ${p => p.theme.fontSizeMedium};
  122. margin-bottom: ${space(1.5)};
  123. ${p =>
  124. !p.isEmpty &&
  125. css`
  126. & > div {
  127. padding: ${space(1)} ${space(2)};
  128. }
  129. `}
  130. `;
  131. const AlignRight = styled('div')`
  132. text-align: right;
  133. font-variant-numeric: tabular-nums;
  134. `;
  135. const StyledDateTime = styled(DateTime)`
  136. white-space: nowrap;
  137. color: ${p => p.theme.subText};
  138. `;
  139. const TitleWrapper = styled('div')`
  140. ${p => p.theme.overflowEllipsis};
  141. display: flex;
  142. gap: ${space(0.5)};
  143. min-width: 200px;
  144. `;
  145. const MessageWrapper = styled('span')`
  146. ${p => p.theme.overflowEllipsis};
  147. color: ${p => p.theme.textColor};
  148. `;
  149. const PaginationWrapper = styled('div')`
  150. display: flex;
  151. align-items: center;
  152. justify-content: flex-end;
  153. margin-bottom: ${space(2)};
  154. `;
  155. const StyledPagination = styled(Pagination)`
  156. margin-top: 0;
  157. `;