codeLocations.tsx 7.4 KB

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