relatedIssues.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  30. function RelatedIssues({rule, organization, projects, query, timePeriod}: Props) {
  31. const router = useRouter();
  32. // Add environment to the query parameters to be picked up by GlobalSelectionLink
  33. // GlobalSelectionLink uses the current query parameters to build links to issue details
  34. useEffect(() => {
  35. const env = rule.environment ?? '';
  36. if (env !== (router.location.query.environment ?? '')) {
  37. router.replace({
  38. pathname: router.location.pathname,
  39. query: {...router.location.query, environment: env},
  40. });
  41. }
  42. }, [rule.environment, router]);
  43. function renderErrorMessage({detail}: {detail: string}, retry: () => void) {
  44. if (
  45. detail === RELATED_ISSUES_BOOLEAN_QUERY_ERROR &&
  46. !isSessionAggregate(rule.aggregate)
  47. ) {
  48. const {buttonText, to} = makeDefaultCta({
  49. organization,
  50. projects,
  51. rule,
  52. query,
  53. timePeriod,
  54. });
  55. return <RelatedIssuesNotAvailable buttonTo={to} buttonText={buttonText} />;
  56. }
  57. return <LoadingError onRetry={retry} />;
  58. }
  59. function renderEmptyMessage() {
  60. return (
  61. <Panel>
  62. <PanelBody>
  63. <EmptyStateWarning small withIcon={false}>
  64. {t('No issues for this alert rule')}
  65. </EmptyStateWarning>
  66. </PanelBody>
  67. </Panel>
  68. );
  69. }
  70. const {start, end} = timePeriod;
  71. const path = `/organizations/${organization.slug}/issues/`;
  72. const queryParams = {
  73. start,
  74. end,
  75. groupStatsPeriod: 'auto',
  76. limit: 5,
  77. ...(rule.environment ? {environment: rule.environment} : {}),
  78. sort: rule.aggregate === 'count_unique(user)' ? 'user' : 'freq',
  79. query,
  80. project: projects.map(project => project.id),
  81. };
  82. const issueSearch = {
  83. pathname: `/organizations/${organization.slug}/issues/`,
  84. query: queryParams,
  85. };
  86. return (
  87. <Fragment>
  88. <ControlsWrapper>
  89. <StyledSectionHeading>{t('Related Issues')}</StyledSectionHeading>
  90. <LinkButton data-test-id="issues-open" size="xs" to={issueSearch}>
  91. {t('Open in Issues')}
  92. </LinkButton>
  93. </ControlsWrapper>
  94. <TableWrapper>
  95. <GroupList
  96. orgSlug={organization.slug}
  97. endpointPath={path}
  98. queryParams={queryParams}
  99. query={`start=${start}&end=${end}&groupStatsPeriod=auto`}
  100. canSelectGroups={false}
  101. renderEmptyMessage={renderEmptyMessage}
  102. renderErrorMessage={renderErrorMessage}
  103. withChart
  104. withPagination={false}
  105. useFilteredStats
  106. customStatsPeriod={timePeriod}
  107. useTintRow={false}
  108. source="alerts-related-issues"
  109. />
  110. </TableWrapper>
  111. </Fragment>
  112. );
  113. }
  114. const StyledSectionHeading = styled(SectionHeading)`
  115. display: flex;
  116. align-items: center;
  117. `;
  118. const ControlsWrapper = styled('div')`
  119. display: flex;
  120. align-items: center;
  121. justify-content: space-between;
  122. margin-bottom: ${space(1)};
  123. `;
  124. const TableWrapper = styled('div')`
  125. margin-bottom: ${space(4)};
  126. ${Panel} {
  127. /* smaller space between table and pagination */
  128. margin-bottom: -${space(1)};
  129. }
  130. `;
  131. export default RelatedIssues;