pageLayout.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if (!defined(projectId) || !defined(transactionName)) {
  63. redirectToPerformanceHomepage(organization, location);
  64. return null;
  65. }
  66. const project = projects.find(p => p.id === projectId);
  67. const eventView = generateEventView({location, transactionName});
  68. return (
  69. <SentryDocumentTitle
  70. title={getDocumentTitle(transactionName)}
  71. orgSlug={organization.slug}
  72. projectSlug={project?.slug}
  73. >
  74. <Feature
  75. features={['performance-view', ...features]}
  76. organization={organization}
  77. renderDisabled={NoAccess}
  78. >
  79. <PerformanceEventViewProvider value={{eventView}}>
  80. <PageFiltersContainer
  81. shouldForceProject={defined(project)}
  82. forceProject={project}
  83. specificProjectSlugs={defined(project) ? [project.slug] : []}
  84. >
  85. <StyledPageContent>
  86. <NoProjectMessage organization={organization}>
  87. <TransactionHeader
  88. eventView={eventView}
  89. location={location}
  90. organization={organization}
  91. projects={projects}
  92. projectId={projectId}
  93. transactionName={transactionName}
  94. currentTab={tab}
  95. hasWebVitals={tab === Tab.WebVitals ? 'yes' : 'maybe'}
  96. onChangeThreshold={(threshold, metric) => {
  97. setTransactionThreshold(threshold);
  98. setTransactionThresholdMetric(metric);
  99. }}
  100. />
  101. <Layout.Body>
  102. {defined(error) && (
  103. <StyledAlert type="error" showIcon>
  104. {error}
  105. </StyledAlert>
  106. )}
  107. <ChildComponent
  108. location={location}
  109. organization={organization}
  110. projects={projects}
  111. eventView={eventView}
  112. projectId={projectId}
  113. transactionName={transactionName}
  114. setError={setError}
  115. transactionThreshold={transactionThreshold}
  116. transactionThresholdMetric={transactionThresholdMetric}
  117. />
  118. </Layout.Body>
  119. </NoProjectMessage>
  120. </StyledPageContent>
  121. </PageFiltersContainer>
  122. </PerformanceEventViewProvider>
  123. </Feature>
  124. </SentryDocumentTitle>
  125. );
  126. }
  127. export function NoAccess() {
  128. return <Alert type="warning">{t("You don't have access to this feature")}</Alert>;
  129. }
  130. const StyledPageContent = styled(PageContent)`
  131. padding: 0;
  132. `;
  133. const StyledAlert = styled(Alert)`
  134. grid-column: 1/3;
  135. margin: 0;
  136. `;
  137. export function redirectToPerformanceHomepage(
  138. organization: Organization,
  139. location: Location
  140. ) {
  141. // If there is no transaction name, redirect to the Performance landing page
  142. browserHistory.replace({
  143. pathname: `/organizations/${organization.slug}/performance/`,
  144. query: {
  145. ...location.query,
  146. },
  147. });
  148. }
  149. export default PageLayout;