redirectToProject.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {Component, Fragment} from 'react';
  2. import {WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {ModalRenderProps} from 'sentry/actionCreators/modal';
  5. import {Button} from 'sentry/components/button';
  6. import Text from 'sentry/components/text';
  7. import {t, tct} from 'sentry/locale';
  8. import recreateRoute from 'sentry/utils/recreateRoute';
  9. // eslint-disable-next-line no-restricted-imports
  10. import withSentryRouter from 'sentry/utils/withSentryRouter';
  11. type Props = ModalRenderProps &
  12. WithRouterProps & {
  13. slug: string;
  14. };
  15. type State = {
  16. timer: number;
  17. };
  18. class RedirectToProjectModal extends Component<Props, State> {
  19. state: State = {
  20. timer: 5,
  21. };
  22. componentDidMount() {
  23. this.redirectInterval = window.setInterval(() => {
  24. if (this.state.timer <= 1) {
  25. window.location.assign(this.newPath);
  26. return;
  27. }
  28. this.setState(state => ({
  29. timer: state.timer - 1,
  30. }));
  31. }, 1000);
  32. }
  33. componentWillUnmount() {
  34. if (this.redirectInterval) {
  35. window.clearInterval(this.redirectInterval);
  36. }
  37. }
  38. redirectInterval: number | null = null;
  39. get newPath() {
  40. const {params, slug} = this.props;
  41. return recreateRoute('', {
  42. ...this.props,
  43. params: {
  44. ...params,
  45. projectId: slug,
  46. },
  47. });
  48. }
  49. render() {
  50. const {slug, Header, Body} = this.props;
  51. return (
  52. <Fragment>
  53. <Header>{t('Redirecting to New Project...')}</Header>
  54. <Body>
  55. <div>
  56. <Text>
  57. <p>{t('The project slug has been changed.')}</p>
  58. <p>
  59. {tct(
  60. 'You will be redirected to the new project [project] in [timer] seconds...',
  61. {
  62. project: <strong>{slug}</strong>,
  63. timer: `${this.state.timer}`,
  64. }
  65. )}
  66. </p>
  67. <ButtonWrapper>
  68. <Button priority="primary" href={this.newPath}>
  69. {t('Continue to %s', slug)}
  70. </Button>
  71. </ButtonWrapper>
  72. </Text>
  73. </div>
  74. </Body>
  75. </Fragment>
  76. );
  77. }
  78. }
  79. export default withSentryRouter(RedirectToProjectModal);
  80. export {RedirectToProjectModal};
  81. const ButtonWrapper = styled('div')`
  82. display: flex;
  83. justify-content: flex-end;
  84. `;