index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {Component} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import * as qs from 'query-string';
  6. import Alert from 'sentry/components/alert';
  7. import Button from 'sentry/components/button';
  8. import ButtonBar from 'sentry/components/buttonBar';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import LoadingError from 'sentry/components/loadingError';
  11. import LoadingIndicator from 'sentry/components/loadingIndicator';
  12. import {t} from 'sentry/locale';
  13. import GroupingStore, {SimilarItem} from 'sentry/stores/groupingStore';
  14. import space from 'sentry/styles/space';
  15. import {Project} from 'sentry/types';
  16. import List from './list';
  17. type RouteParams = {
  18. groupId: string;
  19. orgId: string;
  20. };
  21. type Props = RouteComponentProps<RouteParams, {}> & {
  22. location: Location;
  23. project: Project;
  24. };
  25. type State = {
  26. error: boolean;
  27. filteredSimilarItems: SimilarItem[];
  28. loading: boolean;
  29. similarItems: SimilarItem[];
  30. similarLinks: string | null;
  31. v2: boolean;
  32. };
  33. class SimilarStackTrace extends Component<Props, State> {
  34. state: State = {
  35. similarItems: [],
  36. filteredSimilarItems: [],
  37. similarLinks: null,
  38. loading: true,
  39. error: false,
  40. v2: false,
  41. };
  42. componentDidMount() {
  43. this.fetchData();
  44. }
  45. componentWillReceiveProps(nextProps: Props) {
  46. if (
  47. nextProps.params.groupId !== this.props.params.groupId ||
  48. nextProps.location.search !== this.props.location.search
  49. ) {
  50. this.fetchData();
  51. }
  52. }
  53. componentWillUnmount() {
  54. this.listener?.();
  55. }
  56. onGroupingChange = ({
  57. mergedParent,
  58. similarItems,
  59. similarLinks,
  60. filteredSimilarItems,
  61. loading,
  62. error,
  63. }) => {
  64. if (similarItems) {
  65. this.setState({
  66. similarItems,
  67. similarLinks,
  68. filteredSimilarItems,
  69. loading: loading ?? false,
  70. error: error ?? false,
  71. });
  72. return;
  73. }
  74. if (!mergedParent) {
  75. return;
  76. }
  77. if (mergedParent !== this.props.params.groupId) {
  78. const {params} = this.props;
  79. // Merge success, since we can't specify target, we need to redirect to new parent
  80. browserHistory.push(
  81. `/organizations/${params.orgId}/issues/${mergedParent}/similar/`
  82. );
  83. return;
  84. }
  85. return;
  86. };
  87. listener = GroupingStore.listen(this.onGroupingChange, undefined);
  88. fetchData() {
  89. const {params, location} = this.props;
  90. this.setState({loading: true, error: false});
  91. const reqs: Parameters<typeof GroupingStore.onFetch>[0] = [];
  92. if (this.hasSimilarityFeature()) {
  93. const version = this.state.v2 ? '2' : '1';
  94. reqs.push({
  95. endpoint: `/issues/${params.groupId}/similar/?${qs.stringify({
  96. ...location.query,
  97. limit: 50,
  98. version,
  99. })}`,
  100. dataKey: 'similar',
  101. });
  102. }
  103. GroupingStore.onFetch(reqs);
  104. }
  105. handleMerge = () => {
  106. const {params, location} = this.props;
  107. const query = location.query;
  108. if (!params) {
  109. return;
  110. }
  111. // You need at least 1 similarItem OR filteredSimilarItems to be able to merge,
  112. // so `firstIssue` should always exist from one of those lists.
  113. //
  114. // Similar issues API currently does not return issues across projects,
  115. // so we can assume that the first issues project slug is the project in
  116. // scope
  117. const [firstIssue] = this.state.similarItems.length
  118. ? this.state.similarItems
  119. : this.state.filteredSimilarItems;
  120. GroupingStore.onMerge({
  121. params,
  122. query,
  123. projectId: firstIssue.issue.project.slug,
  124. });
  125. };
  126. hasSimilarityV2Feature() {
  127. return this.props.project.features.includes('similarity-view-v2');
  128. }
  129. hasSimilarityFeature() {
  130. return this.props.project.features.includes('similarity-view');
  131. }
  132. toggleSimilarityVersion = () => {
  133. this.setState(prevState => ({v2: !prevState.v2}), this.fetchData);
  134. };
  135. render() {
  136. const {params, project} = this.props;
  137. const {orgId, groupId} = params;
  138. const {similarItems, filteredSimilarItems, loading, error, v2, similarLinks} =
  139. this.state;
  140. const hasV2 = this.hasSimilarityV2Feature();
  141. const isLoading = loading;
  142. const isError = error && !isLoading;
  143. const isLoadedSuccessfully = !isError && !isLoading;
  144. const hasSimilarItems =
  145. this.hasSimilarityFeature() &&
  146. (similarItems.length > 0 || filteredSimilarItems.length > 0) &&
  147. isLoadedSuccessfully;
  148. return (
  149. <Layout.Body>
  150. <Layout.Main fullWidth>
  151. <Alert type="warning">
  152. {t(
  153. 'This is an experimental feature. Data may not be immediately available while we process merges.'
  154. )}
  155. </Alert>
  156. <HeaderWrapper>
  157. <Title>{t('Issues with a similar stack trace')}</Title>
  158. {hasV2 && (
  159. <ButtonBar merged active={v2 ? 'new' : 'old'}>
  160. <Button barId="old" size="sm" onClick={this.toggleSimilarityVersion}>
  161. {t('Old Algorithm')}
  162. </Button>
  163. <Button barId="new" size="sm" onClick={this.toggleSimilarityVersion}>
  164. {t('New Algorithm')}
  165. </Button>
  166. </ButtonBar>
  167. )}
  168. </HeaderWrapper>
  169. {isLoading && <LoadingIndicator />}
  170. {isError && (
  171. <LoadingError
  172. message={t('Unable to load similar issues, please try again later')}
  173. onRetry={this.fetchData}
  174. />
  175. )}
  176. {hasSimilarItems && (
  177. <List
  178. items={similarItems}
  179. filteredItems={filteredSimilarItems}
  180. onMerge={this.handleMerge}
  181. orgId={orgId}
  182. project={project}
  183. groupId={groupId}
  184. pageLinks={similarLinks}
  185. v2={v2}
  186. />
  187. )}
  188. </Layout.Main>
  189. </Layout.Body>
  190. );
  191. }
  192. }
  193. export default SimilarStackTrace;
  194. const Title = styled('h4')`
  195. margin-bottom: 0;
  196. `;
  197. const HeaderWrapper = styled('div')`
  198. display: flex;
  199. align-items: center;
  200. justify-content: space-between;
  201. margin-bottom: ${space(2)};
  202. `;