stacktracePreview.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import * as React from 'react';
  2. import {withTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Client} from 'app/api';
  5. import {isStacktraceNewestFirst} from 'app/components/events/interfaces/stacktrace';
  6. import StacktraceContent from 'app/components/events/interfaces/stacktraceContent';
  7. import StacktraceContentV2 from 'app/components/events/interfaces/stacktraceContentV2';
  8. import Hovercard, {Body} from 'app/components/hovercard';
  9. import LoadingIndicator from 'app/components/loadingIndicator';
  10. import {t} from 'app/locale';
  11. import space from 'app/styles/space';
  12. import {Organization, PlatformType} from 'app/types';
  13. import {EntryType, Event} from 'app/types/event';
  14. import {StacktraceType} from 'app/types/stacktrace';
  15. import {defined} from 'app/utils';
  16. import {Theme} from 'app/utils/theme';
  17. import withApi from 'app/utils/withApi';
  18. import findBestThread from './events/interfaces/threads/threadSelector/findBestThread';
  19. import getThreadStacktrace from './events/interfaces/threads/threadSelector/getThreadStacktrace';
  20. const HOVERCARD_DELAY = 500;
  21. export const STACKTRACE_PREVIEW_TOOLTIP_DELAY = 1000;
  22. type Props = {
  23. issueId: string;
  24. organization: Organization;
  25. api: Client;
  26. theme: Theme;
  27. groupingCurrentLevel?: number;
  28. disablePreview?: boolean;
  29. eventId?: string;
  30. projectSlug?: string;
  31. className?: string;
  32. };
  33. type State = {
  34. loading: boolean;
  35. loadingVisible: boolean;
  36. event?: Event;
  37. };
  38. class StacktracePreview extends React.Component<Props, State> {
  39. state: State = {
  40. loading: true,
  41. loadingVisible: false,
  42. };
  43. loaderTimeout: number | null = null;
  44. fetchData = async () => {
  45. const {organization, api, issueId, eventId, projectSlug} = this.props;
  46. if (this.state.event || (!issueId && !(eventId && projectSlug))) {
  47. return;
  48. }
  49. this.loaderTimeout = window.setTimeout(() => {
  50. this.setState({loadingVisible: true});
  51. }, HOVERCARD_DELAY);
  52. try {
  53. const event = await api.requestPromise(
  54. eventId && projectSlug
  55. ? `/projects/${organization.slug}/${projectSlug}/events/${eventId}/`
  56. : `/issues/${issueId}/events/latest/`
  57. );
  58. clearTimeout(this.loaderTimeout);
  59. this.setState({event, loading: false, loadingVisible: false});
  60. } catch {
  61. clearTimeout(this.loaderTimeout);
  62. this.setState({loading: false, loadingVisible: false});
  63. }
  64. };
  65. handleStacktracePreviewClick = (event: React.MouseEvent) => {
  66. event.stopPropagation();
  67. };
  68. getStacktrace(): StacktraceType | undefined {
  69. const {event} = this.state;
  70. if (!event) {
  71. return undefined;
  72. }
  73. const exceptionsWithStacktrace =
  74. event.entries
  75. .find(e => e.type === EntryType.EXCEPTION)
  76. ?.data?.values.filter(({stacktrace}) => defined(stacktrace)) ?? [];
  77. const exceptionStacktrace: StacktraceType | undefined = isStacktraceNewestFirst()
  78. ? exceptionsWithStacktrace[exceptionsWithStacktrace.length - 1]?.stacktrace
  79. : exceptionsWithStacktrace[0]?.stacktrace;
  80. if (exceptionStacktrace) {
  81. return exceptionStacktrace;
  82. }
  83. const threads =
  84. event.entries.find(e => e.type === EntryType.THREADS)?.data?.values ?? [];
  85. const bestThread = findBestThread(threads);
  86. if (!bestThread) {
  87. return undefined;
  88. }
  89. const bestThreadStacktrace = getThreadStacktrace(false, bestThread);
  90. if (bestThreadStacktrace) {
  91. return bestThreadStacktrace;
  92. }
  93. return undefined;
  94. }
  95. renderHovercardBody(stacktrace: StacktraceType | undefined) {
  96. const {event, loading, loadingVisible} = this.state;
  97. if (loading && loadingVisible) {
  98. return (
  99. <NoStackTraceWrapper>
  100. <LoadingIndicator hideMessage size={32} />
  101. </NoStackTraceWrapper>
  102. );
  103. }
  104. if (loading) {
  105. return null;
  106. }
  107. if (!stacktrace) {
  108. return (
  109. <NoStackTraceWrapper onClick={this.handleStacktracePreviewClick}>
  110. {t("There's no stack trace available for this issue.")}
  111. </NoStackTraceWrapper>
  112. );
  113. }
  114. const {organization, groupingCurrentLevel} = this.props;
  115. if (event) {
  116. const platform = (event.platform ?? 'other') as PlatformType;
  117. return (
  118. <div onClick={this.handleStacktracePreviewClick}>
  119. {!!organization.features?.includes('grouping-stacktrace-ui') ? (
  120. <StacktraceContentV2
  121. data={stacktrace}
  122. expandFirstFrame={false}
  123. includeSystemFrames={(stacktrace.frames ?? []).every(frame => !frame.inApp)}
  124. platform={platform}
  125. newestFirst={isStacktraceNewestFirst()}
  126. event={event}
  127. groupingCurrentLevel={groupingCurrentLevel}
  128. isHoverPreviewed
  129. />
  130. ) : (
  131. <StacktraceContent
  132. data={stacktrace}
  133. expandFirstFrame={false}
  134. includeSystemFrames={(stacktrace.frames ?? []).every(frame => !frame.inApp)}
  135. platform={platform}
  136. newestFirst={isStacktraceNewestFirst()}
  137. event={event}
  138. isHoverPreviewed
  139. />
  140. )}
  141. </div>
  142. );
  143. }
  144. return null;
  145. }
  146. render() {
  147. const {children, disablePreview, theme, className} = this.props;
  148. const {loading, loadingVisible} = this.state;
  149. const stacktrace = this.getStacktrace();
  150. if (disablePreview) {
  151. return children;
  152. }
  153. return (
  154. <span className={className} onMouseEnter={this.fetchData}>
  155. <StyledHovercard
  156. body={this.renderHovercardBody(stacktrace)}
  157. position="right"
  158. modifiers={{
  159. flip: {
  160. enabled: false,
  161. },
  162. preventOverflow: {
  163. padding: 20,
  164. enabled: true,
  165. boundariesElement: 'viewport',
  166. },
  167. }}
  168. state={loading && loadingVisible ? 'loading' : !stacktrace ? 'empty' : 'done'}
  169. tipBorderColor={theme.border}
  170. tipColor={theme.background}
  171. >
  172. {children}
  173. </StyledHovercard>
  174. </span>
  175. );
  176. }
  177. }
  178. const StyledHovercard = styled(Hovercard)<{state: 'loading' | 'empty' | 'done'}>`
  179. /* Lower z-index to match the modals (10000 vs 10002) to allow stackTraceLinkModal be on top of stack trace preview. */
  180. z-index: ${p => p.theme.zIndex.modal};
  181. width: ${p => {
  182. if (p.state === 'loading') {
  183. return 'auto';
  184. }
  185. if (p.state === 'empty') {
  186. return '340px';
  187. }
  188. return '700px';
  189. }};
  190. ${Body} {
  191. padding: 0;
  192. max-height: 300px;
  193. overflow-y: auto;
  194. border-bottom-left-radius: ${p => p.theme.borderRadius};
  195. border-bottom-right-radius: ${p => p.theme.borderRadius};
  196. }
  197. .traceback {
  198. margin-bottom: 0;
  199. border: 0;
  200. box-shadow: none;
  201. }
  202. .loading {
  203. margin: 0 auto;
  204. .loading-indicator {
  205. /**
  206. * Overriding the .less file - for default 64px loader we have the width of border set to 6px
  207. * For 32px we therefore need 3px to keep the same thickness ratio
  208. */
  209. border-width: 3px;
  210. }
  211. }
  212. @media (max-width: ${p => p.theme.breakpoints[2]}) {
  213. display: none;
  214. }
  215. `;
  216. const NoStackTraceWrapper = styled('div')`
  217. color: ${p => p.theme.subText};
  218. padding: ${space(1.5)};
  219. font-size: ${p => p.theme.fontSizeMedium};
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. min-height: 56px;
  224. `;
  225. export default withApi(withTheme(StacktracePreview));