list.tsx 5.6 KB

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