content.tsx 5.8 KB

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