diff --git a/src/App.tsx b/src/App.tsx index 8956df1..5755be7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,7 +7,8 @@ import { appPrivateRoutes, appPublicRoutes, privateRoutes, - publicRoutes + publicRoutes, + recursiveRouteRenderer } from './routes' import { State } from './store/rootReducer' import { getNsecBunkerDelegatedKey, saveNsecBunkerDelegatedKey } from './utils' @@ -48,39 +49,28 @@ const App = () => { 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 ( }> - {authState?.loggedIn && - privateRoutes.map((route, index) => ( - - ))} - {publicRoutes.map((route, index) => { - if (authState?.loggedIn) { - if (!route.hiddenWhenLoggedIn) { - return ( - - ) - } - } else { - return ( - - ) - } - })} - + {authState?.loggedIn && privateRouteList} + {publicRoutesList} } /> diff --git a/src/pages/landing/LandingPage.tsx b/src/pages/landing/LandingPage.tsx index 2498bb3..ec0baba 100644 --- a/src/pages/landing/LandingPage.tsx +++ b/src/pages/landing/LandingPage.tsx @@ -8,7 +8,7 @@ import { } from '@mui/material' import { useEffect } from 'react' import { useSelector } from 'react-redux' -import { useLocation, useNavigate } from 'react-router-dom' +import { Outlet, useLocation, useNavigate } from 'react-router-dom' import { appPublicRoutes } from '../../routes' import { State } from '../../store/rootReducer' import { saveVisitedLink } from '../../utils' @@ -279,6 +279,7 @@ export const LandingPage = () => { )} */} + ) } diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 9f12299..f7ff68a 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -10,6 +10,7 @@ import { RelaysPage } from '../pages/settings/relays' import { SignPage } from '../pages/sign' import { VerifyPage } from '../pages/verify' import { hexToNpub } from '../utils' +import { Route, RouteProps } from 'react-router-dom' export const appPrivateRoutes = { homePage: '/', @@ -35,16 +36,54 @@ export const getProfileRoute = (hexKey: string) => export const getProfileSettingsRoute = (hexKey: string) => appPrivateRoutes.profileSettings.replace(':npub', hexToNpub(hexKey)) -export const publicRoutes = [ +type CustomRouteProps = T & + Omit & { + children?: Array> + } + +interface CustomRoutes { + routes?: CustomRouteProps[] + renderConditionCallback?: (route: CustomRouteProps) => boolean +} + +type PublicRouteProps = CustomRouteProps<{ + hiddenWhenLoggedIn?: boolean +}> + +export function recursiveRouteRenderer({ + routes, + renderConditionCallback = () => true +}: CustomRoutes) { + if (!routes) return null + + return routes.map((route, index) => + renderConditionCallback(route) ? ( + + {recursiveRouteRenderer({ + routes: route.children, + renderConditionCallback + })} + + ) : null + ) +} + +export const publicRoutes: PublicRouteProps[] = [ { path: appPublicRoutes.landingPage, hiddenWhenLoggedIn: true, - element: - }, - { - path: appPublicRoutes.login, - hiddenWhenLoggedIn: true, - element: + element: , + children: [ + { + path: appPublicRoutes.login, + hiddenWhenLoggedIn: true, + element: + } + ] }, { path: appPublicRoutes.profile,