import { useEffect, useMemo, useState } from 'react' import { NavLink, Outlet, useLocation, useNavigate, useParams } from 'react-router-dom' import { clearToken } from '../api/client' import type { LaneKey } from '../pages/LaneHub' type NavLeaf = { kind?: 'link'; to: string; end?: boolean; ico: string; label: string } type NavBranch = { kind: 'branch' id: string ico: string label: string match: string[] defaultTo: string children: NavLeaf[] } type NavItem = NavLeaf | NavBranch const LANE_META: Record = { cn: { title: '中文用户', sub: 'Chinese · 特殊对待', mark: '中' }, intl: { title: '国际用户', sub: 'International · English', mark: 'EN' }, } function laneNav(lane: LaneKey): NavItem[] { const p = `/${lane}` return [ { to: p, end: true, ico: '◈', label: '模块总览' }, { to: `${p}/switches`, ico: '⏻', label: '动态开关' }, { kind: 'branch', id: 'social', ico: '💕', label: '游民社区', match: [`${p}/social`], defaultTo: `${p}/social/overview`, children: [ { to: `${p}/social/overview`, end: true, ico: '◈', label: '数据概览' }, { to: `${p}/social/roster`, ico: '👤', label: '花名册' }, { to: `${p}/social/messages`, ico: '💬', label: '聊天' }, { to: `${p}/social/reviews`, ico: '☕', label: '咖啡馆评价' }, { to: `${p}/social/gigs`, ico: '🛠️', label: '接单申请' }, { to: `${p}/social/feedback`, ico: '📩', label: '反馈' }, { to: `${p}/social/archives`, ico: '🗄️', label: '存档' }, { to: `${p}/social/catalog`, ico: '📁', label: '目录配置' }, ], }, { to: `${p}/blog`, ico: '📝', label: 'Nomad Blog' }, { to: `${p}/events`, ico: '📊', label: '埋点' }, { to: `${p}/orders`, ico: '🧾', label: '订单' }, { to: `${p}/users`, ico: '👥', label: '用户' }, { to: `${p}/ntfy`, ico: '🔔', label: 'ntfy' }, { to: `${p}/docs`, ico: '✓', label: '部署说明' }, ] } function flattenLeaves(items: NavItem[]): NavLeaf[] { const out: NavLeaf[] = [] for (const item of items) { if (item.kind === 'branch') out.push(...item.children) else out.push(item) } return out } function titleFromPath(lane: LaneKey, pathname: string) { const leaves = flattenLeaves(laneNav(lane)) const sorted = [...leaves].sort((a, b) => b.to.length - a.to.length) const hit = sorted.find((n) => (n.end ? pathname === n.to : pathname.startsWith(n.to))) const mod = LANE_META[lane].title return hit && !(hit.end && hit.to === `/${lane}`) ? `${mod} · ${hit.label}` : mod } function isBranchActive(branch: NavBranch, pathname: string) { return branch.match.some((m) => pathname === m || pathname.startsWith(`${m}/`)) } /** 已选模块后的壳:一级 / 二级侧栏;切模块回选择页 */ export default function WebLayout() { const nav = useNavigate() const { pathname } = useLocation() const { lane: laneParam } = useParams() const lane: LaneKey = laneParam === 'cn' ? 'cn' : 'intl' const meta = LANE_META[lane] const items = useMemo(() => laneNav(lane), [lane]) const [open, setOpen] = useState(false) const socialBranch = items.find((x) => x.kind === 'branch' && x.id === 'social') as | NavBranch | undefined const socialActive = socialBranch ? isBranchActive(socialBranch, pathname) : false const [socialOpen, setSocialOpen] = useState(() => socialActive) useEffect(() => { setOpen(false) }, [pathname]) useEffect(() => { if (socialActive) setSocialOpen(true) }, [socialActive]) useEffect(() => { document.body.style.overflow = open ? 'hidden' : '' return () => { document.body.style.overflow = '' } }, [open]) function logout() { clearToken() nav('/login') } function toggleSocial() { if (!socialBranch) return setSocialOpen((v) => { const next = !v if (next && !socialActive) nav(socialBranch.defaultTo) return next }) } return (
{meta.mark}
{meta.title}
{titleFromPath(lane, pathname)}
{open && (
) }