sigit.io/src/App.tsx

81 lines
2.2 KiB
TypeScript

import { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { Navigate, Route, Routes } from 'react-router-dom'
import { AuthController, NostrController } from './controllers'
import { MainLayout } from './layouts/Main'
import {
appPrivateRoutes,
appPublicRoutes,
privateRoutes,
publicRoutes,
recursiveRouteRenderer
} from './routes'
import { State } from './store/rootReducer'
import { getNsecBunkerDelegatedKey, saveNsecBunkerDelegatedKey } from './utils'
const App = () => {
const authState = useSelector((state: State) => state.auth)
useEffect(() => {
if (window.location.hostname === '0.0.0.0') {
// A change of the host is needed to make library available in windows object
// the app can't encrypt files without the crypto library
// which is only available on https or localhost
window.location.hostname = 'localhost'
}
generateBunkerDelegatedKey()
const authController = new AuthController()
authController.checkSession()
}, [])
const generateBunkerDelegatedKey = () => {
const existingKey = getNsecBunkerDelegatedKey()
if (!existingKey) {
const nostrController = NostrController.getInstance()
const newDelegatedKey = nostrController.generateDelegatedKey()
saveNsecBunkerDelegatedKey(newDelegatedKey)
}
}
const handleRootRedirect = () => {
if (authState.loggedIn) return appPrivateRoutes.homePage
const callbackPathEncoded = btoa(
window.location.href.split(`${window.location.origin}/#`)[1]
)
return `${appPublicRoutes.login}?callbackPath=${callbackPathEncoded}`
}
const publicRoutesList = recursiveRouteRenderer({
routes: publicRoutes,
renderConditionCallback: (r) => {
if (authState?.loggedIn) {
if (!r.hiddenWhenLoggedIn) {
return true
}
} else {
return true
}
return false
}
})
const privateRouteList = recursiveRouteRenderer({ routes: privateRoutes })
return (
<Routes>
<Route element={<MainLayout />}>
{authState?.loggedIn && privateRouteList}
{publicRoutesList}
<Route path="*" element={<Navigate to={handleRootRedirect()} />} />
</Route>
</Routes>
)
}
export default App