stacktracePreview.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import StackTraceContent from 'sentry/components/events/interfaces/crashContent/stackTrace/content';
  4. import StackTraceContentV2 from 'sentry/components/events/interfaces/crashContent/stackTrace/contentV2';
  5. import StackTraceContentV3 from 'sentry/components/events/interfaces/crashContent/stackTrace/contentV3';
  6. import {isStacktraceNewestFirst} from 'sentry/components/events/interfaces/utils';
  7. import {Body, Hovercard} from 'sentry/components/hovercard';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {Organization, PlatformType} from 'sentry/types';
  12. import {EntryType, Event} from 'sentry/types/event';
  13. import {StacktraceType} from 'sentry/types/stacktrace';
  14. import {defined} from 'sentry/utils';
  15. import {isNativePlatform} from 'sentry/utils/platform';
  16. import useApi from 'sentry/utils/useApi';
  17. import findBestThread from './events/interfaces/threads/threadSelector/findBestThread';
  18. import getThreadStacktrace from './events/interfaces/threads/threadSelector/getThreadStacktrace';
  19. const REQUEST_DELAY = 100;
  20. const HOVERCARD_CONTENT_DELAY = 400;
  21. export const STACKTRACE_PREVIEW_TOOLTIP_DELAY = 1000;
  22. function getStacktrace(event: Event): StacktraceType | null {
  23. const exceptionsWithStacktrace =
  24. event.entries
  25. .find(e => e.type === EntryType.EXCEPTION)
  26. ?.data?.values.filter(({stacktrace}) => defined(stacktrace)) ?? [];
  27. const exceptionStacktrace: StacktraceType | undefined = isStacktraceNewestFirst()
  28. ? exceptionsWithStacktrace[exceptionsWithStacktrace.length - 1]?.stacktrace
  29. : exceptionsWithStacktrace[0]?.stacktrace;
  30. if (exceptionStacktrace) {
  31. return exceptionStacktrace;
  32. }
  33. const threads =
  34. event.entries.find(e => e.type === EntryType.THREADS)?.data?.values ?? [];
  35. const bestThread = findBestThread(threads);
  36. if (!bestThread) {
  37. return null;
  38. }
  39. const bestThreadStacktrace = getThreadStacktrace(false, bestThread);
  40. if (bestThreadStacktrace) {
  41. return bestThreadStacktrace;
  42. }
  43. return null;
  44. }
  45. function StackTracePreviewContent({
  46. event,
  47. stacktrace,
  48. orgFeatures = [],
  49. groupingCurrentLevel,
  50. }: {
  51. event: Event;
  52. stacktrace: StacktraceType;
  53. groupingCurrentLevel?: number;
  54. orgFeatures?: string[];
  55. }) {
  56. const includeSystemFrames = useMemo(() => {
  57. return stacktrace?.frames?.every(frame => !frame.inApp) ?? false;
  58. }, [stacktrace]);
  59. const framePlatform = stacktrace?.frames?.find(frame => !!frame.platform)?.platform;
  60. const platform = (framePlatform ?? event.platform ?? 'other') as PlatformType;
  61. const newestFirst = isStacktraceNewestFirst();
  62. const commonProps = {
  63. data: stacktrace,
  64. expandFirstFrame: false,
  65. includeSystemFrames,
  66. platform,
  67. newestFirst,
  68. event,
  69. isHoverPreviewed: true,
  70. };
  71. if (orgFeatures.includes('native-stack-trace-v2') && isNativePlatform(platform)) {
  72. return (
  73. <StackTraceContentV3 {...commonProps} groupingCurrentLevel={groupingCurrentLevel} />
  74. );
  75. }
  76. if (orgFeatures.includes('grouping-stacktrace-ui')) {
  77. return (
  78. <StackTraceContentV2 {...commonProps} groupingCurrentLevel={groupingCurrentLevel} />
  79. );
  80. }
  81. return <StackTraceContent {...commonProps} />;
  82. }
  83. type StackTracePreviewProps = {
  84. children: React.ReactNode;
  85. issueId: string;
  86. organization: Organization;
  87. className?: string;
  88. eventId?: string;
  89. groupingCurrentLevel?: number;
  90. projectSlug?: string;
  91. };
  92. function StackTracePreview(props: StackTracePreviewProps): React.ReactElement {
  93. const api = useApi();
  94. const [loadingVisible, setLoadingVisible] = useState<boolean>(false);
  95. const [status, setStatus] = useState<'loading' | 'loaded' | 'error'>('loading');
  96. const [event, setEvent] = useState<Event | null>(null);
  97. const delayTimeoutRef = useRef<number | undefined>(undefined);
  98. const loaderTimeoutRef = useRef<number | undefined>(undefined);
  99. useEffect(() => {
  100. return () => {
  101. window.clearTimeout(loaderTimeoutRef.current);
  102. window.clearTimeout(delayTimeoutRef.current);
  103. };
  104. }, []);
  105. const fetchData = useCallback(async () => {
  106. // Data is already loaded
  107. if (event) {
  108. return;
  109. }
  110. // These are required props to load data
  111. if (!props.issueId && !props.eventId && !props.projectSlug) {
  112. return;
  113. }
  114. loaderTimeoutRef.current = window.setTimeout(
  115. () => setLoadingVisible(true),
  116. HOVERCARD_CONTENT_DELAY
  117. );
  118. try {
  119. const evt = await api.requestPromise(
  120. props.eventId && props.projectSlug
  121. ? `/projects/${props.organization.slug}/${props.projectSlug}/events/${props.eventId}/`
  122. : `/issues/${props.issueId}/events/latest/?collapse=stacktraceOnly`
  123. );
  124. window.clearTimeout(loaderTimeoutRef.current);
  125. setEvent(evt);
  126. setStatus('loaded');
  127. setLoadingVisible(false);
  128. } catch {
  129. window.clearTimeout(loaderTimeoutRef.current);
  130. setEvent(null);
  131. setStatus('error');
  132. setLoadingVisible(false);
  133. }
  134. }, [
  135. event,
  136. api,
  137. props.organization.slug,
  138. props.projectSlug,
  139. props.eventId,
  140. props.issueId,
  141. ]);
  142. const handleMouseEnter = useCallback(() => {
  143. window.clearTimeout(delayTimeoutRef.current);
  144. delayTimeoutRef.current = window.setTimeout(fetchData, REQUEST_DELAY);
  145. }, [fetchData]);
  146. const handleMouseLeave = useCallback(() => {
  147. window.clearTimeout(delayTimeoutRef.current);
  148. delayTimeoutRef.current = undefined;
  149. }, []);
  150. // Not sure why we need to stop propagation, maybe to prevent the
  151. // hovercard from closing? If we are doing this often, maybe it should be
  152. // part of the hovercard component.
  153. const handleStackTracePreviewClick = useCallback(
  154. (e: React.MouseEvent) => void e.stopPropagation(),
  155. []
  156. );
  157. const stacktrace = useMemo(() => (event ? getStacktrace(event) : null), [event]);
  158. return (
  159. <span
  160. className={props.className}
  161. onMouseEnter={handleMouseEnter}
  162. onMouseLeave={handleMouseLeave}
  163. >
  164. <StacktraceHovercard
  165. body={
  166. status === 'loading' && !loadingVisible ? null : status === 'loading' ? (
  167. <NoStackTraceWrapper onClick={handleStackTracePreviewClick}>
  168. <LoadingIndicator hideMessage size={32} />
  169. </NoStackTraceWrapper>
  170. ) : status === 'error' ? (
  171. <NoStackTraceWrapper onClick={handleStackTracePreviewClick}>
  172. {t('Failed to load stack trace.')}
  173. </NoStackTraceWrapper>
  174. ) : !stacktrace ? (
  175. <NoStackTraceWrapper onClick={handleStackTracePreviewClick}>
  176. {t('There is no stack trace available for this issue.')}
  177. </NoStackTraceWrapper>
  178. ) : !event ? null : (
  179. <div onClick={handleStackTracePreviewClick}>
  180. <StackTracePreviewContent
  181. event={event}
  182. stacktrace={stacktrace}
  183. groupingCurrentLevel={props.groupingCurrentLevel}
  184. orgFeatures={props.organization.features}
  185. />
  186. </div>
  187. )
  188. }
  189. displayTimeout={200}
  190. position="right"
  191. state={
  192. status === 'loading' && loadingVisible
  193. ? 'loading'
  194. : !stacktrace
  195. ? 'empty'
  196. : 'done'
  197. }
  198. tipBorderColor="border"
  199. tipColor="background"
  200. >
  201. {props.children}
  202. </StacktraceHovercard>
  203. </span>
  204. );
  205. }
  206. export {StackTracePreview};
  207. const StacktraceHovercard = styled(Hovercard)<{state: 'loading' | 'empty' | 'done'}>`
  208. /* Lower z-index to match the modals (10000 vs 10002) to allow stackTraceLinkModal be on top of stack trace preview. */
  209. z-index: ${p => p.theme.zIndex.modal};
  210. width: ${p => {
  211. if (p.state === 'loading') {
  212. return 'auto';
  213. }
  214. if (p.state === 'empty') {
  215. return '340px';
  216. }
  217. return '700px';
  218. }};
  219. ${Body} {
  220. padding: 0;
  221. max-height: 300px;
  222. overflow-y: auto;
  223. overscroll-behavior: contain;
  224. border-bottom-left-radius: ${p => p.theme.borderRadius};
  225. border-bottom-right-radius: ${p => p.theme.borderRadius};
  226. }
  227. .traceback {
  228. margin-bottom: 0;
  229. border: 0;
  230. box-shadow: none;
  231. }
  232. .loading {
  233. margin: 0 auto;
  234. .loading-indicator {
  235. /**
  236. * Overriding the .less file - for default 64px loader we have the width of border set to 6px
  237. * For 32px we therefore need 3px to keep the same thickness ratio
  238. */
  239. border-width: 3px;
  240. }
  241. }
  242. @media (max-width: ${p => p.theme.breakpoints.large}) {
  243. display: none;
  244. }
  245. `;
  246. const NoStackTraceWrapper = styled('div')`
  247. color: ${p => p.theme.subText};
  248. padding: ${space(1.5)};
  249. font-size: ${p => p.theme.fontSizeMedium};
  250. display: flex;
  251. align-items: center;
  252. justify-content: center;
  253. min-height: 56px;
  254. `;