2021-12-24 18:17:02 +01:00
|
|
|
import get from 'lodash.get'
|
|
|
|
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'
|
2023-07-13 19:58:42 +02:00
|
|
|
import { BulletIcon, RightIcon } from 'shared/components/icons.mjs'
|
2023-07-14 09:23:37 +02:00
|
|
|
import { pageHasChildren } from 'shared/utils.mjs'
|
2023-07-17 11:40:45 -05:00
|
|
|
import orderBy from 'lodash.orderby'
|
2023-05-21 09:41:20 +02:00
|
|
|
|
2023-07-15 10:40:46 +02:00
|
|
|
export const getRoot = {
|
2023-05-21 09:58:59 +02:00
|
|
|
dev: (root, nav) => {
|
|
|
|
if (!root) return nav
|
|
|
|
if (root.indexOf('/') === -1) return nav[root]
|
|
|
|
return get(nav, root.split('/'))
|
2023-05-21 09:41:20 +02:00
|
|
|
},
|
2023-05-21 09:58:59 +02:00
|
|
|
org: (root, nav) => {
|
2023-05-21 09:41:20 +02:00
|
|
|
// Fixme: make this work for org
|
2023-05-21 09:58:59 +02:00
|
|
|
if (!root) return nav
|
2023-07-03 13:28:41 -05:00
|
|
|
if (root.indexOf('/') === -1) return get(nav, root)
|
2023-05-21 09:58:59 +02:00
|
|
|
return get(nav, root.split('/'))
|
2023-05-21 09:41:20 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-11 21:01:34 +02:00
|
|
|
/*
|
|
|
|
* This is a recursive function, so it needs to be lean
|
|
|
|
*/
|
2023-07-17 11:40:45 -05:00
|
|
|
const RenderTree = ({ tree, recurse, depth = 1, level = 0 }) => {
|
|
|
|
const orderedTree = orderBy(tree, ['o', 't'], ['asc', 'asc']).filter(
|
|
|
|
(item) => typeof item === 'object'
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="w-full list">
|
|
|
|
{orderedTree.map((item, i) => {
|
2023-07-13 19:58:42 +02:00
|
|
|
/*
|
|
|
|
* Does this have children?
|
|
|
|
*/
|
|
|
|
const hasChildren =
|
2023-07-17 11:40:45 -05:00
|
|
|
recurse && (!depth || level < depth) && pageHasChildren(item)
|
|
|
|
? item.s.replaceAll('/', '')
|
2023-07-13 19:58:42 +02:00
|
|
|
: false
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The rotation of the chevron should in principle be possible with Tailwind's group variant modifiers
|
|
|
|
* However, despite my best efforts, I can't seem to make it work. So this relies on a bit of CSS.
|
|
|
|
* The 'summary-chevron' class is what does the trick.
|
|
|
|
*/
|
|
|
|
return (
|
|
|
|
<li key={i} className="w-full flex flex-row items-start gap-0.5 lg:gap-1">
|
|
|
|
{hasChildren ? (
|
|
|
|
<details className={`w-full inline flex flex-row`}>
|
2023-10-12 11:30:52 +02:00
|
|
|
<summary className="hover:bg-opacity-20 bg-secondary bg-opacity-0 block w-full flex flex-row items-center gap-0.5 lg:gap-1 px-1 lg:px-2 py-1">
|
2023-07-13 19:58:42 +02:00
|
|
|
<RightIcon className={`w-4 h-4 summary-chevron transition-all`} stroke={3} />
|
2023-07-17 11:40:45 -05:00
|
|
|
<Link href={`/${item.s}`}>{item.t}</Link>
|
2023-07-13 19:58:42 +02:00
|
|
|
</summary>
|
2023-07-17 11:40:45 -05:00
|
|
|
<RenderTree tree={item} {...{ recurse, depth }} level={level + 1} />
|
2023-07-13 19:58:42 +02:00
|
|
|
</details>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<BulletIcon className="w-2 h-2 mt-2 mx-1 ml-2 lg:ml-3 shrink-0" fill stroke={0} />
|
2023-07-17 11:40:45 -05:00
|
|
|
<Link href={`/${item.s}`} className="break-all">
|
|
|
|
{item.t}
|
2023-07-13 19:58:42 +02:00
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
2023-07-17 11:40:45 -05:00
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
}
|
2023-07-11 21:01:34 +02:00
|
|
|
|
2023-07-21 10:27:15 +02:00
|
|
|
export const ReadMore = ({ recurse = 0, root = false, site = 'org', depth = 99 }) => {
|
2023-07-20 08:42:56 +02:00
|
|
|
const { siteNav, slug } = useContext(NavigationContext)
|
2023-04-16 16:19:49 +02:00
|
|
|
|
|
|
|
// Deal with recurse not being a number
|
2023-05-21 09:41:20 +02:00
|
|
|
if (recurse && recurse !== true) {
|
2023-04-16 16:19:49 +02:00
|
|
|
if (typeof recurse === 'number') recurse--
|
|
|
|
else recurse = 1
|
|
|
|
}
|
|
|
|
|
2023-05-21 09:41:20 +02:00
|
|
|
// Deal with root being passed as true
|
|
|
|
if (root === true) root = ''
|
2021-12-24 18:17:02 +01:00
|
|
|
|
2023-05-22 19:53:40 +02:00
|
|
|
const tree = root === false ? getRoot[site](slug, siteNav) : getRoot[site](root, siteNav)
|
2023-05-22 17:05:59 +02:00
|
|
|
|
2023-07-17 11:40:45 -05:00
|
|
|
if (!tree) return null
|
|
|
|
|
2023-07-13 18:18:07 +02:00
|
|
|
return <RenderTree {...{ tree, recurse, depth }} />
|
2021-12-24 18:17:02 +01:00
|
|
|
}
|