content.tsx 5.0 KB

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