"use client"; import Link from "next/link"; import { useEffect, useId, useRef, useState, useSyncExternalStore, } from "react"; import { LogOut, Settings2, UserRound } from "lucide-react"; import SocialAvatar from "./SocialAvatar"; import { isJoinProfileDone } from "@social/lib/joinFlow"; import { getLivenessServerSnapshot, getLivenessSnapshot, subscribeLivenessState, } from "@social/lib/livenessGate"; import { socialPath } from "@social/lib/socialPath"; import { logoutSocialSession, type SocialSession, } from "@social/lib/socialSession"; import { loadJson } from "@social/lib/storage"; import { socialTx } from "@social/lib/socialTx"; import type { SocialRosterProfile } from "@social/lib/types"; type Tab = "profile" | "options"; const EMPTY_ROSTER: SocialRosterProfile = { name: "", title: "", city: "", phone: "", linktree: "", skills: "", bio: "", avatar: "", intents: "", availability: "", status: "", lookingFor: "", gender: "", }; function readLocalProfile(session: SocialSession) { const roster = loadJson("roster", EMPTY_ROSTER); const avatar = (roster.avatar || "").trim(); const avatarOk = avatar.startsWith("https://") || avatar.startsWith("http://") || avatar.startsWith("/api/social/"); return { name: (roster.name || session.name || "").trim() || session.email.split("@")[0], email: session.email, title: (roster.title || "").trim(), city: (roster.city || "").trim(), avatar: avatarOk ? avatar : "", }; } export default function NavAccountMenu({ session, onCloseMenus, }: { session: SocialSession; onCloseMenus?: () => void; }) { const [open, setOpen] = useState(false); const [tab, setTab] = useState("profile"); const [profile, setProfile] = useState(() => readLocalProfile(session)); const rootRef = useRef(null); const panelId = useId(); const liveSnap = useSyncExternalStore( subscribeLivenessState, getLivenessSnapshot, getLivenessServerSnapshot, ); const liveOk = (JSON.parse(liveSnap) as { livenessStatus?: string }).livenessStatus === "verified"; const [profileDone, setProfileDone] = useState(false); useEffect(() => { setProfileDone(isJoinProfileDone()); }, [session]); useEffect(() => { setProfile(readLocalProfile(session)); }, [session]); useEffect(() => { const refresh = () => setProfile(readLocalProfile(session)); window.addEventListener("storage", refresh); window.addEventListener("nomadro-social-storage", refresh); return () => { window.removeEventListener("storage", refresh); window.removeEventListener("nomadro-social-storage", refresh); }; }, [session]); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setOpen(false); }; const onPointer = (e: PointerEvent) => { if (!rootRef.current?.contains(e.target as Node)) setOpen(false); }; window.addEventListener("keydown", onKey); window.addEventListener("pointerdown", onPointer); return () => { window.removeEventListener("keydown", onKey); window.removeEventListener("pointerdown", onPointer); }; }, [open]); const initial = (profile.name || "?").slice(0, 1).toUpperCase(); return (
{open ? ( ) : null}
); }