index.tsx 12 KB

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