codeLocations.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. onClick={(event: React.MouseEvent) => event.stopPropagation()}
  96. />
  97. <ToggleCodeLocationContextButton
  98. disabled={!hasContext}
  99. isToggled={showContext}
  100. handleToggle={toggleShowContext}
  101. />
  102. </DefaultLineActionButtons>
  103. </DefaultLineTitleWrapper>
  104. </DefaultLine>
  105. {showContext && hasContext && (
  106. <CodeLocationContext frame={frameToShow} isLast={isLast} />
  107. )}
  108. </DefaultLineWrapper>
  109. </CodeLocationWrapper>
  110. );
  111. }
  112. type ToggleCodeLocationContextButtonProps = {
  113. disabled: boolean;
  114. handleToggle: () => void;
  115. isToggled: boolean;
  116. };
  117. function ToggleCodeLocationContextButton({
  118. disabled,
  119. isToggled,
  120. handleToggle,
  121. }: ToggleCodeLocationContextButtonProps) {
  122. return (
  123. <Button
  124. title={disabled ? t('No context available') : t('Toggle Context')}
  125. size="zero"
  126. onClick={event => {
  127. event.stopPropagation();
  128. handleToggle();
  129. }}
  130. disabled={disabled}
  131. >
  132. {/* legacy size is deprecated but the icon is too big without it */}
  133. <IconChevron direction={isToggled ? 'up' : 'down'} size="xs" legacySize="8px" />
  134. </Button>
  135. );
  136. }
  137. type CodeLocationContextProps = {
  138. frame: MetricCodeLocationFrame;
  139. isLast?: boolean;
  140. };
  141. function CodeLocationContext({frame, isLast}: CodeLocationContextProps) {
  142. const lineNo = frame.lineNo ?? 0;
  143. const preContextLines: [number, string][] = useMemo(
  144. () => frame.preContext?.map((line, index) => [lineNo - 5 + index, line]) ?? [],
  145. [frame.preContext, lineNo]
  146. );
  147. const postContextLines: [number, string][] = useMemo(
  148. () => frame.postContext?.map((line, index) => [lineNo + index, line]) ?? [],
  149. [frame.postContext, lineNo]
  150. );
  151. return (
  152. <SourceContextWrapper isLast={isLast}>
  153. {preContextLines.map(line => (
  154. <ContextLine key={`pre-${line[1]}`} line={line} isActive={false} />
  155. ))}
  156. <ContextLine line={[lineNo, frame.contextLine ?? '']} isActive />
  157. {postContextLines.map(line => (
  158. <ContextLine key={`post-${line[1]}`} line={line} isActive={false} />
  159. ))}
  160. </SourceContextWrapper>
  161. );
  162. }
  163. const CodeLocationWrapper = styled('div')`
  164. display: flex;
  165. `;
  166. const DefaultLineActionButtons = styled('div')`
  167. display: flex;
  168. gap: ${space(1)};
  169. `;
  170. const CodeLocationsWrapper = styled('div')`
  171. & code {
  172. font-family: inherit;
  173. font-size: inherit;
  174. }
  175. `;
  176. const SourceContextWrapper = styled('div')<{isLast?: boolean}>`
  177. word-wrap: break-word;
  178. font-family: ${p => p.theme.text.familyMono};
  179. font-size: ${p => p.theme.fontSizeSmall};
  180. /* TODO(ddm): find out how it is done on the issues page */
  181. line-height: 24px;
  182. min-height: ${space(3)};
  183. white-space: pre;
  184. white-space: pre-wrap;
  185. background-color: ${p => p.theme.background};
  186. `;
  187. const DefaultLineWrapper = styled('div')<{
  188. hasContext?: boolean;
  189. isFirst?: boolean;
  190. isLast?: boolean;
  191. }>`
  192. :hover {
  193. cursor: ${p => p.hasContext && 'pointer'};
  194. }
  195. flex-grow: 1;
  196. border-top-left-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)};
  197. border-top-right-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)};
  198. border-bottom-left-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)};
  199. border-bottom-right-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)};
  200. border: 1px solid ${p => p.theme.border};
  201. border-top: ${p => (p.isFirst ? `1px solid ${p.theme.border}` : 'none')};
  202. background-color: ${p => p.theme.backgroundTertiary};
  203. `;
  204. const DefaultLine = styled('div')<{showContext?: boolean}>`
  205. display: flex;
  206. justify-content: space-between;
  207. align-items: center;
  208. border-bottom: ${p => (p.showContext ? `1px solid ${p.theme.border}` : 'none')};
  209. `;
  210. const DefaultLineTitleWrapper = styled('div')`
  211. width: 100%;
  212. display: flex;
  213. align-items: center;
  214. justify-content: space-between;
  215. font-size: ${p => p.theme.codeFontSize};
  216. line-height: ${p => p.theme.fontSizeLarge};
  217. font-style: normal;
  218. padding: ${space(0.75)} ${space(3)} ${space(0.75)} ${space(1.5)};
  219. word-break: break-all;
  220. word-break: break-word;
  221. `;
  222. const LeftLineTitle = styled('div')`
  223. display: flex;
  224. flex-wrap: wrap;
  225. align-items: center;
  226. `;
  227. const CenterContent = styled('div')`
  228. display: flex;
  229. justify-content: center;
  230. align-items: center;
  231. height: 100%;
  232. `;