index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import {FC, Fragment, useEffect, useRef} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {Button} from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import DatePageFilter from 'sentry/components/datePageFilter';
  8. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  13. import TransactionNameSearchBar from 'sentry/components/performance/searchBar';
  14. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  15. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  16. import {Item, TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  17. import {t} from 'sentry/locale';
  18. import space from 'sentry/styles/space';
  19. import {Organization, PageFilters, Project} from 'sentry/types';
  20. import EventView from 'sentry/utils/discover/eventView';
  21. import {GenericQueryBatcher} from 'sentry/utils/performance/contexts/genericQueryBatcher';
  22. import {MetricsCardinalityProvider} from 'sentry/utils/performance/contexts/metricsCardinality';
  23. import {
  24. MEPConsumer,
  25. MEPSettingProvider,
  26. MEPState,
  27. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  28. import {
  29. PageErrorAlert,
  30. PageErrorProvider,
  31. } from 'sentry/utils/performance/contexts/pageError';
  32. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  33. import useTeams from 'sentry/utils/useTeams';
  34. import Onboarding from '../onboarding';
  35. import {MetricsEventsDropdown} from '../transactionSummary/transactionOverview/metricEvents/metricsEventsDropdown';
  36. import {getTransactionSearchQuery} from '../utils';
  37. import {AllTransactionsView} from './views/allTransactionsView';
  38. import {BackendView} from './views/backendView';
  39. import {FrontendOtherView} from './views/frontendOtherView';
  40. import {FrontendPageloadView} from './views/frontendPageloadView';
  41. import {MobileView} from './views/mobileView';
  42. import {MetricsDataSwitcher} from './metricsDataSwitcher';
  43. import {MetricsDataSwitcherAlert} from './metricsDataSwitcherAlert';
  44. import {
  45. getDefaultDisplayForPlatform,
  46. getLandingDisplayFromParam,
  47. handleLandingDisplayChange,
  48. LANDING_DISPLAYS,
  49. LandingDisplayField,
  50. } from './utils';
  51. type Props = {
  52. eventView: EventView;
  53. handleSearch: (searchQuery: string, currentMEPState?: MEPState) => void;
  54. handleTrendsClick: () => void;
  55. location: Location;
  56. onboardingProject: Project | undefined;
  57. organization: Organization;
  58. projects: Project[];
  59. router: InjectedRouter;
  60. selection: PageFilters;
  61. setError: (msg: string | undefined) => void;
  62. withStaticFilters: boolean;
  63. };
  64. const fieldToViewMap: Record<LandingDisplayField, FC<Props>> = {
  65. [LandingDisplayField.ALL]: AllTransactionsView,
  66. [LandingDisplayField.BACKEND]: BackendView,
  67. [LandingDisplayField.FRONTEND_OTHER]: FrontendOtherView,
  68. [LandingDisplayField.FRONTEND_PAGELOAD]: FrontendPageloadView,
  69. [LandingDisplayField.MOBILE]: MobileView,
  70. };
  71. export function PerformanceLanding(props: Props) {
  72. const {
  73. organization,
  74. location,
  75. eventView,
  76. projects,
  77. handleSearch,
  78. handleTrendsClick,
  79. onboardingProject,
  80. } = props;
  81. const {teams, initiallyLoaded} = useTeams({provideUserTeams: true});
  82. const hasMounted = useRef(false);
  83. const paramLandingDisplay = getLandingDisplayFromParam(location);
  84. const defaultLandingDisplayForProjects = getDefaultDisplayForPlatform(
  85. projects,
  86. eventView
  87. );
  88. const landingDisplay = paramLandingDisplay ?? defaultLandingDisplayForProjects;
  89. const showOnboarding = onboardingProject !== undefined;
  90. useEffect(() => {
  91. if (hasMounted.current) {
  92. browserHistory.replace({
  93. pathname: location.pathname,
  94. query: {
  95. ...location.query,
  96. landingDisplay: undefined,
  97. },
  98. });
  99. }
  100. // eslint-disable-next-line react-hooks/exhaustive-deps
  101. }, [eventView.project.join('.')]);
  102. useEffect(() => {
  103. hasMounted.current = true;
  104. }, []);
  105. const getFreeTextFromQuery = (query: string) => {
  106. const conditions = new MutableSearch(query);
  107. const transactionValues = conditions.getFilterValues('transaction');
  108. if (transactionValues.length) {
  109. return transactionValues[0];
  110. }
  111. return '';
  112. };
  113. const derivedQuery = getTransactionSearchQuery(location, eventView.query);
  114. const ViewComponent = fieldToViewMap[landingDisplay.field];
  115. let pageFilters: React.ReactNode = (
  116. <PageFilterBar condensed>
  117. <ProjectPageFilter />
  118. <EnvironmentPageFilter />
  119. <DatePageFilter alignDropdown="left" />
  120. </PageFilterBar>
  121. );
  122. if (showOnboarding) {
  123. pageFilters = <SearchContainerWithFilter>{pageFilters}</SearchContainerWithFilter>;
  124. }
  125. const SearchFilterContainer = organization.features.includes('performance-use-metrics')
  126. ? SearchContainerWithFilterAndMetrics
  127. : SearchContainerWithFilter;
  128. return (
  129. <Layout.Page data-test-id="performance-landing-v3">
  130. <PageErrorProvider>
  131. <Tabs
  132. value={landingDisplay.field}
  133. onChange={field =>
  134. handleLandingDisplayChange(field, location, projects, organization, eventView)
  135. }
  136. >
  137. <Layout.Header>
  138. <Layout.HeaderContent>
  139. <Layout.Title>
  140. {t('Performance')}
  141. <PageHeadingQuestionTooltip
  142. docsUrl="https://docs.sentry.io/product/performance/"
  143. title={t(
  144. 'Your main view for transaction data with graphs that visualize transactions or trends, as well as a table where you can drill down on individual transactions.'
  145. )}
  146. />
  147. </Layout.Title>
  148. </Layout.HeaderContent>
  149. <Layout.HeaderActions>
  150. {!showOnboarding && (
  151. <ButtonBar gap={3}>
  152. <Button
  153. size="sm"
  154. priority="primary"
  155. data-test-id="landing-header-trends"
  156. onClick={() => handleTrendsClick()}
  157. >
  158. {t('View Trends')}
  159. </Button>
  160. </ButtonBar>
  161. )}
  162. </Layout.HeaderActions>
  163. <TabList hideBorder>
  164. {LANDING_DISPLAYS.map(({label, field}) => (
  165. <Item key={field}>{label}</Item>
  166. ))}
  167. </TabList>
  168. </Layout.Header>
  169. <Layout.Body data-test-id="performance-landing-body">
  170. <Layout.Main fullWidth>
  171. <TabPanels>
  172. <Item key={landingDisplay.field}>
  173. <MetricsCardinalityProvider
  174. sendOutcomeAnalytics
  175. organization={organization}
  176. location={location}
  177. >
  178. <MetricsDataSwitcher
  179. organization={organization}
  180. eventView={eventView}
  181. location={location}
  182. >
  183. {metricsDataSide => {
  184. return (
  185. <MEPSettingProvider
  186. location={location}
  187. forceTransactions={metricsDataSide.forceTransactionsOnly}
  188. >
  189. <MetricsDataSwitcherAlert
  190. organization={organization}
  191. eventView={eventView}
  192. projects={projects}
  193. location={location}
  194. router={props.router}
  195. {...metricsDataSide}
  196. />
  197. <PageErrorAlert />
  198. {showOnboarding ? (
  199. <Fragment>
  200. {pageFilters}
  201. <Onboarding
  202. organization={organization}
  203. project={onboardingProject}
  204. />
  205. </Fragment>
  206. ) : (
  207. <Fragment>
  208. <SearchFilterContainer>
  209. {pageFilters}
  210. <MEPConsumer>
  211. {({metricSettingState}) => (
  212. // TODO replace `handleSearch prop` with transaction name search once
  213. // transaction name search becomes the default search bar
  214. <TransactionNameSearchBar
  215. organization={organization}
  216. eventView={eventView}
  217. onSearch={(query: string) => {
  218. handleSearch(
  219. query,
  220. metricSettingState ?? undefined
  221. );
  222. }}
  223. query={getFreeTextFromQuery(derivedQuery)}
  224. />
  225. )}
  226. </MEPConsumer>
  227. <MetricsEventsDropdown />
  228. </SearchFilterContainer>
  229. {initiallyLoaded ? (
  230. <TeamKeyTransactionManager.Provider
  231. organization={organization}
  232. teams={teams}
  233. selectedTeams={['myteams']}
  234. selectedProjects={eventView.project.map(String)}
  235. >
  236. <GenericQueryBatcher>
  237. <ViewComponent {...props} />
  238. </GenericQueryBatcher>
  239. </TeamKeyTransactionManager.Provider>
  240. ) : (
  241. <LoadingIndicator />
  242. )}
  243. </Fragment>
  244. )}
  245. </MEPSettingProvider>
  246. );
  247. }}
  248. </MetricsDataSwitcher>
  249. </MetricsCardinalityProvider>
  250. </Item>
  251. </TabPanels>
  252. </Layout.Main>
  253. </Layout.Body>
  254. </Tabs>
  255. </PageErrorProvider>
  256. </Layout.Page>
  257. );
  258. }
  259. const SearchContainerWithFilter = styled('div')`
  260. display: grid;
  261. grid-template-rows: auto auto;
  262. gap: ${space(2)};
  263. margin-bottom: ${space(2)};
  264. @media (min-width: ${p => p.theme.breakpoints.small}) {
  265. grid-template-rows: auto;
  266. grid-template-columns: auto 1fr;
  267. }
  268. `;
  269. const SearchContainerWithFilterAndMetrics = styled('div')`
  270. display: grid;
  271. grid-template-rows: auto auto auto;
  272. gap: ${space(2)};
  273. margin-bottom: ${space(2)};
  274. @media (min-width: ${p => p.theme.breakpoints.small}) {
  275. grid-template-rows: auto;
  276. grid-template-columns: auto 1fr auto;
  277. }
  278. `;