2023-07-15 11:00:18 +02:00
|
|
|
import Link from 'next/link'
|
2023-07-16 07:27:51 +02:00
|
|
|
import { pageHasChildren, isSlugPart } from 'shared/utils.mjs'
|
2023-07-15 11:00:18 +02:00
|
|
|
import get from 'lodash.get'
|
|
|
|
import { HomeIcon, RightIcon, BulletIcon } from 'shared/components/icons.mjs'
|
|
|
|
import { PageLink } from 'shared/components/page-link.mjs'
|
|
|
|
import orderBy from 'lodash.orderby'
|
|
|
|
import { icons } from 'shared/components/navigation/primary.mjs'
|
2023-07-16 09:51:06 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-07-15 11:00:18 +02:00
|
|
|
|
2023-07-17 11:40:45 -05:00
|
|
|
export const ns = ['sections']
|
2023-07-15 11:00:18 +02:00
|
|
|
/*
|
|
|
|
* This returns only those children that are expected to show up
|
|
|
|
* in the side navigation. Specifically:
|
|
|
|
* - Key length needs to be longer than 1
|
|
|
|
* - Child pages cannot have m or h set (main section or hidden)
|
|
|
|
* - Title may not be 'spacer' (header spacer)
|
|
|
|
*
|
|
|
|
* It also takes care of ordering them, and returns an array
|
|
|
|
*
|
|
|
|
* @params tree {object} - A navigation object as returned by useNavigation => siteNav
|
|
|
|
*/
|
2023-07-16 07:36:13 +02:00
|
|
|
const onlyValidChildren = (tree) =>
|
2023-07-15 11:00:18 +02:00
|
|
|
orderBy(tree, ['o', 't'], ['asc', 'asc']).filter(
|
2023-07-16 09:51:06 +02:00
|
|
|
(entry) => typeof entry === 'object' && entry.t !== 'spacer' && !entry.m && !entry._ && !entry.h
|
2023-07-15 11:00:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This returns only those children that are main sections. Specifically:
|
|
|
|
* - Key length needs to be longer than 1
|
|
|
|
* - Child pages cannot have m set (main section)
|
|
|
|
* - Title may not be 'spacer' (header spacer)
|
|
|
|
*
|
|
|
|
* It also takes care of ordering them, and returns an array
|
|
|
|
*
|
|
|
|
* @params tree {object} - A navigation object as returned by useNavigation => siteNav
|
|
|
|
*/
|
|
|
|
const onlyMainSections = (tree) =>
|
|
|
|
orderBy(tree, ['o', 't'], ['asc', 'asc']).filter((entry) => entry.m)
|
|
|
|
|
|
|
|
const SectionLink = ({ skey, tree, slug }) =>
|
2023-07-15 16:55:22 +02:00
|
|
|
tree[skey]._ ? null : tree[skey].s === slug ? ( // Underscore means always hide
|
2023-07-15 11:00:18 +02:00
|
|
|
<>
|
|
|
|
<span className="pl-2 border-l-2 py-2 block w-full border-secondary bg-opacity-10">
|
2023-07-15 11:54:32 +02:00
|
|
|
{tree[skey].t}
|
2023-07-15 11:00:18 +02:00
|
|
|
</span>
|
|
|
|
{pageHasChildren(tree[skey]) && <Section tree={tree[skey]} slug={slug} />}
|
|
|
|
</>
|
|
|
|
) : isSlugPart(tree[skey].s, slug) ? (
|
|
|
|
<>
|
|
|
|
<Link
|
|
|
|
href={`/${tree[skey].s}`}
|
|
|
|
className="pl-2 border-l-2 py-2 block w-full hover:border-secondary hover:bg-secondary hover:bg-opacity-10"
|
|
|
|
>
|
2023-07-15 11:54:32 +02:00
|
|
|
{tree[skey].t}
|
2023-07-15 11:00:18 +02:00
|
|
|
</Link>
|
|
|
|
{pageHasChildren(tree[skey]) && <Section tree={tree[skey]} slug={slug} />}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<Link
|
|
|
|
href={`/${tree[skey].s}`}
|
|
|
|
className="pl-2 border-l-2 py-2 block w-full hover:border-secondary hover:bg-secondary hover:bg-opacity-10"
|
|
|
|
>
|
2023-07-15 11:54:32 +02:00
|
|
|
{tree[skey].t}
|
2023-07-15 11:00:18 +02:00
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A React component to render a section of the navigation
|
|
|
|
*
|
|
|
|
* @param t {string} - The section title
|
|
|
|
* @param s {string} - The section slug
|
|
|
|
* @param tree {object} - The object describing any futher child pages
|
|
|
|
* @param slug {string} - The slug of the currently active/viewed page
|
|
|
|
*/
|
|
|
|
const Section = ({
|
|
|
|
tree, // Object with the navigation
|
|
|
|
slug, // Slug of the current page (used to make links active)
|
|
|
|
}) => (
|
|
|
|
<ul className="ml-4">
|
|
|
|
{onlyValidChildren(tree).map((page, i) => (
|
|
|
|
<li key={i}>
|
|
|
|
{slug === page.s ? (
|
|
|
|
<>
|
|
|
|
<span
|
|
|
|
className={
|
|
|
|
'pl-2 font-medium border-l-2 py-2 block w-full border-secondary ' +
|
|
|
|
'bg-secondary bg-opacity-10'
|
|
|
|
}
|
|
|
|
>
|
2023-07-15 11:54:32 +02:00
|
|
|
{page.t}
|
2023-07-15 11:00:18 +02:00
|
|
|
</span>
|
|
|
|
{pageHasChildren(page) && <Section tree={page} slug={slug} />}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<SectionLink {...{ skey: page.s.split('/').pop(), tree, slug }} />
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A React component to render a main link navigation
|
|
|
|
*
|
|
|
|
* @param t {string} - The section title
|
|
|
|
* @param s {string} - The section slug
|
|
|
|
*/
|
|
|
|
const MainLink = ({
|
|
|
|
t, // The link title/text
|
|
|
|
s, // The link slug
|
|
|
|
slug, // The current page slug
|
|
|
|
}) => {
|
|
|
|
const classes =
|
|
|
|
'' +
|
2023-07-15 11:54:32 +02:00
|
|
|
'break-all py-2 px-2 block w-full font-bold text-lg ' +
|
2023-07-15 11:00:18 +02:00
|
|
|
'flex flex-row items-start gap-0.5 lg:gap-1 border-l-2'
|
|
|
|
|
|
|
|
return s === slug ? (
|
2023-07-15 11:54:32 +02:00
|
|
|
<span className={`${classes} border-secondary bg-secondary bg-opacity-10`}>{t}</span>
|
2023-07-15 11:00:18 +02:00
|
|
|
) : (
|
|
|
|
<Link
|
|
|
|
href={`/${s}`}
|
|
|
|
className={`${classes} border-transparent hover:border-secondary hover:bg-secondary hover:bg-opacity-10`}
|
|
|
|
>
|
2023-07-15 11:54:32 +02:00
|
|
|
{t}
|
2023-07-15 11:00:18 +02:00
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A React component to render breadcrumbs to the current page
|
|
|
|
*
|
|
|
|
* @param slug {string} - The slug of the current page
|
|
|
|
* @param siteNav {object} - The site navigation object as returned by the useNavigation hook
|
|
|
|
*/
|
2023-07-16 09:51:06 +02:00
|
|
|
export const Breadcrumbs = ({ slug = false, lead = false, siteNav }) => {
|
|
|
|
const { t } = useTranslation(['common'])
|
|
|
|
|
|
|
|
if (slug === false) {
|
2023-07-16 08:25:08 +02:00
|
|
|
console.log('No slug passed to Breadcrumbs')
|
|
|
|
return null
|
|
|
|
}
|
2023-07-15 11:00:18 +02:00
|
|
|
// Start with the home crumb
|
2023-07-16 09:51:06 +02:00
|
|
|
const crumbs = []
|
|
|
|
// Do we need a lead?
|
|
|
|
if (lead)
|
|
|
|
crumbs.push(
|
|
|
|
<li key="lead" className="font-medium text-sm pr-2">
|
|
|
|
{t('youAreHere')}:
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
crumbs.push(
|
2023-07-15 11:00:18 +02:00
|
|
|
<li className="inline" key={0}>
|
|
|
|
<Link href="/" title="FreeSewing">
|
2023-07-16 09:51:06 +02:00
|
|
|
<HomeIcon className="w-4 h-4" stroke={2.5} />
|
2023-07-15 11:00:18 +02:00
|
|
|
</Link>
|
2023-07-16 09:51:06 +02:00
|
|
|
</li>
|
|
|
|
)
|
|
|
|
|
|
|
|
// Home page?
|
|
|
|
if (slug === '') return <ul className="flex flex-row flex-wrap items-center">{crumbs}</ul>
|
|
|
|
|
2023-07-15 11:00:18 +02:00
|
|
|
// Then split the slug and add a crumb for each
|
|
|
|
const chunks = slug.split('/')
|
|
|
|
for (let i = 1; i <= chunks.length; i++) {
|
|
|
|
const page = get(siteNav, chunks.slice(0, i))
|
2023-07-16 08:25:08 +02:00
|
|
|
if (page) {
|
|
|
|
crumbs.push(
|
|
|
|
<li className="pl-1" key={`${i}s`}>
|
|
|
|
<RightIcon className="w-4 h-4 opacity-50" stroke={3} />
|
|
|
|
</li>,
|
|
|
|
i === chunks.length ? (
|
|
|
|
<li className="pl-1" key={i}>
|
|
|
|
<span className="font-medium">{page.t}</span>
|
|
|
|
</li>
|
|
|
|
) : (
|
|
|
|
<li key={i}>
|
|
|
|
<PageLink
|
|
|
|
href={`/${page.s}`}
|
|
|
|
title={page.t}
|
2023-07-16 17:15:10 +02:00
|
|
|
className="font-medium pl-1"
|
2023-07-16 08:25:08 +02:00
|
|
|
txt={page.t}
|
|
|
|
/>
|
|
|
|
</li>
|
|
|
|
)
|
2023-07-15 11:00:18 +02:00
|
|
|
)
|
2023-07-16 08:25:08 +02:00
|
|
|
}
|
2023-07-15 11:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return <ul className="flex flex-row flex-wrap items-center">{crumbs}</ul>
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A React component to render sidebar navigation based on the siteNav object and current slug
|
|
|
|
*
|
2023-07-15 11:54:32 +02:00
|
|
|
* The main sections are determined in the use-navigation hook.
|
|
|
|
* We always display the navigation as:
|
|
|
|
* - Always show all top-level entries
|
|
|
|
* - Always show all direct children of all top-level entries (this allows for better discoverability)
|
|
|
|
* - If we're deeper down, only expand the active page
|
|
|
|
*
|
2023-07-15 11:00:18 +02:00
|
|
|
* @param slug {string} - The slug of the current page
|
|
|
|
* @param siteNav {object} - The siteNav object from the useNavigation hook
|
|
|
|
*/
|
2023-07-16 07:39:45 +02:00
|
|
|
export const NavLinks = ({ slug, siteNav }) => (
|
2023-07-15 11:54:32 +02:00
|
|
|
<ul className="w-full list mb-8 mt-3">
|
2023-07-16 07:39:45 +02:00
|
|
|
{onlyValidChildren(siteNav).map((page, i) => (
|
|
|
|
<li key={i} className="w-full">
|
|
|
|
<MainLink s={page.s} t={page.t} slug={slug} />
|
|
|
|
{pageHasChildren(page) && !page.n && <Section {...{ tree: page, slug }} />}
|
|
|
|
</li>
|
|
|
|
))}
|
2023-07-15 11:54:32 +02:00
|
|
|
</ul>
|
|
|
|
)
|
2023-07-15 11:00:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A React component to render sidebar navigation for the main sections
|
|
|
|
*
|
|
|
|
* @param siteNav {object} - The siteNav object from the useNavigation hook
|
|
|
|
* @param slug {string} - The slug of the current page
|
|
|
|
*/
|
|
|
|
export const MainSections = ({ siteNav, slug }) => {
|
|
|
|
const output = []
|
|
|
|
for (const page of onlyMainSections(siteNav)) {
|
|
|
|
const act = isSlugPart(page.s, slug)
|
|
|
|
const txt = (
|
|
|
|
<>
|
|
|
|
{icons[page.s] ? (
|
|
|
|
icons[page.s](`w-6 h-6 ${act ? 'text-base-100 opacity-70' : ''}`)
|
|
|
|
) : (
|
|
|
|
<BulletIcon fill={act} className={`w-6 h-6 ${act ? 'text-base-100 opacity-70' : ''}`} />
|
|
|
|
)}
|
|
|
|
<span className={`font-bold ${act ? 'text-secondary-content' : ''}`}>{page.t}</span>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
2023-07-16 07:36:13 +02:00
|
|
|
const item = (
|
|
|
|
<li key={page.s}>
|
|
|
|
{act ? (
|
|
|
|
<span
|
|
|
|
title={page.t}
|
|
|
|
className={`flex flex-row gap-4 items-center text-secondary-content
|
|
|
|
hover:text-base-content bg-secondary p-2 px-4 rounded bg-base-200 rounded-none`}
|
|
|
|
>
|
|
|
|
{txt}
|
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<Link
|
|
|
|
href={`/${page.s}`}
|
|
|
|
title={page.t}
|
|
|
|
className={`
|
|
|
|
flex flex-row gap-4 items-center hover:bg-secondary hover:bg-opacity-25
|
|
|
|
hover:cursor-pointer p-2 px-4 rounded rounded-none`}
|
|
|
|
>
|
|
|
|
{txt}
|
|
|
|
</Link>
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
)
|
2023-07-15 11:00:18 +02:00
|
|
|
output.push(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return <ul>{output}</ul>
|
|
|
|
}
|