import usePostGetTotem from "@/avatar/usePostGetTotem"; import useWipeTotem from "@/avatar/useWipeTotem"; import AvatarTitle from "@/components/AvatarTitle"; import { AvatarViewLoading } from "@/components/Loading"; import { useAvatarStore } from "@/store/Stores"; import CryptoJS from "crypto-js"; import { observer } from "mobx-react-lite"; import { useTranslations } from "next-intl"; import Link from "next/link"; import { useState } from "react"; import { Button, Col, Input, Row } from "reactstrap"; import { useTo } from "taehui-ts/fe-utilities"; export default observer( ({ isSessionLoading }: { isSessionLoading: boolean }) => { const t = useTranslations(); const { totem, taehuiAvatarID, taehuiAvatarName } = useAvatarStore(); const [avatarID, setAvatarID] = useState(""); const [avatarCipher, setAvatarCipher] = useState(""); const [autoLogIn, setAutoLogIn] = useState(false); const to = useTo(); const { mutateAsync: postGetTotem } = usePostGetTotem(); const onLogIn = async () => { await postGetTotem({ avatarID, avatarCipher }); if (autoLogIn) { window.localStorage.setItem("avatarID", avatarID); window.localStorage.setItem( "avatarCipher", CryptoJS.AES.encrypt( avatarCipher, "591A6F91-2A27-4A88-88FA-0FEB7CB5FD94", ).toString(), ); } window.localStorage.setItem("autoLogIn", autoLogIn.toString()); }; const { mutateAsync: wipeTotem } = useWipeTotem(); if (isSessionLoading) { return <AvatarViewLoading />; } if (totem) { return ( <Row className="g-0"> <AvatarTitle avatarID={taehuiAvatarID} avatarName={taehuiAvatarName}> <Row className="g-0"> <Col className="m-1" xs="auto"> <Button color="danger" onClick={async () => { await wipeTotem({ totem }); window.localStorage.removeItem("avatarID"); window.localStorage.removeItem("avatarCipher"); window.localStorage.removeItem("autoLogIn"); setAvatarCipher(""); setAutoLogIn(false); }} > {t("notLoggedIn")} </Button> </Col> <Col className="m-1" xs="auto"> <Button onClick={() => { to("/avatar/m"); }} > {t("doModifyAvatar")} </Button> </Col> </Row> </AvatarTitle> </Row> ); } return ( <> <Row className="g-0"> <Col className="m-1" xs="auto"> <Input placeholder={t("avatarID")} value={avatarID} onChange={({ target: { value } }) => { setAvatarID(value); }} /> </Col> </Row> <Row className="g-0"> <Col className="m-1" xs="auto"> <Input type="password" placeholder={t("avatarCipher")} value={avatarCipher} onChange={({ target: { value } }) => { setAvatarCipher(value); }} onKeyDown={async ({ key }) => { if (key === "Enter") { await onLogIn(); } }} /> </Col> <Col className="m-1" xs="auto"> <Button color="success" onClick={onLogIn}> {t("logIn")} </Button> </Col> <Col className="m-1" xs="auto"> <Button color={autoLogIn ? "success" : "danger"} onClick={() => { setAutoLogIn((prevState) => !prevState); }} > {t("autoLogIn")} </Button> </Col> <Col className="m-1" xs="auto"> <Link href="/avatar/w"> <Button color="primary">{t("signUp")}</Button> </Link> </Col> </Row> </> ); }, );