Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 1 | /* |
| 2 | Company: Savoir-faire Linux |
| 3 | Author: Larbi Gharib <larbi.gharib@savoirfairelinux.com> |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 4 | License: AGPL-3 |
| 5 | */ |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 6 | import React, { useState, useEffect } from 'react' |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 7 | import { Route, Switch, Redirect } from 'react-router-dom' |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 8 | import authManager from './AuthManager' |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 9 | //import logo from './logo.svg' |
| 10 | import './App.scss' |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 11 | |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 12 | import SignInPage from "./pages/loginDialog.jsx" |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 13 | import JamiMessenger from "./pages/JamiMessenger.jsx" |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 14 | import AccountSettings from "./pages/accountSettings.jsx" |
| 15 | import AccountSelection from "./pages/accountSelection.jsx" |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 16 | import ServerSetup from "./pages/serverSetup.jsx" |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 17 | import AccountCreationDialog from "./pages/accountCreation.jsx" |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 18 | import NotFoundPage from "./pages/404.jsx" |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 19 | import LoadingPage from './components/loading' |
Adrien Béraud | 88a5244 | 2021-04-26 12:11:41 -0400 | [diff] [blame] | 20 | import JamiAccountDialog from './pages/jamiAccountCreation.jsx' |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 21 | import { AnimatePresence } from 'framer-motion' |
Adrien Béraud | 3499590 | 2021-06-07 10:12:00 -0400 | [diff] [blame] | 22 | import WelcomeAnimation from './components/welcome' |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 23 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 24 | const App = (props) => { |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 25 | const [state, setState] = useState({ |
| 26 | loaded: false, |
| 27 | auth: authManager.getState() |
| 28 | }) |
Adrien Béraud | 3499590 | 2021-06-07 10:12:00 -0400 | [diff] [blame] | 29 | const [displayWelcome, setDisplayWelcome] = useState(true) |
| 30 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 31 | useEffect(() => { |
| 32 | authManager.init(auth => { |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 33 | setState({ loaded: false, auth }) |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 34 | }) |
| 35 | return () => authManager.deinit() |
| 36 | }, []); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 37 | |
Adrien Béraud | 3499590 | 2021-06-07 10:12:00 -0400 | [diff] [blame] | 38 | console.log("App render") |
| 39 | if (displayWelcome) { |
| 40 | return <WelcomeAnimation showSetup={!state.auth.setupComplete} onComplete={() => setDisplayWelcome(false)} /> |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 41 | } else if (!state.auth.setupComplete) { |
| 42 | return <Switch> |
| 43 | <Route path="/setup" component={ServerSetup} /> |
| 44 | <Route><Redirect to="/setup" /></Route> |
| 45 | </Switch> |
| 46 | } |
| 47 | return <React.Fragment> |
Adrien Béraud | 6c93496 | 2021-06-07 10:13:26 -0400 | [diff] [blame] | 48 | <Route |
| 49 | render={({ location }) => { |
| 50 | console.log("App render location") |
| 51 | |
| 52 | return <AnimatePresence exitBeforeEnter initial={false}> |
| 53 | <Switch location={location} key={location.pathname}> |
| 54 | <Route exact path="/"><Redirect to="/account" /></Route> |
| 55 | <Route path="/account/:accountId/settings" component={AccountSettings} /> |
| 56 | <Route path="/account/:accountId" component={JamiMessenger} /> |
| 57 | <Route path="/account" component={AccountSelection} /> |
| 58 | <Route path="/newAccount/jami" component={JamiAccountDialog} /> |
| 59 | <Route path="/newAccount" component={AccountCreationDialog} /> |
| 60 | <Route path="/setup"><Redirect to="/account" /></Route> |
| 61 | <Route component={NotFoundPage} /> |
| 62 | </Switch> |
| 63 | {!state.auth.authenticated && <SignInPage open={!state.auth.authenticated}/>} |
| 64 | </AnimatePresence>}} |
| 65 | /> |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 66 | </React.Fragment> |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 69 | export default App |