redirectDeprecatedProjectRoute.tsx 4.5 KB

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