index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {Component} from 'react';
  2. import {browserHistory, RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import isEqual from 'lodash/isEqual';
  5. import {loadOrganizationTags} from 'sentry/actionCreators/tags';
  6. import {Client} from 'sentry/api';
  7. import NoProjectMessage from 'sentry/components/noProjectMessage';
  8. import GlobalSelectionHeader from 'sentry/components/organizations/globalSelectionHeader';
  9. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  10. import {t} from 'sentry/locale';
  11. import {PageContent} from 'sentry/styles/organization';
  12. import {GlobalSelection, Organization, Project} from 'sentry/types';
  13. import EventView from 'sentry/utils/discover/eventView';
  14. import {WebVital} from 'sentry/utils/discover/fields';
  15. import {PerformanceEventViewProvider} from 'sentry/utils/performance/contexts/performanceEventViewContext';
  16. import {decodeScalar} from 'sentry/utils/queryString';
  17. import withApi from 'sentry/utils/withApi';
  18. import withGlobalSelection from 'sentry/utils/withGlobalSelection';
  19. import withOrganization from 'sentry/utils/withOrganization';
  20. import withProjects from 'sentry/utils/withProjects';
  21. import {generatePerformanceVitalDetailView} from '../data';
  22. import {MetricsSwitchContext} from '../metricsSwitch';
  23. import {addRoutePerformanceContext, getTransactionName} from '../utils';
  24. import VitalDetailContent from './vitalDetailContent';
  25. type Props = RouteComponentProps<{}, {}> & {
  26. api: Client;
  27. organization: Organization;
  28. projects: Project[];
  29. selection: GlobalSelection;
  30. loadingProjects: boolean;
  31. };
  32. type State = {
  33. eventView: EventView;
  34. };
  35. class VitalDetail extends Component<Props, State> {
  36. state: State = {
  37. eventView: generatePerformanceVitalDetailView(
  38. this.props.organization,
  39. this.props.location
  40. ),
  41. };
  42. static getDerivedStateFromProps(nextProps: Readonly<Props>, prevState: State): State {
  43. return {
  44. ...prevState,
  45. eventView: generatePerformanceVitalDetailView(
  46. nextProps.organization,
  47. nextProps.location
  48. ),
  49. };
  50. }
  51. componentDidMount() {
  52. const {api, organization, selection} = this.props;
  53. loadOrganizationTags(api, organization.slug, selection);
  54. addRoutePerformanceContext(selection);
  55. }
  56. componentDidUpdate(prevProps: Props) {
  57. const {api, organization, selection} = this.props;
  58. if (
  59. !isEqual(prevProps.selection.projects, selection.projects) ||
  60. !isEqual(prevProps.selection.datetime, selection.datetime)
  61. ) {
  62. loadOrganizationTags(api, organization.slug, selection);
  63. addRoutePerformanceContext(selection);
  64. }
  65. }
  66. getDocumentTitle(): string {
  67. const name = getTransactionName(this.props.location);
  68. const hasTransactionName = typeof name === 'string' && String(name).trim().length > 0;
  69. if (hasTransactionName) {
  70. return [String(name).trim(), t('Performance')].join(' - ');
  71. }
  72. return [t('Vital Detail'), t('Performance')].join(' - ');
  73. }
  74. render() {
  75. const {organization, location, router} = this.props;
  76. const {eventView} = this.state;
  77. if (!eventView) {
  78. browserHistory.replace({
  79. pathname: `/organizations/${organization.slug}/performance/`,
  80. query: {
  81. ...location.query,
  82. },
  83. });
  84. return null;
  85. }
  86. const vitalNameQuery = decodeScalar(location.query.vitalName);
  87. const vitalName =
  88. Object.values(WebVital).indexOf(vitalNameQuery as WebVital) === -1
  89. ? undefined
  90. : (vitalNameQuery as WebVital);
  91. return (
  92. <SentryDocumentTitle title={this.getDocumentTitle()} orgSlug={organization.slug}>
  93. <PerformanceEventViewProvider value={{eventView: this.state.eventView}}>
  94. <GlobalSelectionHeader>
  95. <StyledPageContent>
  96. <NoProjectMessage organization={organization}>
  97. <MetricsSwitchContext.Consumer>
  98. {({isMetricsData}) => (
  99. <VitalDetailContent
  100. location={location}
  101. organization={organization}
  102. eventView={eventView}
  103. router={router}
  104. vitalName={vitalName || WebVital.LCP}
  105. isMetricsData={isMetricsData}
  106. />
  107. )}
  108. </MetricsSwitchContext.Consumer>
  109. </NoProjectMessage>
  110. </StyledPageContent>
  111. </GlobalSelectionHeader>
  112. </PerformanceEventViewProvider>
  113. </SentryDocumentTitle>
  114. );
  115. }
  116. }
  117. const StyledPageContent = styled(PageContent)`
  118. padding: 0;
  119. `;
  120. export default withApi(withGlobalSelection(withProjects(withOrganization(VitalDetail))));