nomadro/apps/nomadvip/app/vip/components/VipHeader.tsx
eric 9f8fec1723 Ship portal PC/H5 acceptance, VIP ScrollReader, admin analytics and ops anchors.
Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 23:52:00 -05:00

133 lines
4.5 KiB
TypeScript

"use client";
import { useState, useEffect, useRef } from "react";
import Link from "next/link";
import { ThemeToggle } from "@vip/app/components/ThemeToggle";
import { Copyable } from "./Copyable";
import { pbLogout } from "@vip/app/lib/pocketbase";
import styles from "./vip.module.css";
function getAvatarLetter(email: string): string {
const local = email.split("@")[0];
if (!local) return "?";
return local.slice(0, 1).toUpperCase();
}
function LogoIcon() {
return (
<svg className={styles.logoMark} viewBox="0 0 40 40" fill="none" aria-hidden>
<circle cx="20" cy="20" r="18" stroke="url(#vipLogoGrad)" strokeWidth="2.5" />
<path
d="M12 22c4-8 12-8 16 0"
stroke="url(#vipLogoGrad)"
strokeWidth="2"
strokeLinecap="round"
/>
<circle cx="28" cy="14" r="3" fill="url(#vipLogoGrad)" />
<defs>
<linearGradient id="vipLogoGrad" x1="0" y1="0" x2="40" y2="40">
<stop offset="0%" stopColor="#FF6B6B" />
<stop offset="50%" stopColor="#FFE66D" />
<stop offset="100%" stopColor="#4ECDC4" />
</linearGradient>
</defs>
</svg>
);
}
type VipHeaderProps = {
isLoggedIn: boolean;
userName?: string | null;
userEmail?: string | null;
wechatNick?: string | null;
wechatAvatar?: string | null;
onLogout?: () => void;
};
export function VipHeader({
isLoggedIn,
userName,
userEmail,
wechatNick,
wechatAvatar,
onLogout,
}: VipHeaderProps) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
};
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
const handleLogout = async () => {
await pbLogout();
setOpen(false);
onLogout?.();
};
const hasWechatProfile = !!(wechatNick || wechatAvatar);
const showUser = isLoggedIn || !!userEmail || hasWechatProfile;
const displayName = wechatNick || userName || userEmail || "会员";
return (
<header className={styles.header}>
<Link href="/" className={styles.logo} aria-label="返回首页">
<LogoIcon />
<span className={styles.logoBrand}>nomadro</span>
</Link>
<nav className={styles.nav}>
<ThemeToggle />
{showUser ? (
<div className={styles.userBadge} style={{ display: "flex", alignItems: "center", gap: "0.5rem" }}>
<div className="relative" ref={ref}>
<button
type="button"
onClick={() => setOpen((o) => !o)}
className="flex h-9 w-9 shrink-0 items-center justify-center overflow-hidden rounded-full bg-amber-500 text-sm font-semibold text-white shadow-sm hover:bg-amber-600"
aria-label="用户菜单"
>
{wechatAvatar ? (
// eslint-disable-next-line @next/next/no-img-element
<img src={wechatAvatar} alt={displayName} className="h-full w-full object-cover" />
) : (
getAvatarLetter(displayName)
)}
</button>
{isLoggedIn && (
<span
className={styles.vipTag}
title="VIP 会员"
>
VIP
</span>
)}
{open && (
<div className="absolute right-0 top-full z-50 mt-2 min-w-[120px] rounded-lg border border-[var(--border)] bg-[var(--card)] py-1 shadow-lg backdrop-blur-xl">
<span className="block px-4 py-2 text-sm text-[var(--muted-foreground)] truncate max-w-[160px]">
<Copyable text={displayName}>{displayName}</Copyable>
</span>
{isLoggedIn && onLogout && (
<button
onClick={handleLogout}
className="flex w-full items-center gap-2 px-4 py-2 text-left text-sm text-[var(--foreground)] hover:bg-[var(--muted)]"
>
退出
</button>
)}
</div>
)}
</div>
<span className="inline-block max-w-[90px] sm:max-w-[120px] truncate text-sm">
<Copyable text={displayName}>{displayName}</Copyable>
</span>
</div>
) : null}
</nav>
</header>
);
}