index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {Component} from 'react';
  2. import isPropValid from '@emotion/is-prop-valid';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  6. import type {Client} from 'sentry/api';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import type SplitDiff from 'sentry/components/splitDiff';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {Organization} from 'sentry/types/organization';
  12. import type {Project} from 'sentry/types/project';
  13. import {trackAnalytics} from 'sentry/utils/analytics';
  14. import getStacktraceBody from 'sentry/utils/getStacktraceBody';
  15. import withApi from 'sentry/utils/withApi';
  16. const defaultProps = {
  17. baseEventId: 'latest',
  18. targetEventId: 'latest',
  19. };
  20. type DefaultProps = typeof defaultProps;
  21. type Props = {
  22. api: Client;
  23. baseIssueId: string;
  24. location: Location;
  25. orgId: string;
  26. project: Project;
  27. targetIssueId: string;
  28. baseEventId?: string;
  29. className?: string;
  30. organization?: Organization;
  31. shouldBeGrouped?: string;
  32. targetEventId?: string;
  33. };
  34. type State = {
  35. baseEvent: Array<string>;
  36. loading: boolean;
  37. targetEvent: Array<string>;
  38. SplitDiffAsync?: typeof SplitDiff;
  39. };
  40. class IssueDiff extends Component<Props, State> {
  41. static defaultProps: DefaultProps = defaultProps;
  42. state: State = {
  43. loading: true,
  44. baseEvent: [],
  45. targetEvent: [],
  46. // `SplitDiffAsync` is an async-loaded component
  47. // This will eventually contain a reference to the exported component from `./splitDiff`
  48. SplitDiffAsync: undefined,
  49. };
  50. componentDidMount() {
  51. this.fetchData();
  52. }
  53. fetchData() {
  54. const {
  55. baseIssueId,
  56. targetIssueId,
  57. baseEventId,
  58. targetEventId,
  59. organization,
  60. project,
  61. shouldBeGrouped,
  62. location,
  63. } = this.props;
  64. const hasSimilarityEmbeddingsFeature =
  65. project.features.includes('similarity-embeddings') ||
  66. location.query.similarityEmbeddings === '1';
  67. // Fetch component and event data
  68. const asyncFetch = async () => {
  69. try {
  70. const splitdiffPromise = import('../splitDiff');
  71. const {default: SplitDiffAsync} = await splitdiffPromise;
  72. const [baseEventData, targetEventData] = await Promise.all([
  73. this.fetchEvent(baseIssueId, baseEventId ?? 'latest'),
  74. this.fetchEvent(targetIssueId, targetEventId ?? 'latest'),
  75. ]);
  76. const [baseEvent, targetEvent] = await Promise.all([
  77. getStacktraceBody(baseEventData, hasSimilarityEmbeddingsFeature),
  78. getStacktraceBody(targetEventData, hasSimilarityEmbeddingsFeature),
  79. ]);
  80. this.setState({
  81. SplitDiffAsync,
  82. baseEvent,
  83. targetEvent,
  84. loading: false,
  85. });
  86. if (organization && hasSimilarityEmbeddingsFeature) {
  87. trackAnalytics('issue_details.similar_issues.diff_clicked', {
  88. organization,
  89. project_id: baseEventData?.projectID,
  90. group_id: baseEventData?.groupID,
  91. error_message: baseEventData?.message
  92. ? baseEventData.message
  93. : baseEventData?.title,
  94. stacktrace: baseEvent.join('/n '),
  95. transaction: this.getTransaction(
  96. baseEventData?.tags ? baseEventData.tags : []
  97. ),
  98. parent_group_id: targetEventData?.groupID,
  99. parent_error_message: targetEventData?.message
  100. ? targetEventData.message
  101. : targetEventData?.title,
  102. parent_stacktrace: targetEvent.join('/n '),
  103. parent_transaction: this.getTransaction(
  104. targetEventData?.tags ? targetEventData.tags : []
  105. ),
  106. shouldBeGrouped: shouldBeGrouped,
  107. });
  108. }
  109. } catch {
  110. addErrorMessage(t('Error loading events'));
  111. }
  112. };
  113. asyncFetch();
  114. }
  115. fetchEvent = async (issueId: string, eventId: string) => {
  116. const {orgId, project, api} = this.props;
  117. let paramEventId = eventId;
  118. if (eventId === 'latest') {
  119. const event = await api.requestPromise(`/issues/${issueId}/events/latest/`);
  120. paramEventId = event.eventID;
  121. }
  122. const event = await api.requestPromise(
  123. `/projects/${orgId}/${project.slug}/events/${paramEventId}/`
  124. );
  125. return event;
  126. };
  127. getTransaction = (tags: any[]) => {
  128. return tags.find(tag => tag.key === 'transaction');
  129. };
  130. render() {
  131. const {className} = this.props;
  132. const {SplitDiffAsync: DiffComponent, loading, baseEvent, targetEvent} = this.state;
  133. return (
  134. <StyledIssueDiff className={className} loading={loading}>
  135. {loading && <LoadingIndicator />}
  136. {!loading &&
  137. DiffComponent &&
  138. baseEvent.map((value, i) => (
  139. <DiffComponent
  140. key={i}
  141. base={value}
  142. target={targetEvent[i] ?? ''}
  143. type="words"
  144. />
  145. ))}
  146. </StyledIssueDiff>
  147. );
  148. }
  149. }
  150. export default withApi(IssueDiff);
  151. // required for tests which do not provide API as context
  152. export {IssueDiff};
  153. const StyledIssueDiff = styled('div', {
  154. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'loading',
  155. })<Pick<State, 'loading'>>`
  156. background-color: ${p => p.theme.backgroundSecondary};
  157. overflow: auto;
  158. padding: ${space(1)};
  159. flex: 1;
  160. display: flex;
  161. flex-direction: column;
  162. ${p =>
  163. p.loading &&
  164. `
  165. background-color: ${p.theme.background};
  166. justify-content: center;
  167. align-items: center;
  168. `};
  169. `;