relatedIssues.tsx 3.7 KB

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