codeLocations.tsx 7.9 KB

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