index.tsx 12 KB

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