pageLayout.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import {useCallback, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {isString} from '@sentry/utils';
  4. import type {Location} from 'history';
  5. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  6. import Feature from 'sentry/components/acl/feature';
  7. import {Alert} from 'sentry/components/alert';
  8. import {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  12. import PickProjectToContinue from 'sentry/components/pickProjectToContinue';
  13. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  14. import {Tabs} from 'sentry/components/tabs';
  15. import {t} from 'sentry/locale';
  16. import type {Organization} from 'sentry/types/organization';
  17. import type {Project} from 'sentry/types/project';
  18. import {defined} from 'sentry/utils';
  19. import {trackAnalytics} from 'sentry/utils/analytics';
  20. import {browserHistory} from 'sentry/utils/browserHistory';
  21. import DiscoverQuery from 'sentry/utils/discover/discoverQuery';
  22. import type EventView from 'sentry/utils/discover/eventView';
  23. import {useMetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  24. import {PerformanceEventViewProvider} from 'sentry/utils/performance/contexts/performanceEventViewContext';
  25. import {decodeScalar} from 'sentry/utils/queryString';
  26. import useRouter from 'sentry/utils/useRouter';
  27. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  28. import {aggregateWaterfallRouteWithQuery} from 'sentry/views/performance/transactionSummary/aggregateSpanWaterfall/utils';
  29. import {getSelectedProjectPlatforms, getTransactionName} from '../utils';
  30. import {anomaliesRouteWithQuery} from './transactionAnomalies/utils';
  31. import {eventsRouteWithQuery} from './transactionEvents/utils';
  32. import {profilesRouteWithQuery} from './transactionProfiles/utils';
  33. import {replaysRouteWithQuery} from './transactionReplays/utils';
  34. import {spansRouteWithQuery} from './transactionSpans/utils';
  35. import {tagsRouteWithQuery} from './transactionTags/utils';
  36. import {vitalsRouteWithQuery} from './transactionVitals/utils';
  37. import TransactionHeader from './header';
  38. import Tab from './tabs';
  39. import type {TransactionThresholdMetric} from './transactionThresholdModal';
  40. import {generateTransactionSummaryRoute, transactionSummaryRouteWithQuery} from './utils';
  41. type TabEvents =
  42. | 'performance_views.vitals.vitals_tab_clicked'
  43. | 'performance_views.tags.tags_tab_clicked'
  44. | 'performance_views.events.events_tab_clicked'
  45. | 'performance_views.spans.spans_tab_clicked';
  46. const TAB_ANALYTICS: Partial<Record<Tab, TabEvents>> = {
  47. [Tab.WEB_VITALS]: 'performance_views.vitals.vitals_tab_clicked',
  48. [Tab.TAGS]: 'performance_views.tags.tags_tab_clicked',
  49. [Tab.EVENTS]: 'performance_views.events.events_tab_clicked',
  50. [Tab.SPANS]: 'performance_views.spans.spans_tab_clicked',
  51. };
  52. export type ChildProps = {
  53. eventView: EventView;
  54. location: Location;
  55. organization: Organization;
  56. projectId: string;
  57. projects: Project[];
  58. setError: React.Dispatch<React.SetStateAction<string | undefined>>;
  59. transactionName: string;
  60. // These are used to trigger a reload when the threshold/metric changes.
  61. transactionThreshold?: number;
  62. transactionThresholdMetric?: TransactionThresholdMetric;
  63. };
  64. type Props = {
  65. childComponent: (props: ChildProps) => JSX.Element;
  66. generateEventView: (props: {
  67. location: Location;
  68. organization: Organization;
  69. transactionName: string;
  70. }) => EventView;
  71. getDocumentTitle: (name: string) => string;
  72. location: Location;
  73. organization: Organization;
  74. projects: Project[];
  75. tab: Tab;
  76. features?: string[];
  77. };
  78. function PageLayout(props: Props) {
  79. const {
  80. location,
  81. organization,
  82. projects,
  83. tab,
  84. getDocumentTitle,
  85. generateEventView,
  86. childComponent: ChildComponent,
  87. features = [],
  88. } = props;
  89. let projectId: string | undefined;
  90. const filterProjects = location.query.project;
  91. if (isString(filterProjects) && filterProjects !== '-1') {
  92. projectId = filterProjects;
  93. }
  94. const router = useRouter();
  95. const transactionName = getTransactionName(location);
  96. const [error, setError] = useState<string | undefined>();
  97. const metricsCardinality = useMetricsCardinalityContext();
  98. const [transactionThreshold, setTransactionThreshold] = useState<number | undefined>();
  99. const [transactionThresholdMetric, setTransactionThresholdMetric] = useState<
  100. TransactionThresholdMetric | undefined
  101. >();
  102. const getNewRoute = useCallback(
  103. (newTab: Tab) => {
  104. if (!transactionName) {
  105. return {};
  106. }
  107. const routeQuery = {
  108. orgSlug: organization.slug,
  109. transaction: transactionName,
  110. projectID: projectId,
  111. query: location.query,
  112. };
  113. switch (newTab) {
  114. case Tab.TAGS:
  115. return tagsRouteWithQuery(routeQuery);
  116. case Tab.EVENTS:
  117. return eventsRouteWithQuery(routeQuery);
  118. case Tab.SPANS:
  119. return spansRouteWithQuery(routeQuery);
  120. case Tab.ANOMALIES:
  121. return anomaliesRouteWithQuery(routeQuery);
  122. case Tab.REPLAYS:
  123. return replaysRouteWithQuery(routeQuery);
  124. case Tab.PROFILING: {
  125. return profilesRouteWithQuery(routeQuery);
  126. }
  127. case Tab.AGGREGATE_WATERFALL:
  128. return aggregateWaterfallRouteWithQuery(routeQuery);
  129. case Tab.WEB_VITALS:
  130. return vitalsRouteWithQuery({
  131. orgSlug: organization.slug,
  132. transaction: transactionName,
  133. projectID: decodeScalar(location.query.project),
  134. query: location.query,
  135. });
  136. case Tab.TRANSACTION_SUMMARY:
  137. default:
  138. return transactionSummaryRouteWithQuery(routeQuery);
  139. }
  140. },
  141. [location.query, organization.slug, projectId, transactionName]
  142. );
  143. const onTabChange = useCallback(
  144. (newTab: Tab) => {
  145. // Prevent infinite rerenders
  146. if (newTab === tab) {
  147. return;
  148. }
  149. const analyticsKey = TAB_ANALYTICS[newTab];
  150. if (analyticsKey) {
  151. trackAnalytics(analyticsKey, {
  152. organization,
  153. project_platforms: getSelectedProjectPlatforms(location, projects),
  154. });
  155. }
  156. browserHistory.push(normalizeUrl(getNewRoute(newTab)));
  157. },
  158. [getNewRoute, tab, organization, location, projects]
  159. );
  160. if (!defined(transactionName)) {
  161. redirectToPerformanceHomepage(organization, location);
  162. return null;
  163. }
  164. const eventView = generateEventView({location, transactionName, organization});
  165. if (!defined(projectId)) {
  166. // Using a discover query to get the projects associated
  167. // with a transaction name
  168. const nextView = eventView.clone();
  169. nextView.query = `transaction:"${transactionName}"`;
  170. nextView.fields = [
  171. {
  172. field: 'project',
  173. width: COL_WIDTH_UNDEFINED,
  174. },
  175. {
  176. field: 'count()',
  177. width: COL_WIDTH_UNDEFINED,
  178. },
  179. ];
  180. return (
  181. <DiscoverQuery
  182. eventView={nextView}
  183. location={location}
  184. orgSlug={organization.slug}
  185. queryExtras={{project: filterProjects ? filterProjects : undefined}}
  186. referrer="api.performance.transaction-summary"
  187. >
  188. {({isLoading, tableData, error: discoverQueryError}) => {
  189. if (discoverQueryError) {
  190. addErrorMessage(t('Unable to get projects associated with transaction'));
  191. redirectToPerformanceHomepage(organization, location);
  192. return null;
  193. }
  194. if (isLoading) {
  195. return <LoadingIndicator />;
  196. }
  197. const selectableProjects = tableData?.data
  198. .map(row => projects.find(project => project.slug === row.project))
  199. .filter((p): p is Project => p !== undefined);
  200. return (
  201. selectableProjects && (
  202. <PickProjectToContinue
  203. data-test-id="transaction-sumamry-project-picker-modal"
  204. projects={selectableProjects}
  205. router={router}
  206. nextPath={{
  207. pathname: generateTransactionSummaryRoute({orgSlug: organization.slug}),
  208. query: {
  209. project: projectId,
  210. transaction: transactionName,
  211. statsPeriod: eventView.statsPeriod,
  212. referrer: 'performance-transaction-summary',
  213. ...location.query,
  214. },
  215. }}
  216. noProjectRedirectPath="/performance/"
  217. allowAllProjectsSelection
  218. />
  219. )
  220. );
  221. }}
  222. </DiscoverQuery>
  223. );
  224. }
  225. const project = projects.find(p => p.id === projectId);
  226. return (
  227. <SentryDocumentTitle
  228. title={getDocumentTitle(transactionName)}
  229. orgSlug={organization.slug}
  230. projectSlug={project?.slug}
  231. >
  232. <Feature
  233. features={['performance-view', ...features]}
  234. organization={organization}
  235. renderDisabled={NoAccess}
  236. >
  237. <PerformanceEventViewProvider value={{eventView}}>
  238. <PageFiltersContainer
  239. shouldForceProject={defined(project)}
  240. forceProject={project}
  241. specificProjectSlugs={defined(project) ? [project.slug] : []}
  242. >
  243. <Tabs value={tab} onChange={onTabChange}>
  244. <Layout.Page>
  245. <TransactionHeader
  246. eventView={eventView}
  247. location={location}
  248. organization={organization}
  249. projects={projects}
  250. projectId={projectId}
  251. transactionName={transactionName}
  252. currentTab={tab}
  253. hasWebVitals={tab === Tab.WEB_VITALS ? 'yes' : 'maybe'}
  254. onChangeThreshold={(threshold, metric) => {
  255. setTransactionThreshold(threshold);
  256. setTransactionThresholdMetric(metric);
  257. }}
  258. metricsCardinality={metricsCardinality}
  259. />
  260. <Layout.Body>
  261. {defined(error) && (
  262. <StyledAlert type="error" showIcon>
  263. {error}
  264. </StyledAlert>
  265. )}
  266. <ChildComponent
  267. location={location}
  268. organization={organization}
  269. projects={projects}
  270. eventView={eventView}
  271. projectId={projectId}
  272. transactionName={transactionName}
  273. setError={setError}
  274. transactionThreshold={transactionThreshold}
  275. transactionThresholdMetric={transactionThresholdMetric}
  276. />
  277. </Layout.Body>
  278. </Layout.Page>
  279. </Tabs>
  280. </PageFiltersContainer>
  281. </PerformanceEventViewProvider>
  282. </Feature>
  283. </SentryDocumentTitle>
  284. );
  285. }
  286. export function NoAccess() {
  287. return <Alert type="warning">{t("You don't have access to this feature")}</Alert>;
  288. }
  289. const StyledAlert = styled(Alert)`
  290. grid-column: 1/3;
  291. margin: 0;
  292. `;
  293. export function redirectToPerformanceHomepage(
  294. organization: Organization,
  295. location: Location
  296. ) {
  297. // If there is no transaction name, redirect to the Performance landing page
  298. browserHistory.replace(
  299. normalizeUrl({
  300. pathname: `/organizations/${organization.slug}/performance/`,
  301. query: {
  302. ...location.query,
  303. },
  304. })
  305. );
  306. }
  307. export default PageLayout;