relatedIssues.tsx 4.3 KB

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