header.tsx 10 KB

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