Skip to content

Commit 5935d8c

Browse files
committed
fix(cookbook-app-react-state): migrate routing to Fusion route DSL
Migrate app-react-state's Router.tsx from a hand-written RouteObject[] tree to the layout/index/route DSL from @equinor/fusion-framework-react-router/routes, matching the pattern used by app-react-router/app-react-people/app-react-charts. Pages now live under src/routes/ (routes.ts + layout.tsx + per-route folders). Also fixes the sidebar active-link check in the layout, which previously did an exact pathname match even though routes are declared with a /* wildcard (basics/*, profile/*, todos/*) - now uses prefix matching so a link stays active on any sub-path.
1 parent c9d7bd4 commit 5935d8c

9 files changed

Lines changed: 28 additions & 37 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@equinor/fusion-framework-cookbook-app-react-state": patch
3+
---
4+
5+
Internal: migrate routing to the Fusion route DSL (`layout`/`index`/`route` from `@equinor/fusion-framework-react-router/routes`), matching the pattern demonstrated in the router cookbook. Also fix the sidebar active-link check to match sub-paths (e.g. `/todos/*`) instead of only the exact page path.

cookbooks/app-react-state/src/Router.tsx

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
1-
import { Router as FusionRouter, type RouteObject } from '@equinor/fusion-framework-react-router';
1+
import { Router as FusionRouter } from '@equinor/fusion-framework-react-router';
22

3-
import { Basics, Home, Profile, Root, Todo } from './pages';
4-
5-
const routes: RouteObject[] = [
6-
{
7-
path: '/',
8-
element: <Root />,
9-
children: [
10-
{
11-
index: true,
12-
element: <Home />,
13-
},
14-
{
15-
path: 'basics/*',
16-
element: <Basics />,
17-
},
18-
{
19-
path: 'profile/*',
20-
element: <Profile />,
21-
},
22-
{
23-
path: 'todos/*',
24-
element: <Todo />,
25-
},
26-
],
27-
},
28-
];
3+
import routes from './routes/routes';
294

305
/** Renders the application's route tree via the Fusion navigation module. */
316
export default function Router() {

cookbooks/app-react-state/src/pages/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

cookbooks/app-react-state/src/pages/Root.tsx renamed to cookbooks/app-react-state/src/routes/layout.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { SyncStatusMonitor } from '../components/SyncEvents/SyncStatusMonitor';
55
import { SideBar } from '@equinor/eds-core-react';
66
import { home, school, settings, offline_document } from '@equinor/eds-icons';
77

8+
// A route is active on an exact match or on any of its `/*` sub-paths.
9+
const isActive = (pathname: string, path: string) =>
10+
pathname === path || pathname.startsWith(`${path}/`);
11+
812
/** Provides the cookbook navigation and sync monitor shell. */
913
export const Root = () => {
1014
const currentLocation = useLocation();
@@ -25,21 +29,21 @@ export const Root = () => {
2529
as={Link}
2630
to="/basics"
2731
label="basics"
28-
active={currentLocation.pathname === '/basics'}
32+
active={isActive(currentLocation.pathname, '/basics')}
2933
/>
3034
<SideBar.Link
3135
icon={settings}
3236
as={Link}
3337
to="/profile"
3438
label="profile"
35-
active={currentLocation.pathname === '/profile'}
39+
active={isActive(currentLocation.pathname, '/profile')}
3640
/>
3741
<SideBar.Link
3842
icon={offline_document}
3943
as={Link}
4044
to="/todos"
4145
label="todos"
42-
active={currentLocation.pathname === '/todos'}
46+
active={isActive(currentLocation.pathname, '/todos')}
4347
/>
4448
</SideBar>
4549
</div>

cookbooks/app-react-state/src/pages/Profile.tsx renamed to cookbooks/app-react-state/src/routes/profile/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ProfileManager } from '../components/ProfileManager';
1+
import { ProfileManager } from '../../components/ProfileManager';
22

33
/** Demonstrates replicated profile state. */
44
export const Profile = () => {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { index, layout, route } from '@equinor/fusion-framework-react-router/routes';
2+
3+
export const pages = [
4+
index('./index.tsx'),
5+
route('basics/*', './basics/index.tsx'),
6+
route('profile/*', './profile/index.tsx'),
7+
route('todos/*', './todos/index.tsx'),
8+
];
9+
10+
export const routes = layout('./layout.tsx', pages);
11+
12+
export default routes;

cookbooks/app-react-state/src/pages/Todo.tsx renamed to cookbooks/app-react-state/src/routes/todos/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TodoListManager } from '../components/Todo';
1+
import { TodoListManager } from '../../components/Todo';
22

33
/** Demonstrates a replicated task list. */
44
export const Todo = () => {

0 commit comments

Comments
 (0)