index.tsx 14 KB

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