index.tsx 12 KB

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