Advertisement
kervi

Untitled

Jan 29th, 2022
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {lazy, Suspense} from 'react'
  2.  
  3. import {BrowserRouter, Route} from 'react-router-dom'
  4. import {Loader} from 'rsuite';
  5.  
  6. import '@/Components/_config/axios';
  7. import '@/Components/_config/sentry';
  8. import '@/Components/_config/mixpanel';
  9. import 'react-toastify/dist/ReactToastify.min.css';
  10. import 'rsuite/dist/rsuite.min.css';
  11. import '@/assets/scss/main.scss'
  12.  
  13. import {CentrifugeWrapper} from '@/Components/Websocket/Centrifuge';
  14. import {useRegisterSW} from 'virtual:pwa-register/react'
  15.  
  16. const Main = lazy(async () => import('@/Layouts/Main'))
  17. const Login = lazy(async () => import('@/Layouts/Login'))
  18.  
  19. function App() {
  20.  
  21.   const intervalMS = 5000; // 1 minute
  22.  
  23.   const {
  24.     offlineReady: [offlineReady, setOfflineReady],
  25.     needRefresh: [needRefresh, setNeedRefresh],
  26.     updateServiceWorker,
  27.   } = useRegisterSW({
  28.     immediate: true,
  29.     onRegistered(r) {
  30.       if (r) {
  31.         console.log("SW Registered: " + r);
  32.         setInterval(async () => {
  33.           await r.update();
  34.         }, intervalMS);
  35.       }
  36.     },
  37.  
  38.     onRegisterError(error) {
  39.       console.log("SW registration error", error);
  40.     },
  41.   });
  42.  
  43.   let token = localStorage.getItem('token')
  44.  
  45.   if (needRefresh) {
  46.     return <>not found</>
  47.   }
  48.  
  49.   if (token === null) {
  50.     window.history.pushState({}, '', '/')
  51.     return (
  52.         <BrowserRouter>
  53.           <Suspense fallback={<Loader backdrop content='loading...' vertical/>}>
  54.             <Route exact path='/' name='Login' render={props => <Login {...props} />}/>
  55.           </Suspense>
  56.         </BrowserRouter>
  57.     )
  58.   }
  59.   else {
  60.     return (
  61.         <>
  62.           <CentrifugeWrapper/>
  63.           <Suspense fallback={<Loader backdrop content='loading...' vertical/>}>
  64.             <Main/>
  65.           </Suspense>
  66.         </>
  67.     );
  68.   }
  69. }
  70.  
  71. export default App;
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement