1- import React , { useState , useEffect , lazy , Suspense } from 'react'
2- import { Redirect , Route , Switch , useHistory } from 'react-router-dom'
1+ import React , { useState , useEffect , Suspense } from 'react'
2+ import { useNavigate } from 'react-router-dom'
33import * as Sentry from '@sentry/react'
4- import { useRecoilState , useRecoilValue } from 'recoil'
4+ import { useRecoilState , useRecoilValue , useSetRecoilState } from 'recoil'
55import refreshAccess from './api/refreshAccess'
66import userState from './state/userState'
77import Loading from './components/Loading'
8- import NavBar from './components/NavBar'
9- import routes from './constants/routes'
108import gamesState from './state/gamesState'
119import activeGameState from './state/activeGameState'
1210import AuthService from './services/AuthService'
13- import canViewPage from './utils/canViewPage'
14- import GlobalBanners from './components/GlobalBanners'
11+ import Router from './Router'
1512
16- const Login = lazy ( ( ) => import ( /* webpackChunkName: 'login' */ './pages/Login' ) )
17- const Dashboard = lazy ( ( ) => import ( /* webpackChunkName: 'dashboard' */ './pages/Dashboard' ) )
18- const Events = lazy ( ( ) => import ( /* webpackChunkName: 'events' */ './pages/Events' ) )
19- const Players = lazy ( ( ) => import ( /* webpackChunkName: 'players' */ './pages/Players' ) )
20- const Register = lazy ( ( ) => import ( /* webpackChunkName: 'register' */ './pages/Register' ) )
21- const PlayerProps = lazy ( ( ) => import ( /* webpackChunkName: 'player-props' */ './pages/PlayerProps' ) )
22- const APIKeys = lazy ( ( ) => import ( /* webpackChunkName: 'api-keys' */ './pages/APIKeys' ) )
23- const PlayerEvents = lazy ( ( ) => import ( /* webpackChunkName: 'player-events' */ './pages/PlayerEvents' ) )
24- const Demo = lazy ( ( ) => import ( /* webpackChunkName: 'demo' */ './pages/Demo' ) )
25- const DataExports = lazy ( ( ) => import ( /* webpackChunkName: 'data-exports' */ './pages/DataExports' ) )
26- const Leaderboards = lazy ( ( ) => import ( /* webpackChunkName: 'leaderboards' */ './pages/Leaderboards' ) )
27- const LeaderboardEntries = lazy ( ( ) => import ( /* webpackChunkName: 'leaderboard-entries' */ './pages/LeaderboardEntries' ) )
28- const Account = lazy ( ( ) => import ( /* webpackChunkName: 'account' */ './pages/Account' ) )
29- const ConfirmPassword = lazy ( ( ) => import ( /* webpackChunkName: 'confirm-password' */ './pages/ConfirmPassword' ) )
30- const Verify2FA = lazy ( ( ) => import ( /* webpackChunkName: 'verify-2FA' */ './pages/Verify2FA' ) )
31- const RecoverAccount = lazy ( ( ) => import ( /* webpackChunkName: 'recover-account' */ './pages/RecoverAccount' ) )
32- const Activity = lazy ( ( ) => import ( /* webpackChunkName: 'activity' */ './pages/Activity' ) )
33- const Stats = lazy ( ( ) => import ( /* webpackChunkName: 'stats' */ './pages/Stats' ) )
34- const PlayerStats = lazy ( ( ) => import ( /* webpackChunkName: 'player-stats' */ './pages/PlayerStats' ) )
35- const AcceptInvite = lazy ( ( ) => import ( /* webpackChunkName: 'accept-invite' */ './pages/AcceptInvite' ) )
36- const Organisation = lazy ( ( ) => import ( /* webpackChunkName: 'organisation' */ './pages/Organisation' ) )
13+ function AppLoading ( ) {
14+ return (
15+ < main className = 'w-full flex items-center justify-center' >
16+ < Loading />
17+ </ main >
18+ )
19+ }
3720
38- const App = ( ) => {
39- const [ user , setUser ] = useRecoilState ( userState )
21+ function App ( ) {
22+ const setUser = useSetRecoilState ( userState )
4023
4124 const [ isRefreshing , setRefreshing ] = useState ( true )
4225 const [ hasTriedRefreshing , setTriedRefreshing ] = useState ( false )
@@ -45,7 +28,7 @@ const App = () => {
4528 const games = useRecoilValue ( gamesState )
4629 const [ activeGame , setActiveGame ] = useRecoilState ( activeGameState )
4730
48- const history = useHistory ( )
31+ const navigate = useNavigate ( )
4932
5033 const handleRefreshSession = async ( ) => {
5134 try {
@@ -72,72 +55,20 @@ const App = () => {
7255 if ( ! hasTriedRefreshing && intendedUrl ) {
7356 handleRefreshSession ( )
7457 } else if ( hasTriedRefreshing ) {
75- history . replace ( intendedUrl )
58+ navigate ( intendedUrl , { replace : true } )
7659 }
7760 } , [ intendedUrl , hasTriedRefreshing ] )
7861
7962 useEffect ( ( ) => {
8063 if ( ! activeGame && games . length > 0 ) setActiveGame ( games [ 0 ] )
8164 } , [ activeGame , games ] )
8265
83- const appLoading = (
84- < main className = 'w-full flex items-center justify-center' >
85- < Loading />
86- </ main >
87- )
8866
89- if ( isRefreshing ) return appLoading
67+ if ( isRefreshing ) return < AppLoading />
9068
9169 return (
92- < Suspense fallback = { appLoading } >
93- { ! AuthService . getToken ( ) &&
94- < main className = 'bg-gray-800 w-full' >
95- < Switch >
96- < Route exact path = { routes . login } component = { Login } />
97- < Route exact path = { routes . register } component = { Register } />
98- < Route exact path = { routes . demo } component = { Demo } />
99- < Route exact path = { routes . verify2FA } component = { Verify2FA } />
100- < Route exact path = { routes . recover } component = { RecoverAccount } />
101- < Route exact path = { routes . acceptInvite } component = { AcceptInvite } />
102-
103- < Redirect to = { `${ routes . login } ?next=${ intendedUrl } ` } />
104- </ Switch >
105- </ main >
106- }
107-
108- { AuthService . getToken ( ) &&
109- < div className = 'w-full flex flex-col' >
110- < NavBar />
111- < main className = 'bg-gray-800 p-4 md:p-8 text-white' >
112- < GlobalBanners />
113-
114- < Switch >
115- < Route exact path = { routes . dashboard } component = { Dashboard } />
116- < Route exact path = { routes . account } component = { Account } />
117- < Route exact path = { routes . confirmPassword } component = { ConfirmPassword } />
118- { canViewPage ( user , routes . organisation ) && < Route exact path = { routes . organisation } component = { Organisation } /> }
119-
120- { activeGame &&
121- < >
122- < Route exact path = { routes . players } component = { Players } />
123- < Route exact path = { routes . events } component = { Events } />
124- { canViewPage ( user , routes . apiKeys ) && < Route exact path = { routes . apiKeys } component = { APIKeys } /> }
125- < Route exact path = { routes . playerProps } component = { PlayerProps } />
126- < Route exact path = { routes . playerEvents } component = { PlayerEvents } />
127- { canViewPage ( user , routes . dataExports ) && < Route exact path = { routes . dataExports } component = { DataExports } /> }
128- < Route exact path = { routes . leaderboards } component = { Leaderboards } />
129- < Route exact path = { routes . leaderboardEntries } component = { LeaderboardEntries } />
130- { canViewPage ( user , routes . activity ) && < Route exact path = { routes . activity } component = { Activity } /> }
131- < Route exact path = { routes . stats } component = { Stats } />
132- < Route exact path = { routes . playerStats } component = { PlayerStats } />
133- </ >
134- }
135-
136- < Redirect to = { routes . dashboard } />
137- </ Switch >
138- </ main >
139- </ div >
140- }
70+ < Suspense fallback = { < AppLoading /> } >
71+ < Router intendedUrl = { intendedUrl } />
14172 </ Suspense >
14273 )
14374}
0 commit comments