sigit.io/src/App.tsx

75 lines
2.1 KiB
TypeScript
Raw Normal View History

import { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { Navigate, Route, Routes } from 'react-router-dom'
import { NostrController } from './controllers'
import { appPrivateRoutes, privateRoutes, publicRoutes } from './routes'
import { State } from './store/rootReducer'
import { getNsecBunkerDelegatedKey, saveNsecBunkerDelegatedKey } from './utils'
import { MainLayout } from './layouts/Main'
import { LandingPage } from './pages/landing/LandingPage'
2024-02-27 14:03:15 +00:00
const App = () => {
const authState = useSelector((state: State) => state.auth)
useEffect(() => {
generateBunkerDelegatedKey()
}, [])
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
return (
<Routes>
<Route element={<MainLayout />}>
{authState?.loggedIn && (
<Route
path='/'
element={<Navigate to={appPrivateRoutes.homePage} />}
/>
)}
{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}
/>
)
}
})}
{!authState ||
(!authState.loggedIn && <Route path='*' element={<LandingPage />} />)}
</Route>
</Routes>
2024-02-27 14:03:15 +00:00
)
}
export default App