nomadro/src/modules/social/components/SiteShell.tsx
Eric efb408162d Deepen /social UX: people profiles, city directory, date deck, and plain-language IA.
Hide membership entry, brand ebook/video as 共创, harden H5 touch layout, and align copy with everyday nomad language.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-01 21:25:37 +08:00

328 lines
9.1 KiB
TypeScript

"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { MessageCircle, Moon, Sun } from "lucide-react";
import {
useEffect,
useId,
useRef,
useState,
useSyncExternalStore,
type ReactNode,
} from "react";
import { socialPath } from "@social/lib/socialPath";
import { applySocialSkin, SOCIAL_SKIN_DEFAULT } from "@social/lib/skin";
import { useRevealMotion } from "@social/lib/useRevealMotion";
import { SocialMobileDock } from "./SocialNavigation";
import SocialQuickRail from "./SocialQuickRail";
import SocialBrandLogo from "./SocialBrandLogo";
function readTheme(): "light" | "dark" {
try {
return (localStorage.getItem("nomadro-theme") ||
localStorage.getItem("nomad-theme")) === "light"
? "light"
: "dark";
} catch {
return "dark";
}
}
function subscribeTheme(callback: () => void) {
window.addEventListener("storage", callback);
window.addEventListener("nomadro-theme-changed", callback);
return () => {
window.removeEventListener("storage", callback);
window.removeEventListener("nomadro-theme-changed", callback);
};
}
const serverTheme = () => "dark" as const;
const GROUPS = [
{
id: "online",
label: "认识人",
items: [
{ href: "/people", label: "想见的人" },
{ href: "/meetup", label: "开视频聊聊" },
{ href: "/chat", label: "大家聊天" },
{ href: "/date", label: "认识一下" },
],
},
{
id: "offline",
label: "见一面",
items: [
{ href: "/coffeechat", label: "找城市见面" },
{ href: "/roster", label: "我的介绍" },
],
},
{
id: "together",
label: "一起做事",
items: [
{ href: "/team", label: "找搭子做事" },
{ href: "/ebook", label: "共创" },
{ href: "/feedback", label: "说说想法" },
],
},
];
export default function SiteShell({
children,
title,
home = false,
onJoin,
}: {
children: ReactNode;
title?: string;
home?: boolean;
onJoin?: () => void;
}) {
const theme = useSyncExternalStore(subscribeTheme, readTheme, serverTheme);
const [openGroup, setOpenGroup] = useState<string | null>(null);
const [menuOpen, setMenuOpen] = useState(false);
const [scrolled, setScrolled] = useState(false);
const pathname = usePathname();
const menuId = useId();
const menuButtonRef = useRef<HTMLButtonElement>(null);
const navRef = useRef<HTMLElement>(null);
useEffect(() => {
applySocialSkin(SOCIAL_SKIN_DEFAULT);
}, []);
useRevealMotion(home ? ".social-home" : ".social-workspace", [
home,
pathname,
]);
useEffect(() => {
document.documentElement.setAttribute("data-theme", theme);
document.documentElement.classList.toggle("light", theme === "light");
document.documentElement.classList.toggle("dark", theme === "dark");
}, [theme]);
useEffect(() => {
const onScroll = () => setScrolled(window.scrollY > 24);
onScroll();
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
useEffect(() => {
if (!menuOpen && !openGroup) return;
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") {
setMenuOpen(false);
setOpenGroup(null);
if (menuOpen) menuButtonRef.current?.focus();
else
navRef.current
?.querySelector<HTMLButtonElement>('[aria-expanded="true"]')
?.focus();
}
};
const onOutside = (event: PointerEvent) => {
if (!navRef.current?.contains(event.target as Node)) {
setOpenGroup(null);
setMenuOpen(false);
}
};
window.addEventListener("keydown", onKey);
window.addEventListener("pointerdown", onOutside);
return () => {
window.removeEventListener("keydown", onKey);
window.removeEventListener("pointerdown", onOutside);
};
}, [menuOpen, openGroup]);
useEffect(() => {
if (!menuOpen) return;
const previousOverflow = document.body.style.overflow;
document.body.style.overflow = "hidden";
return () => {
document.body.style.overflow = previousOverflow;
};
}, [menuOpen]);
const toggleTheme = () => {
const next = theme === "dark" ? "light" : "dark";
try {
localStorage.setItem("nomadro-theme", next);
} catch {
/* private mode */
}
document.documentElement.setAttribute("data-theme", next);
document.documentElement.classList.toggle("light", next === "light");
document.documentElement.classList.toggle("dark", next === "dark");
window.dispatchEvent(new Event("nomadro-theme-changed"));
};
const closeMenus = () => {
setMenuOpen(false);
setOpenGroup(null);
};
const joinBtn = onJoin ? (
<button
type="button"
className="social-btn coral social-nav-join"
onClick={() => {
closeMenus();
onJoin();
}}
>
加入社区
</button>
) : (
<Link
href={socialPath("/join")}
className="social-btn coral social-nav-join"
onClick={closeMenus}
>
加入社区
</Link>
);
const nav = (
<nav
ref={navRef}
className={`navbar${scrolled ? " scrolled" : ""}`}
aria-label="游民社区"
>
<div className="nav-container">
<SocialBrandLogo onClick={closeMenus} />
<div id={menuId} className={`nav-links${menuOpen ? " open" : ""}`}>
{GROUPS.map((g) => (
<div
key={g.id}
className={`nav-group${openGroup === g.id ? " open" : ""}${g.items.some((item) => pathname.startsWith(socialPath(item.href))) ? " is-active" : ""}`}
onMouseEnter={() => {
if (window.matchMedia("(min-width: 901px)").matches)
setOpenGroup(g.id);
}}
onMouseLeave={(event) => {
if (
window.matchMedia("(min-width: 901px)").matches &&
!event.currentTarget.contains(document.activeElement)
)
setOpenGroup(null);
}}
>
<button
type="button"
className="nav-group-btn"
aria-expanded={openGroup === g.id}
aria-controls={`${menuId}-${g.id}`}
onClick={() => setOpenGroup(openGroup === g.id ? null : g.id)}
>
{g.label}
<span className="nav-group-caret" aria-hidden>
▾
</span>
</button>
<div id={`${menuId}-${g.id}`} className="nav-group-panel">
{g.items.map((item) => (
<Link
key={item.href}
href={socialPath(item.href)}
aria-current={
pathname === socialPath(item.href) ? "page" : undefined
}
onClick={closeMenus}
>
{item.label}
</Link>
))}
</div>
</div>
))}
<div className="nav-mobile-extras">
{joinBtn}
<Link
href="/"
className="btn btn-ghost btn-sm"
onClick={closeMenus}
>
门户
</Link>
</div>
</div>
<div className="nav-actions">
<button
type="button"
className="nav-icon-btn"
onClick={toggleTheme}
aria-label="切换明暗"
suppressHydrationWarning
>
{theme === "dark" ? <Moon size={17} /> : <Sun size={17} />}
</button>
<Link
href={socialPath("/chat")}
className="nav-icon-btn"
aria-label="聊天室"
onClick={closeMenus}
>
<MessageCircle size={17} />
</Link>
<span className="social-nav-desktop-cta">{joinBtn}</span>
<Link
href="/"
className="btn btn-ghost btn-sm social-nav-desktop-cta"
>
门户
</Link>
<button
ref={menuButtonRef}
type="button"
className={`mobile-menu-btn${menuOpen ? " open" : ""}`}
onClick={() => setMenuOpen((v) => !v)}
aria-label="菜单"
aria-expanded={menuOpen}
aria-controls={menuId}
>
<span />
<span />
<span />
</button>
</div>
</div>
</nav>
);
if (home) {
return (
<>
<a className="connection-skip-link" href="#social-main">
跳到主要内容
</a>
{nav}
{children}
<SocialMobileDock />
</>
);
}
return (
<div className="social-flow social-root social-workspace social-workspace-dense">
<a className="connection-skip-link" href="#social-main">
跳到主要内容
</a>
{nav}
<div className="social-workspace-grid">
<main className="social-main" id="social-main" data-page={title || "社区"}>
<SocialQuickRail />
{children}
</main>
</div>
<SocialMobileDock />
</div>
);
}