chore: refactor account UI

This commit is contained in:
Paul Makles
2022-06-12 15:07:30 +01:00
parent db653b3981
commit 98bdea0b65
7 changed files with 145 additions and 207 deletions

View File

@@ -4,6 +4,7 @@ import { API } from "revolt.js";
import { useApplicationState } from "../../../mobx/State";
import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { modalController } from "../../../context/modals";
import { Form } from "./Form";
@@ -43,14 +44,34 @@ export function FormLogin() {
// This should be replaced in the future.
const client = state.config.createClient();
await client.fetchConfiguration();
const session = await client.api.post("/auth/session/login", {
let session = await client.api.post("/auth/session/login", {
...data,
friendly_name,
});
if (session.result !== "Success") {
alert("unsupported!");
return;
if (session.result === "MFA") {
const { allowed_methods } = session;
let mfa_response: API.MFAResponse = await new Promise(
(callback) =>
modalController.push({
type: "mfa_flow",
state: "unknown",
available_methods: allowed_methods,
callback,
}),
);
session = await client.api.post("/auth/session/login", {
mfa_response,
mfa_ticket: session.ticket,
friendly_name,
});
if (session.result === "MFA") {
// unreachable code
return;
}
}
const s = session;

View File

@@ -1,22 +1,19 @@
import { At, Key, Block } from "@styled-icons/boxicons-regular";
import {
Envelope,
HelpCircle,
Lock,
Trash,
Pencil,
} from "@styled-icons/boxicons-solid";
import { Envelope, Lock, Trash, Pencil } from "@styled-icons/boxicons-solid";
import { observer } from "mobx-react-lite";
import { useHistory } from "react-router-dom";
import { API } from "revolt.js";
import { Link } from "react-router-dom";
import styles from "./Panes.module.scss";
import { Text } from "preact-i18n";
import { useContext, useEffect, useState } from "preact/hooks";
import { Button, CategoryButton, LineDivider, Tip } from "@revoltchat/ui";
import { stopPropagation } from "../../../lib/stopPropagation";
import {
AccountDetail,
CategoryButton,
Column,
HiddenValue,
Tip,
} from "@revoltchat/ui";
import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { modalController } from "../../../context/modals";
@@ -27,26 +24,14 @@ import {
useClient,
} from "../../../context/revoltjs/RevoltClient";
import Tooltip from "../../../components/common/Tooltip";
import UserIcon from "../../../components/common/user/UserIcon";
export const Account = observer(() => {
const { openScreen, writeClipboard } = useIntermediate();
const { openScreen } = useIntermediate();
const logOut = useContext(LogOutContext);
const status = useContext(StatusContext);
const client = useClient();
const [email, setEmail] = useState("...");
const [revealEmail, setRevealEmail] = useState(false);
const [profile, setProfile] = useState<undefined | API.UserProfile>(
undefined,
);
const history = useHistory();
function switchPage(to: string) {
history.replace(`/settings/${to}`);
}
useEffect(() => {
if (email === "..." && status === ClientStatus.ONLINE) {
@@ -54,124 +39,48 @@ export const Account = observer(() => {
.get("/auth/account/")
.then((account) => setEmail(account.email));
}
if (profile === undefined && status === ClientStatus.ONLINE) {
client
.user!.fetchProfile()
.then((profile) => setProfile(profile ?? {}));
}
}, [client, email, profile, status]);
}, [client, email, status]);
return (
<div className={styles.user}>
<div className={styles.banner}>
<div className={styles.container}>
<UserIcon
className={styles.avatar}
target={client.user!}
size={72}
onClick={() => switchPage("profile")}
/>
<div className={styles.userDetail}>
<div className={styles.userContainer}>
<UserIcon
className={styles.tinyavatar}
target={client.user!}
size={25}
onClick={() => switchPage("profile")}
/>
<div className={styles.username}>
@{client.user!.username}
</div>
</div>
<div className={styles.userid}>
<Tooltip
content={
<Text id="app.settings.pages.account.unique_id" />
}>
<HelpCircle size={16} />
</Tooltip>
<Tooltip content={<Text id="app.special.copy" />}>
<a
onClick={() =>
writeClipboard(client.user!._id)
}>
{client.user!._id}
</a>
</Tooltip>
</div>
</div>
</div>
<Column group>
<AccountDetail user={client.user!} />
</Column>
{(
[
["username", client.user!.username, At],
["email", email, Envelope],
["password", "•••••••••", Key],
] as const
).map(([field, value, Icon]) => (
<CategoryButton
key={field}
icon={<Icon size={24} />}
description={
field === "email" ? (
<HiddenValue
value={value}
placeholder={"•••••••••••@••••••.•••"}
/>
) : (
value
)
}
account
action={<Pencil size={20} />}
onClick={() =>
openScreen({
id: "modify_account",
field,
})
}>
<Text id={`login.${field}`} />
</CategoryButton>
))}
<Button
onClick={() => switchPage("profile")}
palette="secondary">
<Text id="app.settings.pages.profile.edit_profile" />
</Button>
</div>
<div>
{(
[
[
"username",
client.user!.username,
<At key="at" size={24} />,
],
["email", email, <Envelope key="envelope" size={24} />],
["password", "•••••••••", <Key key="key" size={24} />],
] as const
).map(([field, value, icon]) => (
<CategoryButton
key={field}
icon={icon}
description={
field === "email" ? (
revealEmail ? (
<>
{value}{" "}
<a
style={{ fontSize: "13px" }}
onClick={(ev) =>
stopPropagation(
ev,
setRevealEmail(false),
)
}>
<Text id="app.special.modals.actions.hide" />
</a>
</>
) : (
<>
@.{" "}
<a
style={{ fontSize: "13px" }}
onClick={(ev) =>
stopPropagation(
ev,
setRevealEmail(true),
)
}>
<Text id="app.special.modals.actions.reveal" />
</a>
</>
)
) : (
value
)
}
account
action={<Pencil size={20} />}
onClick={() =>
openScreen({
id: "modify_account",
field,
})
}>
<Text id={`login.${field}`} />
</CategoryButton>
))}
</div>
<hr />
<h3>
<Text id="app.settings.pages.account.2fa.title" />
</h3>
@@ -197,10 +106,13 @@ export const Account = observer(() => {
action="chevron">
View my backup codes
</CategoryButton>*/}
<hr />
<h3>
<Text id="app.settings.pages.account.manage.title" />
</h3>
<h5>
<Text id="app.settings.pages.account.manage.description" />
</h5>
@@ -227,6 +139,7 @@ export const Account = observer(() => {
}>
<Text id="app.settings.pages.account.manage.disable" />
</CategoryButton>
<CategoryButton
icon={<Trash size={24} color="var(--error)" />}
description={
@@ -250,13 +163,14 @@ export const Account = observer(() => {
}>
<Text id="app.settings.pages.account.manage.delete" />
</CategoryButton>
<Tip>
<span>
<Text id="app.settings.tips.account.a" />
</span>{" "}
<a onClick={() => switchPage("profile")}>
<Link to="/settings/profile" replace>
<Text id="app.settings.tips.account.b" />
</a>
</Link>
</Tip>
</div>
);