content.tsx 5.6 KB

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