pageLayout.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import {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 {t} from 'sentry/locale';
  12. import {PageContent} from 'sentry/styles/organization';
  13. import {Organization, Project} from 'sentry/types';
  14. import {defined} from 'sentry/utils';
  15. import EventView from 'sentry/utils/discover/eventView';
  16. import {PerformanceEventViewProvider} from 'sentry/utils/performance/contexts/performanceEventViewContext';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {getTransactionName} from '../utils';
  19. import TransactionHeader from './header';
  20. import Tab from './tabs';
  21. import {TransactionThresholdMetric} from './transactionThresholdModal';
  22. export type ChildProps = {
  23. eventView: EventView;
  24. location: Location;
  25. organization: Organization;
  26. projectId: string;
  27. projects: Project[];
  28. setError: React.Dispatch<React.SetStateAction<string | undefined>>;
  29. transactionName: string;
  30. // These are used to trigger a reload when the threshold/metric changes.
  31. transactionThreshold?: number;
  32. transactionThresholdMetric?: TransactionThresholdMetric;
  33. };
  34. type Props = {
  35. childComponent: (props: ChildProps) => JSX.Element;
  36. generateEventView: (props: {location: Location; transactionName: string}) => EventView;
  37. getDocumentTitle: (name: string) => string;
  38. location: Location;
  39. organization: Organization;
  40. projects: Project[];
  41. tab: Tab;
  42. features?: string[];
  43. };
  44. function PageLayout(props: Props) {
  45. const {
  46. location,
  47. organization,
  48. projects,
  49. tab,
  50. getDocumentTitle,
  51. generateEventView,
  52. childComponent: ChildComponent,
  53. features = [],
  54. } = props;
  55. const projectId = decodeScalar(location.query.project);
  56. const transactionName = getTransactionName(location);
  57. const [error, setError] = useState<string | undefined>();
  58. const [transactionThreshold, setTransactionThreshold] = useState<number | undefined>();
  59. const [transactionThresholdMetric, setTransactionThresholdMetric] = useState<
  60. TransactionThresholdMetric | undefined
  61. >();
  62. const [incompatibleAlertNotice, setIncompatibleAlertNotice] =
  63. useState<React.ReactNode>(null);
  64. if (!defined(projectId) || !defined(transactionName)) {
  65. redirectToPerformanceHomepage(organization, location);
  66. return null;
  67. }
  68. const project = projects.find(p => p.id === projectId);
  69. const handleIncompatibleQuery = (incompatibleAlertNoticeFn, _errors) => {
  70. const notice = incompatibleAlertNoticeFn(() => setIncompatibleAlertNotice(null));
  71. setIncompatibleAlertNotice(notice);
  72. };
  73. const eventView = generateEventView({location, transactionName});
  74. return (
  75. <SentryDocumentTitle
  76. title={getDocumentTitle(transactionName)}
  77. orgSlug={organization.slug}
  78. projectSlug={project?.slug}
  79. >
  80. <Feature
  81. features={['performance-view', ...features]}
  82. organization={organization}
  83. renderDisabled={NoAccess}
  84. >
  85. <PerformanceEventViewProvider value={{eventView}}>
  86. <PageFiltersContainer
  87. shouldForceProject={defined(project)}
  88. forceProject={project}
  89. specificProjectSlugs={defined(project) ? [project.slug] : []}
  90. >
  91. <StyledPageContent>
  92. <NoProjectMessage organization={organization}>
  93. <TransactionHeader
  94. eventView={eventView}
  95. location={location}
  96. organization={organization}
  97. projects={projects}
  98. projectId={projectId}
  99. transactionName={transactionName}
  100. currentTab={tab}
  101. hasWebVitals={tab === Tab.WebVitals ? 'yes' : 'maybe'}
  102. handleIncompatibleQuery={handleIncompatibleQuery}
  103. onChangeThreshold={(threshold, metric) => {
  104. setTransactionThreshold(threshold);
  105. setTransactionThresholdMetric(metric);
  106. }}
  107. />
  108. <Layout.Body>
  109. {defined(error) && (
  110. <StyledAlert type="error" showIcon>
  111. {error}
  112. </StyledAlert>
  113. )}
  114. {incompatibleAlertNotice && (
  115. <Layout.Main fullWidth>{incompatibleAlertNotice}</Layout.Main>
  116. )}
  117. <ChildComponent
  118. location={location}
  119. organization={organization}
  120. projects={projects}
  121. eventView={eventView}
  122. projectId={projectId}
  123. transactionName={transactionName}
  124. setError={setError}
  125. transactionThreshold={transactionThreshold}
  126. transactionThresholdMetric={transactionThresholdMetric}
  127. />
  128. </Layout.Body>
  129. </NoProjectMessage>
  130. </StyledPageContent>
  131. </PageFiltersContainer>
  132. </PerformanceEventViewProvider>
  133. </Feature>
  134. </SentryDocumentTitle>
  135. );
  136. }
  137. export function NoAccess() {
  138. return <Alert type="warning">{t("You don't have access to this feature")}</Alert>;
  139. }
  140. const StyledPageContent = styled(PageContent)`
  141. padding: 0;
  142. `;
  143. const StyledAlert = styled(Alert)`
  144. grid-column: 1/3;
  145. margin: 0;
  146. `;
  147. export function redirectToPerformanceHomepage(
  148. organization: Organization,
  149. location: Location
  150. ) {
  151. // If there is no transaction name, redirect to the Performance landing page
  152. browserHistory.replace({
  153. pathname: `/organizations/${organization.slug}/performance/`,
  154. query: {
  155. ...location.query,
  156. },
  157. });
  158. }
  159. export default PageLayout;