relatedIssues.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {Fragment, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {LinkButton} from 'sentry/components/button';
  4. import {SectionHeading} from 'sentry/components/charts/styles';
  5. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  6. import GroupList from 'sentry/components/issues/groupList';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import Panel from 'sentry/components/panels/panel';
  9. import PanelBody from 'sentry/components/panels/panelBody';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import type {Organization} from 'sentry/types/organization';
  13. import type {Project} from 'sentry/types/project';
  14. import useRouter from 'sentry/utils/useRouter';
  15. import {
  16. RELATED_ISSUES_BOOLEAN_QUERY_ERROR,
  17. RelatedIssuesNotAvailable,
  18. } from 'sentry/views/alerts/rules/metric/details/relatedIssuesNotAvailable';
  19. import {makeDefaultCta} from 'sentry/views/alerts/rules/metric/metricRulePresets';
  20. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  21. import {isSessionAggregate} from 'sentry/views/alerts/utils';
  22. import type {TimePeriodType} from './constants';
  23. interface Props {
  24. organization: Organization;
  25. projects: Project[];
  26. rule: MetricRule;
  27. timePeriod: TimePeriodType;
  28. query?: string;
  29. skipHeader?: boolean;
  30. }
  31. function RelatedIssues({
  32. rule,
  33. organization,
  34. projects,
  35. query,
  36. timePeriod,
  37. skipHeader,
  38. }: Props) {
  39. const router = useRouter();
  40. // Add environment to the query parameters to be picked up by GlobalSelectionLink
  41. // GlobalSelectionLink uses the current query parameters to build links to issue details
  42. useEffect(() => {
  43. const env = rule.environment ?? '';
  44. if (env !== (router.location.query.environment ?? '')) {
  45. router.replace({
  46. pathname: router.location.pathname,
  47. query: {...router.location.query, environment: env},
  48. });
  49. }
  50. }, [rule.environment, router]);
  51. function renderErrorMessage({detail}: {detail: string}, retry: () => void) {
  52. if (
  53. detail === RELATED_ISSUES_BOOLEAN_QUERY_ERROR &&
  54. !isSessionAggregate(rule.aggregate)
  55. ) {
  56. const {buttonText, to} = makeDefaultCta({
  57. organization,
  58. projects,
  59. rule,
  60. query,
  61. timePeriod,
  62. });
  63. return <RelatedIssuesNotAvailable buttonTo={to} buttonText={buttonText} />;
  64. }
  65. return <LoadingError onRetry={retry} />;
  66. }
  67. function renderEmptyMessage() {
  68. return (
  69. <Panel>
  70. <PanelBody>
  71. <EmptyStateWarning small withIcon={false}>
  72. {t('No issues for this alert rule')}
  73. </EmptyStateWarning>
  74. </PanelBody>
  75. </Panel>
  76. );
  77. }
  78. const {start, end} = timePeriod;
  79. const path = `/organizations/${organization.slug}/issues/`;
  80. const queryParams = {
  81. start,
  82. end,
  83. groupStatsPeriod: 'auto',
  84. limit: 5,
  85. ...(rule.environment ? {environment: rule.environment} : {}),
  86. sort: rule.aggregate === 'count_unique(user)' ? 'user' : 'freq',
  87. query,
  88. project: projects.map(project => project.id),
  89. };
  90. const issueSearch = {
  91. pathname: `/organizations/${organization.slug}/issues/`,
  92. query: queryParams,
  93. };
  94. return (
  95. <Fragment>
  96. {!skipHeader && (
  97. <ControlsWrapper>
  98. <SectionHeading>{t('Related Issues')}</SectionHeading>
  99. <LinkButton data-test-id="issues-open" size="xs" to={issueSearch}>
  100. {t('Open in Issues')}
  101. </LinkButton>
  102. </ControlsWrapper>
  103. )}
  104. <TableWrapper>
  105. <GroupList
  106. orgSlug={organization.slug}
  107. endpointPath={path}
  108. queryParams={queryParams}
  109. canSelectGroups={false}
  110. renderEmptyMessage={renderEmptyMessage}
  111. renderErrorMessage={renderErrorMessage}
  112. withChart
  113. withPagination={false}
  114. useFilteredStats
  115. customStatsPeriod={timePeriod}
  116. useTintRow={false}
  117. source="alerts-related-issues"
  118. />
  119. </TableWrapper>
  120. </Fragment>
  121. );
  122. }
  123. const ControlsWrapper = styled('div')`
  124. display: flex;
  125. align-items: center;
  126. justify-content: space-between;
  127. margin-bottom: ${space(1)};
  128. `;
  129. const TableWrapper = styled('div')`
  130. margin-bottom: ${space(4)};
  131. ${Panel} {
  132. /* smaller space between table and pagination */
  133. margin-bottom: -${space(1)};
  134. }
  135. `;
  136. export default RelatedIssues;