codeLocations.tsx 7.8 KB

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