index.tsx 14 KB

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