"use client"; import { CopyIcon, LogOutIcon, UserIcon } from "lucide-react"; import { toast } from "sonner"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { clearStoredAuth, getAccountDisplayName, getStoredAuth } from "@/core/auth"; import { useLiveAccount } from "@/core/auth/use-live-account"; import { OptType, writeYLog } from "@/core/log/y-log"; import { clearYLogToken } from "@/core/log/y-log-token"; import { SettingsSection } from "./settings-section"; function roleLabel(role?: string) { if (role === "admin") return "管理员"; if (role === "user") return "普通用户"; return role || "未知角色"; } function copy(text: string, hint = "已复制") { if (!text) return; navigator.clipboard .writeText(text) .then(() => toast.success(hint)) .catch(() => toast.error("复制失败")); } export function AccountSettingsPage() { // Subscribe to same-tab and cross-tab auth changes. The dialog may stay open // while the active account is switched elsewhere. useLiveAccount(); const auth = getStoredAuth(); if (!auth) { return (
没有可显示的账号信息。
); } const displayName = getAccountDisplayName(auth); const handleLogout = () => { // 操作日志(Y-Log):在清除 token1 之前上报登出(此时 token1 还在),keepalive 保证发出。 writeYLog({ optType: OptType.logout, optDetail: "CM助手-全量-登出", url: "/api/v1/auth/logout", keepalive: true, }); clearStoredAuth(); clearYLogToken(); // Match the rest of the app's logout flow: blow away the cookie session // by hitting the gateway logout endpoint and then forcing a full reload // so any cached query state goes too. void fetch("/api/v1/auth/logout", { method: "POST", credentials: "include" }) .catch(() => undefined) .finally(() => { window.location.assign("/"); }); }; return (
{displayName}
{roleLabel(auth.system_role)}
{roleLabel(auth.system_role)}
copy(displayName, "用户名已复制")} /> copy(auth.user_id, "用户 ID 已复制")} />
); } function InfoRow({ label, value, onCopy, }: { label: string; value: string; onCopy?: () => void; }) { return (
{label}
{value || "—"}
{onCopy && value && ( )}
); }