cosmo/frontend/src/components/UserAuth.tsx

148 lines
5.8 KiB
TypeScript

import { useCallback, useEffect, useState } from 'react';
import { User, LogOut, LayoutDashboard, LogIn, MonitorSmartphone } from 'lucide-react';
import type { AuthUser } from '../utils/auth';
import { fetchMyDevices, type TerminalDevice } from '../utils/api';
import { DeviceBindingModal } from './DeviceBindingModal';
interface UserAuthProps {
user: AuthUser | null | undefined;
onOpenAuth: () => void;
onLogout: () => void;
onNavigateToAdmin: () => void;
}
export function UserAuth({ user, onOpenAuth, onLogout, onNavigateToAdmin }: UserAuthProps) {
const [showUserMenu, setShowUserMenu] = useState(false);
const [showDeviceBinding, setShowDeviceBinding] = useState(false);
const [devices, setDevices] = useState<TerminalDevice[] | null>(null);
// 菜单里直接显示绑定情况(也是解绑入口),每次展开菜单时向后端取一次
const loadDevices = useCallback(() => {
fetchMyDevices().then(setDevices).catch(() => setDevices(null));
}, []);
useEffect(() => {
if (showUserMenu) loadDevices();
}, [showUserMenu, loadDevices]);
const deviceSummary = devices === null
? '加载中...'
: devices.length === 0
? '尚未绑定终端'
: `已绑定 ${devices.length} 台 / ${devices.filter((d) => d.online).length} 台在线`;
if (!user) {
return (
<button
onClick={onOpenAuth}
className="flex items-center gap-2 px-4 py-2 rounded-full bg-white/10 hover:bg-white/20 backdrop-blur-md border border-white/10 text-white font-medium text-sm transition-all duration-200 shadow-lg hover:shadow-blue-500/20 pointer-events-auto"
>
<LogIn size={16} />
<span>登录 / 注册</span>
</button>
);
}
// Helper to get full avatar URL
const getAvatarUrl = () => {
if (!user?.avatar_url) return undefined;
return `/upload/${user.avatar_url}`;
};
return (
<div className="relative pointer-events-auto">
{/* Overlay for closing menu */}
{showUserMenu && (
<div
className="fixed inset-0 z-40"
onClick={() => setShowUserMenu(false)}
/>
)}
<button
onClick={() => setShowUserMenu(!showUserMenu)}
className="flex items-center gap-3 pl-2 pr-4 py-1.5 rounded-full bg-black/40 hover:bg-black/60 backdrop-blur-md border border-white/10 text-white transition-all duration-200 relative z-50"
>
<div className="w-8 h-8 rounded-full overflow-hidden bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center shadow-inner">
{user.avatar_url ? (
<img src={getAvatarUrl()} alt="avatar" className="w-full h-full object-cover" />
) : (
<User size={16} />
)}
</div>
<div className="flex flex-col items-start text-xs">
<span className="text-gray-400 leading-none mb-0.5">已登录</span>
<span className="font-bold leading-none max-w-[80px] truncate">{user.username}</span>
</div>
</button>
{/* Dropdown Menu */}
{showUserMenu && (
<div className="absolute top-full right-0 mt-2 w-48 bg-gray-900/95 backdrop-blur-xl border border-white/10 rounded-xl shadow-2xl overflow-hidden animate-in fade-in slide-in-from-top-2 duration-200 z-50">
<div className="p-4 border-b border-white/10 bg-white/5 flex items-center gap-3">
<div className="w-10 h-10 rounded-full overflow-hidden bg-gradient-to-br from-blue-500 to-purple-600 flex-shrink-0 flex items-center justify-center">
{user.avatar_url ? (
<img src={getAvatarUrl()} alt="avatar" className="w-full h-full object-cover" />
) : (
<User size={20} className="text-white" />
)}
</div>
<div className="overflow-hidden">
<p className="text-sm font-bold text-white truncate">{user.full_name || user.username}</p>
<p className="text-[10px] text-gray-500 truncate">{user.email}</p>
</div>
</div>
<div className="p-1">
<button
onClick={() => {
setShowDeviceBinding(true);
setShowUserMenu(false);
}}
className="w-full text-left px-3 py-2.5 text-sm text-gray-200 hover:bg-white/10 hover:text-white transition-colors rounded-lg"
>
<span className="flex items-center gap-3">
<MonitorSmartphone size={16} className="text-green-400" />
绑定终端设备
</span>
<span className="block pl-7 text-[10px] text-gray-500">{deviceSummary}</span>
</button>
<button
onClick={() => {
onNavigateToAdmin();
setShowUserMenu(false);
}}
className="w-full text-left px-3 py-2.5 text-sm text-gray-200 hover:bg-white/10 hover:text-white flex items-center gap-3 transition-colors rounded-lg"
>
<LayoutDashboard size={16} className="text-blue-400" />
进入后台管理
</button>
<div className="h-px bg-white/10 my-1 mx-2"></div>
<button
onClick={() => {
onLogout();
setShowUserMenu(false);
}}
className="w-full text-left px-3 py-2.5 text-sm text-red-400 hover:bg-red-500/10 hover:text-red-300 flex items-center gap-3 transition-colors rounded-lg"
>
<LogOut size={16} />
退出登录
</button>
</div>
</div>
)}
<DeviceBindingModal
isOpen={showDeviceBinding}
onClose={() => {
setShowDeviceBinding(false);
loadDevices();
}}
/>
</div>
);
}