Files
tuono/packages/fs-router-vite-plugin/src/has-parent-route.ts
T
Valerio Ageno 1726312d34 Support nested roots (#18)
* feat: remove isLoader route property

* fix: has-parent-route fs-router util

* feat: handle routeGenerator nested __root

* feat: enable multiple roots with nested dynamic paths

* feat: enable nested routes

* fix: prevent route path creation for __root

* feat: prevent wrong path on subfolder index routes

* chore: cleaned tutorial example folder

* feat: update version to v0.8.0
2024-07-23 19:13:18 +02:00

38 lines
966 B
TypeScript

import { ROOT_PATH_ID } from './constants'
import { multiSortBy } from './utils'
import type { RouteNode } from './types'
export function hasParentRoute(
routes: RouteNode[],
node: RouteNode,
routePathToCheck = '/',
): RouteNode | null {
const segments = routePathToCheck.split('/')
segments.pop() // Remove the last segment
const parentRoutePath = segments.join('/')
if (!parentRoutePath || parentRoutePath === '/') {
return null
}
const sortedNodes = multiSortBy(routes, [
(d): number => d.routePath.length * -1,
(d): string | undefined => d.variableName,
])
// Exclude base __root file
.filter((d) => d.routePath !== `/${ROOT_PATH_ID}`)
for (const route of sortedNodes) {
if (route.routePath === '/') continue
if (
route.routePath.startsWith(parentRoutePath) &&
route.routePath.endsWith(ROOT_PATH_ID)
) {
return route
}
}
return hasParentRoute(routes, node, parentRoutePath)
}