codeLocations.tsx 7.7 KB

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