codeLocations.tsx 7.5 KB

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