"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(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(null); const navRef = useRef(null); const menuPanelRef = useRef(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('[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("button")?.focus(); const trapFocus = (event: KeyboardEvent) => { if (event.key !== "Tab") return; const items = Array.from(navRef.current?.querySelectorAll('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 = ( ); const nav = ( ); const authModal = ( setLoginOpen(false)} /> ); if (home) { return ( <> {t.skipToMain} {nav} {children} {authModal} ); } if (immersive) { return (
{t.skipToMain} {nav}
{children}
{authModal}
); } if (editorial) { return (
{t.skipToMain} {nav} {/* home: heartbeat only, no presence strip on blog/policy */}
{children}
{authModal}
); } return (
{t.skipToMain} {nav}
{children}
{authModal}
); }