/** * Account Page * Cyberpunk Terminal Aesthetic */ import { useState, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { useAuth } from "@/shared/context/AuthContext"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog"; import { User, Mail, Phone, Shield, Calendar, Save, KeyRound, LogOut, UserPlus, GitBranch, Terminal } from "lucide-react"; import { apiClient } from "@/shared/api/serverClient"; import { toast } from "sonner"; import type { Profile } from "@/shared/types"; export default function Account() { const navigate = useNavigate(); const { logout } = useAuth(); const [profile, setProfile] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [showLogoutDialog, setShowLogoutDialog] = useState(false); const [form, setForm] = useState({ full_name: "", phone: "", github_username: "", gitlab_username: "", gitea_username: "", }); const [passwordForm, setPasswordForm] = useState({ current_password: "", new_password: "", confirm_password: "", }); const [changingPassword, setChangingPassword] = useState(false); useEffect(() => { loadProfile(); }, []); const loadProfile = async () => { try { setLoading(true); const res = await apiClient.get('/users/me'); setProfile(res.data); setForm({ full_name: res.data.full_name || "", phone: res.data.phone || "", github_username: res.data.github_username || "", gitlab_username: res.data.gitlab_username || "", gitea_username: res.data.gitea_username || "", }); } catch (error) { console.error('Failed to load profile:', error); toast.error("加载账号信息失败"); } finally { setLoading(false); } }; const handleSave = async () => { try { setSaving(true); const res = await apiClient.put('/users/me', form); setProfile(res.data); toast.success("账号信息已更新"); } catch (error) { console.error('Failed to update profile:', error); toast.error("更新失败"); } finally { setSaving(false); } }; const handleChangePassword = async () => { if (!passwordForm.new_password || !passwordForm.confirm_password) { toast.error("请填写新密码"); return; } if (passwordForm.new_password !== passwordForm.confirm_password) { toast.error("两次输入的密码不一致"); return; } if (passwordForm.new_password.length < 6) { toast.error("密码长度至少6位"); return; } try { setChangingPassword(true); await apiClient.put('/users/me', { password: passwordForm.new_password }); toast.success("密码已更新"); setPasswordForm({ current_password: "", new_password: "", confirm_password: "" }); } catch (error) { console.error('Failed to change password:', error); toast.error("密码更新失败"); } finally { setChangingPassword(false); } }; const formatDate = (dateString?: string) => { if (!dateString) return "-"; return new Date(dateString).toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' }); }; const getInitials = (name?: string, email?: string) => { if (name) return name.charAt(0).toUpperCase(); if (email) return email.charAt(0).toUpperCase(); return "U"; }; const handleLogout = () => { logout(); toast.success("已退出登录"); navigate('/login'); }; const handleSwitchAccount = () => { logout(); navigate('/login'); }; if (loading) { return (

加载中...

); } return (
{/* Grid background */}
{/* Profile Card */}

用户信息

{getInitials(profile?.full_name, profile?.email)}

{profile?.full_name || "未设置姓名"}

{profile?.email}

角色: {profile?.role === 'admin' ? '管理员' : '成员'}
注册时间: {formatDate(profile?.created_at)}
{/* Edit Form */}

基本信息

邮箱不可修改

setForm({ ...form, full_name: e.target.value })} placeholder="请输入姓名" className="cyber-input" />
setForm({ ...form, phone: e.target.value })} placeholder="请输入手机号" className="cyber-input" />

代码托管账号

setForm({ ...form, github_username: e.target.value })} placeholder="your-github-username" className="cyber-input" />
setForm({ ...form, gitlab_username: e.target.value })} placeholder="your-gitlab-username" className="cyber-input" />
setForm({ ...form, gitea_username: e.target.value })} placeholder="your-gitea-username" className="cyber-input" />
{/* Password Change */}

修改密码

setPasswordForm({ ...passwordForm, new_password: e.target.value })} placeholder="输入新密码" className="cyber-input" />
setPasswordForm({ ...passwordForm, confirm_password: e.target.value })} placeholder="再次输入新密码" className="cyber-input" />
{/* Logout Confirmation Dialog */} 确认退出登录? 退出后需要重新登录才能访问系统。 取消 确认退出
); }