index.tsx 12 KB

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