import { useEffect, useState } from "react"; import { toast } from "sonner"; import { useAuth } from "~/context/AuthContext"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { formFieldClass, formHintClass, formInputClass, modalFormClass, } from "~/features/platform/modalUi"; import { cn } from "~/lib/utils"; import { AppFormDialog, dialogPrimaryButtonClass, dialogSecondaryButtonClass, } from "./AppFormDialog"; type AccountSettingsDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; }; type SettingsTab = "profile" | "password"; const TAB_ITEMS: Array<{ id: SettingsTab; label: string }> = [ { id: "profile", label: "基本信息" }, { id: "password", label: "修改密码" }, ]; export function AccountSettingsDialog({ open, onOpenChange, }: AccountSettingsDialogProps) { const { user, updateProfile, changePassword, logout } = useAuth(); const [tab, setTab] = useState("profile"); const [displayName, setDisplayName] = useState(""); const [email, setEmail] = useState(""); const [savingProfile, setSavingProfile] = useState(false); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [savingPassword, setSavingPassword] = useState(false); useEffect(() => { if (!open || !user) return; setTab("profile"); setDisplayName(user.display_name); setEmail(user.email ?? ""); setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); }, [open, user]); const handleSaveProfile = async (event: React.FormEvent) => { event.preventDefault(); const trimmedName = displayName.trim(); if (!trimmedName) { toast.error("显示名称不能为空"); return; } setSavingProfile(true); try { await updateProfile({ display_name: trimmedName, email: email.trim(), }); toast.success("资料已更新"); onOpenChange(false); } catch (error) { toast.error(error instanceof Error ? error.message : "资料更新失败"); } finally { setSavingProfile(false); } }; const handleChangePassword = async (event: React.FormEvent) => { event.preventDefault(); if (!currentPassword) { toast.error("请输入当前密码"); return; } if (newPassword.length < 8 || newPassword.length > 72) { toast.error("新密码长度需为 8~72 字符"); return; } if (newPassword !== confirmPassword) { toast.error("两次输入的新密码不一致"); return; } if (newPassword === currentPassword) { toast.error("新密码不能与当前密码相同"); return; } setSavingPassword(true); try { await changePassword({ current_password: currentPassword, new_password: newPassword, }); toast.success("密码已修改,请重新登录"); onOpenChange(false); await logout(); } catch (error) { toast.error(error instanceof Error ? error.message : "密码修改失败"); } finally { setSavingPassword(false); } }; const formId = tab === "profile" ? "account-profile-form" : "account-password-form"; const saving = tab === "profile" ? savingProfile : savingPassword; const primaryLabel = tab === "profile" ? savingProfile ? "保存中…" : "保存资料" : savingPassword ? "修改中…" : "修改密码"; return ( } >
{TAB_ITEMS.map((item) => { const active = tab === item.id; return ( ); })}
{tab === "profile" ? (
void handleSaveProfile(event)} >
) : (
void handleChangePassword(event)} >
)}
); }