import * as React from 'react';
import {useState} from 'react';
import {LinearProgress, Stack, useMediaQuery} from "@mui/material";
import Tooltip from '@mui/material/Tooltip';
import Typography from "@mui/material/Typography";
import EditIcon from '@mui/icons-material/Edit';
import Container from "@mui/material/Container";
import Card from "@mui/material/Card";
import Button from "@mui/material/Button";
import {useTranslation} from "react-i18next";
import session from "../app/Session";
import DeleteOutlineIcon from '@mui/icons-material/DeleteOutline';
import theme from "./theme";
import Dialog from "@mui/material/Dialog";
import DialogTitle from "@mui/material/DialogTitle";
import DialogContent from "@mui/material/DialogContent";
import TextField from "@mui/material/TextField";
import DialogActions from "@mui/material/DialogActions";
import routes from "./routes";
import IconButton from "@mui/material/IconButton";
import {useOutletContext} from "react-router-dom";
import {formatBytes} from "../app/utils";
import accountApi, {UnauthorizedError} from "../app/AccountApi";
const Account = () => {
if (!session.exists()) {
window.location.href = routes.app;
return <>>;
}
return (
);
};
const Basics = () => {
const { t } = useTranslation();
return (
Account
);
};
const Stats = () => {
const { t } = useTranslation();
const { account } = useOutletContext();
if (!account) {
return <>>;
}
const accountType = account.plan.code ?? "none";
const normalize = (value, max) => (value / max * 100);
return (
{t("Usage")}
{account?.role === "admin"
? <>Unlimited 👑 >
: t(`account_type_${accountType}`)}
{account.stats.messages}
{account.limits.messages > 0 ? t("of {{limit}}", { limit: account.limits.messages }) : t("Unlimited")}
0 ? normalize(account.stats.messages, account.limits.messages) : 100} />
{account.stats.emails}
{account.limits.emails > 0 ? t("of {{limit}}", { limit: account.limits.emails }) : t("Unlimited")}
0 ? normalize(account.stats.emails, account.limits.emails) : 100} />
{formatBytes(account.stats.attachment_total_size)}
{account.limits.attachment_total_size > 0 ? t("of {{limit}}", { limit: formatBytes(account.limits.attachment_total_size) }) : t("Unlimited")}
0 ? normalize(account.stats.attachment_total_size, account.limits.attachment_total_size) : 100} />
{account.limits.basis === "ip" &&
Usage stats and limits for this account are based on your IP address, so they may be shared
with other users.
}
);
};
const Delete = () => {
const { t } = useTranslation();
return (
{t("Delete account")}
);
};
const Username = () => {
const { t } = useTranslation();
const { account } = useOutletContext();
return (
{session.username()}
{account?.role === "admin"
? <>{" "}👑 >
: ""}
)
};
const ChangePassword = () => {
const { t } = useTranslation();
const [dialogKey, setDialogKey] = useState(0);
const [dialogOpen, setDialogOpen] = useState(false);
const labelId = "prefChangePassword";
const handleDialogOpen = () => {
setDialogKey(prev => prev+1);
setDialogOpen(true);
};
const handleDialogCancel = () => {
setDialogOpen(false);
};
const handleDialogSubmit = async (newPassword) => {
try {
await accountApi.changePassword(newPassword);
setDialogOpen(false);
console.debug(`[Account] Password changed`);
} catch (e) {
console.log(`[Account] Error changing password`, e);
if ((e instanceof UnauthorizedError)) {
session.resetAndRedirect(routes.login);
}
// TODO show error
}
};
return (
⬤⬤⬤⬤⬤⬤⬤⬤⬤⬤
)
};
const ChangePasswordDialog = (props) => {
const { t } = useTranslation();
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const changeButtonEnabled = (() => {
return newPassword.length > 0 && newPassword === confirmPassword;
})();
return (
Change password
setNewPassword(ev.target.value)}
fullWidth
variant="standard"
/>
setConfirmPassword(ev.target.value)}
fullWidth
variant="standard"
/>
{t("Cancel")}
props.onSubmit(newPassword)} disabled={!changeButtonEnabled}>{t("Change password")}
);
};
const DeleteAccount = () => {
const { t } = useTranslation();
const [dialogKey, setDialogKey] = useState(0);
const [dialogOpen, setDialogOpen] = useState(false);
const labelId = "prefDeleteAccount";
const handleDialogOpen = () => {
setDialogKey(prev => prev+1);
setDialogOpen(true);
};
const handleDialogCancel = () => {
setDialogOpen(false);
};
const handleDialogSubmit = async (newPassword) => {
try {
await accountApi.delete();
await db.delete();
setDialogOpen(false);
console.debug(`[Account] Account deleted`);
session.resetAndRedirect(routes.app);
} catch (e) {
console.log(`[Account] Error deleting account`, e);
if ((e instanceof UnauthorizedError)) {
session.resetAndRedirect(routes.login);
}
// TODO show error
}
};
return (
} onClick={handleDialogOpen}>
Delete account
)
};
const DeleteAccountDialog = (props) => {
const { t } = useTranslation();
const [username, setUsername] = useState("");
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
const buttonEnabled = username === session.username();
return (
{t("Delete account")}
{t("This will permanently delete your account, including all data that is stored on the server. If you really want to proceed, please type '{{username}}' in the text box below.", { username: session.username()})}
setUsername(ev.target.value)}
fullWidth
variant="standard"
/>
{t("prefs_users_dialog_button_cancel")}
{t("Permanently delete account")}
);
};
// FIXME duplicate code
const PrefGroup = (props) => {
return (
{props.children}
)
};
const Pref = (props) => {
return (
{props.title} {props.subtitle && ({props.subtitle}) }
{props.description &&
{props.description}
}
{props.children}
);
};
export default Account;