import { useState, useEffect } from 'react' import Link from 'next/link' import orderBy from 'lodash.orderby' import { FreeSewingIcon } from 'shared/components/icons.mjs' import { TopLevelNavigation as TopLevel } from './top-level.mjs' /* helper method to order nav entries */ const order = (obj) => orderBy(obj, ['__order', '__title'], ['asc', 'asc']) // Component for the collapse toggle // Exported for re-use export const Chevron = ({ w = 8, m = 2 }) => ( ) // Helper method to filter out the real children const currentChildren = (current) => Object.values(order(current)).filter((entry) => typeof entry === 'object') // Shared classes for links // Exported for re-use export const linkClasses = ` py-1 text-base-content sm:text-base-content hover:text-secondary sm:hover:text-secondary ` // Figure out whether a page is on the path to the active page const isActive = (slug, active) => { if (slug === active) return true let result = true const slugParts = slug.split('/') const activeParts = active.split('/') for (const i in slugParts) { if (slugParts[i] !== activeParts[i]) result = false } return result } // Component that renders a sublevel of navigation const SubLevel = ({ nodes = {}, active }) => ( ) const LevelHomeButton = ({ setShowLevel, level }) => ( <> ) const colors = ['primary', 'secondary', 'accent'] const LevelButton = ({ title, level, showLevel, setShowLevel, href = false }) => { const props = { className: `h-8 mb-1 flex flex-row p-0 items-center -ml-7 max-w-1/3 ${ showLevel < level ? 'opacity-50' : '' }`, } const content = ( <>
{title}
) return href ? ( {content} ) : ( ) } const Navigation = ({ app, active, className = '' }) => { // Levels const levels = active.split('/') useEffect(() => { setShowLevel(Math.max(levels.length, 2)) }, [active]) const [showLevel, setShowLevel] = useState(Math.max(levels.length, 2)) if (!app.navigation) return null if (levels.length < 1) return null let navigation = app.navigation const shared = { showLevel, setShowLevel } const levelButtons = [] if (levels[0]) { const title = app.navigation[levels[0].__title] || levels[0] navigation = app.navigation[levels[0]] levelButtons.push() levelButtons.push() } if (levels[1]) { const title = app.navigation[levels[0]][levels[1]]?.__title || levels[1] if (showLevel > 0) navigation = navigation[levels[1]] levelButtons.push() } if (levels[2] && levels.length > 3) { if (showLevel > 1) navigation = navigation[levels[2]] levelButtons.push( ) } const output = [
{levelButtons}
, ] if (showLevel < 0) output.push() else output.push() return
{output}
} export const PrimaryNavigation = ({ app, active, before = [], after = [] }) => ( )