2022-10-25 18:57:23 +05:00
|
|
|
import { Breadcrumb, BreadcrumbItemProps } from 'antd'
|
2022-10-31 05:10:28 +05:00
|
|
|
import { Link } from 'react-router-dom'
|
2022-10-25 18:57:23 +05:00
|
|
|
import { join } from 'path'
|
|
|
|
|
2022-12-05 20:35:29 +05:00
|
|
|
import { PrivateMenuItem } from '@components/PrivateMenu'
|
2022-10-31 05:10:28 +05:00
|
|
|
import { FunctionalValue, getFunctionalValue, } from '@utils'
|
2022-10-25 18:57:23 +05:00
|
|
|
|
2022-12-05 20:35:29 +05:00
|
|
|
export const makeBreadcrumbItems = (items: PrivateMenuItem[], pathParts: string[], root: string = '/') => {
|
2022-10-25 18:57:23 +05:00
|
|
|
const out = []
|
|
|
|
const parts = [...pathParts]
|
|
|
|
let route = root
|
2022-12-05 20:35:29 +05:00
|
|
|
let arr: PrivateMenuItem[] | undefined = items
|
2022-10-25 18:57:23 +05:00
|
|
|
while (arr && parts.length > 0) {
|
2022-12-05 20:35:29 +05:00
|
|
|
const child: PrivateMenuItem | undefined = arr.find(elm => elm.route.toLowerCase() === parts[0].toLowerCase())
|
2022-10-25 18:57:23 +05:00
|
|
|
if (!child) break
|
|
|
|
route = join(route, child.route)
|
|
|
|
out.push({ ...child, route })
|
|
|
|
parts.splice(0, 1)
|
|
|
|
arr = child.children
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2022-10-31 05:10:28 +05:00
|
|
|
export const makeMenuBreadcrumbItems = (
|
2022-12-05 20:35:29 +05:00
|
|
|
menuItems: PrivateMenuItem[],
|
2022-10-31 05:10:28 +05:00
|
|
|
path: string,
|
|
|
|
pathRoot: RegExp = /^\//,
|
2022-12-05 20:35:29 +05:00
|
|
|
itemsProps?: FunctionalValue<(item: PrivateMenuItem) => BreadcrumbItemProps>,
|
|
|
|
itemRender?: (item: PrivateMenuItem) => JSX.Element,
|
2022-10-31 05:10:28 +05:00
|
|
|
) => {
|
|
|
|
const getItemProps = getFunctionalValue(itemsProps)
|
2022-10-25 18:57:23 +05:00
|
|
|
|
2022-10-31 05:10:28 +05:00
|
|
|
const rootPart = pathRoot.exec(path)
|
|
|
|
if (!rootPart || rootPart.length <= 0) return []
|
|
|
|
const root = rootPart[0]
|
|
|
|
const parts = path.trim().slice(root.length).split('/')
|
|
|
|
const items = makeBreadcrumbItems(menuItems, parts, root)
|
2022-10-25 18:57:23 +05:00
|
|
|
|
2022-10-31 05:10:28 +05:00
|
|
|
return items.map((item) => (
|
|
|
|
<Breadcrumb.Item key={item.route} {...getItemProps(item)}>
|
|
|
|
{itemRender ? itemRender(item) : (
|
|
|
|
<Link to={item.route}>{item.title}</Link>
|
|
|
|
)}
|
|
|
|
</Breadcrumb.Item>
|
|
|
|
))
|
|
|
|
}
|