menu.ts
1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { MenuNode } from '@/types/auth'
export function flattenMenuPaths(menus: MenuNode[]): string[] {
const paths: string[] = []
const walk = (items: MenuNode[]) => {
for (const item of items) {
if (item.path) paths.push(item.path)
if (item.children?.length) walk(item.children)
}
}
walk(menus)
return paths
}
export function flattenMenus(menus: MenuNode[]): MenuNode[] {
const result: MenuNode[] = []
const walk = (items: MenuNode[]) => {
for (const item of items) {
result.push(item)
if (item.children?.length) walk(item.children)
}
}
walk(menus)
return result
}
export function findMenuByPath(menus: MenuNode[], path: string): MenuNode | null {
for (const item of menus) {
if (item.path === path) return item
if (item.children?.length) {
const found = findMenuByPath(item.children, path)
if (found) return found
}
}
return null
}
export function findMenuTrailByPath(menus: MenuNode[], path: string): MenuNode[] {
for (const item of menus) {
if (item.path === path) return [item]
if (item.children?.length) {
const childTrail = findMenuTrailByPath(item.children, path)
if (childTrail.length) return [item, ...childTrail]
}
}
return []
}
export function findFirstMenuPath(menus: MenuNode[]): string {
for (const item of menus) {
if (item.path) return item.path
if (item.children?.length) {
const childPath = findFirstMenuPath(item.children)
if (childPath) return childPath
}
}
return '/dashboard'
}
export function filterPinnedMenuEntries(
menus: MenuNode[],
pinned: Array<{ path: string; title: string; desc: string }>
) {
const available = new Set(flattenMenuPaths(menus))
return pinned.filter(item => available.has(item.path))
}