index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. 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. const searchQuery =
  213. metricSettingState === MEPState.metricsOnly
  214. ? getFreeTextFromQuery(derivedQuery)
  215. : derivedQuery;
  216. return (metricSettingState ===
  217. MEPState.metricsOnly &&
  218. shouldShowTransactionNameOnlySearch) ||
  219. shouldForceTransactionNameOnlySearch ? (
  220. // TODO replace `handleSearch prop` with transaction name search once
  221. // transaction name search becomes the default search bar
  222. <TransactionNameSearchBar
  223. organization={organization}
  224. eventView={eventView}
  225. onSearch={(query: string) => {
  226. handleSearch(
  227. query,
  228. metricSettingState ?? undefined
  229. );
  230. }}
  231. query={searchQuery}
  232. />
  233. ) : (
  234. <SearchBar
  235. searchSource="performance_landing"
  236. organization={organization}
  237. projectIds={eventView.project}
  238. query={searchQuery}
  239. fields={generateAggregateFields(
  240. organization,
  241. [...eventView.fields, {field: 'tps()'}],
  242. ['epm()', 'eps()']
  243. )}
  244. onSearch={(query: string) =>
  245. handleSearch(
  246. query,
  247. metricSettingState ?? undefined
  248. )
  249. }
  250. maxQueryLength={MAX_QUERY_LENGTH}
  251. />
  252. );
  253. }}
  254. </MEPConsumer>
  255. <MetricsEventsDropdown />
  256. </SearchFilterContainer>
  257. {initiallyLoaded ? (
  258. <TeamKeyTransactionManager.Provider
  259. organization={organization}
  260. teams={teams}
  261. selectedTeams={['myteams']}
  262. selectedProjects={eventView.project.map(String)}
  263. >
  264. <GenericQueryBatcher>
  265. <ViewComponent {...props} />
  266. </GenericQueryBatcher>
  267. </TeamKeyTransactionManager.Provider>
  268. ) : (
  269. <LoadingIndicator />
  270. )}
  271. </Fragment>
  272. )}
  273. </MEPSettingProvider>
  274. );
  275. }}
  276. </MetricsDataSwitcher>
  277. </MetricsCardinalityProvider>
  278. </Item>
  279. </TabPanels>
  280. </Layout.Main>
  281. </Layout.Body>
  282. </Tabs>
  283. </PageErrorProvider>
  284. </StyledPageContent>
  285. );
  286. }
  287. const StyledPageContent = styled(PageContent)`
  288. padding: 0;
  289. `;
  290. const StyledHeading = styled(PageHeading)`
  291. line-height: 40px;
  292. `;
  293. const SearchContainerWithFilter = styled('div')`
  294. display: grid;
  295. grid-template-rows: auto auto;
  296. gap: ${space(2)};
  297. margin-bottom: ${space(2)};
  298. @media (min-width: ${p => p.theme.breakpoints.small}) {
  299. grid-template-rows: auto;
  300. grid-template-columns: auto 1fr;
  301. }
  302. `;
  303. const SearchContainerWithFilterAndMetrics = styled('div')`
  304. display: grid;
  305. grid-template-rows: auto auto auto;
  306. gap: ${space(2)};
  307. margin-bottom: ${space(2)};
  308. @media (min-width: ${p => p.theme.breakpoints.small}) {
  309. grid-template-rows: auto;
  310. grid-template-columns: auto 1fr auto;
  311. }
  312. `;