index.tsx 13 KB

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