content.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {useEffect, useRef, useState} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import * as Sentry from '@sentry/react';
  4. import {Location} from 'history';
  5. import isEqual from 'lodash/isEqual';
  6. import {loadOrganizationTags} from 'sentry/actionCreators/tags';
  7. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  8. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  9. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  10. import {t} from 'sentry/locale';
  11. import {PageFilters, Project} from 'sentry/types';
  12. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  13. import {
  14. canUseMetricsData,
  15. MEPState,
  16. METRIC_SEARCH_SETTING_PARAM,
  17. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  18. import {PerformanceEventViewProvider} from 'sentry/utils/performance/contexts/performanceEventViewContext';
  19. import useRouteAnalyticsEventNames from 'sentry/utils/routeAnalytics/useRouteAnalyticsEventNames';
  20. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  21. import useApi from 'sentry/utils/useApi';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import usePrevious from 'sentry/utils/usePrevious';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import withPageFilters from 'sentry/utils/withPageFilters';
  26. import {getLandingDisplayFromParam} from './landing/utils';
  27. import {DEFAULT_STATS_PERIOD, generatePerformanceEventView} from './data';
  28. import {PerformanceLanding} from './landing';
  29. import {
  30. addRoutePerformanceContext,
  31. getSelectedProjectPlatforms,
  32. handleTrendsClick,
  33. } from './utils';
  34. type Props = {
  35. location: Location;
  36. router: InjectedRouter;
  37. selection: PageFilters;
  38. demoMode?: boolean;
  39. };
  40. type State = {
  41. error?: string;
  42. };
  43. function PerformanceContent({selection, location, demoMode, router}: Props) {
  44. const api = useApi();
  45. const organization = useOrganization();
  46. const {projects} = useProjects();
  47. const mounted = useRef(false);
  48. const previousDateTime = usePrevious(selection.datetime);
  49. const [state, setState] = useState<State>({error: undefined});
  50. const withStaticFilters = canUseMetricsData(organization);
  51. const eventView = generatePerformanceEventView(location, projects, {
  52. withStaticFilters,
  53. });
  54. function getOnboardingProject(): Project | undefined {
  55. // XXX used by getsentry to bypass onboarding for the upsell demo state.
  56. if (demoMode) {
  57. return undefined;
  58. }
  59. if (projects.length === 0) {
  60. return undefined;
  61. }
  62. // Current selection is 'my projects' or 'all projects'
  63. if (eventView.project.length === 0 || eventView.project[0] === ALL_ACCESS_PROJECTS) {
  64. const filtered = projects.filter(p => p.firstTransactionEvent === false);
  65. if (filtered.length === projects.length) {
  66. return filtered[0];
  67. }
  68. }
  69. // Any other subset of projects.
  70. const filtered = projects.filter(
  71. p =>
  72. eventView.project.includes(parseInt(p.id, 10)) &&
  73. p.firstTransactionEvent === false
  74. );
  75. if (filtered.length === eventView.project.length) {
  76. return filtered[0];
  77. }
  78. return undefined;
  79. }
  80. const onboardingProject = getOnboardingProject();
  81. useRouteAnalyticsEventNames(
  82. 'performance_views.overview.view',
  83. 'Performance Views: Transaction overview view'
  84. );
  85. useRouteAnalyticsParams({
  86. project_platforms: getSelectedProjectPlatforms(location, projects),
  87. show_onboarding: onboardingProject !== undefined,
  88. tab: getLandingDisplayFromParam(location)?.field,
  89. });
  90. useEffect(() => {
  91. if (!mounted.current) {
  92. loadOrganizationTags(api, organization.slug, selection);
  93. addRoutePerformanceContext(selection);
  94. mounted.current = true;
  95. return;
  96. }
  97. if (!isEqual(previousDateTime, selection.datetime)) {
  98. loadOrganizationTags(api, organization.slug, selection);
  99. addRoutePerformanceContext(selection);
  100. }
  101. }, [
  102. selection.datetime,
  103. previousDateTime,
  104. selection,
  105. api,
  106. organization,
  107. onboardingProject,
  108. location,
  109. projects,
  110. ]);
  111. function setError(newError?: string) {
  112. if (
  113. typeof newError === 'object' ||
  114. (Array.isArray(newError) && typeof newError[0] === 'object')
  115. ) {
  116. Sentry.withScope(scope => {
  117. scope.setExtra('error', newError);
  118. Sentry.captureException(new Error('setError failed with error type.'));
  119. });
  120. return;
  121. }
  122. setState({...state, error: newError});
  123. }
  124. function handleSearch(searchQuery: string, currentMEPState?: MEPState) {
  125. trackAdvancedAnalyticsEvent('performance_views.overview.search', {organization});
  126. browserHistory.push({
  127. pathname: location.pathname,
  128. query: {
  129. ...location.query,
  130. cursor: undefined,
  131. query: String(searchQuery).trim() || undefined,
  132. [METRIC_SEARCH_SETTING_PARAM]: currentMEPState,
  133. isDefaultQuery: false,
  134. },
  135. });
  136. }
  137. return (
  138. <SentryDocumentTitle title={t('Performance')} orgSlug={organization.slug}>
  139. <PerformanceEventViewProvider value={{eventView}}>
  140. <PageFiltersContainer
  141. defaultSelection={{
  142. datetime: {
  143. start: null,
  144. end: null,
  145. utc: false,
  146. period: DEFAULT_STATS_PERIOD,
  147. },
  148. }}
  149. >
  150. <PerformanceLanding
  151. router={router}
  152. eventView={eventView}
  153. setError={setError}
  154. handleSearch={handleSearch}
  155. handleTrendsClick={() =>
  156. handleTrendsClick({
  157. location,
  158. organization,
  159. projectPlatforms: getSelectedProjectPlatforms(location, projects),
  160. })
  161. }
  162. onboardingProject={onboardingProject}
  163. organization={organization}
  164. location={location}
  165. projects={projects}
  166. selection={selection}
  167. withStaticFilters={withStaticFilters}
  168. />
  169. </PageFiltersContainer>
  170. </PerformanceEventViewProvider>
  171. </SentryDocumentTitle>
  172. );
  173. }
  174. export default withPageFilters(PerformanceContent);