import React, { useEffect, useState } from 'react'; import { Helmet } from 'react-helmet'; import { useMatch, Router, navigate, RouteComponentProps, Redirect } from '@reach/router'; import { Toaster } from 'react-hot-toast'; import { isValid } from 'src/services/api'; import { useAuth } from 'src/services/auth'; import { Apps, Dashboard, Users, Login, AppSingle } from './modules'; import { Layout } from './components'; type AppProps = RouteComponentProps; // eslint-disable-next-line @typescript-eslint/no-unused-vars function App(_: AppProps) { const { auth } = useAuth(); const isLoginPage = useMatch('/login'); const [initialized, setInitialized] = useState(false); const [initializedToken, setInitializedToken] = useState(null); useEffect(() => { if (!isValid(auth) && !isLoginPage) { navigate('/login'); } }, [auth, isLoginPage]); useEffect(() => { if (isValid(auth) && (!initialized || initializedToken !== auth.token)) { setInitialized(true); setInitializedToken(auth.token); } }, [auth, initialized, initializedToken]); return ( <> Stackspin
{!isValid(auth) ? ( ) : ( )} {isValid(auth) ? : } {/* Place to load notifications */}
); } export default App;