index.tsx 12 KB

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