focalboard/webapp/src/app.tsx

127 lines
5.7 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'
2020-10-23 13:40:39 +02:00
import {IntlProvider} from 'react-intl'
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 {DndProvider} from 'react-dnd'
import {HTML5Backend} from 'react-dnd-html5-backend'
import {TouchBackend} from 'react-dnd-touch-backend'
import {FlashMessages} from './components/flashMessages'
2021-01-21 19:16:40 +01:00
import {getCurrentLanguage, getMessages, storeLanguage} from './i18n'
2021-03-26 19:01:54 +01:00
import {default as client} from './octoClient'
2021-01-21 19:16:40 +01:00
import BoardPage from './pages/boardPage'
import ChangePasswordPage from './pages/changePasswordPage'
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-01-21 19:16:40 +01:00
import {IUser, UserContext} from './user'
import {Utils} from './utils'
const App = React.memo((): JSX.Element => {
const [language, setLanguage] = useState(getCurrentLanguage())
const [user, setUser] = useState<IUser|undefined>(undefined)
const [initialLoad, setInitialLoad] = useState(false)
2020-12-07 20:40:16 +01:00
useEffect(() => {
client.getMe().then((loadedUser?: IUser) => {
setUser(loadedUser)
setInitialLoad(true)
2020-12-07 20:40:16 +01:00
})
}, [])
2020-12-07 20:40:16 +01:00
const setAndStoreLanguage = (lang: string): void => {
2020-10-23 13:40:39 +02:00
storeLanguage(lang)
setLanguage(lang)
2020-12-07 20:40:16 +01:00
}
return (
<IntlProvider
locale={language}
messages={getMessages(language)}
>
<DndProvider backend={Utils.isMobile() ? TouchBackend : HTML5Backend}>
<UserContext.Provider value={user}>
<FlashMessages milliseconds={2000}/>
<Router forceRefresh={true}>
<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'>
<BoardPage
workspaceId='0'
readonly={true}
setLanguage={setAndStoreLanguage}
2021-01-14 20:33:07 +01:00
/>
</Route>
<Route path='/board'>
{initialLoad && !user && <Redirect to='/login'/>}
<BoardPage
workspaceId='0'
setLanguage={setAndStoreLanguage}
2021-03-26 19:01:54 +01:00
/>
</Route>
<Route
path='/workspace/:workspaceId/shared'
render={({match}) => {
return (
<BoardPage
workspaceId={match.params.workspaceId}
readonly={true}
setLanguage={setAndStoreLanguage}
/>
)
}}
/>
<Route
path='/workspace/:workspaceId/'
render={({match}) => {
if (initialLoad && !user) {
const redirectUrl = `/workspace/${match.params.workspaceId}/`
const loginUrl = `/login?r=${encodeURIComponent(redirectUrl)}`
return <Redirect to={loginUrl}/>
}
return (
<BoardPage
workspaceId={match.params.workspaceId}
setLanguage={setAndStoreLanguage}
/>
)
}}
/>
<Route path='/'>
{initialLoad && !user && <Redirect to='/login'/>}
<BoardPage
workspaceId='0'
setLanguage={setAndStoreLanguage}
/>
</Route>
</Switch>
2020-12-07 20:40:16 +01:00
</div>
</div>
</Router>
</UserContext.Provider>
</DndProvider>
</IntlProvider>
)
})
export default App