redirectDeprecatedProjectRoute.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import {Component} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import isString from 'lodash/isString';
  5. import {Client, ResponseMeta} from 'sentry/api';
  6. import {Alert} from 'sentry/components/alert';
  7. import LoadingError from 'sentry/components/loadingError';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {Project} from 'sentry/types';
  12. import {trackAnalytics} from 'sentry/utils/analytics';
  13. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  14. import Redirect from 'sentry/utils/redirect';
  15. import withApi from 'sentry/utils/withApi';
  16. type DetailsProps = {
  17. api: Client;
  18. children: (props: ChildProps) => React.ReactNode;
  19. orgId: string;
  20. projectSlug: string;
  21. };
  22. type DetailsState = {
  23. error: null | ResponseMeta;
  24. loading: boolean;
  25. project: null | Project;
  26. };
  27. type ChildProps = DetailsState & {
  28. hasProjectId: boolean;
  29. organizationId: null | string;
  30. projectId: null | string;
  31. };
  32. class ProjectDetailsInner extends Component<DetailsProps, DetailsState> {
  33. state: DetailsState = {
  34. loading: true,
  35. error: null,
  36. project: null,
  37. };
  38. componentDidMount() {
  39. this.fetchData();
  40. }
  41. fetchData = async () => {
  42. this.setState({
  43. loading: true,
  44. error: null,
  45. });
  46. const {orgId, projectSlug} = this.props;
  47. try {
  48. const project = await this.props.api.requestPromise(
  49. `/projects/${orgId}/${projectSlug}/`
  50. );
  51. this.setState({
  52. loading: false,
  53. error: null,
  54. project,
  55. });
  56. } catch (error) {
  57. this.setState({
  58. loading: false,
  59. error,
  60. project: null,
  61. });
  62. }
  63. };
  64. getProjectId() {
  65. if (this.state.project) {
  66. return this.state.project.id;
  67. }
  68. return null;
  69. }
  70. hasProjectId() {
  71. const projectID = this.getProjectId();
  72. return isString(projectID) && projectID.length > 0;
  73. }
  74. getOrganizationId() {
  75. if (this.state.project) {
  76. return this.state.project.organization.id;
  77. }
  78. return null;
  79. }
  80. render() {
  81. const childrenProps: ChildProps = {
  82. ...this.state,
  83. projectId: this.getProjectId(),
  84. hasProjectId: this.hasProjectId(),
  85. organizationId: this.getOrganizationId(),
  86. };
  87. return this.props.children(childrenProps);
  88. }
  89. }
  90. const ProjectDetails = withApi(ProjectDetailsInner);
  91. type Params = {orgId: string; projectId: string} & Record<string, any>;
  92. type Props = RouteComponentProps<Params, {}>;
  93. type RedirectOptions = {
  94. orgId: string;
  95. projectId: null | string;
  96. router: {
  97. params: Params;
  98. };
  99. };
  100. type RedirectCallback = (options: RedirectOptions) => string;
  101. const redirectDeprecatedProjectRoute = (generateRedirectRoute: RedirectCallback) =>
  102. function ({params, router, routes}: Props) {
  103. // TODO(epurkhiser): The way this function get's called as a side-effect of
  104. // the render is pretty janky and incorrect... we should fix it.
  105. function trackRedirect(organizationId: string, nextRoute: string) {
  106. const payload = {
  107. feature: 'global_views',
  108. url: getRouteStringFromRoutes(routes), // the URL being redirected from
  109. organization: organizationId,
  110. };
  111. // track redirects of deprecated URLs for analytics
  112. trackAnalytics('deprecated_urls.redirect', payload);
  113. return nextRoute;
  114. }
  115. const {orgId} = params;
  116. return (
  117. <Wrapper>
  118. <ProjectDetails orgId={orgId} projectSlug={params.projectId}>
  119. {({loading, error, hasProjectId, projectId, organizationId}) => {
  120. if (loading) {
  121. return <LoadingIndicator />;
  122. }
  123. if (!hasProjectId || !organizationId) {
  124. if (error && error.status === 404) {
  125. return (
  126. <Alert type="error">
  127. {t('The project you were looking for was not found.')}
  128. </Alert>
  129. );
  130. }
  131. return <LoadingError />;
  132. }
  133. const routeProps: RedirectOptions = {
  134. orgId,
  135. projectId,
  136. router: {params},
  137. };
  138. return (
  139. <Redirect
  140. router={router}
  141. to={trackRedirect(organizationId, generateRedirectRoute(routeProps))}
  142. />
  143. );
  144. }}
  145. </ProjectDetails>
  146. </Wrapper>
  147. );
  148. };
  149. export default redirectDeprecatedProjectRoute;
  150. const Wrapper = styled('div')`
  151. flex: 1;
  152. padding: ${space(3)};
  153. `;