"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(null); const [menuOpen, setMenuOpen] = useState(false); const [scrolled, setScrolled] = useState(false); const pathname = usePathname(); const menuId = useId(); const menuButtonRef = useRef(null); const navRef = useRef(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('[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 ? ( ) : ( 加入社区 ); const nav = ( ); if (home) { return ( <> 跳到主要内容 {nav} {children} ); } return (
跳到主要内容 {nav}
{children}
); }