redirectToProject.tsx 2.3 KB

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