import { useContext, useState } from "react"; import appContext from "../stores/appContext"; import { userService } from "../services"; import utils from "../helpers/utils"; import { validate, ValidatorConfig } from "../helpers/validator"; import toastHelper from "./Toast"; import showChangePasswordDialog from "./ChangePasswordDialog"; import showConfirmResetOpenIdDialog from "./ConfirmResetOpenIdDialog"; import "../less/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 Model.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 (user.name === "guest") { toastHelper.info("Do not change my username"); return; } if (username === user.name) { return; } const usernameValidResult = validate(username, validateConfig); if (!usernameValidResult.result) { toastHelper.error("Username " + usernameValidResult.reason); return; } try { const isUsable = await userService.checkUsernameUsable(username); if (!isUsable) { toastHelper.error("Username is not available"); return; } await userService.updateUsername(username); await userService.doSignIn(); toastHelper.info("Username changed"); } catch (error: any) { toastHelper.error(error.message); } }; const handleChangePasswordBtnClick = () => { if (user.name === "guest") { toastHelper.info("Do not change my password"); return; } showChangePasswordDialog(); }; const handleResetOpenIdBtnClick = async () => { showConfirmResetOpenIdDialog(); }; const handlePreventDefault = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); }; return ( <>

Account Information

Open API (Experimental feature)

{openAPIRoute}

Reset API

Usage guide:

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