index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 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 SamplingModal, {modalCss} from './samplingModal';
  43. import {
  44. getDefaultDisplayForPlatform,
  45. getLandingDisplayFromParam,
  46. handleLandingDisplayChange,
  47. LANDING_DISPLAYS,
  48. LandingDisplayField,
  49. } from './utils';
  50. type Props = {
  51. eventView: EventView;
  52. handleSearch: (searchQuery: string) => void;
  53. handleTrendsClick: () => void;
  54. location: Location;
  55. onboardingProject: Project | undefined;
  56. organization: Organization;
  57. projects: Project[];
  58. selection: PageFilters;
  59. setError: (msg: string | undefined) => void;
  60. withStaticFilters: boolean;
  61. };
  62. const fieldToViewMap: Record<LandingDisplayField, FC<Props>> = {
  63. [LandingDisplayField.ALL]: AllTransactionsView,
  64. [LandingDisplayField.BACKEND]: BackendView,
  65. [LandingDisplayField.FRONTEND_OTHER]: FrontendOtherView,
  66. [LandingDisplayField.FRONTEND_PAGELOAD]: FrontendPageloadView,
  67. [LandingDisplayField.MOBILE]: MobileView,
  68. };
  69. export function PerformanceLanding(props: Props) {
  70. const {
  71. organization,
  72. location,
  73. eventView,
  74. projects,
  75. handleSearch,
  76. handleTrendsClick,
  77. onboardingProject,
  78. withStaticFilters,
  79. } = props;
  80. const {teams, initiallyLoaded} = useTeams({provideUserTeams: true});
  81. const hasMounted = useRef(false);
  82. const paramLandingDisplay = getLandingDisplayFromParam(location);
  83. const defaultLandingDisplayForProjects = getDefaultDisplayForPlatform(
  84. projects,
  85. eventView
  86. );
  87. const landingDisplay = paramLandingDisplay ?? defaultLandingDisplayForProjects;
  88. const showOnboarding = onboardingProject !== undefined;
  89. useEffect(() => {
  90. if (hasMounted.current) {
  91. browserHistory.replace({
  92. pathname: location.pathname,
  93. query: {
  94. ...location.query,
  95. landingDisplay: undefined,
  96. },
  97. });
  98. }
  99. }, [eventView.project.join('.')]);
  100. useEffect(() => {
  101. hasMounted.current = true;
  102. }, []);
  103. const filterString = withStaticFilters
  104. ? 'transaction.duration:<15m'
  105. : getTransactionSearchQuery(location, eventView.query);
  106. const ViewComponent = fieldToViewMap[landingDisplay.field];
  107. const fnOpenModal = () => {
  108. openModal(
  109. modalProps => (
  110. <SamplingModal
  111. {...modalProps}
  112. organization={organization}
  113. eventView={eventView}
  114. projects={projects}
  115. onApply={() => {}}
  116. isMEPEnabled
  117. />
  118. ),
  119. {modalCss, backdrop: 'static'}
  120. );
  121. };
  122. let pageFilters: React.ReactNode = (
  123. <PageFilterBar condensed>
  124. <ProjectPageFilter />
  125. <EnvironmentPageFilter />
  126. <DatePageFilter alignDropdown="left" />
  127. </PageFilterBar>
  128. );
  129. if (showOnboarding) {
  130. pageFilters = <SearchContainerWithFilter>{pageFilters}</SearchContainerWithFilter>;
  131. }
  132. const SearchFilterContainer =
  133. organization.features.includes('performance-use-metrics') &&
  134. !organization.features.includes('performance-transaction-name-only-search')
  135. ? SearchContainerWithFilterAndMetrics
  136. : SearchContainerWithFilter;
  137. return (
  138. <StyledPageContent data-test-id="performance-landing-v3">
  139. <PageErrorProvider>
  140. <Layout.Header>
  141. <Layout.HeaderContent>
  142. <StyledHeading>{t('Performance')}</StyledHeading>
  143. </Layout.HeaderContent>
  144. <Layout.HeaderActions>
  145. {!showOnboarding && (
  146. <ButtonBar gap={3}>
  147. <Button
  148. priority="primary"
  149. data-test-id="landing-header-trends"
  150. onClick={() => handleTrendsClick()}
  151. >
  152. {t('View Trends')}
  153. </Button>
  154. <Feature features={['organizations:performance-use-metrics']}>
  155. <Button
  156. onClick={() => fnOpenModal()}
  157. icon={<IconSettings />}
  158. aria-label={t('Settings')}
  159. data-test-id="open-meps-settings"
  160. />
  161. </Feature>
  162. </ButtonBar>
  163. )}
  164. </Layout.HeaderActions>
  165. <Layout.HeaderNavTabs>
  166. {LANDING_DISPLAYS.map(({label, field}) => (
  167. <li key={label} className={landingDisplay.field === field ? 'active' : ''}>
  168. <a
  169. href="#"
  170. data-test-id={`landing-tab-${field}`}
  171. onClick={() =>
  172. handleLandingDisplayChange(
  173. field,
  174. location,
  175. projects,
  176. organization,
  177. eventView
  178. )
  179. }
  180. >
  181. {t(label)}
  182. </a>
  183. </li>
  184. ))}
  185. </Layout.HeaderNavTabs>
  186. </Layout.Header>
  187. <Layout.Body>
  188. <Layout.Main fullWidth>
  189. <GlobalSdkUpdateAlert />
  190. <PageErrorAlert />
  191. {showOnboarding ? (
  192. <Fragment>
  193. {pageFilters}
  194. <Onboarding organization={organization} project={onboardingProject} />
  195. </Fragment>
  196. ) : (
  197. <Fragment>
  198. <SearchFilterContainer>
  199. {pageFilters}
  200. <Feature
  201. features={['organizations:performance-transaction-name-only-search']}
  202. >
  203. {({hasFeature}) =>
  204. hasFeature ? (
  205. // TODO replace `handleSearch prop` with transaction name search once
  206. // transaction name search becomes the default search bar
  207. <TransactionNameSearchBar
  208. organization={organization}
  209. location={location}
  210. eventView={eventView}
  211. />
  212. ) : (
  213. <SearchBar
  214. searchSource="performance_landing"
  215. organization={organization}
  216. projectIds={eventView.project}
  217. query={filterString}
  218. fields={generateAggregateFields(
  219. organization,
  220. [...eventView.fields, {field: 'tps()'}],
  221. ['epm()', 'eps()']
  222. )}
  223. onSearch={handleSearch}
  224. maxQueryLength={MAX_QUERY_LENGTH}
  225. />
  226. )
  227. }
  228. </Feature>
  229. <Feature
  230. features={['organizations:performance-transaction-name-only-search']}
  231. >
  232. {({hasFeature}) => !hasFeature && <MetricsEventsDropdown />}
  233. </Feature>
  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. </Layout.Main>
  252. </Layout.Body>
  253. </PageErrorProvider>
  254. </StyledPageContent>
  255. );
  256. }
  257. const StyledPageContent = styled(PageContent)`
  258. padding: 0;
  259. `;
  260. const StyledHeading = styled(PageHeading)`
  261. line-height: 40px;
  262. `;
  263. const SearchContainerWithFilter = styled('div')`
  264. display: grid;
  265. grid-template-rows: auto auto;
  266. gap: ${space(2)};
  267. margin-bottom: ${space(2)};
  268. @media (min-width: ${p => p.theme.breakpoints.small}) {
  269. grid-template-rows: auto;
  270. grid-template-columns: auto 1fr;
  271. }
  272. `;
  273. const SearchContainerWithFilterAndMetrics = styled('div')`
  274. display: grid;
  275. grid-template-rows: auto auto auto;
  276. gap: ${space(2)};
  277. margin-bottom: ${space(2)};
  278. @media (min-width: ${p => p.theme.breakpoints.small}) {
  279. grid-template-rows: auto;
  280. grid-template-columns: auto 1fr auto;
  281. }
  282. `;