nomadro/apps/com/admin/src/components/WebLayout.tsx
Eric b06a14b6aa Ship social admin nested menus with list/detail CRUD, soft-archive UGC, and PocketBase-ready stores.
admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 16:41:43 +08:00

238 lines
8.0 KiB
TypeScript

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<LaneKey, { title: string; sub: string; mark: string }> = {
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 (
<div className={`com-shell com-shell--${lane}${open ? ' com-shell--nav-open' : ''}`}>
<header className="com-topbar">
<button
type="button"
className="com-topbar-menu"
aria-label={open ? '关闭菜单' : '打开菜单'}
aria-expanded={open}
onClick={() => setOpen((v) => !v)}
>
<span />
<span />
<span />
</button>
<div className="com-topbar-brand">
<span className="com-topbar-mark">{meta.mark}</span>
<div>
<div className="com-topbar-title">{meta.title}</div>
<div className="com-topbar-sub">{titleFromPath(lane, pathname)}</div>
</div>
</div>
<button type="button" className="com-topbar-action" onClick={() => nav('/')}>
换模块
</button>
</header>
{open && (
<button type="button" className="com-nav-scrim" aria-label="关闭菜单" onClick={() => setOpen(false)} />
)}
<aside className="com-side" aria-hidden={!open}>
<div className="com-logo">
<span className="com-mark">{meta.mark}</span>
<div>
<div className="com-title">{meta.title}</div>
<div className="com-sub">{meta.sub}</div>
</div>
</div>
<button type="button" className="com-pill com-pill-btn" onClick={() => nav('/')}>
<span className="com-pill-dot" />
切换模块 · 中文 / 国际
</button>
<nav className="com-nav" aria-label="后台导航">
{items.map((item) => {
if (item.kind === 'branch') {
const branchOpen = item.id === 'social' ? socialOpen : false
const active = isBranchActive(item, pathname)
return (
<div
key={item.id}
className={`com-nav-branch${branchOpen ? ' is-open' : ''}${active ? ' is-active' : ''}`}
>
<button
type="button"
className="com-nav-parent"
aria-expanded={branchOpen}
onClick={toggleSocial}
>
<span className="com-nav-ico" aria-hidden>
{item.ico}
</span>
<span className="com-nav-parent-label">{item.label}</span>
<span className="com-nav-caret" aria-hidden>
{branchOpen ? '▾' : '▸'}
</span>
</button>
{branchOpen ? (
<div className="com-nav-sub" role="group" aria-label={`${item.label}子菜单`}>
{item.children.map((child) => (
<NavLink
key={child.to}
to={child.to}
end={child.end}
className={({ isActive }) =>
`com-nav-sublink${isActive ? ' active' : ''}`
}
>
<span className="com-nav-ico" aria-hidden>
{child.ico}
</span>
{child.label}
</NavLink>
))}
</div>
) : null}
</div>
)
}
return (
<NavLink
key={item.to}
to={item.to}
end={item.end}
className={({ isActive }) => (isActive ? 'active' : '')}
>
<span className="com-nav-ico" aria-hidden>
{item.ico}
</span>
{item.label}
</NavLink>
)
})}
</nav>
<button type="button" className="com-logout" onClick={logout}>
退出登录
</button>
</aside>
<main className="com-main">
<Outlet />
</main>
</div>
)
}