import { useState } from "react"; import useI18n from "../../hooks/useI18n"; import { useAppSelector } from "../../store"; import { userService } from "../../services"; import { validate, ValidatorConfig } from "../../helpers/validator"; import toastHelper from "../Toast"; import { showCommonDialog } from "../Dialog/CommonDialog"; import showChangePasswordDialog from "../ChangePasswordDialog"; 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 { t } = useI18n(); const user = useAppSelector((state) => state.user.user as User); const [username, setUsername] = useState(user.name); const openAPIRoute = `${window.location.origin}/api/memo?openId=${user.openId}`; 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.patchUser({ id: user.id, name: username, }); toastHelper.info("Username changed"); } catch (error: any) { console.error(error); toastHelper.error(error.response.data.message); } }; const handleChangePasswordBtnClick = () => { showChangePasswordDialog(); }; const handleResetOpenIdBtnClick = async () => { showCommonDialog({ title: "Reset Open API", content: "❗️The existing API will be invalidated and a new one will be generated, are you sure you want to reset?", style: "warning", }); }; const handlePreventDefault = (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); }; return ( <>

{t("setting.account-section.title")}

Open API

{openAPIRoute}

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