stacktracePreview.tsx 6.5 KB

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