traceTimeline.tsx 3.0 KB

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