Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imprv. RoutesBuilder.js #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/routes/RoutesBuilder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useEffect } from 'react';
import React, { useContext, useEffect, useMemo } from 'react';
import { Route, Routes } from 'react-router-dom';
import { AppContext } from '../AppProvider';
import { getRouteComponent } from './utils';
Expand All @@ -9,20 +9,31 @@ const RoutesBuilder = ({
routes,
configs,
entry,
requireOnboarding = true,
..._
requireOnboarding = true, // Default value for requireOnboarding
}) => {
const navigate = useNavigation();
const [{ accounts }] = useContext(AppContext);

// Check if onboarding is required and accounts are available
const needsOnboarding = requireOnboarding && accounts.length === 0;

// Navigate to onboarding if required
useEffect(() => {
if (requireOnboarding && !accounts.length) {
if (needsOnboarding) {
navigate(ROUTES_MAP.ONBOARDING);
}
}, [requireOnboarding, navigate, accounts]);
}, [needsOnboarding, navigate]);

// Memoized computation of the entry component
const EntryComponent = useMemo(
() => (entry ? getRouteComponent(routes, entry) : null),
[entry, routes]
);

const EntryComponent = entry ? getRouteComponent(routes, entry) : null;
// Render routes conditionally based on onboarding status
if (needsOnboarding) return null;

return !requireOnboarding || (requireOnboarding && accounts.length > 0) ? (
return (
<Routes>
{EntryComponent && <Route path="/" element={<EntryComponent />} />}
{routes.map(({ key, name, path, Component }) => (
Expand All @@ -33,7 +44,7 @@ const RoutesBuilder = ({
/>
))}
</Routes>
) : null;
);
};

export default RoutesBuilder;