2020-10-20 21:50:53 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
2021-04-09 19:34:35 +02:00
|
|
|
import React, {useState, useEffect} from 'react'
|
2020-10-09 17:10:26 +02:00
|
|
|
import {
|
2020-10-20 21:50:53 +02:00
|
|
|
BrowserRouter as Router,
|
2020-12-04 16:03:09 +01:00
|
|
|
Redirect,
|
2021-01-21 19:16:40 +01:00
|
|
|
Route,
|
|
|
|
Switch,
|
2020-10-20 21:52:56 +02:00
|
|
|
} from 'react-router-dom'
|
2021-07-14 21:22:40 +02:00
|
|
|
import {IntlProvider} from 'react-intl'
|
|
|
|
import {DndProvider} from 'react-dnd'
|
|
|
|
import {HTML5Backend} from 'react-dnd-html5-backend'
|
|
|
|
import {TouchBackend} from 'react-dnd-touch-backend'
|
2020-10-09 17:10:26 +02:00
|
|
|
|
2021-07-14 21:22:40 +02:00
|
|
|
import {getMessages} from './i18n'
|
2020-10-28 17:56:27 +01:00
|
|
|
import {FlashMessages} from './components/flashMessages'
|
2021-01-21 19:16:40 +01:00
|
|
|
import BoardPage from './pages/boardPage'
|
|
|
|
import ChangePasswordPage from './pages/changePasswordPage'
|
2021-07-26 14:17:28 +02:00
|
|
|
import DashboardPage from './pages/dashboardPage'
|
2021-03-26 19:01:54 +01:00
|
|
|
import ErrorPage from './pages/errorPage'
|
2020-10-20 21:52:56 +02:00
|
|
|
import LoginPage from './pages/loginPage'
|
2020-12-04 16:03:09 +01:00
|
|
|
import RegisterPage from './pages/registerPage'
|
2021-04-12 17:47:47 +02:00
|
|
|
import {IUser} from './user'
|
2021-04-16 13:42:37 +02:00
|
|
|
import {Utils} from './utils'
|
2021-07-28 18:14:18 +02:00
|
|
|
import wsClient from './wsclient'
|
2021-04-28 00:09:26 +02:00
|
|
|
import {importNativeAppSettings} from './nativeApp'
|
2021-07-14 21:22:40 +02:00
|
|
|
import {fetchCurrentUser, getCurrentUser} from './store/currentUser'
|
|
|
|
import {getLanguage, fetchLanguage} from './store/language'
|
|
|
|
import {useAppSelector, useAppDispatch} from './store/hooks'
|
2020-10-09 17:10:26 +02:00
|
|
|
|
2021-04-09 19:34:35 +02:00
|
|
|
const App = React.memo((): JSX.Element => {
|
2021-04-28 00:09:26 +02:00
|
|
|
importNativeAppSettings()
|
|
|
|
|
2021-07-14 21:22:40 +02:00
|
|
|
const language = useAppSelector<string>(getLanguage)
|
|
|
|
|
|
|
|
const user = useAppSelector<IUser|null>(getCurrentUser)
|
|
|
|
const dispatch = useAppDispatch()
|
2021-04-09 19:34:35 +02:00
|
|
|
const [initialLoad, setInitialLoad] = useState(false)
|
2020-12-07 20:40:16 +01:00
|
|
|
|
2021-04-09 19:34:35 +02:00
|
|
|
useEffect(() => {
|
2021-07-14 21:22:40 +02:00
|
|
|
dispatch(fetchLanguage())
|
|
|
|
dispatch(fetchCurrentUser()).then(() => {
|
2021-04-09 19:34:35 +02:00
|
|
|
setInitialLoad(true)
|
2020-12-07 20:40:16 +01:00
|
|
|
})
|
2021-04-09 19:34:35 +02:00
|
|
|
}, [])
|
2020-12-07 20:40:16 +01:00
|
|
|
|
2021-07-28 18:14:18 +02:00
|
|
|
useEffect(() => {
|
|
|
|
wsClient.open()
|
|
|
|
return () => {
|
|
|
|
wsClient.close()
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2021-04-09 19:34:35 +02:00
|
|
|
return (
|
2021-07-14 21:22:40 +02:00
|
|
|
<IntlProvider
|
|
|
|
locale={language.split(/[_]/)[0]}
|
|
|
|
messages={getMessages(language)}
|
2021-04-09 19:34:35 +02:00
|
|
|
>
|
2021-07-14 21:22:40 +02:00
|
|
|
<DndProvider backend={Utils.isMobile() ? TouchBackend : HTML5Backend}>
|
|
|
|
<FlashMessages milliseconds={2000}/>
|
2021-07-29 20:08:39 +02:00
|
|
|
<Router basename={Utils.getFrontendBaseURL()}>
|
2021-07-14 21:22:40 +02:00
|
|
|
<div id='frame'>
|
|
|
|
<div id='main'>
|
|
|
|
<Switch>
|
|
|
|
<Route path='/error'>
|
|
|
|
<ErrorPage/>
|
|
|
|
</Route>
|
|
|
|
<Route path='/login'>
|
|
|
|
<LoginPage/>
|
|
|
|
</Route>
|
|
|
|
<Route path='/register'>
|
|
|
|
<RegisterPage/>
|
|
|
|
</Route>
|
|
|
|
<Route path='/change_password'>
|
|
|
|
<ChangePasswordPage/>
|
|
|
|
</Route>
|
|
|
|
<Route path='/shared/:boardId?/:viewId?'>
|
|
|
|
<BoardPage readonly={true}/>
|
|
|
|
</Route>
|
|
|
|
<Route path='/board/:boardId?/:viewId?'>
|
|
|
|
{initialLoad && !user && <Redirect to='/login'/>}
|
|
|
|
<BoardPage/>
|
|
|
|
</Route>
|
|
|
|
<Route path='/workspace/:workspaceId/shared/:boardId?/:viewId?'>
|
|
|
|
<BoardPage readonly={true}/>
|
|
|
|
</Route>
|
|
|
|
<Route
|
|
|
|
path='/workspace/:workspaceId/:boardId?/:viewId?'
|
|
|
|
render={({match}) => {
|
|
|
|
if (initialLoad && !user) {
|
|
|
|
let redirectUrl = '/' + Utils.buildURL(`/workspace/${match.params.workspaceId}/`)
|
|
|
|
if (redirectUrl.indexOf('//') === 0) {
|
|
|
|
redirectUrl = redirectUrl.slice(1)
|
|
|
|
}
|
|
|
|
const loginUrl = `/login?r=${encodeURIComponent(redirectUrl)}`
|
|
|
|
return <Redirect to={loginUrl}/>
|
2021-04-26 13:43:02 +02:00
|
|
|
}
|
2021-07-14 21:22:40 +02:00
|
|
|
return (
|
|
|
|
<BoardPage/>
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
2021-07-26 14:17:28 +02:00
|
|
|
<Route
|
|
|
|
exact={true}
|
|
|
|
path='/dashboard'
|
|
|
|
>
|
|
|
|
<DashboardPage/>
|
|
|
|
</Route>
|
2021-07-14 21:22:40 +02:00
|
|
|
<Route path='/:boardId?/:viewId?'>
|
|
|
|
{initialLoad && !user && <Redirect to='/login'/>}
|
|
|
|
<BoardPage/>
|
|
|
|
</Route>
|
|
|
|
</Switch>
|
|
|
|
</div>
|
2021-04-12 17:47:47 +02:00
|
|
|
</div>
|
2021-07-14 21:22:40 +02:00
|
|
|
</Router>
|
|
|
|
</DndProvider>
|
|
|
|
</IntlProvider>
|
2021-04-09 19:34:35 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
export default App
|