index.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {useCallback, useEffect, useRef, useState} from 'react';
  2. import Button from 'sentry/components/button';
  3. import LoadingIndicator from 'sentry/components/loadingIndicator';
  4. import ThemeAndStyleProvider from 'sentry/components/themeAndStyleProvider';
  5. import {t} from 'sentry/locale';
  6. import useApi from 'sentry/utils/useApi';
  7. type Props = {
  8. hash?: boolean | string;
  9. };
  10. function SetupWizard({hash = false}: Props) {
  11. const api = useApi();
  12. const closeTimeoutRef = useRef<number | undefined>(undefined);
  13. const [finished, setFinished] = useState(false);
  14. useEffect(() => {
  15. return () => {
  16. if (closeTimeoutRef.current) {
  17. window.clearTimeout(closeTimeoutRef.current);
  18. }
  19. };
  20. });
  21. useEffect(() => {
  22. return () => {
  23. window.clearTimeout(closeTimeoutRef.current);
  24. };
  25. });
  26. const checkFinished = useCallback(async () => {
  27. try {
  28. await api.requestPromise(`/wizard/${hash}/`);
  29. } catch {
  30. setFinished(true);
  31. window.clearTimeout(closeTimeoutRef.current);
  32. closeTimeoutRef.current = window.setTimeout(() => window.close(), 10000);
  33. }
  34. }, [api, hash]);
  35. useEffect(() => {
  36. const pollingInterval = window.setInterval(checkFinished, 1000);
  37. return () => window.clearInterval(pollingInterval);
  38. }, [checkFinished]);
  39. return (
  40. <ThemeAndStyleProvider>
  41. <div className="container">
  42. {!finished ? (
  43. <LoadingIndicator style={{margin: '2em auto'}}>
  44. <div className="row">
  45. <h5>{t('Waiting for wizard to connect')}</h5>
  46. </div>
  47. </LoadingIndicator>
  48. ) : (
  49. <div className="row">
  50. <h5>{t('Return to your terminal to complete your setup')}</h5>
  51. <h5>{t('(This window will close in 10 seconds)')}</h5>
  52. <Button onClick={() => window.close()}>{t('Close browser tab')}</Button>
  53. </div>
  54. )}
  55. </div>
  56. </ThemeAndStyleProvider>
  57. );
  58. }
  59. export default SetupWizard;