traceTimeline.tsx 3.9 KB

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