header.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import {Fragment, useCallback} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import Feature from 'sentry/components/acl/feature';
  5. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  6. import FeatureBadge from 'sentry/components/badge/featureBadge';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import {CreateAlertFromViewButton} from 'sentry/components/createAlertButton';
  9. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  10. import IdBadge from 'sentry/components/idBadge';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  12. import ReplayCountBadge from 'sentry/components/replays/replayCountBadge';
  13. import {TabList} from 'sentry/components/tabs';
  14. import {Tooltip} from 'sentry/components/tooltip';
  15. import {t} from 'sentry/locale';
  16. import type {Organization} from 'sentry/types/organization';
  17. import type {Project} from 'sentry/types/project';
  18. import {trackAnalytics} from 'sentry/utils/analytics';
  19. import type EventView from 'sentry/utils/discover/eventView';
  20. import type {MetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  21. import HasMeasurementsQuery from 'sentry/utils/performance/vitals/hasMeasurementsQuery';
  22. import {isProfilingSupportedOrProjectHasProfiles} from 'sentry/utils/profiling/platforms';
  23. import useReplayCountForTransactions from 'sentry/utils/replayCount/useReplayCountForTransactions';
  24. import projectSupportsReplay from 'sentry/utils/replays/projectSupportsReplay';
  25. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  26. import {useNavigate} from 'sentry/utils/useNavigate';
  27. import {AiHeader} from 'sentry/views/insights/pages/ai/aiPageHeader';
  28. import {AI_LANDING_SUB_PATH} from 'sentry/views/insights/pages/ai/settings';
  29. import {BackendHeader} from 'sentry/views/insights/pages/backend/backendPageHeader';
  30. import {BACKEND_LANDING_SUB_PATH} from 'sentry/views/insights/pages/backend/settings';
  31. import {FrontendHeader} from 'sentry/views/insights/pages/frontend/frontendPageHeader';
  32. import {FRONTEND_LANDING_SUB_PATH} from 'sentry/views/insights/pages/frontend/settings';
  33. import {MobileHeader} from 'sentry/views/insights/pages/mobile/mobilePageHeader';
  34. import {MOBILE_LANDING_SUB_PATH} from 'sentry/views/insights/pages/mobile/settings';
  35. import {useDomainViewFilters} from 'sentry/views/insights/pages/useFilters';
  36. import Breadcrumb, {getTabCrumbs} from 'sentry/views/performance/breadcrumb';
  37. import {aggregateWaterfallRouteWithQuery} from 'sentry/views/performance/transactionSummary/aggregateSpanWaterfall/utils';
  38. import {TAB_ANALYTICS} from 'sentry/views/performance/transactionSummary/pageLayout';
  39. import {anomaliesRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionAnomalies/utils';
  40. import {eventsRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionEvents/utils';
  41. import {profilesRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionProfiles/utils';
  42. import {replaysRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionReplays/utils';
  43. import {spansRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionSpans/utils';
  44. import {tagsRouteWithQuery} from 'sentry/views/performance/transactionSummary/transactionTags/utils';
  45. import {transactionSummaryRouteWithQuery} from 'sentry/views/performance/transactionSummary/utils';
  46. import {getSelectedProjectPlatforms} from 'sentry/views/performance/utils';
  47. import {getCurrentLandingDisplay, LandingDisplayField} from '../landing/utils';
  48. import Tab from './tabs';
  49. import TeamKeyTransactionButton from './teamKeyTransactionButton';
  50. import TransactionThresholdButton from './transactionThresholdButton';
  51. import type {TransactionThresholdMetric} from './transactionThresholdModal';
  52. export type Props = {
  53. currentTab: Tab;
  54. eventView: EventView;
  55. hasWebVitals: 'maybe' | 'yes' | 'no';
  56. location: Location;
  57. organization: Organization;
  58. projectId: string;
  59. projects: Project[];
  60. transactionName: string;
  61. metricsCardinality?: MetricsCardinalityContext;
  62. onChangeThreshold?: (threshold: number, metric: TransactionThresholdMetric) => void;
  63. };
  64. function TransactionHeader({
  65. eventView,
  66. organization,
  67. projects,
  68. projectId,
  69. metricsCardinality,
  70. location,
  71. transactionName,
  72. onChangeThreshold,
  73. currentTab,
  74. hasWebVitals,
  75. }: Props) {
  76. const {isInDomainView, view} = useDomainViewFilters();
  77. const navigate = useNavigate();
  78. const getNewRoute = useCallback(
  79. (newTab: Tab) => {
  80. if (!transactionName) {
  81. return {};
  82. }
  83. const routeQuery = {
  84. orgSlug: organization.slug,
  85. transaction: transactionName,
  86. projectID: projectId,
  87. query: location.query,
  88. view,
  89. };
  90. switch (newTab) {
  91. case Tab.TAGS:
  92. return tagsRouteWithQuery(routeQuery);
  93. case Tab.EVENTS:
  94. return eventsRouteWithQuery(routeQuery);
  95. case Tab.SPANS:
  96. return spansRouteWithQuery(routeQuery);
  97. case Tab.ANOMALIES:
  98. return anomaliesRouteWithQuery(routeQuery);
  99. case Tab.REPLAYS:
  100. return replaysRouteWithQuery(routeQuery);
  101. case Tab.PROFILING: {
  102. return profilesRouteWithQuery(routeQuery);
  103. }
  104. case Tab.AGGREGATE_WATERFALL:
  105. return aggregateWaterfallRouteWithQuery(routeQuery);
  106. case Tab.TRANSACTION_SUMMARY:
  107. default:
  108. return transactionSummaryRouteWithQuery(routeQuery);
  109. }
  110. },
  111. [location.query, organization.slug, projectId, transactionName, view]
  112. );
  113. const onTabChange = useCallback(
  114. (newTab: string) => {
  115. // Prevent infinite rerenders
  116. if (newTab === currentTab) {
  117. return;
  118. }
  119. const analyticsKey = TAB_ANALYTICS[newTab];
  120. if (analyticsKey) {
  121. trackAnalytics(analyticsKey, {
  122. organization,
  123. project_platforms: getSelectedProjectPlatforms(location, projects),
  124. });
  125. }
  126. navigate(normalizeUrl(getNewRoute(newTab as Tab)));
  127. },
  128. [getNewRoute, organization, location, projects, currentTab, navigate]
  129. );
  130. function handleCreateAlertSuccess() {
  131. trackAnalytics('performance_views.summary.create_alert_clicked', {
  132. organization,
  133. });
  134. }
  135. const project = projects.find(p => p.id === projectId);
  136. const hasAnomalyDetection = organization.features.includes(
  137. 'performance-anomaly-detection-ui'
  138. );
  139. const hasSessionReplay =
  140. organization.features.includes('session-replay') &&
  141. project &&
  142. projectSupportsReplay(project);
  143. const hasProfiling =
  144. project &&
  145. organization.features.includes('profiling') &&
  146. isProfilingSupportedOrProjectHasProfiles(project);
  147. const hasAggregateWaterfall = organization.features.includes(
  148. 'insights-initial-modules'
  149. );
  150. const getWebVitals = useCallback(
  151. (hasMeasurements: boolean) => {
  152. switch (hasWebVitals) {
  153. case 'maybe':
  154. // need to check if the web vitals tab should be shown
  155. // frontend projects should always show the web vitals tab
  156. if (
  157. getCurrentLandingDisplay(location, projects, eventView).field ===
  158. LandingDisplayField.FRONTEND_OTHER
  159. ) {
  160. return true;
  161. }
  162. // if it is not a frontend project, then we check to see if there
  163. // are any web vitals associated with the transaction recently
  164. return hasMeasurements;
  165. case 'yes':
  166. // always show the web vitals tab
  167. return true;
  168. case 'no':
  169. default:
  170. // never show the web vitals tab
  171. return false;
  172. }
  173. },
  174. [hasWebVitals, location, projects, eventView]
  175. );
  176. const {getReplayCountForTransaction} = useReplayCountForTransactions({
  177. statsPeriod: '90d',
  178. });
  179. const replaysCount = getReplayCountForTransaction(transactionName);
  180. const tabList = (
  181. <HasMeasurementsQuery
  182. location={location}
  183. orgSlug={organization.slug}
  184. eventView={eventView}
  185. transaction={transactionName}
  186. type="web"
  187. >
  188. {({hasMeasurements}) => {
  189. const renderWebVitals = getWebVitals(!!hasMeasurements);
  190. return (
  191. <TabList
  192. hideBorder
  193. outerWrapStyles={{
  194. gridColumn: '1 / -1',
  195. }}
  196. >
  197. <TabList.Item key={Tab.TRANSACTION_SUMMARY}>{t('Overview')}</TabList.Item>
  198. <TabList.Item key={Tab.EVENTS}>{t('Sampled Events')}</TabList.Item>
  199. <TabList.Item key={Tab.TAGS}>{t('Tags')}</TabList.Item>
  200. <TabList.Item key={Tab.SPANS}>{t('Spans')}</TabList.Item>
  201. <TabList.Item
  202. key={Tab.ANOMALIES}
  203. textValue={t('Anomalies')}
  204. hidden={!hasAnomalyDetection}
  205. >
  206. {t('Anomalies')}
  207. <FeatureBadge type="alpha" tooltipProps={{disabled: true}} />
  208. </TabList.Item>
  209. <TabList.Item
  210. key={Tab.WEB_VITALS}
  211. textValue={t('Web Vitals')}
  212. hidden={!renderWebVitals}
  213. >
  214. {t('Web Vitals')}
  215. </TabList.Item>
  216. <TabList.Item
  217. key={Tab.REPLAYS}
  218. textValue={t('Replays')}
  219. hidden={!hasSessionReplay}
  220. >
  221. {t('Replays')}
  222. <ReplayCountBadge count={replaysCount} />
  223. </TabList.Item>
  224. <TabList.Item
  225. key={Tab.PROFILING}
  226. textValue={t('Profiling')}
  227. hidden={!hasProfiling}
  228. >
  229. {t('Profiles')}
  230. </TabList.Item>
  231. <TabList.Item
  232. key={Tab.AGGREGATE_WATERFALL}
  233. textValue={t('Aggregate Spans')}
  234. hidden={!hasAggregateWaterfall}
  235. >
  236. {t('Aggregate Spans')}
  237. </TabList.Item>
  238. </TabList>
  239. );
  240. }}
  241. </HasMeasurementsQuery>
  242. );
  243. if (isInDomainView) {
  244. const headerProps = {
  245. headerTitle: (
  246. <Fragment>
  247. {project && (
  248. <IdBadge
  249. project={project}
  250. avatarSize={28}
  251. hideName
  252. avatarProps={{hasTooltip: true, tooltip: project.slug}}
  253. />
  254. )}
  255. <Tooltip showOnlyOnOverflow skipWrapper title={transactionName}>
  256. <TransactionName>{transactionName}</TransactionName>
  257. </Tooltip>
  258. </Fragment>
  259. ),
  260. hideDefaultTabs: true,
  261. tabs: {
  262. onTabChange,
  263. tabList,
  264. value: currentTab,
  265. },
  266. breadcrumbs: getTabCrumbs({
  267. location,
  268. organization,
  269. tab: currentTab,
  270. transaction: {
  271. name: transactionName,
  272. project: projectId,
  273. },
  274. view,
  275. }),
  276. };
  277. if (view === FRONTEND_LANDING_SUB_PATH) {
  278. return <FrontendHeader {...headerProps} />;
  279. }
  280. if (view === BACKEND_LANDING_SUB_PATH) {
  281. return <BackendHeader {...headerProps} />;
  282. }
  283. if (view === AI_LANDING_SUB_PATH) {
  284. return <AiHeader {...headerProps} />;
  285. }
  286. if (view === MOBILE_LANDING_SUB_PATH) {
  287. return <MobileHeader {...headerProps} />;
  288. }
  289. }
  290. return (
  291. <Layout.Header>
  292. <Layout.HeaderContent>
  293. <Breadcrumb
  294. organization={organization}
  295. location={location}
  296. transaction={{
  297. project: projectId,
  298. name: transactionName,
  299. }}
  300. tab={currentTab}
  301. />
  302. <Layout.Title>
  303. {project && (
  304. <IdBadge
  305. project={project}
  306. avatarSize={28}
  307. hideName
  308. avatarProps={{hasTooltip: true, tooltip: project.slug}}
  309. />
  310. )}
  311. <Tooltip showOnlyOnOverflow skipWrapper title={transactionName}>
  312. <TransactionName>{transactionName}</TransactionName>
  313. </Tooltip>
  314. </Layout.Title>
  315. </Layout.HeaderContent>
  316. <Layout.HeaderActions>
  317. <ButtonBar gap={1}>
  318. <Feature organization={organization} features="incidents">
  319. {({hasFeature}) =>
  320. hasFeature && !metricsCardinality?.isLoading ? (
  321. <CreateAlertFromViewButton
  322. size="sm"
  323. eventView={eventView}
  324. organization={organization}
  325. projects={projects}
  326. onClick={handleCreateAlertSuccess}
  327. referrer="performance"
  328. alertType="trans_duration"
  329. aria-label={t('Create Alert')}
  330. disableMetricDataset={
  331. metricsCardinality?.outcome?.forceTransactionsOnly
  332. }
  333. />
  334. ) : null
  335. }
  336. </Feature>
  337. <TeamKeyTransactionButton
  338. transactionName={transactionName}
  339. eventView={eventView}
  340. organization={organization}
  341. />
  342. <GuideAnchor target="project_transaction_threshold_override" position="bottom">
  343. <TransactionThresholdButton
  344. organization={organization}
  345. transactionName={transactionName}
  346. eventView={eventView}
  347. onChangeThreshold={onChangeThreshold}
  348. />
  349. </GuideAnchor>
  350. <FeedbackWidgetButton />
  351. </ButtonBar>
  352. </Layout.HeaderActions>
  353. <HasMeasurementsQuery
  354. location={location}
  355. orgSlug={organization.slug}
  356. eventView={eventView}
  357. transaction={transactionName}
  358. type="web"
  359. >
  360. {({hasMeasurements}) => {
  361. const renderWebVitals = getWebVitals(!!hasMeasurements);
  362. return (
  363. <TabList
  364. hideBorder
  365. outerWrapStyles={{
  366. gridColumn: '1 / -1',
  367. }}
  368. >
  369. <TabList.Item key={Tab.TRANSACTION_SUMMARY}>{t('Overview')}</TabList.Item>
  370. <TabList.Item key={Tab.EVENTS}>{t('Sampled Events')}</TabList.Item>
  371. <TabList.Item key={Tab.TAGS}>{t('Tags')}</TabList.Item>
  372. <TabList.Item key={Tab.SPANS}>{t('Spans')}</TabList.Item>
  373. <TabList.Item
  374. key={Tab.ANOMALIES}
  375. textValue={t('Anomalies')}
  376. hidden={!hasAnomalyDetection}
  377. >
  378. {t('Anomalies')}
  379. <FeatureBadge type="alpha" tooltipProps={{disabled: true}} />
  380. </TabList.Item>
  381. <TabList.Item
  382. key={Tab.WEB_VITALS}
  383. textValue={t('Web Vitals')}
  384. hidden={!renderWebVitals}
  385. >
  386. {t('Web Vitals')}
  387. </TabList.Item>
  388. <TabList.Item
  389. key={Tab.REPLAYS}
  390. textValue={t('Replays')}
  391. hidden={!hasSessionReplay}
  392. >
  393. {t('Replays')}
  394. <ReplayCountBadge count={replaysCount} />
  395. </TabList.Item>
  396. <TabList.Item
  397. key={Tab.PROFILING}
  398. textValue={t('Profiling')}
  399. hidden={!hasProfiling}
  400. >
  401. {t('Profiles')}
  402. </TabList.Item>
  403. <TabList.Item
  404. key={Tab.AGGREGATE_WATERFALL}
  405. textValue={t('Aggregate Spans')}
  406. hidden={!hasAggregateWaterfall}
  407. >
  408. {t('Aggregate Spans')}
  409. </TabList.Item>
  410. </TabList>
  411. );
  412. }}
  413. </HasMeasurementsQuery>
  414. </Layout.Header>
  415. );
  416. }
  417. const TransactionName = styled('div')`
  418. ${p => p.theme.overflowEllipsis}
  419. `;
  420. export default TransactionHeader;