|
| 1 | +'use client' |
| 2 | + |
| 3 | +/** |
| 4 | + * MarketingNav — the ONE marketing bar the Hanzo properties render. |
| 5 | + * |
| 6 | + * It replaces five hand-maintained copies of the same 160-line `DesktopNav.tsx` |
| 7 | + * (hanzo.id, hanzo.network, hanzo.one, sensei.group, hanzo.app). Two of those were |
| 8 | + * byte-identical and two differed only in an accent colour, so the variation this |
| 9 | + * component actually needs is: the menus (data), the accent, and how the host |
| 10 | + * navigates. |
| 11 | + * |
| 12 | + * ROUTER-AGNOSTIC by construction. The copies hard-imported `react-router-dom`'s |
| 13 | + * `<Link to>`, which is why hanzo.ai — a Next app using `<Link href>` — could never |
| 14 | + * share them and grew a sixth nav instead. Here the host passes `link`, so a Vite |
| 15 | + * SPA, a Next app and a plain-anchor page all use the same component. External |
| 16 | + * destinations bypass it and render a guarded anchor, because a client router |
| 17 | + * cannot navigate off-site. |
| 18 | + * |
| 19 | + * This lives in the v5/Tailwind lane beside `hanzo-shell` because that is the lane |
| 20 | + * these properties are on. `hanzo-shell` is the SIGNED-IN app chrome (billing, |
| 21 | + * account, console, chat, platform); this is the signed-out marketing bar. Same |
| 22 | + * repo, different job — not a second implementation of either. |
| 23 | + */ |
| 24 | +import * as React from 'react' |
| 25 | + |
| 26 | +import { HANZO_MARKETING_MENUS, isMenu, type MarketingMenus, type NavLink } from './menus' |
| 27 | + |
| 28 | +/** How the host navigates internally — `next/link`, a router `Link`, or an anchor. */ |
| 29 | +export type LinkRender = (props: { |
| 30 | + href: string |
| 31 | + className?: string |
| 32 | + onClick?: () => void |
| 33 | + children: React.ReactNode |
| 34 | +}) => React.ReactNode |
| 35 | + |
| 36 | +const anchorLink: LinkRender = ({ href, className, onClick, children }) => ( |
| 37 | + <a href={href} className={className} onClick={onClick}> |
| 38 | + {children} |
| 39 | + </a> |
| 40 | +) |
| 41 | + |
| 42 | +export interface MarketingNavProps { |
| 43 | + /** Menus to render. Defaults to the shared Hanzo bar. */ |
| 44 | + menus?: MarketingMenus |
| 45 | + /** Host navigation primitive. Defaults to a plain anchor. */ |
| 46 | + link?: LinkRender |
| 47 | + /** |
| 48 | + * Brand accent, as a Tailwind colour STEM (`neutral`, `purple`, …). It is the |
| 49 | + * only thing that differed between two of the copies. Interpolating a stem into |
| 50 | + * a class name is safe here because the set is closed and declared in `safelist` |
| 51 | + * — never accept an arbitrary string from user input. |
| 52 | + */ |
| 53 | + accent?: string |
| 54 | + className?: string |
| 55 | +} |
| 56 | + |
| 57 | +/** One destination. External links never go through the host router. */ |
| 58 | +function Item({ |
| 59 | + link, |
| 60 | + item, |
| 61 | + accent, |
| 62 | + close, |
| 63 | +}: { |
| 64 | + link: LinkRender |
| 65 | + item: NavLink |
| 66 | + accent: string |
| 67 | + close: () => void |
| 68 | +}) { |
| 69 | + const plain = 'text-sm text-neutral-300 hover:text-white transition-colors' |
| 70 | + const body = item.note ? ( |
| 71 | + <span className="group flex items-start gap-2"> |
| 72 | + {item.glyph ? <span className={`text-${accent}-400 text-lg`}>{item.glyph}</span> : null} |
| 73 | + <span> |
| 74 | + <span className={`text-sm text-white font-medium group-hover:text-${accent}-400 transition-colors`}> |
| 75 | + {item.label} |
| 76 | + </span> |
| 77 | + <p className="text-xs text-neutral-500">{item.note}</p> |
| 78 | + </span> |
| 79 | + </span> |
| 80 | + ) : ( |
| 81 | + item.label |
| 82 | + ) |
| 83 | + |
| 84 | + if (item.external) { |
| 85 | + return ( |
| 86 | + <a href={item.href} target="_blank" rel="noopener noreferrer" className={plain}> |
| 87 | + {body} |
| 88 | + </a> |
| 89 | + ) |
| 90 | + } |
| 91 | + return <>{link({ href: item.href, className: plain, onClick: close, children: body })}</> |
| 92 | +} |
| 93 | + |
| 94 | +/** A dropdown, opened on click and dismissed on Escape or an outside click. */ |
| 95 | +function Menu({ |
| 96 | + label, |
| 97 | + columns, |
| 98 | + link, |
| 99 | + accent, |
| 100 | +}: { |
| 101 | + label: string |
| 102 | + columns: { title?: string; links: NavLink[] }[] |
| 103 | + link: LinkRender |
| 104 | + accent: string |
| 105 | +}) { |
| 106 | + const [open, setOpen] = React.useState(false) |
| 107 | + const root = React.useRef<HTMLDivElement | null>(null) |
| 108 | + const close = () => setOpen(false) |
| 109 | + |
| 110 | + React.useEffect(() => { |
| 111 | + if (!open) return |
| 112 | + const onKey = (e: KeyboardEvent) => e.key === 'Escape' && close() |
| 113 | + const onDown = (e: MouseEvent) => { |
| 114 | + if (root.current && !root.current.contains(e.target as Node)) close() |
| 115 | + } |
| 116 | + document.addEventListener('keydown', onKey) |
| 117 | + document.addEventListener('mousedown', onDown) |
| 118 | + return () => { |
| 119 | + document.removeEventListener('keydown', onKey) |
| 120 | + document.removeEventListener('mousedown', onDown) |
| 121 | + } |
| 122 | + }, [open]) |
| 123 | + |
| 124 | + return ( |
| 125 | + <div ref={root} className="relative"> |
| 126 | + <button |
| 127 | + type="button" |
| 128 | + onClick={() => setOpen(!open)} |
| 129 | + aria-expanded={open} |
| 130 | + className="text-neutral-400 hover:text-white transition-colors text-sm font-medium" |
| 131 | + > |
| 132 | + {label} |
| 133 | + </button> |
| 134 | + {open ? ( |
| 135 | + <div className="absolute left-0 top-full mt-3 z-50 rounded-xl border border-neutral-700/50 bg-neutral-900 p-6 shadow-xl"> |
| 136 | + <div className="flex gap-8"> |
| 137 | + {columns.map((col, i) => ( |
| 138 | + <div key={col.title ?? i} className="min-w-[11rem]"> |
| 139 | + {col.title ? ( |
| 140 | + <h3 className="text-neutral-500 text-xs font-medium mb-3 uppercase tracking-wider"> |
| 141 | + {col.title} |
| 142 | + </h3> |
| 143 | + ) : null} |
| 144 | + <ul className="space-y-2"> |
| 145 | + {col.links.map((l) => ( |
| 146 | + <li key={l.href + l.label}> |
| 147 | + <Item link={link} item={l} accent={accent} close={close} /> |
| 148 | + </li> |
| 149 | + ))} |
| 150 | + </ul> |
| 151 | + </div> |
| 152 | + ))} |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + ) : null} |
| 156 | + </div> |
| 157 | + ) |
| 158 | +} |
| 159 | + |
| 160 | +export function MarketingNav({ |
| 161 | + menus = HANZO_MARKETING_MENUS, |
| 162 | + link = anchorLink, |
| 163 | + accent = 'neutral', |
| 164 | + className, |
| 165 | +}: MarketingNavProps) { |
| 166 | + return ( |
| 167 | + <div className={className ?? 'hidden md:flex items-center space-x-6'}> |
| 168 | + {menus.map((item) => |
| 169 | + isMenu(item) ? ( |
| 170 | + <Menu key={item.label} label={item.label} columns={item.columns} link={link} accent={accent} /> |
| 171 | + ) : ( |
| 172 | + <React.Fragment key={item.href}> |
| 173 | + {link({ |
| 174 | + href: item.href, |
| 175 | + className: 'text-neutral-400 hover:text-white transition-colors text-sm font-medium', |
| 176 | + children: item.label, |
| 177 | + })} |
| 178 | + </React.Fragment> |
| 179 | + ), |
| 180 | + )} |
| 181 | + </div> |
| 182 | + ) |
| 183 | +} |
| 184 | + |
| 185 | +export default MarketingNav |
0 commit comments