-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNavData.tsx
53 lines (42 loc) · 1.26 KB
/
NavData.tsx
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
import type { ReactElement } from 'react'
import { type DocSection, docsStructure } from './routing/docs-structure'
export type NavItem = {
title?: string
href?: string
toMenu?: MenuId
icon?: ReactElement
sections?: NavItem[]
}
export type NavMenu = NavItem[]
export type NavMenuId = 'docs'
export type MenuId = NavMenuId | 'plural'
export type NavData = Record<NavMenuId, NavMenu>
export function findNavItem(
test: (arg: NavItem) => boolean,
section: NavMenu
): NavItem | null {
for (const item of section) {
if (test(item)) {
return item
}
const search = findNavItem(test, item.sections || [])
if (search) {
return search
}
}
return null
}
export const getNavData = (): NavData => ({
docs: docsStructureToNavMenu,
})
const docsStructureToNavMenu: NavMenu = docsStructure.map((section) =>
docSectionToNavItem(section)
)
// these data structures are slightly different so need to convert, but docsStructure is the single source of truth
function docSectionToNavItem(section: DocSection, parentPath = ''): NavItem {
const path = `${parentPath}/${section.path}`
const sections = section.sections?.map((subSection) =>
docSectionToNavItem(subSection, path)
)
return { title: section.title, href: path, sections }
}