import { CallbackError, UserMySelf } from "lingchair-client-protocol" import ClientCache from "../../ClientCache.ts" import AvatarMySelf from "../AvatarMySelf.tsx" import useAsyncEffect from "../../utils/useAsyncEffect.ts" import getClient from "../../getClient.ts" import showSnackbar from "../../utils/showSnackbar.ts" import useEventListener from "../../utils/useEventListener.ts" import { Dialog, TextField } from "mdui" import * as React from 'react' export default function EditMyProfileDialog({ useRef }: { useRef: React.MutableRefObject }) { const [mySelf, setMySelf] = React.useState() useAsyncEffect(async () => setMySelf(await ClientCache.getMySelf() as UserMySelf)) const chooseAvatarFileRef = React.useRef(null) const editNickNameRef = React.useRef(null) const editUserNameRef = React.useRef(null) useEventListener(chooseAvatarFileRef, 'change', async (_) => { const file = chooseAvatarFileRef.current!.files?.[0] as File if (file == null) return try { const hash = await getClient().uploadFile({ fileName: 'UserAvatar', fileData: file, }) await mySelf?.setAvatarFileHashOrThrow(hash) showSnackbar({ message: "修改成功, 刷新页面以更新", }) } catch (e) { console.error(e) if (e instanceof CallbackError) showSnackbar({ message: '上传头像失败: ' + e.message }) showSnackbar({ message: '上传头像失败: ' + (e instanceof Error ? e.message : e) }) } }) return (
{ chooseAvatarFileRef.current!.value = '' chooseAvatarFileRef.current!.click() }} style={{ width: '50px', height: '50px', }} />
{ const input = e.target as HTMLInputElement input.select() input.setSelectionRange(0, 1145141919810) }}> useRef.current!.open = false}>取消 { try { await mySelf?.updateProfileOrThrow({ nickname: editNickNameRef.current?.value, username: editUserNameRef.current?.value, }) } catch (e) { if (e instanceof CallbackError) showSnackbar({ message: '更新资料失败: ' + e.message }) } showSnackbar({ message: "修改成功, 刷新页面以更新", }) }}>更新
) }