sigit.io/src/App.tsx

89 lines
2.3 KiB
TypeScript
Raw Normal View History

import { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { Navigate, Route, Routes } from 'react-router-dom'
2024-03-19 10:27:18 +00:00
import { AuthController, NostrController } from './controllers'
import { MainLayout } from './layouts/Main'
import {
appPrivateRoutes,
appPublicRoutes,
privateRoutes,
publicRoutes
} from './routes'
2024-02-29 07:15:50 +00:00
import { State } from './store/rootReducer'
import { getNsecBunkerDelegatedKey, saveNsecBunkerDelegatedKey } from './utils'
2024-02-27 14:03:15 +00:00
const App = () => {
const authState = useSelector((state: State) => state.auth)
useEffect(() => {
generateBunkerDelegatedKey()
2024-03-19 10:27:18 +00:00
const authController = new AuthController()
authController.checkSession()
}, [])
const generateBunkerDelegatedKey = () => {
const existingKey = getNsecBunkerDelegatedKey()
if (!existingKey) {
const nostrController = NostrController.getInstance()
const newDelegatedKey = nostrController.generateDelegatedKey()
saveNsecBunkerDelegatedKey(newDelegatedKey)
}
}
2024-02-27 14:03:15 +00:00
const handleRootRedirect = () => {
if (authState.loggedIn) return appPrivateRoutes.homePage
const callbackPathEncoded = btoa(window.location.href.split(`${window.location.origin}/#`)[1])
return `${appPublicRoutes.login}?callbackPath=${callbackPathEncoded}`
}
2024-02-27 14:03:15 +00:00
return (
<Routes>
<Route element={<MainLayout />}>
{authState?.loggedIn &&
privateRoutes.map((route, index) => (
<Route
key={route.path + index}
path={route.path}
element={route.element}
/>
))}
{publicRoutes.map((route, index) => {
if (authState?.loggedIn) {
if (!route.hiddenWhenLoggedIn) {
return (
<Route
key={route.path + index}
path={route.path}
element={route.element}
/>
)
}
} else {
return (
<Route
key={route.path + index}
path={route.path}
element={route.element}
/>
)
}
})}
<Route
2024-05-15 08:50:21 +00:00
path="*"
element={
<Navigate
to={handleRootRedirect()}
/>
}
/>
</Route>
</Routes>
2024-02-27 14:03:15 +00:00
)
}
export default App