nomadro/src/modules/social/components/NavAccountMenu.tsx
Eric 499f2d3e26 Ship coffeechat scheduling, notify PB access, and social/admin UI polish for .com.
Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence.

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

258 lines
7.7 KiB
TypeScript

"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<SocialRosterProfile>("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<Tab>("profile");
const [profile, setProfile] = useState(() => readLocalProfile(session));
const rootRef = useRef<HTMLDivElement>(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 (
<div className="social-nav-account" ref={rootRef} data-open={open ? "1" : "0"}>
<button
type="button"
className="social-nav-avatar-btn"
aria-label={socialTx("我的账号")}
aria-expanded={open}
aria-controls={panelId}
onClick={() => setOpen((v) => !v)}
>
{profile.avatar ? (
<SocialAvatar src={profile.avatar} size={36} allowPlaceholder />
) : (
<span className="social-nav-avatar-fallback" aria-hidden>
{initial}
</span>
)}
</button>
{open ? (
<div
id={panelId}
className="social-nav-account-panel"
role="dialog"
aria-label={socialTx("我的账号")}
>
<div className="social-nav-account-tabs" role="tablist">
<button
type="button"
role="tab"
aria-selected={tab === "profile"}
className={tab === "profile" ? "is-on" : undefined}
onClick={() => setTab("profile")}
>
<UserRound size={14} aria-hidden />
{socialTx("个人信息")}
</button>
<button
type="button"
role="tab"
aria-selected={tab === "options"}
className={tab === "options" ? "is-on" : undefined}
onClick={() => setTab("options")}
>
<Settings2 size={14} aria-hidden />
{socialTx("选项")}
</button>
</div>
{tab === "profile" ? (
<div className="social-nav-account-body" role="tabpanel">
<div className="social-nav-account-hero">
{profile.avatar ? (
<SocialAvatar src={profile.avatar} size={56} allowPlaceholder />
) : (
<span className="social-nav-avatar-fallback is-lg" aria-hidden>
{initial}
</span>
)}
<div>
<strong>{profile.name}</strong>
<p>{profile.email}</p>
{profile.title || profile.city ? (
<small>
{[profile.title, profile.city].filter(Boolean).join(" · ")}
</small>
) : null}
</div>
</div>
<Link
href={
profileDone
? socialPath("/roster")
: socialPath("/join?step=profile")
}
className="social-nav-account-link"
onClick={() => {
setOpen(false);
onCloseMenus?.();
}}
>
{socialTx("编辑资料")}
</Link>
</div>
) : (
<div className="social-nav-account-body" role="tabpanel">
{!profileDone ? (
<Link
href={socialPath("/join?step=profile")}
className="social-nav-account-link"
onClick={() => {
setOpen(false);
onCloseMenus?.();
}}
>
{socialTx("完善资料")}
</Link>
) : null}
{!liveOk ? (
<Link
href={socialPath("/join?step=liveness")}
className="social-nav-account-link"
onClick={() => {
setOpen(false);
onCloseMenus?.();
}}
>
{socialTx("去拍自拍")}
</Link>
) : null}
<Link
href={socialPath("/roster")}
className="social-nav-account-link"
onClick={() => {
setOpen(false);
onCloseMenus?.();
}}
>
{socialTx("我的主页")}
</Link>
<button
type="button"
className="social-nav-account-logout"
onClick={() => {
logoutSocialSession();
setOpen(false);
onCloseMenus?.();
}}
>
<LogOut size={14} aria-hidden />
{socialTx("退出登录")}
</button>
</div>
)}
</div>
) : null}
</div>
);
}