index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {Component} from 'react';
  2. import isPropValid from '@emotion/is-prop-valid';
  3. import styled from '@emotion/styled';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import {Client} from 'sentry/api';
  6. import Button from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import SplitDiff from 'sentry/components/splitDiff';
  10. import {t} from 'sentry/locale';
  11. import space from 'sentry/styles/space';
  12. import {Project} from 'sentry/types';
  13. import getStacktraceBody from 'sentry/utils/getStacktraceBody';
  14. import withApi from 'sentry/utils/withApi';
  15. import renderGroupingInfo from './renderGroupingInfo';
  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. orgId: string;
  25. project: Project;
  26. targetIssueId: string;
  27. baseEventId?: string;
  28. className?: string;
  29. targetEventId?: string;
  30. };
  31. type State = {
  32. baseEvent: Array<string>;
  33. groupingDiff: boolean;
  34. loading: boolean;
  35. targetEvent: Array<string>;
  36. SplitDiffAsync?: typeof SplitDiff;
  37. };
  38. class IssueDiff extends Component<Props, State> {
  39. static defaultProps: DefaultProps = defaultProps;
  40. state: State = {
  41. loading: true,
  42. groupingDiff: false,
  43. baseEvent: [],
  44. targetEvent: [],
  45. // `SplitDiffAsync` is an async-loaded component
  46. // This will eventually contain a reference to the exported component from `./splitDiff`
  47. SplitDiffAsync: undefined,
  48. };
  49. componentDidMount() {
  50. this.fetchData();
  51. }
  52. fetchData() {
  53. const {baseIssueId, targetIssueId, baseEventId, targetEventId} = this.props;
  54. // Fetch component and event data
  55. Promise.all([
  56. import('../splitDiff'),
  57. this.fetchEventData(baseIssueId, baseEventId ?? 'latest'),
  58. this.fetchEventData(targetIssueId, targetEventId ?? 'latest'),
  59. ])
  60. .then(([{default: SplitDiffAsync}, baseEvent, targetEvent]) => {
  61. this.setState({
  62. SplitDiffAsync,
  63. baseEvent,
  64. targetEvent,
  65. loading: false,
  66. });
  67. })
  68. .catch(() => {
  69. addErrorMessage(t('Error loading events'));
  70. });
  71. }
  72. toggleDiffMode = () => {
  73. this.setState(
  74. state => ({groupingDiff: !state.groupingDiff, loading: true}),
  75. this.fetchData
  76. );
  77. };
  78. fetchEventData = async (issueId: string, eventId: string) => {
  79. const {orgId, project, api} = this.props;
  80. const {groupingDiff} = this.state;
  81. let paramEventId = eventId;
  82. if (eventId === 'latest') {
  83. const event = await api.requestPromise(`/issues/${issueId}/events/latest/`);
  84. paramEventId = event.eventID;
  85. }
  86. if (groupingDiff) {
  87. const groupingInfo = await api.requestPromise(
  88. `/projects/${orgId}/${project.slug}/events/${paramEventId}/grouping-info/`
  89. );
  90. return renderGroupingInfo(groupingInfo);
  91. }
  92. const event = await api.requestPromise(
  93. `/projects/${orgId}/${project.slug}/events/${paramEventId}/`
  94. );
  95. return getStacktraceBody(event);
  96. };
  97. render() {
  98. const {className, project} = this.props;
  99. const {
  100. SplitDiffAsync: DiffComponent,
  101. loading,
  102. groupingDiff,
  103. baseEvent,
  104. targetEvent,
  105. } = this.state;
  106. const showDiffToggle = project.features.includes('similarity-view-v2');
  107. return (
  108. <StyledIssueDiff className={className} loading={loading}>
  109. {loading && <LoadingIndicator />}
  110. {!loading && showDiffToggle && (
  111. <HeaderWrapper>
  112. <ButtonBar merged active={groupingDiff ? 'grouping' : 'event'}>
  113. <Button barId="event" size="sm" onClick={this.toggleDiffMode}>
  114. {t('Diff stack trace and message')}
  115. </Button>
  116. <Button barId="grouping" size="sm" onClick={this.toggleDiffMode}>
  117. {t('Diff grouping information')}
  118. </Button>
  119. </ButtonBar>
  120. </HeaderWrapper>
  121. )}
  122. {!loading &&
  123. DiffComponent &&
  124. baseEvent.map((value, i) => (
  125. <DiffComponent
  126. key={i}
  127. base={value}
  128. target={targetEvent[i] ?? ''}
  129. type="words"
  130. />
  131. ))}
  132. </StyledIssueDiff>
  133. );
  134. }
  135. }
  136. export default withApi(IssueDiff);
  137. // required for tests which do not provide API as context
  138. export {IssueDiff};
  139. const StyledIssueDiff = styled('div', {
  140. shouldForwardProp: p => typeof p === 'string' && isPropValid(p) && p !== 'loading',
  141. })<Pick<State, 'loading'>>`
  142. background-color: ${p => p.theme.backgroundSecondary};
  143. overflow: auto;
  144. padding: ${space(1)};
  145. flex: 1;
  146. display: flex;
  147. flex-direction: column;
  148. ${p =>
  149. p.loading &&
  150. `
  151. background-color: ${p.theme.background};
  152. justify-content: center;
  153. align-items: center;
  154. `};
  155. `;
  156. const HeaderWrapper = styled('div')`
  157. display: flex;
  158. align-items: center;
  159. margin-bottom: ${space(2)};
  160. `;