index.tsx 12 KB

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