import React, { useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { useNavigate } from 'react-router-dom'; import { ChevronDown, LogOut, Phone, Building2, Shield, User, Lock, X, KeyRound, Copy } from 'lucide-react'; import { useAuth } from '../hooks/useAuth'; import { supabase } from '../supabase/client'; import { notify } from '../utils/notify'; import { ChangePasswordForm } from './ChangePasswordForm'; import { useClickOutside } from '../hooks/useClickOutside'; import { useI18n } from '../i18n/I18nProvider'; import { useRoleLabels } from '../hooks/useRoleLabels'; function DetailRow({ icon: Icon, label, value, }: { icon: React.ElementType; label: string; value: string; }) { return (

{label}

{value}

); } export const UserAccountMenu: React.FC = () => { const { t } = useI18n(); const navigate = useNavigate(); const { profile, logout } = useAuth(); const [open, setOpen] = useState(false); const [showChangePassword, setShowChangePassword] = useState(false); const [accessCodes, setAccessCodes] = useState< Array<{ branch_code: string; branch_name: string; access_code: string }> >([]); const containerRef = useRef(null); const displayName = profile?.full_name || profile?.username || t('layout.userFallback'); // 统一口径:本地化文案 → role_definitions.role_name → 内置角色表 → 角色代码。 // 自建角色没有对应翻译条目,此前直接把 i18n key 显示到了界面上。 const { roleLabel: resolveRoleLabel } = useRoleLabels(); const primaryRole = profile?.roles?.[0]; const roleLabel = primaryRole ? resolveRoleLabel(primaryRole) : t('layout.unknownRole'); const roleText = profile?.roles?.length ? profile.roles.map((role) => resolveRoleLabel(role)).join('、') : roleLabel; const isSuperAdmin = Boolean(profile?.roles?.includes('super_admin')); const companyName = profile?.branch_name || (isSuperAdmin ? t('layout.allPlatform') : null); useClickOutside(containerRef, open, () => setOpen(false)); /** * 「接入编码」:**分公司管理员**看本分公司的码;**超级管理员不展示**(2026-10-13 开发者要求) * —— 超管要看全部编码请去「分公司管理」(那里有「接入编码」列 + 复制 + 绑定关系), * 账号菜单里再列一遍既长又没必要。其它角色整行不展示。 */ const canSeeAccessCode = !isSuperAdmin && Boolean(profile?.roles?.some((role) => role === 'admin')); useEffect(() => { if (!open || !canSeeAccessCode) return; let alive = true; void (async () => { const { data } = await supabase.auth.admin.getAccessCodes(); if (alive && data?.access_codes) setAccessCodes(data.access_codes); })(); return () => { alive = false; }; }, [open, canSeeAccessCode]); const handleLogout = async () => { setOpen(false); await logout(); navigate('/login'); }; /** 复制接入编码(与分公司管理页同款实现与文案) */ const copyAccessCode = async (code: string) => { try { await navigator.clipboard.writeText(code); notify.success(t('system.branches.toast.copied')); } catch { notify.warning(code); } }; return (
{open && (

{displayName}

@{profile?.username || '-'}

{profile?.phone && } {canSeeAccessCode && accessCodes.length > 0 && (

{t('layout.accessCode')}

{accessCodes.map((row) => (
{accessCodes.length > 1 ? `${row.branch_name || row.branch_code}:` : ''} {row.access_code}
))}
)}
)} {showChangePassword && createPortal(

{t('layout.changePassword')}

setShowChangePassword(false)} />
, document.body )}
); };