codeLocations.tsx 7.6 KB

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