index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 hasDomainViews = organization.features.includes('insights-domain-view');
  93. const performanceMovingAlert = useMemo(() => {
  94. if (!slug) {
  95. return undefined;
  96. }
  97. return (
  98. <Fragment>
  99. {t(
  100. `To make it easier to see what's relevant for you, Sentry's Performance landing page is now being split into separate `
  101. )}
  102. <Link to={getPerformanceBaseUrl(slug, 'frontend')}>{FRONTEND_SIDEBAR_LABEL}</Link>
  103. {`, `}
  104. <Link to={getPerformanceBaseUrl(slug, 'backend')}>{BACKEND_SIDEBAR_LABEL}</Link>
  105. {`, `}
  106. <Link to={getPerformanceBaseUrl(slug, 'mobile')}>{MOBILE_SIDEBAR_LABEL}</Link>
  107. {t(', and ')}
  108. <Link to={getPerformanceBaseUrl(slug, 'ai')}>{AI_SIDEBAR_LABEL}</Link>
  109. {t(' performance pages. They can all be found in the Insights tab.')}
  110. </Fragment>
  111. );
  112. }, [slug]);
  113. const hasMounted = useRef(false);
  114. const paramLandingDisplay = getLandingDisplayFromParam(location);
  115. const defaultLandingDisplayForProjects = getDefaultDisplayForPlatform(
  116. projects,
  117. eventView
  118. );
  119. const landingDisplay = paramLandingDisplay ?? defaultLandingDisplayForProjects;
  120. const showOnboarding = onboardingProject !== undefined;
  121. if (
  122. hasDomainViews &&
  123. performanceMovingAlert &&
  124. pageAlert?.message !== performanceMovingAlert
  125. ) {
  126. setPageInfo(performanceMovingAlert);
  127. }
  128. useEffect(() => {
  129. if (hasMounted.current) {
  130. browserHistory.replace({
  131. pathname: location.pathname,
  132. query: {
  133. ...location.query,
  134. landingDisplay: undefined,
  135. },
  136. });
  137. }
  138. // eslint-disable-next-line react-hooks/exhaustive-deps
  139. }, [eventView.project.join('.')]);
  140. useEffect(() => {
  141. hasMounted.current = true;
  142. }, []);
  143. useEffect(() => {
  144. if (showOnboarding) {
  145. trackAnalytics('performance_views.overview.has_data', {
  146. table_data_state: 'onboarding',
  147. tab: paramLandingDisplay?.field,
  148. organization,
  149. });
  150. }
  151. }, [showOnboarding, paramLandingDisplay, organization]);
  152. const getFreeTextFromQuery = (query: string) => {
  153. const conditions = new MutableSearch(query);
  154. const transactionValues = conditions.getFilterValues('transaction');
  155. if (transactionValues.length) {
  156. return transactionValues[0];
  157. }
  158. if (conditions.freeText.length > 0) {
  159. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  160. // so no need to wrap it here
  161. return conditions.freeText.join(' ');
  162. }
  163. return '';
  164. };
  165. const derivedQuery = getTransactionSearchQuery(location, eventView.query);
  166. const ViewComponent = fieldToViewMap[landingDisplay.field];
  167. let pageFilters: React.ReactNode = (
  168. <PageFilterBar condensed>
  169. <ProjectPageFilter />
  170. <EnvironmentPageFilter />
  171. <DatePageFilter />
  172. </PageFilterBar>
  173. );
  174. if (showOnboarding) {
  175. pageFilters = <SearchContainerWithFilter>{pageFilters}</SearchContainerWithFilter>;
  176. }
  177. const SearchFilterContainer = organization.features.includes('performance-use-metrics')
  178. ? SearchContainerWithFilterAndMetrics
  179. : SearchContainerWithFilter;
  180. return (
  181. <Layout.Page data-test-id="performance-landing-v3">
  182. <Tabs
  183. value={landingDisplay.field}
  184. onChange={field =>
  185. handleLandingDisplayChange(field, location, projects, organization, eventView)
  186. }
  187. >
  188. <Layout.Header>
  189. <Layout.HeaderContent>
  190. <Layout.Title>
  191. {t('Performance')}
  192. <PageHeadingQuestionTooltip
  193. docsUrl="https://docs.sentry.io/product/performance/"
  194. title={t(
  195. '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.'
  196. )}
  197. />
  198. </Layout.Title>
  199. </Layout.HeaderContent>
  200. <Layout.HeaderActions>
  201. {!showOnboarding && (
  202. <ButtonBar gap={1}>
  203. <Button
  204. size="sm"
  205. priority="primary"
  206. data-test-id="landing-header-trends"
  207. onClick={() => handleTrendsClick()}
  208. >
  209. {t('View Trends')}
  210. </Button>
  211. <FeedbackWidgetButton />
  212. </ButtonBar>
  213. )}
  214. </Layout.HeaderActions>
  215. <TabList hideBorder>
  216. {LANDING_DISPLAYS.map(({label, field}) => (
  217. <TabList.Item key={field}>{label}</TabList.Item>
  218. ))}
  219. </TabList>
  220. </Layout.Header>
  221. <Layout.Body data-test-id="performance-landing-body">
  222. <Layout.Main fullWidth>
  223. <TabPanels>
  224. <TabPanels.Item key={landingDisplay.field}>
  225. <MetricsCardinalityProvider
  226. sendOutcomeAnalytics
  227. organization={organization}
  228. location={location}
  229. >
  230. <MetricsDataSwitcher
  231. organization={organization}
  232. eventView={eventView}
  233. location={location}
  234. >
  235. {metricsDataSide => {
  236. return (
  237. <MEPSettingProvider
  238. location={location}
  239. forceTransactions={metricsDataSide.forceTransactionsOnly}
  240. >
  241. <MetricsDataSwitcherAlert
  242. organization={organization}
  243. eventView={eventView}
  244. projects={projects}
  245. location={location}
  246. router={props.router}
  247. {...metricsDataSide}
  248. />
  249. <PageAlert />
  250. {showOnboarding ? (
  251. <Fragment>
  252. {pageFilters}
  253. <Onboarding
  254. organization={organization}
  255. project={onboardingProject}
  256. />
  257. </Fragment>
  258. ) : (
  259. <Fragment>
  260. <SearchFilterContainer>
  261. {pageFilters}
  262. <MEPConsumer>
  263. {({metricSettingState}) => (
  264. // TODO replace `handleSearch prop` with transaction name search once
  265. // transaction name search becomes the default search bar
  266. <TransactionNameSearchBar
  267. organization={organization}
  268. eventView={eventView}
  269. onSearch={(query: string) => {
  270. handleSearch(
  271. query,
  272. metricSettingState ?? undefined
  273. );
  274. }}
  275. query={getFreeTextFromQuery(derivedQuery)}
  276. />
  277. )}
  278. </MEPConsumer>
  279. <MetricsEventsDropdown />
  280. </SearchFilterContainer>
  281. {initiallyLoaded ? (
  282. <TeamKeyTransactionManager.Provider
  283. organization={organization}
  284. teams={teams}
  285. selectedTeams={['myteams']}
  286. selectedProjects={eventView.project.map(String)}
  287. >
  288. <GenericQueryBatcher>
  289. <ViewComponent {...props} />
  290. </GenericQueryBatcher>
  291. </TeamKeyTransactionManager.Provider>
  292. ) : (
  293. <LoadingIndicator />
  294. )}
  295. </Fragment>
  296. )}
  297. </MEPSettingProvider>
  298. );
  299. }}
  300. </MetricsDataSwitcher>
  301. </MetricsCardinalityProvider>
  302. </TabPanels.Item>
  303. </TabPanels>
  304. </Layout.Main>
  305. </Layout.Body>
  306. </Tabs>
  307. </Layout.Page>
  308. );
  309. }
  310. const SearchContainerWithFilter = styled('div')`
  311. display: grid;
  312. grid-template-rows: auto auto;
  313. gap: ${space(2)};
  314. margin-bottom: ${space(2)};
  315. @media (min-width: ${p => p.theme.breakpoints.small}) {
  316. grid-template-rows: auto;
  317. grid-template-columns: auto 1fr;
  318. }
  319. `;
  320. const SearchContainerWithFilterAndMetrics = styled('div')`
  321. display: grid;
  322. grid-template-rows: auto auto auto;
  323. gap: ${space(2)};
  324. margin-bottom: ${space(2)};
  325. @media (min-width: ${p => p.theme.breakpoints.small}) {
  326. grid-template-rows: auto;
  327. grid-template-columns: auto 1fr auto;
  328. }
  329. `;