codeLocations.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /* TODO(ddm): find out how it is done on the issues page */
  192. line-height: 24px;
  193. min-height: ${space(3)};
  194. white-space: pre;
  195. white-space: pre-wrap;
  196. background-color: ${p => p.theme.background};
  197. `;
  198. const DefaultLineWrapper = styled('div')<{
  199. hasContext?: boolean;
  200. isFirst?: boolean;
  201. isLast?: boolean;
  202. }>`
  203. :hover {
  204. cursor: ${p => p.hasContext && 'pointer'};
  205. }
  206. flex-grow: 1;
  207. border-top-left-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)};
  208. border-top-right-radius: ${p => (p.isFirst ? p.theme.borderRadius : 0)};
  209. border-bottom-left-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)};
  210. border-bottom-right-radius: ${p => (p.isLast ? p.theme.borderRadius : 0)};
  211. border: 1px solid ${p => p.theme.border};
  212. border-top: ${p => (p.isFirst ? `1px solid ${p.theme.border}` : 'none')};
  213. background-color: ${p => p.theme.backgroundTertiary};
  214. `;
  215. const DefaultLine = styled('div')<{showContext?: boolean}>`
  216. display: flex;
  217. justify-content: space-between;
  218. align-items: center;
  219. border-bottom: ${p => (p.showContext ? `1px solid ${p.theme.border}` : 'none')};
  220. `;
  221. const DefaultLineTitleWrapper = styled('div')`
  222. width: 100%;
  223. display: flex;
  224. align-items: center;
  225. justify-content: space-between;
  226. font-size: ${p => p.theme.codeFontSize};
  227. line-height: ${p => p.theme.fontSizeLarge};
  228. font-style: normal;
  229. padding: ${space(0.75)} ${space(3)} ${space(0.75)} ${space(1.5)};
  230. word-break: break-all;
  231. word-break: break-word;
  232. `;
  233. const LeftLineTitle = styled('div')`
  234. display: flex;
  235. flex-wrap: wrap;
  236. align-items: center;
  237. `;
  238. const CenterContent = styled('div')`
  239. display: flex;
  240. justify-content: center;
  241. align-items: center;
  242. height: 100%;
  243. `;