content.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import {useEffect, 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 previousDateTime = usePrevious(selection.datetime);
  36. const [state, setState] = useState<State>({error: undefined});
  37. useEffect(() => {
  38. loadOrganizationTags(api, organization.slug, selection);
  39. addRoutePerformanceContext(selection);
  40. trackAdvancedAnalyticsEvent('performance_views.overview.view', {
  41. organization,
  42. show_onboarding: shouldShowOnboarding(),
  43. });
  44. }, []);
  45. useEffect(() => {
  46. loadOrganizationTags(api, organization.slug, selection);
  47. addRoutePerformanceContext(selection);
  48. }, [selection.projects]);
  49. useEffect(() => {
  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. <PageFiltersContainer
  108. defaultSelection={{
  109. datetime: {
  110. start: null,
  111. end: null,
  112. utc: false,
  113. period: DEFAULT_STATS_PERIOD,
  114. },
  115. }}
  116. >
  117. <PerformanceLanding
  118. eventView={eventView}
  119. setError={setError}
  120. handleSearch={handleSearch}
  121. handleTrendsClick={() => handleTrendsClick({location, organization})}
  122. shouldShowOnboarding={shouldShowOnboarding()}
  123. organization={organization}
  124. location={location}
  125. projects={projects}
  126. selection={selection}
  127. />
  128. </PageFiltersContainer>
  129. </PerformanceEventViewProvider>
  130. </SentryDocumentTitle>
  131. );
  132. }
  133. export default withPageFilters(PerformanceContent);