1
0
Fork 0
freesewing/sites/shared/components/mdx/read-more.mjs

35 lines
1 KiB
JavaScript
Raw Normal View History

2021-12-24 18:17:02 +01:00
import get from 'lodash.get'
import orderBy from 'lodash.orderby'
import Link from 'next/link'
2023-05-15 20:00:45 +02:00
import { useContext } from 'react'
import { NavigationContext } from 'shared/context/navigation-context.mjs'
2021-12-24 18:17:02 +01:00
// Helper method to filter out the real children
2023-04-16 16:19:49 +02:00
const order = (obj) => orderBy(obj, ['o', 't'], ['asc', 'asc'])
const currentChildren = (current) =>
Object.values(order(current)).filter((entry) => typeof entry === 'object')
2021-12-24 18:17:02 +01:00
2023-05-15 20:00:45 +02:00
export const ReadMore = ({ app, recurse = 0 }) => {
const { nav, slug } = useContext(NavigationContext)
2023-04-16 16:19:49 +02:00
// Deal with recurse not being a number
if (recurse) {
if (typeof recurse === 'number') recurse--
else recurse = 1
}
2023-05-15 20:00:45 +02:00
const root = slug && slug !== 'docs' ? get(nav, slug.split('/').slice(1)) : nav
2021-12-24 18:17:02 +01:00
const list = []
for (const page of currentChildren(root)) {
list.push(
2023-04-16 16:19:49 +02:00
<li key={page.s}>
<Link href={`/${page.s}`}>{page.t}</Link>
{recurse > 0 ? <ReadMore app={app} slug={page.s} recurse={recurse} /> : null}
</li>
)
2021-12-24 18:17:02 +01:00
}
2023-05-15 20:00:45 +02:00
2021-12-24 18:17:02 +01:00
return <ul>{list}</ul>
}