| 1 | /* 个人信息页:头像(首字母占位)、显示名、邮箱、改密码。 */ |
| 2 | import { useState } from "react"; |
| 3 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 4 | import { api } from "../api"; |
| 5 | import { S } from "../i18n"; |
| 6 | import { Loading } from "../ui"; |
| 7 | import { toast } from "../toast"; |
| 8 | import { Avatar } from "./UserMenu"; |
| 9 | |
| 10 | export function Account() { |
| 11 | const queryClient = useQueryClient(); |
| 12 | const me = useQuery({ queryKey: ["me"], queryFn: api.me }); |
| 13 | |
| 14 | const [name, setName] = useState<string | null>(null); |
| 15 | const [curPw, setCurPw] = useState(""); |
| 16 | const [newPw, setNewPw] = useState(""); |
| 17 | |
| 18 | const saveName = useMutation({ |
| 19 | mutationFn: (n: string) => api.updateMe(n), |
| 20 | onSuccess: () => { |
| 21 | queryClient.invalidateQueries({ queryKey: ["me"] }); |
| 22 | setName(null); |
| 23 | toast.success(S.toast.saved); |
| 24 | }, |
| 25 | onError: (e) => toast.error((e as Error).message), |
| 26 | }); |
| 27 | |
| 28 | const changePw = useMutation({ |
| 29 | mutationFn: () => api.changePassword(curPw, newPw), |
| 30 | onSuccess: () => { |
| 31 | setCurPw(""); |
| 32 | setNewPw(""); |
| 33 | toast.success(S.account.passwordChanged); |
| 34 | }, |
| 35 | onError: (e) => toast.error((e as Error).message), |
| 36 | }); |
| 37 | |
| 38 | if (!me.data) return <Loading>{S.nav.loading}</Loading>; |
| 39 | const displayName = name ?? me.data.display_name; |
| 40 | const nameDirty = displayName.trim() !== me.data.display_name && displayName.trim(); |
| 41 | |
| 42 | const field = (label: string, node: React.ReactNode) => ( |
| 43 | <div className="mb-4"> |
| 44 | <div className="mb-1 text-[11px] font-medium text-neutral-500">{label}</div> |
| 45 | {node} |
| 46 | </div> |
| 47 | ); |
| 48 | |
| 49 | return ( |
| 50 | <div className="max-w-lg p-8"> |
| 51 | <h1 className="u-title text-lg mb-6">{S.account.profileTitle}</h1> |
| 52 | |
| 53 | <div className="glass rounded-2xl p-5 mb-4"> |
| 54 | <div className="flex items-center gap-4 mb-5"> |
| 55 | <Avatar name={me.data.display_name} size={56} /> |
| 56 | <p className="text-xs text-neutral-500">{S.account.avatarHint}</p> |
| 57 | </div> |
| 58 | |
| 59 | {field( |
| 60 | S.account.displayName, |
| 61 | <div className="flex gap-2"> |
| 62 | <input |
| 63 | className="input-dark flex-1 px-3 py-2 text-sm" |
| 64 | value={displayName} |
| 65 | onChange={(e) => setName(e.target.value)} |
| 66 | /> |
| 67 | <button |
| 68 | className="u-btn u-btn-primary px-3.5 text-xs" |
| 69 | disabled={!nameDirty || saveName.isPending} |
| 70 | onClick={() => saveName.mutate(displayName.trim())} |
| 71 | > |
| 72 | {S.account.save} |
| 73 | </button> |
| 74 | </div>, |
| 75 | )} |
| 76 | |
| 77 | {field( |
| 78 | S.account.email, |
| 79 | <div className="text-sm text-neutral-400 px-0.5">{me.data.email}</div>, |
| 80 | )} |
| 81 | </div> |
| 82 | |
| 83 | <div className="glass rounded-2xl p-5"> |
| 84 | <h2 className="text-[13px] font-medium text-neutral-200 mb-4"> |
| 85 | {S.account.passwordTitle} |
| 86 | </h2> |
| 87 | {field( |
| 88 | S.account.currentPassword, |
| 89 | <input |
| 90 | type="password" |
| 91 | autoComplete="current-password" |
| 92 | className="input-dark w-full px-3 py-2 text-sm" |
| 93 | value={curPw} |
| 94 | onChange={(e) => setCurPw(e.target.value)} |
| 95 | />, |
| 96 | )} |
| 97 | {field( |
| 98 | S.account.newPassword, |
| 99 | <input |
| 100 | type="password" |
| 101 | autoComplete="new-password" |
| 102 | className="input-dark w-full px-3 py-2 text-sm" |
| 103 | value={newPw} |
| 104 | onChange={(e) => setNewPw(e.target.value)} |
| 105 | />, |
| 106 | )} |
| 107 | <div className="flex justify-end"> |
| 108 | <button |
| 109 | className="u-btn u-btn-primary px-3.5 py-1.5 text-xs" |
| 110 | disabled={!curPw || newPw.length < 8 || changePw.isPending} |
| 111 | onClick={() => changePw.mutate()} |
| 112 | > |
| 113 | {S.account.changePassword} |
| 114 | </button> |
| 115 | </div> |
| 116 | </div> |
| 117 | </div> |
| 118 | ); |
| 119 | } |