header.tsx 11 KB

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