codeLocations.tsx 7.9 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, SelectionRange} from 'sentry/utils/metrics/types';
  15. import {useMetricsCodeLocations} from 'sentry/utils/metrics/useMetricsCodeLocations';
  16. interface CodeLocationsProps extends SelectionRange {
  17. mri?: MRI;
  18. }
  19. export function CodeLocations({mri, ...rangeOpts}: CodeLocationsProps) {
  20. const {data, isFetching, isError, refetch} = useMetricsCodeLocations(mri, rangeOpts);
  21. if (isFetching) {
  22. return <LoadingIndicator />;
  23. }
  24. if (isError) {
  25. return <LoadingError onRetry={refetch} />;
  26. }
  27. if (!mri) {
  28. return (
  29. <CenterContent>
  30. <EmptyMessage
  31. style={{margin: 'auto'}}
  32. icon={<IconSearch size="xxl" />}
  33. title={t('Nothing to show!')}
  34. description={t('Choose a metric to display data.')}
  35. />
  36. </CenterContent>
  37. );
  38. }
  39. if (!Array.isArray(data?.metrics) || data?.metrics.length === 0) {
  40. return (
  41. <CenterContent>
  42. <EmptyMessage
  43. style={{margin: 'auto'}}
  44. icon={<IconSearch size="xxl" />}
  45. title={t('Nothing to show!')}
  46. description={t('No code locations found for this metric.')}
  47. />
  48. </CenterContent>
  49. );
  50. }
  51. const codeLocations = data.metrics[0].codeLocations ?? [];
  52. // We only want to show the first 5 code locations
  53. const codeLocationsToShow = codeLocations.slice(0, 5);
  54. return (
  55. <CodeLocationsWrapper>
  56. {codeLocationsToShow.slice(0, 5).map((location, index) => (
  57. <CodeLocation
  58. key={`location-${index}`}
  59. codeLocation={location}
  60. isFirst={index === 0}
  61. isLast={index === codeLocationsToShow.length - 1}
  62. />
  63. ))}
  64. </CodeLocationsWrapper>
  65. );
  66. }
  67. type CodeLocationProps = {
  68. codeLocation: MetricCodeLocationFrame;
  69. isFirst?: boolean;
  70. isLast?: boolean;
  71. };
  72. function CodeLocation({codeLocation, isFirst, isLast}: CodeLocationProps) {
  73. const [showContext, setShowContext] = useState(!!isFirst);
  74. const toggleShowContext = useCallback(() => {
  75. setShowContext(prevState => !prevState);
  76. }, []);
  77. const hasContext = !!codeLocation.contextLine;
  78. return (
  79. <CodeLocationWrapper>
  80. <DefaultLineWrapper
  81. onClick={() => {
  82. if (!hasContext) {
  83. return;
  84. }
  85. toggleShowContext();
  86. }}
  87. isFirst={isFirst}
  88. isLast={isLast}
  89. hasContext={hasContext}
  90. >
  91. <DefaultLine className="title" showContext={showContext}>
  92. <DefaultLineTitleWrapper>
  93. <LeftLineTitle>
  94. <DefaultTitle
  95. frame={codeLocation as Frame}
  96. isHoverPreviewed={false}
  97. platform="other"
  98. />
  99. </LeftLineTitle>
  100. <DefaultLineActionButtons>
  101. <CopyToClipboardButton
  102. text={`${codeLocation.filename}:${codeLocation.lineNo}`}
  103. size="zero"
  104. iconSize="xs"
  105. borderless
  106. />
  107. <ToggleCodeLocationContextButton
  108. disabled={!hasContext}
  109. isToggled={showContext}
  110. handleToggle={toggleShowContext}
  111. />
  112. </DefaultLineActionButtons>
  113. </DefaultLineTitleWrapper>
  114. </DefaultLine>
  115. {showContext && hasContext && (
  116. <CodeLocationContext codeLocation={codeLocation} isLast={isLast} />
  117. )}
  118. </DefaultLineWrapper>
  119. </CodeLocationWrapper>
  120. );
  121. }
  122. type ToggleCodeLocationContextButtonProps = {
  123. disabled: boolean;
  124. handleToggle: () => void;
  125. isToggled: boolean;
  126. };
  127. function ToggleCodeLocationContextButton({
  128. disabled,
  129. isToggled,
  130. handleToggle,
  131. }: ToggleCodeLocationContextButtonProps) {
  132. return (
  133. <Button
  134. title={disabled ? t('No context available') : t('Toggle Context')}
  135. size="zero"
  136. onClick={event => {
  137. event.stopPropagation();
  138. handleToggle();
  139. }}
  140. disabled={disabled}
  141. >
  142. {/* legacy size is deprecated but the icon is too big without it */}
  143. <IconChevron direction={isToggled ? 'up' : 'down'} size="xs" legacySize="8px" />
  144. </Button>
  145. );
  146. }
  147. type CodeLocationContextProps = {
  148. codeLocation: MetricCodeLocationFrame;
  149. isLast?: boolean;
  150. };
  151. function CodeLocationContext({codeLocation, isLast}: CodeLocationContextProps) {
  152. const lineNo = codeLocation.lineNo ?? 0;
  153. const preContextLines: [number, string][] = useMemo(
  154. () => codeLocation.preContext?.map((line, index) => [lineNo - 5 + index, line]) ?? [],
  155. [codeLocation.preContext, lineNo]
  156. );
  157. const postContextLines: [number, string][] = useMemo(
  158. () => codeLocation.postContext?.map((line, index) => [lineNo + index, line]) ?? [],
  159. [codeLocation.postContext, lineNo]
  160. );
  161. return (
  162. <SourceContextWrapper isLast={isLast}>
  163. {preContextLines.map(line => (
  164. <ContextLine key={`pre-${line[0]}-${line[1]}`} line={line} isActive={false} />
  165. ))}
  166. <ContextLine line={[lineNo, codeLocation.contextLine ?? '']} isActive />
  167. {postContextLines.map(line => (
  168. <ContextLine key={`post-${line[0]}-${line[1]}`} line={line} isActive={false} />
  169. ))}
  170. </SourceContextWrapper>
  171. );
  172. }
  173. const CodeLocationWrapper = styled('div')`
  174. display: flex;
  175. `;
  176. const DefaultLineActionButtons = styled('div')`
  177. display: flex;
  178. gap: ${space(1)};
  179. `;
  180. const CodeLocationsWrapper = styled('div')`
  181. & code {
  182. font-family: inherit;
  183. font-size: inherit;
  184. }
  185. `;
  186. const SourceContextWrapper = styled('div')<{isLast?: boolean}>`
  187. word-wrap: break-word;
  188. font-family: ${p => p.theme.text.familyMono};
  189. font-size: ${p => p.theme.fontSizeSmall};
  190. /* TODO(ddm): find out how it is done on the issues page */
  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. `;