fix: add conditional for getCustomViewByRoute

This commit is contained in:
Paul Popus
2024-03-15 10:26:46 -03:00
parent 39600fb0f5
commit e301393f33

View File

@@ -18,26 +18,29 @@ export const getCustomViewByRoute = ({
const currentRoute = currentRouteWithAdmin.replace(adminRoute, '')
const foundViewConfig = Object.entries(views).find(([, view]) => {
if (typeof view === 'object') {
const { exact, path: viewPath, sensitive, strict } = view
const foundViewConfig =
views &&
typeof views === 'object' &&
Object.entries(views).find(([, view]) => {
if (typeof view === 'object') {
const { exact, path: viewPath, sensitive, strict } = view
const keys = []
const keys = []
// run the view path through `pathToRegexp` to resolve any dynamic segments
// i.e. `/admin/custom-view/:id` -> `/admin/custom-view/123`
const regex = pathToRegexp(viewPath, keys, {
sensitive,
strict,
})
// run the view path through `pathToRegexp` to resolve any dynamic segments
// i.e. `/admin/custom-view/:id` -> `/admin/custom-view/123`
const regex = pathToRegexp(viewPath, keys, {
sensitive,
strict,
})
const match = regex.exec(currentRoute)
const viewRoute = match?.[0] || viewPath
const match = regex.exec(currentRoute)
const viewRoute = match?.[0] || viewPath
if (exact) return currentRoute === viewRoute
if (!exact) return viewRoute.startsWith(currentRoute)
}
})?.[1]
if (exact) return currentRoute === viewRoute
if (!exact) return viewRoute.startsWith(currentRoute)
}
})?.[1]
return typeof foundViewConfig === 'object' ? foundViewConfig.Component : null
}