mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-30 06:12:47 -07:00
38 lines
966 B
TypeScript
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)
|
||
|
|
}
|