content.tsx 4.7 KB

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