codeLocations.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import {useCallback, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton';
  5. import ContextLine from 'sentry/components/events/interfaces/frame/contextLine';
  6. import DefaultTitle from 'sentry/components/events/interfaces/frame/defaultTitle';
  7. import {IconChevron} from 'sentry/icons';
  8. import {t, tn} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {Frame} from 'sentry/types';
  11. import {hasDDMExperimentalFeature} from 'sentry/utils/metrics/features';
  12. import {useMetricsCodeLocations} from 'sentry/utils/metrics/useMetricsCodeLocations';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import Collapsible from '../../components/collapsible';
  15. import {MetricCodeLocationFrame, MetricMetaCodeLocation} from '../../utils/metrics/index';
  16. export function CodeLocations({mri}: {mri: string}) {
  17. const {data} = useMetricsCodeLocations(mri);
  18. // Keeps track of which code location has expanded source context
  19. const [expandedCodeLocation, setExpandedCodeLocation] = useState(null);
  20. // Keeps track of whether the code locations are collapsed or not
  21. const [collapsed, setCollapsed] = useState(true);
  22. const organization = useOrganization();
  23. const toggleExpandedLocation = useCallback(
  24. index => {
  25. if (expandedCodeLocation === index) {
  26. setExpandedCodeLocation(null);
  27. } else {
  28. setExpandedCodeLocation(index);
  29. }
  30. },
  31. [expandedCodeLocation]
  32. );
  33. if (!hasDDMExperimentalFeature(organization)) {
  34. return null;
  35. }
  36. if (!Array.isArray(data?.codeLocations) || data?.codeLocations.length === 0) {
  37. return null;
  38. }
  39. const codeLocations = data?.codeLocations ?? [];
  40. // We only want to show the first 5 code locations
  41. const reversedCodeLocations = codeLocations.slice(0, 5);
  42. return (
  43. <CodeLocationsWrapper>
  44. <Collapsible
  45. maxVisibleItems={1}
  46. expandButton={({onExpand, numberOfHiddenItems}) => (
  47. <CollapsibleButton
  48. priority="link"
  49. onClick={() => {
  50. setCollapsed(false);
  51. onExpand();
  52. }}
  53. >
  54. {tn(
  55. 'Show %s more code location',
  56. 'Show %s more code locations',
  57. numberOfHiddenItems
  58. )}
  59. </CollapsibleButton>
  60. )}
  61. collapseButton={({onCollapse}) => (
  62. <CollapsibleButton
  63. priority="link"
  64. onClick={() => {
  65. setCollapsed(true);
  66. onCollapse();
  67. }}
  68. >
  69. {t('Collapse')}
  70. </CollapsibleButton>
  71. )}
  72. >
  73. {reversedCodeLocations.map((location, index) => (
  74. <CodeLocation
  75. key={`location-${index}`}
  76. codeLocation={location}
  77. showContext={expandedCodeLocation === index}
  78. handleShowContext={() => toggleExpandedLocation(index)}
  79. isFirst={index === 0}
  80. isLast={collapsed || index === reversedCodeLocations.length - 1}
  81. />
  82. ))}
  83. </Collapsible>
  84. </CodeLocationsWrapper>
  85. );
  86. }
  87. type CodeLocationProps = {
  88. codeLocation: MetricMetaCodeLocation;
  89. handleShowContext: () => void;
  90. showContext: boolean;
  91. isFirst?: boolean;
  92. isLast?: boolean;
  93. };
  94. function CodeLocation({
  95. codeLocation,
  96. showContext,
  97. handleShowContext,
  98. isFirst,
  99. isLast,
  100. }: CodeLocationProps) {
  101. const frameToShow = codeLocation.frames[0];
  102. if (!frameToShow) {
  103. return null;
  104. }
  105. const hasContext = !!frameToShow.contextLine;
  106. return (
  107. <CodeLocationWrapper>
  108. <DefaultLineWrapper
  109. onClick={() => {
  110. if (!hasContext) {
  111. return;
  112. }
  113. handleShowContext();
  114. }}
  115. isFirst={isFirst}
  116. isLast={isLast}
  117. hasContext={hasContext}
  118. >
  119. <DefaultLine className="title" showContext={showContext}>
  120. <DefaultLineTitleWrapper>
  121. <LeftLineTitle>
  122. <DefaultTitle
  123. frame={frameToShow as Frame}
  124. isHoverPreviewed={false}
  125. platform="other"
  126. />
  127. </LeftLineTitle>
  128. <DefaultLineActionButtons>
  129. <CopyToClipboardButton
  130. text={`${frameToShow.filename}:${frameToShow.lineNo}`}
  131. size="zero"
  132. iconSize="xs"
  133. borderless
  134. />
  135. <ToggleCodeLocationContextButton
  136. disabled={!hasContext}
  137. isToggled={showContext}
  138. handleToggle={handleShowContext}
  139. />
  140. </DefaultLineActionButtons>
  141. </DefaultLineTitleWrapper>
  142. </DefaultLine>
  143. {showContext && hasContext && (
  144. <CodeLocationContext frame={frameToShow} isLast={isLast} />
  145. )}
  146. </DefaultLineWrapper>
  147. </CodeLocationWrapper>
  148. );
  149. }
  150. type ToggleCodeLocationContextButtonProps = {
  151. disabled: boolean;
  152. handleToggle: () => void;
  153. isToggled: boolean;
  154. };
  155. function ToggleCodeLocationContextButton({
  156. disabled,
  157. isToggled,
  158. handleToggle,
  159. }: ToggleCodeLocationContextButtonProps) {
  160. return (
  161. <Button
  162. title={disabled ? t('No context available') : t('Toggle Context')}
  163. size="zero"
  164. onClick={handleToggle}
  165. disabled={disabled}
  166. >
  167. {/* legacy size is deprecated but the icon is too big without it */}
  168. <IconChevron direction={isToggled ? 'up' : 'down'} size="xs" legacySize="8px" />
  169. </Button>
  170. );
  171. }
  172. type CodeLocationContextProps = {
  173. frame: MetricCodeLocationFrame;
  174. isLast?: boolean;
  175. };
  176. function CodeLocationContext({frame, isLast}: CodeLocationContextProps) {
  177. const lineNo = frame.lineNo ?? 0;
  178. const preContextLines: [number, string][] = useMemo(
  179. () => frame.preContext?.map((line, index) => [lineNo - 5 + index, line]) ?? [],
  180. [frame.preContext, lineNo]
  181. );
  182. const postContextLines: [number, string][] = useMemo(
  183. () => frame.postContext?.map((line, index) => [lineNo + index, line]) ?? [],
  184. [frame.postContext, lineNo]
  185. );
  186. return (
  187. <SourceContextWrapper isLast={isLast}>
  188. {preContextLines.map(line => (
  189. <ContextLine key={`pre-${line[1]}`} line={line} isActive={false} />
  190. ))}
  191. <ContextLine line={[lineNo, frame.contextLine ?? '']} isActive />
  192. {postContextLines.map(line => (
  193. <ContextLine key={`post-${line[1]}`} line={line} isActive={false} />
  194. ))}
  195. </SourceContextWrapper>
  196. );
  197. }
  198. const CodeLocationWrapper = styled('div')`
  199. display: flex;
  200. `;
  201. const DefaultLineActionButtons = styled('div')`
  202. display: flex;
  203. gap: ${space(1)};
  204. `;
  205. const CodeLocationsWrapper = styled('div')`
  206. margin-top: ${space(1)};
  207. & code {
  208. font-family: inherit;
  209. font-size: inherit;
  210. }
  211. `;
  212. const SourceContextWrapper = styled('div')<{isLast?: boolean}>`
  213. word-wrap: break-word;
  214. font-family: ${p => p.theme.text.familyMono};
  215. font-size: ${p => p.theme.fontSizeSmall};
  216. /* TODO(ddm): find out how it is done on the issues page */
  217. line-height: 24px;
  218. min-height: ${space(3)};
  219. white-space: pre;
  220. white-space: pre-wrap;
  221. background-color: ${p => p.theme.background};
  222. `;
  223. const DefaultLineWrapper = styled('div')<{
  224. hasContext?: boolean;
  225. isFirst?: boolean;
  226. isLast?: boolean;
  227. }>`
  228. :hover {
  229. cursor: ${p => p.hasContext && 'pointer'};
  230. }
  231. flex-grow: 1;
  232. border-top-left-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)};
  233. border-top-right-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)};
  234. border-bottom-left-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)};
  235. border-bottom-right-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)};
  236. border: 1px solid ${p => p.theme.border};
  237. border-top: ${p => (p.isFirst ? `1px solid ${p.theme.border}` : 'none')};
  238. background-color: ${p => p.theme.backgroundTertiary};
  239. `;
  240. const DefaultLine = styled('div')<{showContext?: boolean}>`
  241. display: flex;
  242. justify-content: space-between;
  243. align-items: center;
  244. border-bottom: ${p => (p.showContext ? `1px solid ${p.theme.border}` : 'none')};
  245. `;
  246. const DefaultLineTitleWrapper = styled('div')`
  247. width: 100%;
  248. display: flex;
  249. align-items: center;
  250. justify-content: space-between;
  251. font-size: ${p => p.theme.codeFontSize};
  252. line-height: ${p => p.theme.fontSizeLarge};
  253. font-style: normal;
  254. padding: ${space(0.75)} ${space(3)} ${space(0.75)} ${space(1.5)};
  255. word-break: break-all;
  256. word-break: break-word;
  257. `;
  258. const LeftLineTitle = styled('div')`
  259. display: flex;
  260. flex-wrap: wrap;
  261. align-items: center;
  262. `;
  263. // done to align the collapsible button with the text in default line title
  264. const CollapsibleButton = styled(Button)`
  265. margin-top: ${space(0.25)};
  266. margin-left: ${space(2)};
  267. `;