content.tsx 6.7 KB

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