list.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {Component, Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Sentry from '@sentry/react';
  5. import {Location} from 'history';
  6. import pick from 'lodash/pick';
  7. import {Client} from 'sentry/api';
  8. import DateTime from 'sentry/components/dateTime';
  9. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  10. import GroupListHeader from 'sentry/components/issues/groupListHeader';
  11. import LoadingError from 'sentry/components/loadingError';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import Pagination, {CursorHandler} from 'sentry/components/pagination';
  14. import {Panel, PanelBody} from 'sentry/components/panels';
  15. import StreamGroup from 'sentry/components/stream/group';
  16. import {URL_PARAM} from 'sentry/constants/pageFilters';
  17. import {tct} from 'sentry/locale';
  18. import GroupStore from 'sentry/stores/groupStore';
  19. import {GroupResolution} from 'sentry/types';
  20. import {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  21. import withApi from 'sentry/utils/withApi';
  22. type CustomGroup = GroupResolution & {
  23. eventID: string;
  24. groupID: string;
  25. };
  26. type Period = {
  27. end: string;
  28. start: string;
  29. };
  30. type Props = {
  31. api: Client;
  32. issues: Array<TableDataRow>;
  33. location: Location;
  34. orgSlug: string;
  35. pageLinks: string | null;
  36. period: Period;
  37. traceID: string;
  38. };
  39. type State = {
  40. groups: Array<CustomGroup>;
  41. hasError: boolean;
  42. isLoading: boolean;
  43. };
  44. class List extends Component<Props, State> {
  45. state: State = {
  46. groups: [],
  47. hasError: false,
  48. isLoading: true,
  49. };
  50. componentDidMount() {
  51. this.getGroups();
  52. }
  53. getGroups = async () => {
  54. const {api, orgSlug, location, issues} = this.props;
  55. if (!issues.length) {
  56. this.setState({isLoading: false});
  57. return;
  58. }
  59. const issuesIds = issues.map(issue => `group=${issue['issue.id']}`).join('&');
  60. try {
  61. const groups = await api.requestPromise(
  62. `/organizations/${orgSlug}/issues/?${issuesIds}`,
  63. {
  64. method: 'GET',
  65. data: {
  66. sort: 'new',
  67. ...pick(location.query, [...Object.values(URL_PARAM), 'cursor']),
  68. },
  69. }
  70. );
  71. const convertedGroups = this.convertGroupsIntoEventFormat(groups);
  72. // this is necessary, because the AssigneeSelector component fetches the group from the GroupStore
  73. GroupStore.add(convertedGroups);
  74. this.setState({groups: convertedGroups, isLoading: false});
  75. } catch (error) {
  76. Sentry.captureException(error);
  77. this.setState({isLoading: false, hasError: true});
  78. }
  79. };
  80. // this little hack is necessary until we factored the groupStore or the EventOrGroupHeader component
  81. // the goal of this function is to insert the properties eventID and groupID, so then the link rendered
  82. // in the EventOrGroupHeader component will always have the structure '/organization/:orgSlug/issues/:groupId/event/:eventId/',
  83. // providing a smooth navigation between issues with the same trace ID
  84. convertGroupsIntoEventFormat = (groups: Array<GroupResolution>) => {
  85. const {issues} = this.props;
  86. return groups
  87. .map(group => {
  88. // the issue must always be found
  89. const foundIssue = issues.find(issue => group.id === String(issue['issue.id']));
  90. if (foundIssue) {
  91. // the eventID is the reason why we need to use the DiscoverQuery component.
  92. // At the moment the /issues/ endpoint above doesn't return this information
  93. return {...group, eventID: foundIssue.id, groupID: group.id};
  94. }
  95. return undefined;
  96. })
  97. .filter(event => !!event) as Array<CustomGroup>;
  98. };
  99. handleCursorChange: CursorHandler = (cursor, path, query, delta) =>
  100. browserHistory.push({
  101. pathname: path,
  102. query: {...query, cursor: delta <= 0 ? undefined : cursor},
  103. });
  104. handleRetry = () => {
  105. this.getGroups();
  106. };
  107. renderContent = () => {
  108. const {issues, period, traceID} = this.props;
  109. if (!issues.length) {
  110. return (
  111. <EmptyStateWarning small withIcon={false}>
  112. {tct(
  113. 'No issues with the same trace ID [traceID] were found in the period between [start] and [end]',
  114. {
  115. traceID,
  116. start: <DateTime date={period.start} />,
  117. end: <DateTime date={period.start} />,
  118. }
  119. )}
  120. </EmptyStateWarning>
  121. );
  122. }
  123. return issues.map(issue => (
  124. <StreamGroup
  125. key={issue.id}
  126. id={String(issue['issue.id'])}
  127. canSelect={false}
  128. withChart={false}
  129. />
  130. ));
  131. };
  132. render() {
  133. const {pageLinks, traceID} = this.props;
  134. const {isLoading, hasError} = this.state;
  135. if (isLoading) {
  136. return <LoadingIndicator />;
  137. }
  138. if (hasError) {
  139. return (
  140. <LoadingError
  141. message={tct(
  142. 'An error occurred while fetching issues with the trace ID [traceID]',
  143. {
  144. traceID,
  145. }
  146. )}
  147. onRetry={this.handleRetry}
  148. />
  149. );
  150. }
  151. return (
  152. <Fragment>
  153. <StyledPanel>
  154. <GroupListHeader withChart={false} />
  155. <PanelBody>{this.renderContent()}</PanelBody>
  156. </StyledPanel>
  157. <StyledPagination pageLinks={pageLinks} onCursor={this.handleCursorChange} />
  158. </Fragment>
  159. );
  160. }
  161. }
  162. export default withApi(List);
  163. const StyledPagination = styled(Pagination)`
  164. margin-top: 0;
  165. `;
  166. const StyledPanel = styled(Panel)`
  167. margin-bottom: 0;
  168. `;