index.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import Breadcrumbs from 'sentry/components/breadcrumbs';
  4. import ButtonBar from 'sentry/components/buttonBar';
  5. import DiscoverButton from 'sentry/components/discoverButton';
  6. import {HighlightsIconSummary} from 'sentry/components/events/highlights/highlightsIconSummary';
  7. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import type {EventTransaction} from 'sentry/types/event';
  13. import type {Organization} from 'sentry/types/organization';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import type EventView from 'sentry/utils/discover/eventView';
  16. import {SavedQueryDatasets} from 'sentry/utils/discover/types';
  17. import type {UseApiQueryResult} from 'sentry/utils/queryClient';
  18. import type RequestError from 'sentry/utils/requestError/requestError';
  19. import {useLocation} from 'sentry/utils/useLocation';
  20. import {hasDatasetSelector} from 'sentry/views/dashboards/utils';
  21. import {ProjectsRenderer} from 'sentry/views/explore/tables/tracesTable/fieldRenderers';
  22. import {useModuleURLBuilder} from 'sentry/views/insights/common/utils/useModuleURL';
  23. import {useDomainViewFilters} from 'sentry/views/insights/pages/useFilters';
  24. import {useTraceStateDispatch} from 'sentry/views/performance/newTraceDetails/traceState/traceStateProvider';
  25. import {isRootTransaction} from '../../traceDetails/utils';
  26. import type {TraceMetaQueryResults} from '../traceApi/useTraceMeta';
  27. import TraceConfigurations from '../traceConfigurations';
  28. import {isTraceNode} from '../traceGuards';
  29. import type {TraceTree} from '../traceModels/traceTree';
  30. import {useHasTraceNewUi} from '../useHasTraceNewUi';
  31. import {getTraceViewBreadcrumbs} from './breadcrumbs';
  32. import {Meta} from './meta';
  33. import {Title} from './title';
  34. interface TraceMetadataHeaderProps {
  35. metaResults: TraceMetaQueryResults;
  36. organization: Organization;
  37. rootEventResults: UseApiQueryResult<EventTransaction, RequestError>;
  38. traceEventView: EventView;
  39. traceSlug: string;
  40. tree: TraceTree;
  41. }
  42. function PlaceHolder({organization}: {organization: Organization}) {
  43. const {view} = useDomainViewFilters();
  44. const moduleURLBuilder = useModuleURLBuilder(true);
  45. const location = useLocation();
  46. return (
  47. <Layout.Header>
  48. <HeaderContent>
  49. <HeaderRow>
  50. <Breadcrumbs
  51. crumbs={getTraceViewBreadcrumbs(
  52. organization,
  53. location,
  54. moduleURLBuilder,
  55. view
  56. )}
  57. />
  58. </HeaderRow>
  59. <HeaderRow>
  60. <PlaceHolderTitleWrapper>
  61. <StyledPlaceholder _width={300} _height={20} />
  62. <StyledPlaceholder _width={200} _height={18} />
  63. </PlaceHolderTitleWrapper>
  64. <PlaceHolderTitleWrapper>
  65. <StyledPlaceholder _width={300} _height={18} />
  66. <StyledPlaceholder _width={300} _height={24} />
  67. </PlaceHolderTitleWrapper>
  68. </HeaderRow>
  69. <StyledBreak />
  70. <HeaderRow>
  71. <PlaceHolderHighlightWrapper>
  72. <StyledPlaceholder _width={150} _height={20} />
  73. <StyledPlaceholder _width={150} _height={20} />
  74. <StyledPlaceholder _width={150} _height={20} />
  75. </PlaceHolderHighlightWrapper>
  76. <StyledPlaceholder _width={50} _height={28} />
  77. </HeaderRow>
  78. </HeaderContent>
  79. </Layout.Header>
  80. );
  81. }
  82. const PlaceHolderTitleWrapper = styled('div')`
  83. display: flex;
  84. flex-direction: column;
  85. gap: ${space(0.5)};
  86. `;
  87. const PlaceHolderHighlightWrapper = styled('div')`
  88. display: flex;
  89. align-items: center;
  90. gap: ${space(1)};
  91. `;
  92. const StyledPlaceholder = styled(Placeholder)<{_height: number; _width: number}>`
  93. border-radius: ${p => p.theme.borderRadius};
  94. height: ${p => p._height}px;
  95. width: ${p => p._width}px;
  96. `;
  97. const CANDIDATE_TRACE_TITLE_OPS = ['pageload', 'navigation'];
  98. export const getRepresentativeTransaction = (
  99. tree: TraceTree
  100. ): TraceTree.Transaction | null => {
  101. const traceNode = tree.root.children[0];
  102. if (!traceNode) {
  103. return null;
  104. }
  105. if (!isTraceNode(traceNode)) {
  106. throw new TypeError('Not trace node');
  107. }
  108. let firstRootTransaction: TraceTree.Transaction | null = null;
  109. let candidateTransaction: TraceTree.Transaction | null = null;
  110. let firstTransaction: TraceTree.Transaction | null = null;
  111. for (const transaction of traceNode.value.transactions || []) {
  112. // If we find a root transaction, we can stop looking and use it for the title.
  113. if (!firstRootTransaction && isRootTransaction(transaction)) {
  114. firstRootTransaction = transaction;
  115. break;
  116. } else if (
  117. // If we haven't found a root transaction, but we found a candidate transaction
  118. // with an op that we care about, we can use it for the title. We keep looking for
  119. // a root.
  120. !candidateTransaction &&
  121. CANDIDATE_TRACE_TITLE_OPS.includes(transaction['transaction.op'])
  122. ) {
  123. candidateTransaction = transaction;
  124. continue;
  125. } else if (!firstTransaction) {
  126. // If we haven't found a root or candidate transaction, we can use the first transaction
  127. // in the trace for the title.
  128. firstTransaction = transaction;
  129. }
  130. }
  131. return firstRootTransaction ?? candidateTransaction ?? firstTransaction;
  132. };
  133. function LegacyTraceMetadataHeader(props: TraceMetadataHeaderProps) {
  134. const location = useLocation();
  135. const {view} = useDomainViewFilters();
  136. const moduleURLBuilder = useModuleURLBuilder(true);
  137. const trackOpenInDiscover = useCallback(() => {
  138. trackAnalytics('performance_views.trace_view.open_in_discover', {
  139. organization: props.organization,
  140. });
  141. }, [props.organization]);
  142. return (
  143. <Layout.Header>
  144. <Layout.HeaderContent>
  145. <Breadcrumbs
  146. crumbs={getTraceViewBreadcrumbs(
  147. props.organization,
  148. location,
  149. moduleURLBuilder,
  150. view
  151. )}
  152. />
  153. </Layout.HeaderContent>
  154. <Layout.HeaderActions>
  155. <ButtonBar gap={1}>
  156. <TraceConfigurations rootEventResults={props.rootEventResults} />
  157. <DiscoverButton
  158. size="sm"
  159. to={props.traceEventView.getResultsViewUrlTarget(
  160. props.organization.slug,
  161. false,
  162. hasDatasetSelector(props.organization)
  163. ? SavedQueryDatasets.TRANSACTIONS
  164. : undefined
  165. )}
  166. onClick={trackOpenInDiscover}
  167. >
  168. {t('Open in Discover')}
  169. </DiscoverButton>
  170. <FeedbackWidgetButton />
  171. </ButtonBar>
  172. </Layout.HeaderActions>
  173. </Layout.Header>
  174. );
  175. }
  176. export function TraceMetaDataHeader(props: TraceMetadataHeaderProps) {
  177. const location = useLocation();
  178. const hasNewTraceViewUi = useHasTraceNewUi();
  179. const {view} = useDomainViewFilters();
  180. const moduleURLBuilder = useModuleURLBuilder(true);
  181. const dispatch = useTraceStateDispatch();
  182. const onProjectClick = useCallback(
  183. (projectSlug: string) => {
  184. dispatch({type: 'set query', query: `project:${projectSlug}`, source: 'external'});
  185. },
  186. [dispatch]
  187. );
  188. const projectSlugs = useMemo(() => {
  189. return Array.from(props.tree.projects.values()).map(project => project.slug);
  190. }, [props.tree.projects]);
  191. if (!hasNewTraceViewUi) {
  192. return <LegacyTraceMetadataHeader {...props} />;
  193. }
  194. const isLoading =
  195. props.metaResults.status === 'pending' ||
  196. props.rootEventResults.isPending ||
  197. props.tree.type === 'loading';
  198. if (isLoading) {
  199. return <PlaceHolder organization={props.organization} />;
  200. }
  201. const representativeTransaction = getRepresentativeTransaction(props.tree);
  202. return (
  203. <HeaderLayout>
  204. <HeaderContent>
  205. <HeaderRow>
  206. <Breadcrumbs
  207. crumbs={getTraceViewBreadcrumbs(
  208. props.organization,
  209. location,
  210. moduleURLBuilder,
  211. view
  212. )}
  213. />
  214. </HeaderRow>
  215. <HeaderRow>
  216. <Title
  217. traceSlug={props.traceSlug}
  218. representativeTransaction={representativeTransaction}
  219. />
  220. <Meta
  221. organization={props.organization}
  222. rootEventResults={props.rootEventResults}
  223. tree={props.tree}
  224. meta={props.metaResults.data}
  225. representativeTransaction={representativeTransaction}
  226. />
  227. </HeaderRow>
  228. <StyledBreak />
  229. {props.rootEventResults.data ? (
  230. <HeaderRow>
  231. <StyledWrapper>
  232. <HighlightsIconSummary event={props.rootEventResults.data} />
  233. </StyledWrapper>
  234. <ProjectsRendererWrapper>
  235. <ProjectsRenderer
  236. disableLink
  237. onProjectClick={onProjectClick}
  238. projectSlugs={projectSlugs}
  239. visibleAvatarSize={24}
  240. maxVisibleProjects={3}
  241. />
  242. </ProjectsRendererWrapper>
  243. </HeaderRow>
  244. ) : null}
  245. </HeaderContent>
  246. </HeaderLayout>
  247. );
  248. }
  249. // We cannot change the cursor of the ProjectBadge component so we need to wrap it in a div
  250. const ProjectsRendererWrapper = styled('div')`
  251. img {
  252. cursor: pointer;
  253. }
  254. `;
  255. const HeaderLayout = styled(Layout.Header)`
  256. background-color: ${p => p.theme.background};
  257. padding: ${space(2)} ${space(2)} 0 !important;
  258. `;
  259. const HeaderRow = styled('div')`
  260. display: flex;
  261. justify-content: space-between;
  262. gap: ${space(2)};
  263. &:not(:first-child) {
  264. margin: ${space(1)} 0;
  265. }
  266. @media (max-width: ${p => p.theme.breakpoints.small}) {
  267. gap: ${space(1)};
  268. flex-direction: column;
  269. }
  270. `;
  271. const HeaderContent = styled('div')`
  272. display: flex;
  273. flex-direction: column;
  274. `;
  275. const StyledBreak = styled('hr')`
  276. margin: 0;
  277. border-color: ${p => p.theme.border};
  278. `;
  279. const StyledWrapper = styled('span')`
  280. display: flex;
  281. align-items: center;
  282. & > div {
  283. padding: 0;
  284. }
  285. `;