spanDescriptionCell.tsx 4.6 KB

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