index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import {FC, Fragment, useEffect, useRef} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import Feature from 'sentry/components/acl/feature';
  7. import Button from 'sentry/components/button';
  8. import ButtonBar from 'sentry/components/buttonBar';
  9. import DatePageFilter from 'sentry/components/datePageFilter';
  10. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  11. import SearchBar from 'sentry/components/events/searchBar';
  12. import {GlobalSdkUpdateAlert} from 'sentry/components/globalSdkUpdateAlert';
  13. import * as Layout from 'sentry/components/layouts/thirds';
  14. import LoadingIndicator from 'sentry/components/loadingIndicator';
  15. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  16. import PageHeading from 'sentry/components/pageHeading';
  17. import TransactionNameSearchBar from 'sentry/components/performance/searchBar';
  18. import * as TeamKeyTransactionManager from 'sentry/components/performance/teamKeyTransactionsManager';
  19. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  20. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  21. import {IconSettings} from 'sentry/icons';
  22. import {t} from 'sentry/locale';
  23. import {PageContent} from 'sentry/styles/organization';
  24. import space from 'sentry/styles/space';
  25. import {Organization, PageFilters, Project} from 'sentry/types';
  26. import EventView from 'sentry/utils/discover/eventView';
  27. import {generateAggregateFields} from 'sentry/utils/discover/fields';
  28. import {GenericQueryBatcher} from 'sentry/utils/performance/contexts/genericQueryBatcher';
  29. import {
  30. PageErrorAlert,
  31. PageErrorProvider,
  32. } from 'sentry/utils/performance/contexts/pageError';
  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 SamplingModal, {modalCss} from './samplingModal';
  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) => void;
  54. handleTrendsClick: () => void;
  55. location: Location;
  56. onboardingProject: Project | undefined;
  57. organization: Organization;
  58. projects: Project[];
  59. selection: PageFilters;
  60. setError: (msg: string | undefined) => void;
  61. withStaticFilters: boolean;
  62. };
  63. const fieldToViewMap: Record<LandingDisplayField, FC<Props>> = {
  64. [LandingDisplayField.ALL]: AllTransactionsView,
  65. [LandingDisplayField.BACKEND]: BackendView,
  66. [LandingDisplayField.FRONTEND_OTHER]: FrontendOtherView,
  67. [LandingDisplayField.FRONTEND_PAGELOAD]: FrontendPageloadView,
  68. [LandingDisplayField.MOBILE]: MobileView,
  69. };
  70. export function PerformanceLanding(props: Props) {
  71. const {
  72. organization,
  73. location,
  74. eventView,
  75. projects,
  76. handleSearch,
  77. handleTrendsClick,
  78. onboardingProject,
  79. withStaticFilters,
  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. }, [eventView.project.join('.')]);
  101. useEffect(() => {
  102. hasMounted.current = true;
  103. }, []);
  104. const getFreeTextFromQuery = (query: string) => {
  105. const conditions = new MutableSearch(query);
  106. const transactionValues = conditions.getFilterValues('transaction');
  107. if (transactionValues.length) {
  108. return transactionValues[0];
  109. }
  110. return '';
  111. };
  112. const derivedQuery = getTransactionSearchQuery(location, eventView.query);
  113. const searchQuery = withStaticFilters
  114. ? getFreeTextFromQuery(derivedQuery)
  115. : derivedQuery;
  116. const ViewComponent = fieldToViewMap[landingDisplay.field];
  117. const fnOpenModal = () => {
  118. openModal(
  119. modalProps => (
  120. <SamplingModal
  121. {...modalProps}
  122. organization={organization}
  123. eventView={eventView}
  124. projects={projects}
  125. onApply={() => {}}
  126. isMEPEnabled
  127. />
  128. ),
  129. {modalCss, backdrop: 'static'}
  130. );
  131. };
  132. let pageFilters: React.ReactNode = (
  133. <PageFilterBar condensed>
  134. <ProjectPageFilter />
  135. <EnvironmentPageFilter />
  136. <DatePageFilter alignDropdown="left" />
  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. <StyledPageContent data-test-id="performance-landing-v3">
  147. <PageErrorProvider>
  148. <Layout.Header>
  149. <Layout.HeaderContent>
  150. <StyledHeading>{t('Performance')}</StyledHeading>
  151. </Layout.HeaderContent>
  152. <Layout.HeaderActions>
  153. {!showOnboarding && (
  154. <ButtonBar gap={3}>
  155. <Button
  156. priority="primary"
  157. data-test-id="landing-header-trends"
  158. onClick={() => handleTrendsClick()}
  159. >
  160. {t('View Trends')}
  161. </Button>
  162. <Feature features={['organizations:performance-use-metrics']}>
  163. <Button
  164. onClick={() => fnOpenModal()}
  165. icon={<IconSettings />}
  166. aria-label={t('Settings')}
  167. data-test-id="open-meps-settings"
  168. />
  169. </Feature>
  170. </ButtonBar>
  171. )}
  172. </Layout.HeaderActions>
  173. <Layout.HeaderNavTabs>
  174. {LANDING_DISPLAYS.map(({label, field}) => (
  175. <li key={label} className={landingDisplay.field === field ? 'active' : ''}>
  176. <a
  177. href="#"
  178. data-test-id={`landing-tab-${field}`}
  179. onClick={() =>
  180. handleLandingDisplayChange(
  181. field,
  182. location,
  183. projects,
  184. organization,
  185. eventView
  186. )
  187. }
  188. >
  189. {t(label)}
  190. </a>
  191. </li>
  192. ))}
  193. </Layout.HeaderNavTabs>
  194. </Layout.Header>
  195. <Layout.Body>
  196. <Layout.Main fullWidth>
  197. <GlobalSdkUpdateAlert />
  198. <PageErrorAlert />
  199. {showOnboarding ? (
  200. <Fragment>
  201. {pageFilters}
  202. <Onboarding organization={organization} project={onboardingProject} />
  203. </Fragment>
  204. ) : (
  205. <Fragment>
  206. <SearchFilterContainer>
  207. {pageFilters}
  208. <Feature
  209. features={['organizations:performance-transaction-name-only-search']}
  210. >
  211. {({hasFeature}) =>
  212. hasFeature ? (
  213. // TODO replace `handleSearch prop` with transaction name search once
  214. // transaction name search becomes the default search bar
  215. <TransactionNameSearchBar
  216. organization={organization}
  217. location={location}
  218. eventView={eventView}
  219. onSearch={handleSearch}
  220. query={searchQuery}
  221. />
  222. ) : (
  223. <SearchBar
  224. searchSource="performance_landing"
  225. organization={organization}
  226. projectIds={eventView.project}
  227. query={searchQuery}
  228. fields={generateAggregateFields(
  229. organization,
  230. [...eventView.fields, {field: 'tps()'}],
  231. ['epm()', 'eps()']
  232. )}
  233. onSearch={handleSearch}
  234. maxQueryLength={MAX_QUERY_LENGTH}
  235. />
  236. )
  237. }
  238. </Feature>
  239. <MetricsEventsDropdown />
  240. </SearchFilterContainer>
  241. {initiallyLoaded ? (
  242. <TeamKeyTransactionManager.Provider
  243. organization={organization}
  244. teams={teams}
  245. selectedTeams={['myteams']}
  246. selectedProjects={eventView.project.map(String)}
  247. >
  248. <GenericQueryBatcher>
  249. <ViewComponent {...props} />
  250. </GenericQueryBatcher>
  251. </TeamKeyTransactionManager.Provider>
  252. ) : (
  253. <LoadingIndicator />
  254. )}
  255. </Fragment>
  256. )}
  257. </Layout.Main>
  258. </Layout.Body>
  259. </PageErrorProvider>
  260. </StyledPageContent>
  261. );
  262. }
  263. const StyledPageContent = styled(PageContent)`
  264. padding: 0;
  265. `;
  266. const StyledHeading = styled(PageHeading)`
  267. line-height: 40px;
  268. `;
  269. const SearchContainerWithFilter = styled('div')`
  270. display: grid;
  271. grid-template-rows: auto auto;
  272. gap: ${space(2)};
  273. margin-bottom: ${space(2)};
  274. @media (min-width: ${p => p.theme.breakpoints.small}) {
  275. grid-template-rows: auto;
  276. grid-template-columns: auto 1fr;
  277. }
  278. `;
  279. const SearchContainerWithFilterAndMetrics = styled('div')`
  280. display: grid;
  281. grid-template-rows: auto auto auto;
  282. gap: ${space(2)};
  283. margin-bottom: ${space(2)};
  284. @media (min-width: ${p => p.theme.breakpoints.small}) {
  285. grid-template-rows: auto;
  286. grid-template-columns: auto 1fr auto;
  287. }
  288. `;