import React, { useState, useRef } from 'react'; import { Account } from '@/types/account'; import { Card, CardContent } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Badge } from '@/components/ui/badge'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { MoreVertical, Trash2, RefreshCw } from 'lucide-react'; import { formatDistanceToNow } from 'date-fns'; import { enUS, zhCN, ru } from 'date-fns/locale'; import { cn } from '@/lib/utils'; import { useTranslation } from 'react-i18next'; interface AccountCardProps { account: Account; isCurrent: boolean; onSwitch: (id: string) => void; onDelete: (id: string) => void; isSwitching?: boolean; isDeleting?: boolean; } export const AccountCard: React.FC = ({ account, isCurrent, onSwitch, onDelete, isSwitching, isDeleting, }) => { const { t, i18n } = useTranslation(); const cardRef = useRef(null); const [transform, setTransform] = useState(''); const [glarePosition, setGlarePosition] = useState({ x: 50, y: 50 }); const initials = account.name ? account.name .split(' ') .map((n) => n[0]) .join('') .toUpperCase() .slice(0, 2) : account.email[0].toUpperCase(); // 处理鼠标移动,计算 3D 旋转角度 const handleMouseMove = (e: React.MouseEvent) => { if (!cardRef.current) return; const rect = cardRef.current.getBoundingClientRect(); const centerX = rect.left + rect.width / 2; const centerY = rect.top + rect.height / 2; // 计算鼠标相对于卡片中心的位置 (-1 到 1) const relativeX = (e.clientX - centerX) / (rect.width / 2); const relativeY = (e.clientY - centerY) / (rect.height / 2); // 最大旋转角度 const maxRotation = 15; const rotateX = -relativeY * maxRotation; // 上下倾斜 const rotateY = relativeX * maxRotation; // 左右倾斜 setTransform( `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale3d(1.02, 1.02, 1.02)`, ); // 更新光泽位置 setGlarePosition({ x: ((e.clientX - rect.left) / rect.width) * 100, y: ((e.clientY - rect.top) / rect.height) * 100, }); }; // 鼠标离开时重置 const handleMouseLeave = () => { setTransform('perspective(1000px) rotateX(0deg) rotateY(0deg) scale3d(1, 1, 1)'); setGlarePosition({ x: 50, y: 50 }); }; return (
{/* 光泽效果层 */}
!isCurrent && !isSwitching && !isDeleting && onSwitch(account.id)} >
{initials}

{account.name}

{isCurrent && ( {t('account.current')} )} {isSwitching && ( )}

{account.email}

{t('account.lastUsed', { time: formatDistanceToNow(new Date(account.last_used), { addSuffix: true, locale: { en: enUS, 'zh-CN': zhCN, ru: ru }[i18n.language] || enUS, }), })}

{ e.stopPropagation(); onDelete(account.id); }} disabled={isDeleting} > {t('action.deleteBackup')}
); };