import { useContext, useState } from "react"; import appContext from "../../stores/appContext"; import { userService } from "../../services"; import { validate, ValidatorConfig } from "../../helpers/validator"; import toastHelper from "../Toast"; import showChangePasswordDialog from "../ChangePasswordDialog"; import showConfirmResetOpenIdDialog from "../ConfirmResetOpenIdDialog"; import "../../less/settings/my-account-section.less"; const validateConfig: ValidatorConfig = { minLength: 4, maxLength: 24, noSpace: true, noChinese: true, }; interface Props {} const MyAccountSection: React.FC = () => { const { userState } = useContext(appContext); const user = userState.user as User; const [username, setUsername] = useState(user.name); const openAPIRoute = `${window.location.origin}/h/${user.openId}/memo`; const handleUsernameChanged = (e: React.ChangeEvent) => { const nextUsername = e.target.value as string; setUsername(nextUsername); }; const handleConfirmEditUsernameBtnClick = async () => { if (username === user.name) { return; } const usernameValidResult = validate(username, validateConfig); if (!usernameValidResult.result) { toastHelper.error("Username " + usernameValidResult.reason); return; } try { await userService.updateUsername(username); await userService.doSignIn(); toastHelper.info("Username changed"); } catch (error: any) { toastHelper.error(error.message); } }; const handleChangePasswordBtnClick = () => { showChangePasswordDialog(); }; const handleResetOpenIdBtnClick = async () => { showConfirmResetOpenIdDialog(); }; const handlePreventDefault = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); }; return ( <>

Account Information

Open API

{openAPIRoute}

Reset API

Usage guide:

{`POST ${openAPIRoute}\nContent-type: application/json\n{\n  "content": "Hello, #memos ${window.location.origin}"\n}`}
); }; export default MyAccountSection;