codeLocations.tsx 7.4 KB

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