index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import type {FC} from 'react';
  2. import {Fragment, useEffect, useMemo, 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 Link from 'sentry/components/links/link';
  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 {PageFilters} from 'sentry/types/core';
  22. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  23. import type {Organization} from 'sentry/types/organization';
  24. import type {Project} from 'sentry/types/project';
  25. import {trackAnalytics} from 'sentry/utils/analytics';
  26. import {browserHistory} from 'sentry/utils/browserHistory';
  27. import type EventView from 'sentry/utils/discover/eventView';
  28. import {GenericQueryBatcher} from 'sentry/utils/performance/contexts/genericQueryBatcher';
  29. import {MetricsCardinalityProvider} from 'sentry/utils/performance/contexts/metricsCardinality';
  30. import type {MEPState} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  31. import {
  32. MEPConsumer,
  33. MEPSettingProvider,
  34. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  35. import {PageAlert, usePageAlert} from 'sentry/utils/performance/contexts/pageAlert';
  36. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  37. import {useTeams} from 'sentry/utils/useTeams';
  38. import {AI_SIDEBAR_LABEL} from 'sentry/views/insights/pages/ai/settings';
  39. import {BACKEND_SIDEBAR_LABEL} from 'sentry/views/insights/pages/backend/settings';
  40. import {FRONTEND_SIDEBAR_LABEL} from 'sentry/views/insights/pages/frontend/settings';
  41. import {MOBILE_SIDEBAR_LABEL} from 'sentry/views/insights/pages/mobile/settings';
  42. import Onboarding from '../onboarding';
  43. import {MetricsEventsDropdown} from '../transactionSummary/transactionOverview/metricEvents/metricsEventsDropdown';
  44. import {getPerformanceBaseUrl, getTransactionSearchQuery} from '../utils';
  45. import {AllTransactionsView} from './views/allTransactionsView';
  46. import {BackendView} from './views/backendView';
  47. import {FrontendOtherView} from './views/frontendOtherView';
  48. import {FrontendPageloadView} from './views/frontendPageloadView';
  49. import {MobileView} from './views/mobileView';
  50. import {MetricsDataSwitcher} from './metricsDataSwitcher';
  51. import {MetricsDataSwitcherAlert} from './metricsDataSwitcherAlert';
  52. import {
  53. getDefaultDisplayForPlatform,
  54. getLandingDisplayFromParam,
  55. handleLandingDisplayChange,
  56. LANDING_DISPLAYS,
  57. LandingDisplayField,
  58. } from './utils';
  59. type Props = {
  60. eventView: EventView;
  61. handleSearch: (searchQuery: string, currentMEPState?: MEPState) => void;
  62. handleTrendsClick: () => void;
  63. location: Location;
  64. onboardingProject: Project | undefined;
  65. organization: Organization;
  66. projects: Project[];
  67. router: InjectedRouter;
  68. selection: PageFilters;
  69. setError: (msg: string | undefined) => void;
  70. withStaticFilters: boolean;
  71. };
  72. const fieldToViewMap: Record<LandingDisplayField, FC<Props>> = {
  73. [LandingDisplayField.ALL]: AllTransactionsView,
  74. [LandingDisplayField.BACKEND]: BackendView,
  75. [LandingDisplayField.FRONTEND_OTHER]: FrontendOtherView,
  76. [LandingDisplayField.FRONTEND_PAGELOAD]: FrontendPageloadView,
  77. [LandingDisplayField.MOBILE]: MobileView,
  78. };
  79. export function PerformanceLanding(props: Props) {
  80. const {
  81. organization,
  82. location,
  83. eventView,
  84. projects,
  85. handleSearch,
  86. handleTrendsClick,
  87. onboardingProject,
  88. } = props;
  89. const {setPageInfo, pageAlert} = usePageAlert();
  90. const {teams, initiallyLoaded} = useTeams({provideUserTeams: true});
  91. const {slug} = organization;
  92. const performanceMovingAlert = useMemo(() => {
  93. if (!slug) {
  94. return undefined;
  95. }
  96. return (
  97. <Fragment>
  98. {t(
  99. `To make it easier to see what's relevant for you, Sentry's Performance landing page is now being split into separate `
  100. )}
  101. <Link to={`${getPerformanceBaseUrl(slug, 'frontend')}/`}>
  102. {FRONTEND_SIDEBAR_LABEL}
  103. </Link>
  104. {`, `}
  105. <Link to={`${getPerformanceBaseUrl(slug, 'backend')}/`}>
  106. {BACKEND_SIDEBAR_LABEL}
  107. </Link>
  108. {`, `}
  109. <Link to={`${getPerformanceBaseUrl(slug, 'mobile')}/`}>
  110. {MOBILE_SIDEBAR_LABEL}
  111. </Link>
  112. {t(', and ')}
  113. <Link to={`${getPerformanceBaseUrl(slug, 'ai')}/`}>{AI_SIDEBAR_LABEL}</Link>
  114. {t(' performance pages. They can all be found in the Insights tab.')}
  115. </Fragment>
  116. );
  117. }, [slug]);
  118. const hasMounted = useRef(false);
  119. const paramLandingDisplay = getLandingDisplayFromParam(location);
  120. const defaultLandingDisplayForProjects = getDefaultDisplayForPlatform(
  121. projects,
  122. eventView
  123. );
  124. const landingDisplay = paramLandingDisplay ?? defaultLandingDisplayForProjects;
  125. const showOnboarding = onboardingProject !== undefined;
  126. useEffect(() => {
  127. if (performanceMovingAlert && pageAlert?.message !== performanceMovingAlert) {
  128. setPageInfo(performanceMovingAlert);
  129. }
  130. }, [pageAlert?.message, performanceMovingAlert, setPageInfo]);
  131. useEffect(() => {
  132. if (hasMounted.current) {
  133. browserHistory.replace({
  134. pathname: location.pathname,
  135. query: {
  136. ...location.query,
  137. landingDisplay: undefined,
  138. },
  139. });
  140. }
  141. // eslint-disable-next-line react-hooks/exhaustive-deps
  142. }, [eventView.project.join('.')]);
  143. useEffect(() => {
  144. hasMounted.current = true;
  145. }, []);
  146. useEffect(() => {
  147. if (showOnboarding) {
  148. trackAnalytics('performance_views.overview.has_data', {
  149. table_data_state: 'onboarding',
  150. tab: paramLandingDisplay?.field,
  151. organization,
  152. });
  153. }
  154. }, [showOnboarding, paramLandingDisplay, organization]);
  155. const getFreeTextFromQuery = (query: string) => {
  156. const conditions = new MutableSearch(query);
  157. const transactionValues = conditions.getFilterValues('transaction');
  158. if (transactionValues.length) {
  159. return transactionValues[0];
  160. }
  161. if (conditions.freeText.length > 0) {
  162. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  163. // so no need to wrap it here
  164. return conditions.freeText.join(' ');
  165. }
  166. return '';
  167. };
  168. const derivedQuery = getTransactionSearchQuery(location, eventView.query);
  169. const ViewComponent = fieldToViewMap[landingDisplay.field];
  170. let pageFilters: React.ReactNode = (
  171. <PageFilterBar condensed>
  172. <ProjectPageFilter />
  173. <EnvironmentPageFilter />
  174. <DatePageFilter />
  175. </PageFilterBar>
  176. );
  177. if (showOnboarding) {
  178. pageFilters = <SearchContainerWithFilter>{pageFilters}</SearchContainerWithFilter>;
  179. }
  180. const SearchFilterContainer = organization.features.includes('performance-use-metrics')
  181. ? SearchContainerWithFilterAndMetrics
  182. : SearchContainerWithFilter;
  183. return (
  184. <Layout.Page data-test-id="performance-landing-v3">
  185. <Tabs
  186. value={landingDisplay.field}
  187. onChange={field =>
  188. handleLandingDisplayChange(field, location, projects, organization, eventView)
  189. }
  190. >
  191. <Layout.Header>
  192. <Layout.HeaderContent>
  193. <Layout.Title>
  194. {t('Performance')}
  195. <PageHeadingQuestionTooltip
  196. docsUrl="https://docs.sentry.io/product/performance/"
  197. title={t(
  198. '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.'
  199. )}
  200. />
  201. </Layout.Title>
  202. </Layout.HeaderContent>
  203. <Layout.HeaderActions>
  204. {!showOnboarding && (
  205. <ButtonBar gap={1}>
  206. <Button
  207. size="sm"
  208. priority="primary"
  209. data-test-id="landing-header-trends"
  210. onClick={() => handleTrendsClick()}
  211. >
  212. {t('View Trends')}
  213. </Button>
  214. <FeedbackWidgetButton />
  215. </ButtonBar>
  216. )}
  217. </Layout.HeaderActions>
  218. <TabList hideBorder>
  219. {LANDING_DISPLAYS.map(({label, field}) => (
  220. <TabList.Item key={field}>{label}</TabList.Item>
  221. ))}
  222. </TabList>
  223. </Layout.Header>
  224. <Layout.Body data-test-id="performance-landing-body">
  225. <Layout.Main fullWidth>
  226. <TabPanels>
  227. <TabPanels.Item key={landingDisplay.field}>
  228. <MetricsCardinalityProvider
  229. sendOutcomeAnalytics
  230. organization={organization}
  231. location={location}
  232. >
  233. <MetricsDataSwitcher
  234. organization={organization}
  235. eventView={eventView}
  236. location={location}
  237. >
  238. {metricsDataSide => {
  239. return (
  240. <MEPSettingProvider
  241. location={location}
  242. forceTransactions={metricsDataSide.forceTransactionsOnly}
  243. >
  244. <MetricsDataSwitcherAlert
  245. organization={organization}
  246. eventView={eventView}
  247. projects={projects}
  248. location={location}
  249. router={props.router}
  250. {...metricsDataSide}
  251. />
  252. <PageAlert />
  253. {showOnboarding ? (
  254. <Fragment>
  255. {pageFilters}
  256. <Onboarding
  257. organization={organization}
  258. project={onboardingProject}
  259. />
  260. </Fragment>
  261. ) : (
  262. <Fragment>
  263. <SearchFilterContainer>
  264. {pageFilters}
  265. <MEPConsumer>
  266. {({metricSettingState}) => (
  267. // TODO replace `handleSearch prop` with transaction name search once
  268. // transaction name search becomes the default search bar
  269. <TransactionNameSearchBar
  270. organization={organization}
  271. eventView={eventView}
  272. onSearch={(query: string) => {
  273. handleSearch(
  274. query,
  275. metricSettingState ?? undefined
  276. );
  277. }}
  278. query={getFreeTextFromQuery(derivedQuery)}
  279. />
  280. )}
  281. </MEPConsumer>
  282. <MetricsEventsDropdown />
  283. </SearchFilterContainer>
  284. {initiallyLoaded ? (
  285. <TeamKeyTransactionManager.Provider
  286. organization={organization}
  287. teams={teams}
  288. selectedTeams={['myteams']}
  289. selectedProjects={eventView.project.map(String)}
  290. >
  291. <GenericQueryBatcher>
  292. <ViewComponent {...props} />
  293. </GenericQueryBatcher>
  294. </TeamKeyTransactionManager.Provider>
  295. ) : (
  296. <LoadingIndicator />
  297. )}
  298. </Fragment>
  299. )}
  300. </MEPSettingProvider>
  301. );
  302. }}
  303. </MetricsDataSwitcher>
  304. </MetricsCardinalityProvider>
  305. </TabPanels.Item>
  306. </TabPanels>
  307. </Layout.Main>
  308. </Layout.Body>
  309. </Tabs>
  310. </Layout.Page>
  311. );
  312. }
  313. const SearchContainerWithFilter = styled('div')`
  314. display: grid;
  315. grid-template-rows: auto auto;
  316. gap: ${space(2)};
  317. margin-bottom: ${space(2)};
  318. @media (min-width: ${p => p.theme.breakpoints.small}) {
  319. grid-template-rows: auto;
  320. grid-template-columns: auto 1fr;
  321. }
  322. `;
  323. const SearchContainerWithFilterAndMetrics = styled('div')`
  324. display: grid;
  325. grid-template-rows: auto auto auto;
  326. gap: ${space(2)};
  327. margin-bottom: ${space(2)};
  328. @media (min-width: ${p => p.theme.breakpoints.small}) {
  329. grid-template-rows: auto;
  330. grid-template-columns: auto 1fr auto;
  331. }
  332. `;