1
0
Fork 0
freesewing/sites/shared/components/mdx/prev-next.mjs

114 lines
3 KiB
JavaScript
Raw Normal View History

2021-12-25 13:43:41 +01:00
import get from 'lodash.get'
import orderBy from 'lodash.orderby'
import Link from 'next/link'
import { LeftIcon, RightIcon } from 'shared/components/icons.mjs'
2021-12-25 13:43:41 +01:00
// helper method to order nav entries
const order = (obj) => orderBy(obj, ['o', 't'], ['asc', 'asc'])
2021-12-25 13:43:41 +01:00
// Helper method to filter out page nodes
const nodesOnly = (current) =>
2022-10-04 19:25:57 +02:00
Object.values(order(current)).filter((entry) => typeof entry === 'object')
2021-12-25 13:43:41 +01:00
// Helper method to get the siblings
const siblings = (app) =>
app.state.slug ? nodesOnly(get(app.state.nav, app.state.slug.split('/').slice(1, -1))) : []
2021-12-25 13:43:41 +01:00
// Helper method to get the current parent
const currentParent = (app) =>
app.state.slug ? [get(app.state.nav, app.state.slug.split('/').slice(1, -1))] : []
// Helper method to get the next parent
const nextParent = (app) => {
if (app.state.slug)
return app.state.slug.split('/').length < 4
? nodesOnly(app.state.nav)
: nodesOnly(get(app.state.nav, app.state.slug.split('/').slice(1, -2)))
return []
}
2021-12-25 13:43:41 +01:00
// Helper method to get current node
const current = (app) => (app.state.slug ? get(app.state.nav, app.state.slug.split('/')) : null)
2021-12-25 13:43:41 +01:00
2022-10-04 19:25:57 +02:00
const previous = (app) => {
2021-12-25 13:43:41 +01:00
// Previous sibling (aside)
const aside = siblings(app)
2021-12-25 13:43:41 +01:00
if (aside.length > 0) {
let next = false
for (const node of aside.reverse()) {
if (next) return node
if (node?.s && node.s === app.state.slug) next = true
2021-12-25 13:43:41 +01:00
}
}
// Previous parent (up)
const up = currentParent(app)
if (up.length === 1) return up.pop()
2021-12-25 13:43:41 +01:00
return false
}
2022-10-04 19:25:57 +02:00
const next = (app) => {
2021-12-25 13:43:41 +01:00
// Next child (down)
const down = nodesOnly(current(app))
2021-12-25 13:43:41 +01:00
if (down.length > 0) return down[0]
// Next sibling (aside)
const aside = siblings(app)
2021-12-25 13:43:41 +01:00
if (aside.length > 0) {
let next = false
for (const node of aside) {
if (next) return node
if (node?.s && node.s === app.state.slug) next = true
2021-12-25 13:43:41 +01:00
}
}
// Next parent (up)
const up = nextParent(app)
2021-12-25 13:43:41 +01:00
if (up.length > 0) {
let next = false
for (const node of up) {
if (next) return node
if (node?.s && node.s === app.state.slug.slice(0, node.s.length)) next = true
2021-12-25 13:43:41 +01:00
}
}
return false
}
2022-10-04 19:25:57 +02:00
const renderPrevious = (node) =>
node ? (
<div className="flex flex-row gap-2 items-center">
<LeftIcon className="w-6 h-6 shrink-0" />
<Link href={'/' + node.s} className="text-secondary break-words">
{node.t}
</Link>
</div>
2022-10-04 19:25:57 +02:00
) : (
<span></span>
)
2021-12-25 13:43:41 +01:00
2022-10-04 19:25:57 +02:00
const renderNext = (node) =>
node ? (
<div className="flex flex-row gap-2 items-center justify-end">
<Link href={'/' + node.s} className="text-right break-words">
{node.t}
</Link>
<RightIcon className="w-6 h-6 shrink-0" />
</div>
2022-10-04 19:25:57 +02:00
) : (
<span></span>
)
2021-12-25 13:43:41 +01:00
export const PrevNext = ({ app }) => {
2021-12-25 13:43:41 +01:00
return (
<div className="grid grid-cols-2 gap-4 border-t mt-12 py-2">
2021-12-25 13:43:41 +01:00
{renderPrevious(previous(app))}
{renderNext(next(app))}
</div>
)
}
//<pre>{JSON.stringify(app.state.nav, null ,2)}</pre>
//<pre>{JSON.stringify(app.state.slug, null ,2)}</pre>