fix(dashboard): Prevent reusing same Component for nested UI routes (#9725)

**What**
- Fixes a bug that caused nested UI routes to reuse their parents Component.
This commit is contained in:
Kasper Fabricius Kristensen
2024-10-24 13:06:49 +02:00
committed by GitHub
parent 5a1b6f0810
commit 7b6793f846
2 changed files with 30 additions and 22 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/dashboard": patch
---
fix(dashboard): Prevent nested UI Routes from re-using parent component

View File

@@ -1,5 +1,6 @@
import { ComponentType } from "react"
import { RouteObject } from "react-router-dom"
import { ErrorBoundary } from "../../components/utilities/error-boundary"
import { RouteExtension, RouteModule } from "../types"
/**
@@ -27,41 +28,43 @@ export const createRouteMap = (
const root: RouteObject[] = []
const addRoute = (
fullPath: string,
pathSegments: string[],
Component: ComponentType,
currentLevel: RouteObject[]
) => {
const pathSegments = fullPath.split("/").filter(Boolean)
let currentArray = currentLevel
if (!pathSegments.length) {
return
}
for (let i = 0; i < pathSegments.length; i++) {
const segment = pathSegments[i]
let route = currentArray.find((r) => r.path === segment)
const [currentSegment, ...remainingSegments] = pathSegments
let route = currentLevel.find((r) => r.path === currentSegment)
if (!route) {
route = {
path: segment,
lazy: async () => ({ Component }),
}
currentArray.push(route)
}
if (!route) {
route = { path: currentSegment, children: [] }
currentLevel.push(route)
}
if (i < pathSegments.length - 1) {
// This is not the last segment, so we need to move to the next level
if (!route.children) {
route.children = []
}
currentArray = route.children
}
if (remainingSegments.length === 0) {
route.children ||= []
route.children.push({
path: "",
ErrorBoundary: ErrorBoundary,
async lazy() {
return { Component }
},
})
} else {
route.children ||= []
addRoute(remainingSegments, Component, route.children)
}
}
routes.forEach(({ path, Component }) => {
// Remove the ignore segment from the path if it is provided
const cleanedPath = ignore
? path.replace(ignore, "").replace(/^\/+/, "")
: path.replace(/^\/+/, "")
addRoute(cleanedPath, Component, root)
const pathSegments = cleanedPath.split("/").filter(Boolean)
addRoute(pathSegments, Component, root)
})
return root