focalboard/webapp/src/app.tsx

123 lines
5 KiB
TypeScript
Raw Normal View History

2020-10-20 21:50:53 +02:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useState, useEffect} from 'react'
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'
import {IntlProvider} from 'react-intl'
import {DndProvider} from 'react-dnd'
import {HTML5Backend} from 'react-dnd-html5-backend'
import {TouchBackend} from 'react-dnd-touch-backend'
import {getMessages} from './i18n'
import {FlashMessages} from './components/flashMessages'
2021-01-21 19:16:40 +01:00
import BoardPage from './pages/boardPage'
import ChangePasswordPage from './pages/changePasswordPage'
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'
import {IUser} from './user'
import {Utils} from './utils'
import wsClient from './wsclient'
import {importNativeAppSettings} from './nativeApp'
import {fetchCurrentUser, getCurrentUser} from './store/currentUser'
import {getLanguage, fetchLanguage} from './store/language'
import {useAppSelector, useAppDispatch} from './store/hooks'
const App = React.memo((): JSX.Element => {
importNativeAppSettings()
const language = useAppSelector<string>(getLanguage)
const user = useAppSelector<IUser|null>(getCurrentUser)
const dispatch = useAppDispatch()
const [initialLoad, setInitialLoad] = useState(false)
2020-12-07 20:40:16 +01:00
useEffect(() => {
dispatch(fetchLanguage())
dispatch(fetchCurrentUser()).then(() => {
setInitialLoad(true)
2020-12-07 20:40:16 +01:00
})
}, [])
2020-12-07 20:40:16 +01:00
useEffect(() => {
wsClient.open()
return () => {
wsClient.close()
}
}, [])
return (
<IntlProvider
locale={language.split(/[_]/)[0]}
messages={getMessages(language)}
>
<DndProvider backend={Utils.isMobile() ? TouchBackend : HTML5Backend}>
<FlashMessages milliseconds={2000}/>
<Router basename={Utils.getFrontendBaseURL()}>
<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
}
return (
<BoardPage/>
)
}}
/>
<Route
exact={true}
path='/dashboard'
>
<DashboardPage/>
</Route>
<Route path='/:boardId?/:viewId?'>
{initialLoad && !user && <Redirect to='/login'/>}
<BoardPage/>
</Route>
</Switch>
</div>
</div>
</Router>
</DndProvider>
</IntlProvider>
)
})
export default App