header.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Location} from 'history';
  4. import Feature from 'sentry/components/acl/feature';
  5. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import {CreateAlertFromViewButton} from 'sentry/components/createAlertButton';
  8. import FeatureBadge from 'sentry/components/featureBadge';
  9. import IdBadge from 'sentry/components/idBadge';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import ListLink from 'sentry/components/links/listLink';
  12. import NavTabs from 'sentry/components/navTabs';
  13. import ReplaysFeatureBadge from 'sentry/components/replays/replaysFeatureBadge';
  14. import {t} from 'sentry/locale';
  15. import space from 'sentry/styles/space';
  16. import {Organization, Project} from 'sentry/types';
  17. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  18. import EventView from 'sentry/utils/discover/eventView';
  19. import {MetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  20. import HasMeasurementsQuery from 'sentry/utils/performance/vitals/hasMeasurementsQuery';
  21. import {decodeScalar} from 'sentry/utils/queryString';
  22. import Breadcrumb from 'sentry/views/performance/breadcrumb';
  23. import {getCurrentLandingDisplay, LandingDisplayField} from '../landing/utils';
  24. import {getSelectedProjectPlatforms} from '../utils';
  25. import {anomaliesRouteWithQuery} from './transactionAnomalies/utils';
  26. import {eventsRouteWithQuery} from './transactionEvents/utils';
  27. import {replaysRouteWithQuery} from './transactionReplays/utils';
  28. import {spansRouteWithQuery} from './transactionSpans/utils';
  29. import {tagsRouteWithQuery} from './transactionTags/utils';
  30. import {vitalsRouteWithQuery} from './transactionVitals/utils';
  31. import Tab from './tabs';
  32. import TeamKeyTransactionButton from './teamKeyTransactionButton';
  33. import TransactionThresholdButton from './transactionThresholdButton';
  34. import {TransactionThresholdMetric} from './transactionThresholdModal';
  35. import {transactionSummaryRouteWithQuery} from './utils';
  36. type AnalyticInfo = {
  37. eventKey: string;
  38. eventName: string;
  39. };
  40. const TAB_ANALYTICS: Partial<Record<Tab, AnalyticInfo>> = {
  41. [Tab.WebVitals]: {
  42. eventKey: 'performance_views.vitals.vitals_tab_clicked',
  43. eventName: 'Performance Views: Vitals tab clicked',
  44. },
  45. [Tab.Tags]: {
  46. eventKey: 'performance_views.tags.tags_tab_clicked',
  47. eventName: 'Performance Views: Tags tab clicked',
  48. },
  49. [Tab.Events]: {
  50. eventKey: 'performance_views.events.events_tab_clicked',
  51. eventName: 'Performance Views: Events tab clicked',
  52. },
  53. [Tab.Spans]: {
  54. eventKey: 'performance_views.spans.spans_tab_clicked',
  55. eventName: 'Performance Views: Spans tab clicked',
  56. },
  57. [Tab.Anomalies]: {
  58. eventKey: 'performance_views.anomalies.anomalies_tab_clicked',
  59. eventName: 'Performance Views: Anomalies tab clicked',
  60. },
  61. };
  62. type Props = {
  63. currentTab: Tab;
  64. eventView: EventView;
  65. hasWebVitals: 'maybe' | 'yes' | 'no';
  66. location: Location;
  67. organization: Organization;
  68. projectId: string;
  69. projects: Project[];
  70. transactionName: string;
  71. metricsCardinality?: MetricsCardinalityContext;
  72. onChangeThreshold?: (threshold: number, metric: TransactionThresholdMetric) => void;
  73. };
  74. class TransactionHeader extends Component<Props> {
  75. trackTabClick = (tab: Tab) => () => {
  76. const analyticKeys = TAB_ANALYTICS[tab];
  77. if (!analyticKeys) {
  78. return;
  79. }
  80. const {location, projects} = this.props;
  81. trackAnalyticsEvent({
  82. ...analyticKeys,
  83. organization_id: this.props.organization.id,
  84. project_platforms: getSelectedProjectPlatforms(location, projects),
  85. });
  86. };
  87. handleCreateAlertSuccess = () => {
  88. trackAnalyticsEvent({
  89. eventKey: 'performance_views.summary.create_alert_clicked',
  90. eventName: 'Performance Views: Create alert clicked',
  91. organization_id: this.props.organization.id,
  92. });
  93. };
  94. renderCreateAlertButton() {
  95. const {eventView, organization, projects, metricsCardinality} = this.props;
  96. if (metricsCardinality?.isLoading) {
  97. return <Fragment />;
  98. }
  99. return (
  100. <CreateAlertFromViewButton
  101. eventView={eventView}
  102. organization={organization}
  103. projects={projects}
  104. onClick={this.handleCreateAlertSuccess}
  105. referrer="performance"
  106. alertType="trans_duration"
  107. aria-label={t('Create Alert')}
  108. disableMetricDataset={metricsCardinality?.outcome?.forceTransactionsOnly}
  109. />
  110. );
  111. }
  112. renderKeyTransactionButton() {
  113. const {eventView, organization, transactionName} = this.props;
  114. return (
  115. <TeamKeyTransactionButton
  116. transactionName={transactionName}
  117. eventView={eventView}
  118. organization={organization}
  119. />
  120. );
  121. }
  122. renderSettingsButton() {
  123. const {organization, transactionName, eventView, onChangeThreshold} = this.props;
  124. return (
  125. <GuideAnchor target="project_transaction_threshold_override" position="bottom">
  126. <TransactionThresholdButton
  127. organization={organization}
  128. transactionName={transactionName}
  129. eventView={eventView}
  130. onChangeThreshold={onChangeThreshold}
  131. />
  132. </GuideAnchor>
  133. );
  134. }
  135. renderWebVitalsTab() {
  136. const {
  137. organization,
  138. eventView,
  139. location,
  140. projects,
  141. transactionName,
  142. currentTab,
  143. hasWebVitals,
  144. } = this.props;
  145. const vitalsTarget = vitalsRouteWithQuery({
  146. orgSlug: organization.slug,
  147. transaction: transactionName,
  148. projectID: decodeScalar(location.query.project),
  149. query: location.query,
  150. });
  151. const tab = (
  152. <ListLink
  153. href=""
  154. to={vitalsTarget}
  155. isActive={() => currentTab === Tab.WebVitals}
  156. onClick={this.trackTabClick(Tab.WebVitals)}
  157. >
  158. {t('Web Vitals')}
  159. </ListLink>
  160. );
  161. switch (hasWebVitals) {
  162. case 'maybe':
  163. // need to check if the web vitals tab should be shown
  164. // frontend projects should always show the web vitals tab
  165. if (
  166. getCurrentLandingDisplay(location, projects, eventView).field ===
  167. LandingDisplayField.FRONTEND_PAGELOAD
  168. ) {
  169. return tab;
  170. }
  171. // if it is not a frontend project, then we check to see if there
  172. // are any web vitals associated with the transaction recently
  173. return (
  174. <HasMeasurementsQuery
  175. location={location}
  176. orgSlug={organization.slug}
  177. eventView={eventView}
  178. transaction={transactionName}
  179. type="web"
  180. >
  181. {({hasMeasurements}) => (hasMeasurements ? tab : null)}
  182. </HasMeasurementsQuery>
  183. );
  184. case 'yes':
  185. // always show the web vitals tab
  186. return tab;
  187. case 'no':
  188. default:
  189. // never show the web vitals tab
  190. return null;
  191. }
  192. }
  193. render() {
  194. const {organization, location, projectId, transactionName, currentTab, projects} =
  195. this.props;
  196. const routeQuery = {
  197. orgSlug: organization.slug,
  198. transaction: transactionName,
  199. projectID: projectId,
  200. query: location.query,
  201. };
  202. const summaryTarget = transactionSummaryRouteWithQuery(routeQuery);
  203. const tagsTarget = tagsRouteWithQuery(routeQuery);
  204. const eventsTarget = eventsRouteWithQuery(routeQuery);
  205. const spansTarget = spansRouteWithQuery(routeQuery);
  206. const anomaliesTarget = anomaliesRouteWithQuery(routeQuery);
  207. const replaysTarget = replaysRouteWithQuery(routeQuery);
  208. const project = projects.find(p => p.id === projectId);
  209. return (
  210. <Layout.Header>
  211. <Layout.HeaderContent>
  212. <Breadcrumb
  213. organization={organization}
  214. location={location}
  215. transaction={{
  216. project: projectId,
  217. name: transactionName,
  218. }}
  219. tab={currentTab}
  220. />
  221. <Layout.Title>
  222. <TransactionName>
  223. {project && (
  224. <IdBadge
  225. project={project}
  226. avatarSize={28}
  227. hideName
  228. avatarProps={{hasTooltip: true, tooltip: project.slug}}
  229. />
  230. )}
  231. {transactionName}
  232. </TransactionName>
  233. </Layout.Title>
  234. </Layout.HeaderContent>
  235. <Layout.HeaderActions>
  236. <ButtonBar gap={1}>
  237. <Feature organization={organization} features={['incidents']}>
  238. {({hasFeature}) => hasFeature && this.renderCreateAlertButton()}
  239. </Feature>
  240. {this.renderKeyTransactionButton()}
  241. {this.renderSettingsButton()}
  242. </ButtonBar>
  243. </Layout.HeaderActions>
  244. <Fragment>
  245. <StyledNavTabs>
  246. <ListLink
  247. to={summaryTarget}
  248. isActive={() => currentTab === Tab.TransactionSummary}
  249. >
  250. {t('Overview')}
  251. </ListLink>
  252. <ListLink
  253. to={eventsTarget}
  254. isActive={() => currentTab === Tab.Events}
  255. onClick={this.trackTabClick(Tab.Events)}
  256. >
  257. {t('All Events')}
  258. </ListLink>
  259. <ListLink
  260. to={tagsTarget}
  261. isActive={() => currentTab === Tab.Tags}
  262. onClick={this.trackTabClick(Tab.Tags)}
  263. >
  264. {t('Tags')}
  265. </ListLink>
  266. <Feature
  267. organization={organization}
  268. features={['organizations:performance-suspect-spans-view']}
  269. >
  270. <ListLink
  271. isActive={() => currentTab === Tab.Spans}
  272. onClick={this.trackTabClick(Tab.Spans)}
  273. href=""
  274. to={spansTarget}
  275. >
  276. {t('Spans')}
  277. </ListLink>
  278. </Feature>
  279. <Feature
  280. organization={organization}
  281. features={['organizations:performance-anomaly-detection-ui']}
  282. >
  283. <ListLink
  284. data-test-id="anomalies-tab"
  285. to={anomaliesTarget}
  286. isActive={() => currentTab === Tab.Anomalies}
  287. onClick={this.trackTabClick(Tab.Anomalies)}
  288. >
  289. {t('Anomalies')}
  290. <FeatureBadge type="alpha" noTooltip />
  291. </ListLink>
  292. </Feature>
  293. {this.renderWebVitalsTab()}
  294. <Feature features={['session-replay-ui']} organization={organization}>
  295. <ListLink
  296. data-test-id="replays-tab"
  297. to={replaysTarget}
  298. isActive={() => currentTab === Tab.Replays}
  299. onClick={this.trackTabClick(Tab.Replays)}
  300. >
  301. {t('Replays')}
  302. <ReplaysFeatureBadge noTooltip />
  303. </ListLink>
  304. </Feature>
  305. </StyledNavTabs>
  306. </Fragment>
  307. </Layout.Header>
  308. );
  309. }
  310. }
  311. const StyledNavTabs = styled(NavTabs)`
  312. margin-bottom: 0;
  313. /* Makes sure the tabs are pushed into another row */
  314. width: 100%;
  315. `;
  316. const TransactionName = styled('div')`
  317. display: grid;
  318. grid-template-columns: max-content 1fr;
  319. grid-column-gap: ${space(1)};
  320. align-items: center;
  321. `;
  322. export default TransactionHeader;