header.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import {useCallback} 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 {isProfilingSupportedOrProjectHasProfiles} from 'sentry/components/profiling/ProfilingOnboarding/util';
  12. import ReplayCountBadge from 'sentry/components/replays/replayCountBadge';
  13. import ReplaysFeatureBadge from 'sentry/components/replays/replaysFeatureBadge';
  14. import useReplaysCount from 'sentry/components/replays/useReplaysCount';
  15. import {Item, TabList} from 'sentry/components/tabs';
  16. import {t} from 'sentry/locale';
  17. import space from 'sentry/styles/space';
  18. import {Organization, Project} from 'sentry/types';
  19. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  20. import EventView from 'sentry/utils/discover/eventView';
  21. import {MetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  22. import HasMeasurementsQuery from 'sentry/utils/performance/vitals/hasMeasurementsQuery';
  23. import projectSupportsReplay from 'sentry/utils/replays/projectSupportsReplay';
  24. import Breadcrumb from 'sentry/views/performance/breadcrumb';
  25. import {getCurrentLandingDisplay, LandingDisplayField} from '../landing/utils';
  26. import Tab from './tabs';
  27. import TeamKeyTransactionButton from './teamKeyTransactionButton';
  28. import TransactionThresholdButton from './transactionThresholdButton';
  29. import {TransactionThresholdMetric} from './transactionThresholdModal';
  30. type Props = {
  31. currentTab: Tab;
  32. eventView: EventView;
  33. hasWebVitals: 'maybe' | 'yes' | 'no';
  34. location: Location;
  35. organization: Organization;
  36. projectId: string;
  37. projects: Project[];
  38. transactionName: string;
  39. metricsCardinality?: MetricsCardinalityContext;
  40. onChangeThreshold?: (threshold: number, metric: TransactionThresholdMetric) => void;
  41. };
  42. function TransactionHeader({
  43. eventView,
  44. organization,
  45. projects,
  46. projectId,
  47. metricsCardinality,
  48. location,
  49. transactionName,
  50. onChangeThreshold,
  51. currentTab,
  52. hasWebVitals,
  53. }: Props) {
  54. function handleCreateAlertSuccess() {
  55. trackAdvancedAnalyticsEvent('performance_views.summary.create_alert_clicked', {
  56. organization,
  57. });
  58. }
  59. const project = projects.find(p => p.id === projectId);
  60. const hasAnomalyDetection = organization.features?.includes(
  61. 'performance-anomaly-detection-ui'
  62. );
  63. const hasSessionReplay =
  64. organization.features.includes('session-replay-ui') && projectSupportsReplay(project);
  65. const hasProfiling =
  66. project &&
  67. organization.features.includes('profiling') &&
  68. isProfilingSupportedOrProjectHasProfiles(project);
  69. const getWebVitals = useCallback(
  70. (hasMeasurements: boolean) => {
  71. switch (hasWebVitals) {
  72. case 'maybe':
  73. // need to check if the web vitals tab should be shown
  74. // frontend projects should always show the web vitals tab
  75. if (
  76. getCurrentLandingDisplay(location, projects, eventView).field ===
  77. LandingDisplayField.FRONTEND_PAGELOAD
  78. ) {
  79. return true;
  80. }
  81. // if it is not a frontend project, then we check to see if there
  82. // are any web vitals associated with the transaction recently
  83. return hasMeasurements;
  84. case 'yes':
  85. // always show the web vitals tab
  86. return true;
  87. case 'no':
  88. default:
  89. // never show the web vitals tab
  90. return false;
  91. }
  92. },
  93. [hasWebVitals, location, projects, eventView]
  94. );
  95. const replaysCount = useReplaysCount({
  96. transactionNames: transactionName,
  97. organization,
  98. projectIds: project ? [Number(project.id)] : [],
  99. })[transactionName];
  100. return (
  101. <Layout.Header>
  102. <Layout.HeaderContent>
  103. <Breadcrumb
  104. organization={organization}
  105. location={location}
  106. transaction={{
  107. project: projectId,
  108. name: transactionName,
  109. }}
  110. tab={currentTab}
  111. />
  112. <Layout.Title>
  113. <TransactionName>
  114. {project && (
  115. <IdBadge
  116. project={project}
  117. avatarSize={28}
  118. hideName
  119. avatarProps={{hasTooltip: true, tooltip: project.slug}}
  120. />
  121. )}
  122. {transactionName}
  123. </TransactionName>
  124. </Layout.Title>
  125. </Layout.HeaderContent>
  126. <Layout.HeaderActions>
  127. <ButtonBar gap={1}>
  128. <Feature organization={organization} features={['incidents']}>
  129. {({hasFeature}) =>
  130. hasFeature && !metricsCardinality?.isLoading ? (
  131. <CreateAlertFromViewButton
  132. size="sm"
  133. eventView={eventView}
  134. organization={organization}
  135. projects={projects}
  136. onClick={handleCreateAlertSuccess}
  137. referrer="performance"
  138. alertType="trans_duration"
  139. aria-label={t('Create Alert')}
  140. disableMetricDataset={
  141. metricsCardinality?.outcome?.forceTransactionsOnly
  142. }
  143. />
  144. ) : null
  145. }
  146. </Feature>
  147. <TeamKeyTransactionButton
  148. transactionName={transactionName}
  149. eventView={eventView}
  150. organization={organization}
  151. />
  152. <GuideAnchor target="project_transaction_threshold_override" position="bottom">
  153. <TransactionThresholdButton
  154. organization={organization}
  155. transactionName={transactionName}
  156. eventView={eventView}
  157. onChangeThreshold={onChangeThreshold}
  158. />
  159. </GuideAnchor>
  160. </ButtonBar>
  161. </Layout.HeaderActions>
  162. <HasMeasurementsQuery
  163. location={location}
  164. orgSlug={organization.slug}
  165. eventView={eventView}
  166. transaction={transactionName}
  167. type="web"
  168. >
  169. {({hasMeasurements}) => {
  170. const renderWebVitals = getWebVitals(!!hasMeasurements);
  171. return (
  172. <TabList
  173. hideBorder
  174. outerWrapStyles={{
  175. gridColumn: '1 / -1',
  176. }}
  177. >
  178. <Item key={Tab.TransactionSummary}>{t('Overview')}</Item>
  179. <Item key={Tab.Events}>{t('All Events')}</Item>
  180. <Item key={Tab.Tags}>{t('Tags')}</Item>
  181. <Item key={Tab.Spans}>{t('Spans')}</Item>
  182. <Item
  183. key={Tab.Anomalies}
  184. textValue={t('Anomalies')}
  185. hidden={!hasAnomalyDetection}
  186. >
  187. {t('Anomalies')}
  188. <FeatureBadge type="alpha" noTooltip />
  189. </Item>
  190. <Item
  191. key={Tab.WebVitals}
  192. textValue={t('Web Vitals')}
  193. hidden={!renderWebVitals}
  194. >
  195. {t('Web Vitals')}
  196. </Item>
  197. <Item key={Tab.Replays} textValue={t('Replays')} hidden={!hasSessionReplay}>
  198. {t('Replays')}
  199. <ReplayCountBadge count={replaysCount} />
  200. <ReplaysFeatureBadge noTooltip />
  201. </Item>
  202. <Item key={Tab.Profiling} textValue={t('Profiling')} hidden={!hasProfiling}>
  203. {t('Profiling')}
  204. <FeatureBadge type="beta" noTooltip />
  205. </Item>
  206. </TabList>
  207. );
  208. }}
  209. </HasMeasurementsQuery>
  210. </Layout.Header>
  211. );
  212. }
  213. const TransactionName = styled('div')`
  214. display: grid;
  215. grid-template-columns: max-content 1fr;
  216. grid-column-gap: ${space(1)};
  217. align-items: center;
  218. `;
  219. export default TransactionHeader;