traceTimeline.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {useRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import ErrorBoundary from 'sentry/components/errorBoundary';
  4. import Placeholder from 'sentry/components/placeholder';
  5. import QuestionTooltip from 'sentry/components/questionTooltip';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {Event} from 'sentry/types';
  9. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  10. import {useDimensions} from 'sentry/utils/useDimensions';
  11. import useOrganization from 'sentry/utils/useOrganization';
  12. import {hasTraceTimelineFeature} from 'sentry/views/issueDetails/traceTimeline/utils';
  13. import {TraceTimelineEvents} from './traceTimelineEvents';
  14. import {useTraceTimelineEvents} from './useTraceTimelineEvents';
  15. interface TraceTimelineProps {
  16. event: Event;
  17. }
  18. export function TraceTimeline({event}: TraceTimelineProps) {
  19. const organization = useOrganization({allowNull: true});
  20. const timelineRef = useRef<HTMLDivElement>(null);
  21. const {width} = useDimensions({elementRef: timelineRef});
  22. const hasFeature = hasTraceTimelineFeature(organization);
  23. const {isError, isLoading, traceEvents} = useTraceTimelineEvents({event}, hasFeature);
  24. const hasTraceId = !!event.contexts?.trace?.trace_id;
  25. let timelineStatus: string | undefined;
  26. if (hasFeature) {
  27. if (hasTraceId && !isLoading) {
  28. timelineStatus = traceEvents.length > 1 ? 'shown' : 'empty';
  29. } else if (!hasTraceId) {
  30. timelineStatus = 'no_trace_id';
  31. }
  32. }
  33. useRouteAnalyticsParams(timelineStatus ? {trace_timeline_status: timelineStatus} : {});
  34. if (!hasFeature || !hasTraceId) {
  35. return null;
  36. }
  37. const noEvents = !isLoading && traceEvents.length === 0;
  38. // Timelines with only the current event are not useful
  39. const onlySelfEvent =
  40. !isLoading &&
  41. traceEvents.length > 0 &&
  42. traceEvents.every(item => item.id === event.id);
  43. if (isError || noEvents || onlySelfEvent) {
  44. // display empty placeholder to reduce layout shift
  45. return <div style={{height: 38}} data-test-id="trace-timeline-empty" />;
  46. }
  47. return (
  48. <ErrorBoundary mini>
  49. <TimelineWrapper>
  50. <div ref={timelineRef}>
  51. {isLoading ? (
  52. <LoadingSkeleton>
  53. <Placeholder height="14px" />
  54. <Placeholder height="8px" />
  55. </LoadingSkeleton>
  56. ) : (
  57. <TimelineEventsContainer>
  58. <TimelineOutline />
  59. {/* Sets a min width of 200 for testing */}
  60. <TraceTimelineEvents event={event} width={Math.max(width, 200)} />
  61. </TimelineEventsContainer>
  62. )}
  63. </div>
  64. <QuestionTooltipWrapper>
  65. <QuestionTooltip
  66. size="sm"
  67. title={t(
  68. 'This is a trace timeline showing all related events happening upstream and downstream of this event'
  69. )}
  70. position="bottom"
  71. />
  72. </QuestionTooltipWrapper>
  73. </TimelineWrapper>
  74. </ErrorBoundary>
  75. );
  76. }
  77. const TimelineWrapper = styled('div')`
  78. display: grid;
  79. grid-template-columns: 1fr auto;
  80. align-items: start;
  81. gap: ${space(2)};
  82. margin-top: ${space(0.5)};
  83. `;
  84. const QuestionTooltipWrapper = styled('div')`
  85. margin-top: ${space(0.25)};
  86. `;
  87. /**
  88. * Displays the container the dots appear inside of
  89. */
  90. const TimelineOutline = styled('div')`
  91. position: absolute;
  92. left: 0;
  93. top: 3.5px;
  94. width: 100%;
  95. height: 10px;
  96. border: 1px solid ${p => p.theme.innerBorder};
  97. border-radius: ${p => p.theme.borderRadius};
  98. background-color: ${p => p.theme.backgroundSecondary};
  99. `;
  100. const TimelineEventsContainer = styled('div')`
  101. position: relative;
  102. height: 34px;
  103. padding-top: 10px;
  104. `;
  105. const LoadingSkeleton = styled('div')`
  106. display: flex;
  107. flex-direction: column;
  108. gap: ${space(0.25)};
  109. padding: ${space(0.5)} 0 ${space(1)};
  110. height: 34px;
  111. `;