import { PureComponent, lazy, Suspense } from 'react'; import Alert from '@mui/material/Alert'; import Box from '@mui/material/Box'; import CircularProgress from '@mui/material/CircularProgress'; import authAutomat from './automats/authAutomat'; import LoginPage from './components/LoginPage'; const CompanySetupDialog = lazy(() => import('./components/CompanySetupDialog')); const AppRouter = lazy(() => import('./appRouter')); const OAuthAuthorizePage = lazy(() => import('./routes/oauth/OAuthAuthorizePage')); const SseMonitor = import.meta.env.DEV ? lazy(() => import('./devtools/SseMonitor')) : null; function PageFallback() { return ( ); } class App extends PureComponent { constructor(props) { super(props); this.state = authAutomat.state; } componentDidMount() { this.unsubscribe = authAutomat.subscribe(this, [ 'status', 'user', 'hasAuth', 'hasCompany', 'retryNotification', ]); } componentWillUnmount() { this.unsubscribe?.(); } render() { const { status, user, hasAuth, hasCompany, retryNotification } = this.state; const isAuthed = Boolean(hasAuth ?? (status === 'authed' && user)); const isCompanySelected = Boolean(hasCompany ?? (isAuthed && user?.selectedCompanyId != null)); if (status === 'loading') { return ( {retryNotification ? ( {retryNotification} ) : null} ); } if (!isAuthed) { if (typeof window !== 'undefined' && window.location.pathname === '/oauth/authorize') { const returnTo = encodeURIComponent(window.location.pathname + window.location.search); window.history.replaceState(null, '', `/login?returnTo=${returnTo}`); } return ; } const isOAuthAuthorize = typeof window !== 'undefined' && window.location.pathname === '/oauth/authorize'; return ( <> }> {!isCompanySelected ? ( isOAuthAuthorize ? ( ) : ( ) ) : ( )} {SseMonitor ? ( ) : null} ); } } export default App;