relatedIssues.tsx 3.7 KB

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