import { FormEvent, useState } from 'react' import toast from 'react-hot-toast' import { EyeOff, KeyRound, Save } from 'lucide-react' import { authAPI } from '../api/auth' import { profileAPI } from '../api/profile' import { requestPassword } from '../components/PasswordDialog' import { useAuthStore } from '../stores/auth' export function ProfilePage() { const user = useAuthStore((s) => s.user) const setUser = useAuthStore((s) => s.setUser) const [username, setUsername] = useState(user?.username ?? '') const [nickname, setNickname] = useState(user?.nickname ?? '') const [email, setEmail] = useState(user?.email ?? '') const [avatar, setAvatar] = useState(user?.avatar_url ?? '') const [hideAdult, setHideAdult] = useState(Boolean(user?.hide_adult)) const [oldPwd, setOldPwd] = useState('') const [newPwd, setNewPwd] = useState('') const onProfile = async (e: FormEvent) => { e.preventDefault() try { let password: string | undefined const hideAdultChanged = hideAdult !== Boolean(user?.hide_adult) if (hideAdultChanged) { const input = await requestPassword({ title: hideAdult ? '隐藏成人目录' : '取消隐藏成人目录', message: '此设置会同步影响 Web 与 Emby/Jellyfin/Infuse 等第三方客户端,请输入当前账号密码确认。', confirmText: '保存设置', }) if (!input) return password = input } const patch: Parameters[0] = { username, nickname, email, avatar_url: avatar, password, } if (hideAdultChanged) { patch.hide_adult = hideAdult } const u = await profileAPI.update(patch) setUser(u) toast.success('资料已更新') } catch (err: unknown) { const msg = (err as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '保存失败' toast.error(msg) } } const onPwd = async (e: FormEvent) => { e.preventDefault() try { await authAPI.changePassword(oldPwd, newPwd) toast.success('密码已更新') setOldPwd('') setNewPwd('') } catch (err: unknown) { const msg = (err as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '密码更新失败' toast.error(msg) } } return (

个人资料

基本信息

setUsername(e.target.value)} autoComplete="username" /> setNickname(e.target.value)} /> setEmail(e.target.value)} /> setAvatar(e.target.value)} />

修改密码

setOldPwd(e.target.value)} autoComplete="current-password" /> setNewPwd(e.target.value)} autoComplete="new-password" />
) } function Field({ label, children }: { label: string; children: React.ReactNode }) { return ( ) }