issues.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. import {useMemo} from 'react';
  2. import type {Theme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  5. import Count from 'sentry/components/count';
  6. import EventOrGroupExtraDetails from 'sentry/components/eventOrGroupExtraDetails';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import Panel from 'sentry/components/panels/panel';
  10. import PanelHeader from 'sentry/components/panels/panelHeader';
  11. import PanelItem from 'sentry/components/panels/panelItem';
  12. import {IconWrapper} from 'sentry/components/sidebarSection';
  13. import GroupChart from 'sentry/components/stream/groupChart';
  14. import {IconUser} from 'sentry/icons';
  15. import {t, tct, tn} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Group} from 'sentry/types/group';
  18. import type {Organization} from 'sentry/types/organization';
  19. import type {
  20. TraceError,
  21. TraceErrorOrIssue,
  22. TracePerformanceIssue,
  23. } from 'sentry/utils/performance/quickTrace/types';
  24. import {useApiQuery} from 'sentry/utils/queryClient';
  25. import type {
  26. TraceTree,
  27. TraceTreeNode,
  28. } from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  29. import {TraceDrawerComponents} from '../styles';
  30. import {IssueSummary} from './issueSummary';
  31. type IssueProps = {
  32. issue: TraceErrorOrIssue;
  33. organization: Organization;
  34. };
  35. const MAX_DISPLAYED_ISSUES_COUNT = 3;
  36. const TABLE_WIDTH_BREAKPOINTS = {
  37. FIRST: 800,
  38. SECOND: 600,
  39. THIRD: 500,
  40. FOURTH: 400,
  41. };
  42. const issueOrderPriority: Record<keyof Theme['level'], number> = {
  43. fatal: 0,
  44. error: 1,
  45. warning: 2,
  46. sample: 3,
  47. info: 4,
  48. default: 5,
  49. unknown: 6,
  50. };
  51. function sortIssuesByLevel(a: TraceError, b: TraceError): number {
  52. // If the level is not defined in the priority map, default to unknown
  53. const aPriority = issueOrderPriority[a.level] ?? issueOrderPriority.unknown;
  54. const bPriority = issueOrderPriority[b.level] ?? issueOrderPriority.unknown;
  55. return aPriority - bPriority;
  56. }
  57. function Issue(props: IssueProps) {
  58. const {
  59. isPending,
  60. data: fetchedIssue,
  61. isError,
  62. } = useApiQuery<Group>(
  63. [
  64. `/issues/${props.issue.issue_id}/`,
  65. {
  66. query: {
  67. collapse: 'release',
  68. expand: 'inbox',
  69. },
  70. },
  71. ],
  72. {
  73. staleTime: 2 * 60 * 1000,
  74. }
  75. );
  76. return isPending ? (
  77. <StyledLoadingIndicatorWrapper>
  78. <LoadingIndicator size={24} mini />
  79. </StyledLoadingIndicatorWrapper>
  80. ) : fetchedIssue ? (
  81. <StyledPanelItem>
  82. <IssueSummaryWrapper>
  83. <IssueSummary
  84. data={fetchedIssue}
  85. organization={props.organization}
  86. event_id={props.issue.event_id}
  87. />
  88. <EventOrGroupExtraDetails data={fetchedIssue} />
  89. </IssueSummaryWrapper>
  90. <ChartWrapper>
  91. <GroupChart
  92. stats={
  93. fetchedIssue.filtered
  94. ? fetchedIssue.filtered.stats?.['24h']
  95. : fetchedIssue.stats?.['24h']
  96. }
  97. secondaryStats={fetchedIssue.filtered ? fetchedIssue.stats?.['24h'] : []}
  98. showSecondaryPoints
  99. showMarkLine
  100. />
  101. </ChartWrapper>
  102. <EventsWrapper>
  103. <PrimaryCount
  104. value={fetchedIssue.filtered ? fetchedIssue.filtered.count : fetchedIssue.count}
  105. />
  106. </EventsWrapper>
  107. <UserCountWrapper>
  108. <PrimaryCount
  109. value={
  110. fetchedIssue.filtered
  111. ? fetchedIssue.filtered.userCount
  112. : fetchedIssue.userCount
  113. }
  114. />
  115. </UserCountWrapper>
  116. <AssineeWrapper>
  117. {fetchedIssue.assignedTo ? (
  118. <ActorAvatar actor={fetchedIssue.assignedTo} hasTooltip size={24} />
  119. ) : (
  120. <StyledIconWrapper>
  121. <IconUser size="md" />
  122. </StyledIconWrapper>
  123. )}
  124. </AssineeWrapper>
  125. </StyledPanelItem>
  126. ) : isError ? (
  127. <LoadingError message={t('Failed to fetch issue')} />
  128. ) : null;
  129. }
  130. type IssueListProps = {
  131. issues: TraceErrorOrIssue[];
  132. node: TraceTreeNode<TraceTree.NodeValue>;
  133. organization: Organization;
  134. };
  135. export function IssueList({issues, node, organization}: IssueListProps) {
  136. const uniqueErrorIssues = useMemo(() => {
  137. const unique: TraceError[] = [];
  138. const seenIssues: Set<number> = new Set();
  139. for (const issue of node.errors) {
  140. if (seenIssues.has(issue.issue_id)) {
  141. continue;
  142. }
  143. seenIssues.add(issue.issue_id);
  144. unique.push(issue);
  145. }
  146. return unique;
  147. // eslint-disable-next-line react-hooks/exhaustive-deps
  148. }, [node, node.errors.size]);
  149. const uniquePerformanceIssues = useMemo(() => {
  150. const unique: TracePerformanceIssue[] = [];
  151. const seenIssues: Set<number> = new Set();
  152. for (const issue of node.performance_issues) {
  153. if (seenIssues.has(issue.issue_id)) {
  154. continue;
  155. }
  156. seenIssues.add(issue.issue_id);
  157. unique.push(issue);
  158. }
  159. return unique;
  160. // eslint-disable-next-line react-hooks/exhaustive-deps
  161. }, [node, node.performance_issues.size]);
  162. const uniqueIssues = useMemo(() => {
  163. return [...uniquePerformanceIssues, ...uniqueErrorIssues.sort(sortIssuesByLevel)];
  164. }, [uniqueErrorIssues, uniquePerformanceIssues]);
  165. if (!issues.length) {
  166. return null;
  167. }
  168. return (
  169. <StyledPanel>
  170. <IssueListHeader
  171. node={node}
  172. errorIssues={uniqueErrorIssues}
  173. performanceIssues={uniquePerformanceIssues}
  174. />
  175. {uniqueIssues.slice(0, MAX_DISPLAYED_ISSUES_COUNT).map((issue, index) => (
  176. <Issue key={index} issue={issue} organization={organization} />
  177. ))}
  178. </StyledPanel>
  179. );
  180. }
  181. function IssueListHeader({
  182. node,
  183. errorIssues,
  184. performanceIssues,
  185. }: {
  186. errorIssues: TraceError[];
  187. node: TraceTreeNode<TraceTree.NodeValue>;
  188. performanceIssues: TracePerformanceIssue[];
  189. }) {
  190. const [singular, plural] = useMemo((): [string, string] => {
  191. const label = [t('Issue'), t('Issues')] as [string, string];
  192. for (const event of errorIssues) {
  193. if (event.level === 'error' || event.level === 'fatal') {
  194. return [t('Error'), t('Errors')];
  195. }
  196. }
  197. return label;
  198. }, [errorIssues]);
  199. return (
  200. <StyledPanelHeader disablePadding>
  201. <IssueHeading>
  202. {errorIssues.length + performanceIssues.length > MAX_DISPLAYED_ISSUES_COUNT
  203. ? tct(`[count]+ issues, [link]`, {
  204. count: MAX_DISPLAYED_ISSUES_COUNT,
  205. link: <StyledIssuesLink node={node}>{t('View All')}</StyledIssuesLink>,
  206. })
  207. : errorIssues.length > 0 && performanceIssues.length === 0
  208. ? tct('[count] [text]', {
  209. count: errorIssues.length,
  210. text: errorIssues.length > 1 ? plural : singular,
  211. })
  212. : performanceIssues.length > 0 && errorIssues.length === 0
  213. ? tct('[count] [text]', {
  214. count: performanceIssues.length,
  215. text: tn(
  216. 'Performance issue',
  217. 'Performance Issues',
  218. performanceIssues.length
  219. ),
  220. })
  221. : tct(
  222. '[errors] [errorsText] and [performance_issues] [performanceIssuesText]',
  223. {
  224. errors: errorIssues.length,
  225. performance_issues: performanceIssues.length,
  226. errorsText: errorIssues.length > 1 ? plural : singular,
  227. performanceIssuesText: tn(
  228. 'performance issue',
  229. 'performance issues',
  230. performanceIssues.length
  231. ),
  232. }
  233. )}
  234. </IssueHeading>
  235. <GraphHeading>{t('Graph')}</GraphHeading>
  236. <EventsHeading>{t('Events')}</EventsHeading>
  237. <UsersHeading>{t('Users')}</UsersHeading>
  238. <AssigneeHeading>{t('Assignee')}</AssigneeHeading>
  239. </StyledPanelHeader>
  240. );
  241. }
  242. const StyledIssuesLink = styled(TraceDrawerComponents.IssuesLink)`
  243. margin-left: ${space(0.5)};
  244. `;
  245. const Heading = styled('div')`
  246. display: flex;
  247. align-self: center;
  248. margin: 0 ${space(2)};
  249. width: 60px;
  250. color: ${p => p.theme.subText};
  251. `;
  252. const IssueHeading = styled(Heading)`
  253. flex: 1;
  254. width: 66.66%;
  255. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  256. width: 50%;
  257. }
  258. `;
  259. const GraphHeading = styled(Heading)`
  260. width: 160px;
  261. display: flex;
  262. justify-content: center;
  263. @container (width < ${TABLE_WIDTH_BREAKPOINTS.FIRST}px) {
  264. display: none;
  265. }
  266. `;
  267. const EventsHeading = styled(Heading)`
  268. @container (width < ${TABLE_WIDTH_BREAKPOINTS.SECOND}px) {
  269. display: none;
  270. }
  271. `;
  272. const UsersHeading = styled(Heading)`
  273. display: flex;
  274. justify-content: center;
  275. @container (width < ${TABLE_WIDTH_BREAKPOINTS.THIRD}px) {
  276. display: none;
  277. }
  278. `;
  279. const AssigneeHeading = styled(Heading)`
  280. @container (width < ${TABLE_WIDTH_BREAKPOINTS.FOURTH}px) {
  281. display: none;
  282. }
  283. `;
  284. const StyledPanel = styled(Panel)`
  285. container-type: inline-size;
  286. `;
  287. const StyledPanelHeader = styled(PanelHeader)`
  288. padding-top: ${space(1)};
  289. padding-bottom: ${space(1)};
  290. `;
  291. const StyledLoadingIndicatorWrapper = styled('div')`
  292. display: flex;
  293. justify-content: center;
  294. width: 100%;
  295. padding: ${space(2)} 0;
  296. height: 84px;
  297. /* Add a border between two rows of loading issue states */
  298. & + & {
  299. border-top: 1px solid ${p => p.theme.border};
  300. }
  301. `;
  302. const StyledIconWrapper = styled(IconWrapper)`
  303. margin: 0;
  304. `;
  305. const IssueSummaryWrapper = styled('div')`
  306. overflow: hidden;
  307. flex: 1;
  308. width: 66.66%;
  309. @media (min-width: ${p => p.theme.breakpoints.medium}) {
  310. width: 50%;
  311. }
  312. `;
  313. const ColumnWrapper = styled('div')`
  314. display: flex;
  315. justify-content: flex-end;
  316. align-self: center;
  317. width: 60px;
  318. margin: 0 ${space(2)};
  319. `;
  320. const EventsWrapper = styled(ColumnWrapper)`
  321. @container (width < ${TABLE_WIDTH_BREAKPOINTS.SECOND}px) {
  322. display: none;
  323. }
  324. `;
  325. const UserCountWrapper = styled(ColumnWrapper)`
  326. @container (width < ${TABLE_WIDTH_BREAKPOINTS.THIRD}px) {
  327. display: none;
  328. }
  329. `;
  330. const AssineeWrapper = styled(ColumnWrapper)`
  331. @container (width < ${TABLE_WIDTH_BREAKPOINTS.FOURTH}px) {
  332. display: none;
  333. }
  334. `;
  335. const ChartWrapper = styled('div')`
  336. width: 200px;
  337. align-self: center;
  338. @container (width < ${TABLE_WIDTH_BREAKPOINTS.FIRST}px) {
  339. display: none;
  340. }
  341. `;
  342. const PrimaryCount = styled(Count)`
  343. font-size: ${p => p.theme.fontSizeLarge};
  344. font-variant-numeric: tabular-nums;
  345. `;
  346. const StyledPanelItem = styled(PanelItem)`
  347. padding-top: ${space(1)};
  348. padding-bottom: ${space(1)};
  349. height: 84px;
  350. `;