|
| 1 | +import {Fragment, useState} from 'react'; |
| 2 | +import styled from '@emotion/styled'; |
| 3 | +import isNil from 'lodash/isNil'; |
| 4 | + |
| 5 | +import {isStacktraceNewestFirst} from 'app/components/events/interfaces/stacktrace'; |
| 6 | +import Pill from 'app/components/pill'; |
| 7 | +import Pills from 'app/components/pills'; |
| 8 | +import {t} from 'app/locale'; |
| 9 | +import {PlatformType, Project} from 'app/types'; |
| 10 | +import {Event} from 'app/types/event'; |
| 11 | +import {Thread} from 'app/types/events'; |
| 12 | +import {STACK_TYPE, STACK_VIEW} from 'app/types/stacktrace'; |
| 13 | +import {defined} from 'app/utils'; |
| 14 | + |
| 15 | +import TraceEventDataSection from '../../traceEventDataSection'; |
| 16 | +import {DisplayOption} from '../../traceEventDataSection/displayOptions'; |
| 17 | +import Exception from '../crashContent/exception'; |
| 18 | +import StackTrace from '../crashContent/stackTrace'; |
| 19 | +import NoStackTraceMessage from '../noStackTraceMessage'; |
| 20 | +import ThreadSelector from '../threads/threadSelector'; |
| 21 | +import findBestThread from '../threads/threadSelector/findBestThread'; |
| 22 | +import getThreadException from '../threads/threadSelector/getThreadException'; |
| 23 | +import getThreadStacktrace from '../threads/threadSelector/getThreadStacktrace'; |
| 24 | + |
| 25 | +type ExceptionProps = React.ComponentProps<typeof Exception>; |
| 26 | + |
| 27 | +type Props = Pick<ExceptionProps, 'groupingCurrentLevel' | 'hasHierarchicalGrouping'> & { |
| 28 | + event: Event; |
| 29 | + projectId: Project['id']; |
| 30 | + type: string; |
| 31 | + data: { |
| 32 | + values?: Array<Thread>; |
| 33 | + }; |
| 34 | +}; |
| 35 | + |
| 36 | +type State = { |
| 37 | + stackType: STACK_TYPE; |
| 38 | + activeThread?: Thread; |
| 39 | +}; |
| 40 | + |
| 41 | +function getIntendedStackView(thread: Thread, event: Event) { |
| 42 | + const exception = getThreadException(event, thread); |
| 43 | + if (exception) { |
| 44 | + return !!exception.values.find(value => !!value.stacktrace?.hasSystemFrames) |
| 45 | + ? STACK_VIEW.APP |
| 46 | + : STACK_VIEW.FULL; |
| 47 | + } |
| 48 | + |
| 49 | + const stacktrace = getThreadStacktrace(false, thread); |
| 50 | + |
| 51 | + return stacktrace?.hasSystemFrames ? STACK_VIEW.APP : STACK_VIEW.FULL; |
| 52 | +} |
| 53 | + |
| 54 | +function Threads({ |
| 55 | + data, |
| 56 | + event, |
| 57 | + projectId, |
| 58 | + type, |
| 59 | + hasHierarchicalGrouping, |
| 60 | + groupingCurrentLevel, |
| 61 | +}: Props) { |
| 62 | + const [state, setState] = useState<State>(() => { |
| 63 | + const thread = defined(data.values) ? findBestThread(data.values) : undefined; |
| 64 | + return { |
| 65 | + activeThread: thread, |
| 66 | + stackType: STACK_TYPE.ORIGINAL, |
| 67 | + }; |
| 68 | + }); |
| 69 | + |
| 70 | + if (!data.values) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + const threads = data.values; |
| 75 | + const {stackType, activeThread} = state; |
| 76 | + |
| 77 | + const platform = (event.platform ?? 'other') as PlatformType; |
| 78 | + const hasMoreThanOneThread = threads.length > 1; |
| 79 | + const exception = getThreadException(event, activeThread); |
| 80 | + const stackView = activeThread ? getIntendedStackView(activeThread, event) : undefined; |
| 81 | + |
| 82 | + function renderPills() { |
| 83 | + const {id, name, current, crashed} = activeThread ?? {}; |
| 84 | + |
| 85 | + if (isNil(id) || !name) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + return ( |
| 90 | + <Pills> |
| 91 | + {!isNil(id) && <Pill name={t('id')} value={String(id)} />} |
| 92 | + {!!name?.trim() && <Pill name={t('name')} value={name} />} |
| 93 | + {current !== undefined && <Pill name={t('was active')} value={current} />} |
| 94 | + {crashed !== undefined && ( |
| 95 | + <Pill name={t('errored')} className={crashed ? 'false' : 'true'}> |
| 96 | + {crashed ? t('yes') : t('no')} |
| 97 | + </Pill> |
| 98 | + )} |
| 99 | + </Pills> |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + function renderContent({ |
| 104 | + recentFirst, |
| 105 | + raw, |
| 106 | + activeDisplayOptions, |
| 107 | + }: Parameters<React.ComponentProps<typeof TraceEventDataSection>['children']>[0]) { |
| 108 | + if (exception) { |
| 109 | + return ( |
| 110 | + <Exception |
| 111 | + stackType={stackType} |
| 112 | + stackView={ |
| 113 | + raw |
| 114 | + ? STACK_VIEW.RAW |
| 115 | + : activeDisplayOptions.includes(DisplayOption.FULL_STACK_TRACE) |
| 116 | + ? STACK_VIEW.FULL |
| 117 | + : STACK_VIEW.APP |
| 118 | + } |
| 119 | + projectId={projectId} |
| 120 | + newestFirst={recentFirst} |
| 121 | + event={event} |
| 122 | + platform={platform} |
| 123 | + values={exception.values} |
| 124 | + groupingCurrentLevel={groupingCurrentLevel} |
| 125 | + hasHierarchicalGrouping={hasHierarchicalGrouping} |
| 126 | + /> |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + const stacktrace = getThreadStacktrace( |
| 131 | + stackType !== STACK_TYPE.ORIGINAL, |
| 132 | + activeThread |
| 133 | + ); |
| 134 | + |
| 135 | + if (stacktrace) { |
| 136 | + return ( |
| 137 | + <StackTrace |
| 138 | + stacktrace={stacktrace} |
| 139 | + stackView={ |
| 140 | + raw |
| 141 | + ? STACK_VIEW.RAW |
| 142 | + : activeDisplayOptions.includes(DisplayOption.FULL_STACK_TRACE) |
| 143 | + ? STACK_VIEW.FULL |
| 144 | + : STACK_VIEW.APP |
| 145 | + } |
| 146 | + newestFirst={recentFirst} |
| 147 | + event={event} |
| 148 | + platform={platform} |
| 149 | + groupingCurrentLevel={groupingCurrentLevel} |
| 150 | + hasHierarchicalGrouping={hasHierarchicalGrouping} |
| 151 | + /> |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + return ( |
| 156 | + <NoStackTraceMessage |
| 157 | + message={activeThread?.crashed ? t('Thread Errored') : undefined} |
| 158 | + /> |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + function getTitle() { |
| 163 | + if (hasMoreThanOneThread && activeThread) { |
| 164 | + return ( |
| 165 | + <ThreadSelector |
| 166 | + threads={threads} |
| 167 | + activeThread={activeThread} |
| 168 | + event={event} |
| 169 | + onChange={thread => { |
| 170 | + setState({ |
| 171 | + ...state, |
| 172 | + activeThread: thread, |
| 173 | + stackType: STACK_TYPE.ORIGINAL, |
| 174 | + }); |
| 175 | + }} |
| 176 | + exception={exception} |
| 177 | + fullWidth |
| 178 | + /> |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + return <Title>{t('Stack Trace')}</Title>; |
| 183 | + } |
| 184 | + |
| 185 | + return ( |
| 186 | + <TraceEventDataSection |
| 187 | + type={type} |
| 188 | + stackType={stackType} |
| 189 | + projectId={projectId} |
| 190 | + eventId={event.id} |
| 191 | + recentFirst={isStacktraceNewestFirst()} |
| 192 | + fullStackTrace={stackView === STACK_VIEW.FULL} |
| 193 | + title={getTitle()} |
| 194 | + platform={platform} |
| 195 | + showPermalink={!hasMoreThanOneThread} |
| 196 | + wrapTitle={false} |
| 197 | + > |
| 198 | + {childrenProps => ( |
| 199 | + <Fragment> |
| 200 | + {renderPills()} |
| 201 | + {renderContent(childrenProps)} |
| 202 | + </Fragment> |
| 203 | + )} |
| 204 | + </TraceEventDataSection> |
| 205 | + ); |
| 206 | +} |
| 207 | + |
| 208 | +export default Threads; |
| 209 | + |
| 210 | +const Title = styled('h3')` |
| 211 | + margin-bottom: 0; |
| 212 | +`; |
0 commit comments