nomadro/src/modules/social/components/SiteShell.tsx
Eric 83f87e5ef3 Ship room-archive identity + join gates, and fix ntfy Beijing time for second timestamps.
Room archives resolve guest nick/email for admin lists; social join/date/people/coffee use clearer access gates; ntfy no longer shows 1970 when track.ts sends Unix seconds.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 20:52:37 +08:00

393 lines
12 KiB
TypeScript

"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { LogOut } from "lucide-react";
import {
useEffect,
useId,
useRef,
useState,
useSyncExternalStore,
type ReactNode,
} from "react";
import NotifyBell from "@notify/components/NotifyBell";
import { socialPath } from "@social/lib/socialPath";
import { applySocialSkin, SOCIAL_SKIN_DEFAULT } from "@social/lib/skin";
import {
getSocialSession,
logoutSocialSession,
subscribeSocialSession,
} from "@social/lib/socialSession";
import { useRevealMotion } from "@social/lib/useRevealMotion";
import { useSocialI18n } from "@social/lib/useSocialI18n";
import { socialTx } from "@social/lib/socialTx";
import { SocialMobileDock } from "./SocialNavigation";
import SocialQuickRail from "./SocialQuickRail";
import SocialBrandLogo from "./SocialBrandLogo";
import JoinPayModal from "./JoinPayModal";
import NavAccountMenu from "./NavAccountMenu";
import TrialNudgeBanner from "./TrialNudgeBanner";
import SocialLiveShell from "./SocialLiveShell";
import {
consumeOpenJoinModalFlag,
subscribeSocialJoinModal,
} from "@social/lib/joinModal";
export default function SiteShell({
children,
title,
home = false,
immersive = false,
/** Blog / policy pages — content-first, no QuickRail (AdSense readability) */
editorial = false,
onJoin,
}: {
children: ReactNode;
title?: string;
home?: boolean;
/** Full-bleed pages (map) — no quick rail / dense workspace chrome */
immersive?: boolean;
editorial?: boolean;
onJoin?: () => void;
}) {
const { t } = useSocialI18n();
const [openGroup, setOpenGroup] = useState<string | null>(null);
const [menuOpen, setMenuOpen] = useState(false);
const [scrolled, setScrolled] = useState(false);
const [loginOpen, setLoginOpen] = useState(false);
const [joinHint, setJoinHint] = useState<string | undefined>(undefined);
const pathname = usePathname();
const menuId = useId();
const menuButtonRef = useRef<HTMLButtonElement>(null);
const navRef = useRef<HTMLElement>(null);
const menuPanelRef = useRef<HTMLDivElement>(null);
const sessionSnap = useSyncExternalStore(
subscribeSocialSession,
() => JSON.stringify(getSocialSession()),
() => "null",
);
const session = JSON.parse(sessionSnap) as ReturnType<
typeof getSocialSession
>;
useEffect(() => {
applySocialSkin(SOCIAL_SKIN_DEFAULT);
}, []);
const openJoin = () => {
setLoginOpen(true);
onJoin?.();
};
useEffect(() => {
return subscribeSocialJoinModal((detail) => {
setJoinHint(detail.hint || undefined);
setLoginOpen(true);
});
}, []);
useEffect(() => {
if (consumeOpenJoinModalFlag()) setLoginOpen(true);
}, [pathname]);
useEffect(() => {
try {
const q = new URLSearchParams(window.location.search);
if (q.get("join") === "1" || q.get("social_join") === "1") {
setLoginOpen(true);
}
} catch {
/* ignore */
}
}, []);
useRevealMotion(home ? ".social-home" : ".social-workspace", [
home,
pathname,
]);
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";
menuPanelRef.current?.querySelector<HTMLButtonElement>("button")?.focus();
const trapFocus = (event: KeyboardEvent) => {
if (event.key !== "Tab") return;
const items = Array.from(navRef.current?.querySelectorAll<HTMLElement>('a[href], button:not(:disabled)') || [])
.filter((element) => element.getClientRects().length > 0 && getComputedStyle(element).visibility !== "hidden");
const first = items[0], last = items[items.length - 1];
if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last?.focus(); }
else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first?.focus(); }
};
const media = matchMedia("(min-width: 901px)");
const closeOnDesktop = () => { if (media.matches) setMenuOpen(false); };
document.addEventListener("keydown", trapFocus);
media.addEventListener("change", closeOnDesktop);
return () => {
document.body.style.overflow = previousOverflow;
document.removeEventListener("keydown", trapFocus);
media.removeEventListener("change", closeOnDesktop);
};
}, [menuOpen]);
const closeMenus = () => {
setMenuOpen(false);
setOpenGroup(null);
};
const joinBtn = (
<button
type="button"
className="social-btn coral social-nav-join"
onClick={() => {
closeMenus();
openJoin();
}}
>
{t.joinCommunity}
</button>
);
const nav = (
<nav
ref={navRef}
className={`navbar${scrolled ? " scrolled" : ""}`}
aria-label={t.navAria}
>
<div className="nav-container">
<SocialBrandLogo onClick={closeMenus} />
<div ref={menuPanelRef} id={menuId} className={`nav-links${menuOpen ? " open" : ""}`}>
{t.navGroups.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">
{session ? (
<button
type="button"
className="social-btn social-nav-logout"
onClick={() => {
logoutSocialSession();
closeMenus();
}}
>
<LogOut size={14} /> {socialTx("退出")}
</button>
) : (
<>
<button
type="button"
className="social-btn social-nav-login"
onClick={() => {
closeMenus();
openJoin();
}}
>
{socialTx("登录")}
</button>
{joinBtn}
</>
)}
</div>
</div>
<div className="nav-actions">
{session ? <NotifyBell onNavigate={closeMenus} /> : null}
{session ? (
<NavAccountMenu session={session} onCloseMenus={closeMenus} />
) : (
<>
<button
type="button"
className="social-btn social-nav-login"
onClick={() => {
closeMenus();
openJoin();
}}
>
{socialTx("登录")}
</button>
<span className="social-nav-desktop-cta">{joinBtn}</span>
</>
)}
<button
ref={menuButtonRef}
type="button"
className={`mobile-menu-btn${menuOpen ? " open" : ""}`}
onClick={() => setMenuOpen((v) => !v)}
aria-label={t.menu}
aria-expanded={menuOpen}
aria-controls={menuId}
>
<span />
<span />
<span />
</button>
</div>
</div>
</nav>
);
const authModal = (
<JoinPayModal
open={loginOpen}
hint={joinHint}
onClose={() => {
setLoginOpen(false);
setJoinHint(undefined);
}}
/>
);
if (home) {
return (
<>
<a className="connection-skip-link" href="#social-main">
{t.skipToMain}
</a>
{nav}
<SocialLiveShell home />
{children}
<SocialMobileDock />
{authModal}
</>
);
}
if (immersive) {
return (
<div className="social-flow social-root social-immersive">
<a className="connection-skip-link" href="#social-main">
{t.skipToMain}
</a>
{nav}
<SocialLiveShell />
<main className="social-main social-main--immersive" id="social-main" data-page={title || t.brand}>
<TrialNudgeBanner />
{children}
</main>
<SocialMobileDock />
{authModal}
</div>
);
}
if (editorial) {
return (
<div className="social-flow social-root social-workspace social-editorial">
<a className="connection-skip-link" href="#social-main">
{t.skipToMain}
</a>
{nav}
{/* home: heartbeat only, no presence strip on blog/policy */}
<SocialLiveShell home />
<div className="social-editorial-frame">
<main
className="social-main social-main--editorial"
id="social-main"
data-page={title || t.brand}
>
{children}
</main>
</div>
<SocialMobileDock />
{authModal}
</div>
);
}
return (
<div className="social-flow social-root social-workspace social-workspace-dense">
<a className="connection-skip-link" href="#social-main">
{t.skipToMain}
</a>
{nav}
<SocialLiveShell />
<div className="social-workspace-grid">
<main className="social-main" id="social-main" data-page={title || t.brand}>
<SocialQuickRail />
<TrialNudgeBanner />
{children}
</main>
</div>
<SocialMobileDock />
{authModal}
</div>
);
}