Freeze experimental social/admin UX on test12 for reference only; see docs/archive/test12-NEVER-DEPLOY.md. Co-authored-by: Cursor <cursoragent@cursor.com>
405 lines
14 KiB
TypeScript
405 lines
14 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import { usePathname } from "next/navigation";
|
|
import { ArrowUpRight, ChevronDown, LogOut, Moon, Sun } from "lucide-react";
|
|
import { useTheme } from "~/components/theme-provider";
|
|
import { navigationDetail, routeMatches } from "@social/lib/navigation";
|
|
import { navigationVisible } from "@social/lib/experience";
|
|
import { useSocialConfig } from "./SocialExperience";
|
|
import SocialCommandMenu from "./SocialCommandMenu";
|
|
import SocialLangSwitch from "./SocialLangSwitch";
|
|
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 { SocialFooter, 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, tx, locale } = useSocialI18n();
|
|
const config = useSocialConfig();
|
|
const { resolvedTheme, setTheme } = useTheme();
|
|
const [openGroup, setOpenGroup] = useState<string | null>(null);
|
|
const [menuOpen, setMenuOpen] = useState(false);
|
|
const [scrolled, setScrolled] = useState(false);
|
|
const [loginOpen, setLoginOpen] = useState(false);
|
|
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(() => 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) => ({ ...g, items: g.items.filter(item => navigationVisible(config, item.href)) })).filter(g => g.items.length).map((g) => (
|
|
<div
|
|
key={g.id}
|
|
className={`nav-group${openGroup === g.id ? " open" : ""}${g.items.some((item) => routeMatches(pathname, socialPath(item.href))) ? " is-active" : ""}`}
|
|
onBlur={(event) => { if (!event.currentTarget.contains(event.relatedTarget as Node)) setOpenGroup(null); }}
|
|
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(current => window.matchMedia("(hover: hover)").matches ? g.id : current === g.id ? null : g.id)}
|
|
onKeyDown={event => {
|
|
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
|
event.preventDefault(); setOpenGroup(g.id);
|
|
requestAnimationFrame(() => {
|
|
const links = document.getElementById(`${menuId}-${g.id}`)?.querySelectorAll("a");
|
|
links?.[event.key === "ArrowUp" ? links.length - 1 : 0]?.focus();
|
|
});
|
|
}
|
|
}}
|
|
>
|
|
{g.label}
|
|
<ChevronDown className="nav-group-caret" size={14} aria-hidden />
|
|
</button>
|
|
<div id={`${menuId}-${g.id}`} className="nav-group-panel">
|
|
<div className="nav-panel-heading"><span>{g.label}</span><small>{tx("用自己的节奏,找到连接", "Good connections start here")}</small></div>
|
|
<div className="nav-panel-entries">{g.items.map((item) => { const detail = navigationDetail(item.href, locale); const Icon = detail.icon; return (
|
|
<Link
|
|
key={item.href}
|
|
href={socialPath(item.href)}
|
|
aria-current={
|
|
routeMatches(pathname, socialPath(item.href)) ? "page" : undefined
|
|
}
|
|
onClick={closeMenus}
|
|
>
|
|
<span className="nav-entry-icon"><Icon size={19} strokeWidth={1.7} /></span>
|
|
<span className="nav-entry-copy"><strong>{item.label}</strong><small>{detail.description}</small></span>
|
|
<ArrowUpRight className="nav-entry-arrow" size={15} aria-hidden />
|
|
</Link>
|
|
); })}</div>
|
|
</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">
|
|
<SocialCommandMenu />
|
|
<SocialLangSwitch />
|
|
<button className="nav-icon-btn social-theme-toggle" type="button" onClick={() => setTheme(resolvedTheme === "dark" ? "light" : "dark")} aria-label={resolvedTheme === "dark" ? t.themeLight : t.themeDark}>{resolvedTheme === "dark" ? <Sun size={18} /> : <Moon size={18} />}</button>
|
|
{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} onClose={() => setLoginOpen(false)} />
|
|
);
|
|
|
|
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}
|
|
<SocialFooter />
|
|
</main>
|
|
</div>
|
|
<SocialMobileDock />
|
|
{authModal}
|
|
</div>
|
|
);
|
|
}
|