spanDescriptionCell.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {createPortal} from 'react-dom';
  2. import {Link} from 'react-router';
  3. import {useTheme} from '@emotion/react';
  4. import styled from '@emotion/styled';
  5. import {AnimatePresence} from 'framer-motion';
  6. import * as qs from 'query-string';
  7. import {CodeSnippet} from 'sentry/components/codeSnippet';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {Overlay, PositionWrapper} from 'sentry/components/overlay';
  10. import {space} from 'sentry/styles/space';
  11. import {useHoverOverlay, UseHoverOverlayProps} from 'sentry/utils/useHoverOverlay';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import {OverflowEllipsisTextContainer} from 'sentry/views/starfish/components/textAlign';
  14. import {useFullSpanFromTrace} from 'sentry/views/starfish/queries/useFullSpanFromTrace';
  15. import {ModuleName, StarfishFunctions} from 'sentry/views/starfish/types';
  16. import {extractRoute} from 'sentry/views/starfish/utils/extractRoute';
  17. import {useRoutingContext} from 'sentry/views/starfish/utils/routingContext';
  18. import {SQLishFormatter} from 'sentry/views/starfish/utils/sqlish/SQLishFormatter';
  19. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  20. interface Props {
  21. moduleName: ModuleName;
  22. projectId: number;
  23. description?: string;
  24. endpoint?: string;
  25. endpointMethod?: string;
  26. group?: string;
  27. }
  28. const formatter = new SQLishFormatter();
  29. export function SpanDescriptionCell({
  30. description,
  31. group,
  32. moduleName,
  33. endpoint,
  34. endpointMethod,
  35. projectId,
  36. }: Props) {
  37. const location = useLocation();
  38. const routingContext = useRoutingContext();
  39. const hoverOverlayProps = useHoverOverlay('overlay', OVERLAY_OPTIONS);
  40. if (!description) {
  41. return NULL_DESCRIPTION;
  42. }
  43. const queryString = {
  44. ...location.query,
  45. project: projectId,
  46. endpoint,
  47. endpointMethod,
  48. };
  49. const sort: string | undefined = queryString[QueryParameterNames.SORT];
  50. // the spans page uses time_spent_percentage(local), so to persist the sort upon navigation we need to replace
  51. if (sort?.includes(`${StarfishFunctions.TIME_SPENT_PERCENTAGE}()`)) {
  52. queryString[QueryParameterNames.SORT] = sort.replace(
  53. `${StarfishFunctions.TIME_SPENT_PERCENTAGE}()`,
  54. `${StarfishFunctions.TIME_SPENT_PERCENTAGE}(local)`
  55. );
  56. }
  57. const formattedDescription =
  58. moduleName === ModuleName.DB ? formatter.toSimpleMarkup(description) : description;
  59. const overlayContent = moduleName === ModuleName.DB && hoverOverlayProps.isOpen && (
  60. <QueryDescriptionOverlay
  61. group={group}
  62. shortDescription={description}
  63. hoverOverlayProps={hoverOverlayProps}
  64. />
  65. );
  66. return (
  67. <DescriptionWrapper>
  68. {hoverOverlayProps.wrapTrigger(
  69. <OverflowEllipsisTextContainer>
  70. {group ? (
  71. <Link
  72. to={`${routingContext.baseURL}/${
  73. extractRoute(location) ?? 'spans'
  74. }/span/${group}?${qs.stringify(queryString)}`}
  75. >
  76. {formattedDescription}
  77. </Link>
  78. ) : (
  79. formattedDescription
  80. )}
  81. </OverflowEllipsisTextContainer>
  82. )}
  83. {createPortal(<AnimatePresence>{overlayContent}</AnimatePresence>, document.body)}
  84. </DescriptionWrapper>
  85. );
  86. }
  87. const DescriptionWrapper = styled('div')`
  88. display: inline-flex;
  89. `;
  90. const OVERLAY_OPTIONS: UseHoverOverlayProps = {
  91. position: 'right',
  92. isHoverable: true,
  93. skipWrapper: true,
  94. };
  95. const NULL_DESCRIPTION = <span>&lt;null&gt;</span>;
  96. interface QueryDescriptionOverlayProps {
  97. hoverOverlayProps: ReturnType<typeof useHoverOverlay>;
  98. group?: string;
  99. shortDescription?: string;
  100. }
  101. function QueryDescriptionOverlay({
  102. group,
  103. shortDescription,
  104. hoverOverlayProps,
  105. }: QueryDescriptionOverlayProps) {
  106. const theme = useTheme();
  107. const {
  108. data: fullSpan,
  109. isLoading,
  110. isFetching,
  111. } = useFullSpanFromTrace(group, Boolean(group));
  112. const description = fullSpan?.description ?? shortDescription;
  113. return description ? (
  114. <PositionWrapper zIndex={theme.zIndex.tooltip} {...hoverOverlayProps.overlayProps}>
  115. <OverlayContent
  116. animated
  117. originPoint={hoverOverlayProps.arrowData}
  118. arrowProps={hoverOverlayProps.arrowProps}
  119. placement={hoverOverlayProps.placement}
  120. >
  121. {/* N.B. A `disabled` query still returns `isLoading: true`, so we also
  122. check the fetching status explicitly. */}
  123. {isLoading && isFetching ? (
  124. <PaddedSpinner>
  125. <LoadingIndicator mini />
  126. </PaddedSpinner>
  127. ) : (
  128. <CodeSnippet language="sql">{formatter.toString(description)}</CodeSnippet>
  129. )}
  130. </OverlayContent>
  131. </PositionWrapper>
  132. ) : null;
  133. }
  134. const OverlayContent = styled(Overlay)`
  135. max-width: 500px;
  136. `;
  137. const PaddedSpinner = styled('div')`
  138. padding: ${space(1)};
  139. `;