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' |
| 7 | import { Route, Switch, Redirect, useHistory, useLocation } from 'react-router-dom' |
| 8 | import { CircularProgress, Container, CssBaseline } from '@material-ui/core' |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 9 | import authManager from './AuthManager' |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 10 | //import logo from './logo.svg' |
| 11 | import './App.scss' |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 12 | |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 13 | import SignInPage from "./pages/loginDialog.jsx" |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 14 | import JamiMessenger from "./pages/messenger.jsx" |
| 15 | import AccountSettings from "./pages/accountSettings.jsx" |
| 16 | import AccountSelection from "./pages/accountSelection.jsx" |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 17 | import ServerSetup from "./pages/serverSetup.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' |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 20 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 21 | const App = (props) => { |
| 22 | const history = useHistory() |
| 23 | const { location } = useLocation() |
| 24 | const [state, setState] = useState({ |
| 25 | loaded: false, |
| 26 | auth: authManager.getState() |
| 27 | }) |
| 28 | useEffect(() => { |
| 29 | authManager.init(auth => { |
| 30 | setState({ loaded: true, auth }) |
| 31 | }) |
| 32 | return () => authManager.deinit() |
| 33 | }, []); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 34 | |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 35 | console.log("App render") |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 36 | console.log(state) |
| 37 | console.log(location) |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 38 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 39 | if (!state.loaded) { |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 40 | return <LoadingPage /> |
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 | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 48 | <Switch> |
| 49 | <Route exact path="/"><Redirect to="/account" /></Route> |
| 50 | <Route path="/account/:accountId/settings" component={AccountSettings} /> |
| 51 | <Route path="/account/:accountId/addContact/:contactId" component={JamiMessenger} /> |
| 52 | <Route path="/account/:accountId/conversation/:conversationId" component={JamiMessenger} /> |
| 53 | <Route path="/account/:accountId" component={JamiMessenger} /> |
| 54 | <Route path="/account" component={AccountSelection} /> |
| 55 | <Route component={NotFoundPage} /> |
| 56 | </Switch> |
| 57 | {!state.auth.authenticated && <SignInPage open={!state.auth.authenticated}/>} |
| 58 | </React.Fragment> |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 61 | export default App |