index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import type {FC} from 'react';
  2. import {Fragment, useEffect, useRef} from 'react';
  3. import styled from '@emotion/styled';
  4. import type {Location} from 'history';
  5. import {Button} from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  8. import * as Layout from 'sentry/components/layouts/thirds';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  11. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  12. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  13. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  14. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  15. import TransactionNameSearchBar from 'sentry/components/performance/searchBar';
  16. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  17. import {TabList, TabPanels, Tabs} from 'sentry/components/tabs';
  18. import {t} from 'sentry/locale';
  19. import {space} from 'sentry/styles/space';
  20. import type {PageFilters} from 'sentry/types/core';
  21. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  22. import type {Organization} from 'sentry/types/organization';
  23. import type {Project} from 'sentry/types/project';
  24. import {trackAnalytics} from 'sentry/utils/analytics';
  25. import {browserHistory} from 'sentry/utils/browserHistory';
  26. import type EventView from 'sentry/utils/discover/eventView';
  27. import {GenericQueryBatcher} from 'sentry/utils/performance/contexts/genericQueryBatcher';
  28. import {MetricsCardinalityProvider} from 'sentry/utils/performance/contexts/metricsCardinality';
  29. import type {MEPState} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  30. import {
  31. MEPConsumer,
  32. MEPSettingProvider,
  33. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  34. import {PageAlert, PageAlertProvider} from 'sentry/utils/performance/contexts/pageAlert';
  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. useEffect(() => {
  109. if (showOnboarding) {
  110. trackAnalytics('performance_views.overview.has_data', {
  111. table_data_state: 'onboarding',
  112. tab: paramLandingDisplay?.field,
  113. organization,
  114. });
  115. }
  116. }, [showOnboarding, paramLandingDisplay, organization]);
  117. const getFreeTextFromQuery = (query: string) => {
  118. const conditions = new MutableSearch(query);
  119. const transactionValues = conditions.getFilterValues('transaction');
  120. if (transactionValues.length) {
  121. return transactionValues[0];
  122. }
  123. if (conditions.freeText.length > 0) {
  124. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  125. // so no need to wrap it here
  126. return conditions.freeText.join(' ');
  127. }
  128. return '';
  129. };
  130. const derivedQuery = getTransactionSearchQuery(location, eventView.query);
  131. const ViewComponent = fieldToViewMap[landingDisplay.field];
  132. let pageFilters: React.ReactNode = (
  133. <PageFilterBar condensed>
  134. <ProjectPageFilter />
  135. <EnvironmentPageFilter />
  136. <DatePageFilter />
  137. </PageFilterBar>
  138. );
  139. if (showOnboarding) {
  140. pageFilters = <SearchContainerWithFilter>{pageFilters}</SearchContainerWithFilter>;
  141. }
  142. const SearchFilterContainer = organization.features.includes('performance-use-metrics')
  143. ? SearchContainerWithFilterAndMetrics
  144. : SearchContainerWithFilter;
  145. return (
  146. <Layout.Page data-test-id="performance-landing-v3">
  147. <PageAlertProvider>
  148. <Tabs
  149. value={landingDisplay.field}
  150. onChange={field =>
  151. handleLandingDisplayChange(field, location, projects, organization, eventView)
  152. }
  153. >
  154. <Layout.Header>
  155. <Layout.HeaderContent>
  156. <Layout.Title>
  157. {t('Performance')}
  158. <PageHeadingQuestionTooltip
  159. docsUrl="https://docs.sentry.io/product/performance/"
  160. title={t(
  161. '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.'
  162. )}
  163. />
  164. </Layout.Title>
  165. </Layout.HeaderContent>
  166. <Layout.HeaderActions>
  167. {!showOnboarding && (
  168. <ButtonBar gap={1}>
  169. <Button
  170. size="sm"
  171. priority="primary"
  172. data-test-id="landing-header-trends"
  173. onClick={() => handleTrendsClick()}
  174. >
  175. {t('View Trends')}
  176. </Button>
  177. <FeedbackWidgetButton />
  178. </ButtonBar>
  179. )}
  180. </Layout.HeaderActions>
  181. <TabList hideBorder>
  182. {LANDING_DISPLAYS.map(({label, field}) => (
  183. <TabList.Item key={field}>{label}</TabList.Item>
  184. ))}
  185. </TabList>
  186. </Layout.Header>
  187. <Layout.Body data-test-id="performance-landing-body">
  188. <Layout.Main fullWidth>
  189. <TabPanels>
  190. <TabPanels.Item key={landingDisplay.field}>
  191. <MetricsCardinalityProvider
  192. sendOutcomeAnalytics
  193. organization={organization}
  194. location={location}
  195. >
  196. <MetricsDataSwitcher
  197. organization={organization}
  198. eventView={eventView}
  199. location={location}
  200. >
  201. {metricsDataSide => {
  202. return (
  203. <MEPSettingProvider
  204. location={location}
  205. forceTransactions={metricsDataSide.forceTransactionsOnly}
  206. >
  207. <MetricsDataSwitcherAlert
  208. organization={organization}
  209. eventView={eventView}
  210. projects={projects}
  211. location={location}
  212. router={props.router}
  213. {...metricsDataSide}
  214. />
  215. <PageAlert />
  216. {showOnboarding ? (
  217. <Fragment>
  218. {pageFilters}
  219. <Onboarding
  220. organization={organization}
  221. project={onboardingProject}
  222. />
  223. </Fragment>
  224. ) : (
  225. <Fragment>
  226. <SearchFilterContainer>
  227. {pageFilters}
  228. <MEPConsumer>
  229. {({metricSettingState}) => (
  230. // TODO replace `handleSearch prop` with transaction name search once
  231. // transaction name search becomes the default search bar
  232. <TransactionNameSearchBar
  233. organization={organization}
  234. eventView={eventView}
  235. onSearch={(query: string) => {
  236. handleSearch(
  237. query,
  238. metricSettingState ?? undefined
  239. );
  240. }}
  241. query={getFreeTextFromQuery(derivedQuery)}
  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. }}
  266. </MetricsDataSwitcher>
  267. </MetricsCardinalityProvider>
  268. </TabPanels.Item>
  269. </TabPanels>
  270. </Layout.Main>
  271. </Layout.Body>
  272. </Tabs>
  273. </PageAlertProvider>
  274. </Layout.Page>
  275. );
  276. }
  277. const SearchContainerWithFilter = styled('div')`
  278. display: grid;
  279. grid-template-rows: auto auto;
  280. gap: ${space(2)};
  281. margin-bottom: ${space(2)};
  282. @media (min-width: ${p => p.theme.breakpoints.small}) {
  283. grid-template-rows: auto;
  284. grid-template-columns: auto 1fr;
  285. }
  286. `;
  287. const SearchContainerWithFilterAndMetrics = styled('div')`
  288. display: grid;
  289. grid-template-rows: auto auto auto;
  290. gap: ${space(2)};
  291. margin-bottom: ${space(2)};
  292. @media (min-width: ${p => p.theme.breakpoints.small}) {
  293. grid-template-rows: auto;
  294. grid-template-columns: auto 1fr auto;
  295. }
  296. `;