sigit.io/src/App.tsx

72 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'
2024-03-19 10:27:18 +00:00
import { AuthController, NostrController } from './controllers'
import { MainLayout } from './layouts/Main'
import {
appPrivateRoutes,
appPublicRoutes,
privateRoutes,
publicRoutes,
recursiveRouteRenderer
} from './routes'
2024-02-29 07:15:50 +00:00
import { State } from './store/rootReducer'
import { getNsecBunkerDelegatedKey, saveNsecBunkerDelegatedKey } from './utils'
2024-07-30 10:23:38 +00:00
import './App.scss'
2024-02-27 14:03:15 +00:00
const App = () => {
const authState = useSelector((state: State) => state.auth)
useEffect(() => {
2024-05-29 14:03:00 +00:00
if (window.location.hostname === '0.0.0.0') {
2024-05-29 14:06:47 +00:00
// A change of the host is needed to make library available in windows object
2024-05-29 14:08:38 +00:00
// the app can't encrypt files without the crypto library
// which is only available on https or localhost
2024-05-29 14:03:00 +00:00
window.location.hostname = 'localhost'
}
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
2024-05-17 13:28:47 +00:00
const callbackPathEncoded = btoa(
window.location.href.split(`${window.location.origin}/#`)[1]
)
return `${appPublicRoutes.login}?callbackPath=${callbackPathEncoded}`
}
// Hide route only if loggedIn and r.hiddenWhenLoggedIn are both true
const publicRoutesList = recursiveRouteRenderer(publicRoutes, (r) => {
return !authState.loggedIn || !r.hiddenWhenLoggedIn
})
const privateRouteList = recursiveRouteRenderer(privateRoutes)
2024-02-27 14:03:15 +00:00
return (
<Routes>
<Route element={<MainLayout />}>
{authState?.loggedIn && privateRouteList}
{publicRoutesList}
2024-05-17 13:28:47 +00:00
<Route path="*" element={<Navigate to={handleRootRedirect()} />} />
</Route>
</Routes>
2024-02-27 14:03:15 +00:00
)
}
export default App