index.tsx 12 KB

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