pageLayout.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {useCallback, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import Feature from 'sentry/components/acl/feature';
  6. import Alert from 'sentry/components/alert';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import NoProjectMessage from 'sentry/components/noProjectMessage';
  9. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  10. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  11. import {Tabs} from 'sentry/components/tabs';
  12. import {t} from 'sentry/locale';
  13. import {PageContent} from 'sentry/styles/organization';
  14. import {Organization, Project} from 'sentry/types';
  15. import {defined} from 'sentry/utils';
  16. import {trackAnalyticsEvent} from 'sentry/utils/analytics';
  17. import EventView from 'sentry/utils/discover/eventView';
  18. import {useMetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  19. import {PerformanceEventViewProvider} from 'sentry/utils/performance/contexts/performanceEventViewContext';
  20. import {decodeScalar} from 'sentry/utils/queryString';
  21. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  22. import {getSelectedProjectPlatforms, getTransactionName} from '../utils';
  23. import {anomaliesRouteWithQuery} from './transactionAnomalies/utils';
  24. import {eventsRouteWithQuery} from './transactionEvents/utils';
  25. import {replaysRouteWithQuery} from './transactionReplays/utils';
  26. import {spansRouteWithQuery} from './transactionSpans/utils';
  27. import {tagsRouteWithQuery} from './transactionTags/utils';
  28. import {vitalsRouteWithQuery} from './transactionVitals/utils';
  29. import TransactionHeader from './header';
  30. import Tab from './tabs';
  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. export type ChildProps = {
  60. eventView: EventView;
  61. location: Location;
  62. organization: Organization;
  63. projectId: string;
  64. projects: Project[];
  65. setError: React.Dispatch<React.SetStateAction<string | undefined>>;
  66. transactionName: string;
  67. // These are used to trigger a reload when the threshold/metric changes.
  68. transactionThreshold?: number;
  69. transactionThresholdMetric?: TransactionThresholdMetric;
  70. };
  71. type Props = {
  72. childComponent: (props: ChildProps) => JSX.Element;
  73. generateEventView: (props: {
  74. location: Location;
  75. organization: Organization;
  76. transactionName: string;
  77. }) => EventView;
  78. getDocumentTitle: (name: string) => string;
  79. location: Location;
  80. organization: Organization;
  81. projects: Project[];
  82. tab: Tab;
  83. features?: string[];
  84. };
  85. function PageLayout(props: Props) {
  86. const {
  87. location,
  88. organization,
  89. projects,
  90. tab,
  91. getDocumentTitle,
  92. generateEventView,
  93. childComponent: ChildComponent,
  94. features = [],
  95. } = props;
  96. const projectId = decodeScalar(location.query.project);
  97. const transactionName = getTransactionName(location);
  98. const [error, setError] = useState<string | undefined>();
  99. const metricsCardinality = useMetricsCardinalityContext();
  100. const [transactionThreshold, setTransactionThreshold] = useState<number | undefined>();
  101. const [transactionThresholdMetric, setTransactionThresholdMetric] = useState<
  102. TransactionThresholdMetric | undefined
  103. >();
  104. const getNewRoute = useCallback(
  105. (newTab: Tab) => {
  106. if (!transactionName) {
  107. return {};
  108. }
  109. const routeQuery = {
  110. orgSlug: organization.slug,
  111. transaction: transactionName,
  112. projectID: projectId,
  113. query: location.query,
  114. };
  115. switch (newTab) {
  116. case Tab.Tags:
  117. return tagsRouteWithQuery(routeQuery);
  118. case Tab.Events:
  119. return eventsRouteWithQuery(routeQuery);
  120. case Tab.Spans:
  121. return spansRouteWithQuery(routeQuery);
  122. case Tab.Anomalies:
  123. return anomaliesRouteWithQuery(routeQuery);
  124. case Tab.Replays:
  125. return replaysRouteWithQuery(routeQuery);
  126. case Tab.WebVitals:
  127. return vitalsRouteWithQuery({
  128. orgSlug: organization.slug,
  129. transaction: transactionName,
  130. projectID: decodeScalar(location.query.project),
  131. query: location.query,
  132. });
  133. case Tab.TransactionSummary:
  134. default:
  135. return transactionSummaryRouteWithQuery(routeQuery);
  136. }
  137. },
  138. [location.query, organization.slug, projectId, transactionName]
  139. );
  140. const onTabChange = useCallback(
  141. (newTab: Tab) => {
  142. // Prevent infinite rerenders
  143. if (newTab === tab) {
  144. return;
  145. }
  146. const analyticKeys = TAB_ANALYTICS[newTab];
  147. if (analyticKeys) {
  148. trackAnalyticsEvent({
  149. ...analyticKeys,
  150. organization_id: organization.id,
  151. project_platforms: getSelectedProjectPlatforms(location, projects),
  152. });
  153. }
  154. browserHistory.push(normalizeUrl(getNewRoute(newTab)));
  155. },
  156. [getNewRoute, tab, organization, location, projects]
  157. );
  158. if (!defined(projectId) || !defined(transactionName)) {
  159. redirectToPerformanceHomepage(organization, location);
  160. return null;
  161. }
  162. const project = projects.find(p => p.id === projectId);
  163. const eventView = generateEventView({location, transactionName, organization});
  164. return (
  165. <SentryDocumentTitle
  166. title={getDocumentTitle(transactionName)}
  167. orgSlug={organization.slug}
  168. projectSlug={project?.slug}
  169. >
  170. <Feature
  171. features={['performance-view', ...features]}
  172. organization={organization}
  173. renderDisabled={NoAccess}
  174. >
  175. <PerformanceEventViewProvider value={{eventView}}>
  176. <PageFiltersContainer
  177. shouldForceProject={defined(project)}
  178. forceProject={project}
  179. specificProjectSlugs={defined(project) ? [project.slug] : []}
  180. >
  181. <Tabs value={tab} onChange={onTabChange}>
  182. <StyledPageContent>
  183. <NoProjectMessage organization={organization}>
  184. <TransactionHeader
  185. eventView={eventView}
  186. location={location}
  187. organization={organization}
  188. projects={projects}
  189. projectId={projectId}
  190. transactionName={transactionName}
  191. currentTab={tab}
  192. hasWebVitals={tab === Tab.WebVitals ? 'yes' : 'maybe'}
  193. onChangeThreshold={(threshold, metric) => {
  194. setTransactionThreshold(threshold);
  195. setTransactionThresholdMetric(metric);
  196. }}
  197. metricsCardinality={metricsCardinality}
  198. />
  199. <Layout.Body>
  200. {defined(error) && (
  201. <StyledAlert type="error" showIcon>
  202. {error}
  203. </StyledAlert>
  204. )}
  205. <ChildComponent
  206. location={location}
  207. organization={organization}
  208. projects={projects}
  209. eventView={eventView}
  210. projectId={projectId}
  211. transactionName={transactionName}
  212. setError={setError}
  213. transactionThreshold={transactionThreshold}
  214. transactionThresholdMetric={transactionThresholdMetric}
  215. />
  216. </Layout.Body>
  217. </NoProjectMessage>
  218. </StyledPageContent>
  219. </Tabs>
  220. </PageFiltersContainer>
  221. </PerformanceEventViewProvider>
  222. </Feature>
  223. </SentryDocumentTitle>
  224. );
  225. }
  226. export function NoAccess() {
  227. return <Alert type="warning">{t("You don't have access to this feature")}</Alert>;
  228. }
  229. const StyledPageContent = styled(PageContent)`
  230. padding: 0;
  231. `;
  232. const StyledAlert = styled(Alert)`
  233. grid-column: 1/3;
  234. margin: 0;
  235. `;
  236. export function redirectToPerformanceHomepage(
  237. organization: Organization,
  238. location: Location
  239. ) {
  240. // If there is no transaction name, redirect to the Performance landing page
  241. browserHistory.replace({
  242. pathname: `/organizations/${organization.slug}/performance/`,
  243. query: {
  244. ...location.query,
  245. },
  246. });
  247. }
  248. export default PageLayout;