| 1 |
import * as React from "react"; |
| 2 |
import { useContext, useState } from "react"; |
| 3 |
import { |
| 4 |
Alert, |
| 5 |
Box, |
| 6 |
CardActions, |
| 7 |
CardContent, |
| 8 |
Chip, |
| 9 |
FormControl, |
| 10 |
FormControlLabel, |
| 11 |
LinearProgress, |
| 12 |
Link, |
| 13 |
Portal, |
| 14 |
Radio, |
| 15 |
RadioGroup, |
| 16 |
Select, |
| 17 |
Snackbar, |
| 18 |
Stack, |
| 19 |
Table, |
| 20 |
TableBody, |
| 21 |
TableCell, |
| 22 |
TableHead, |
| 23 |
TableRow, |
| 24 |
useMediaQuery, |
| 25 |
Tooltip, |
| 26 |
Typography, |
| 27 |
Container, |
| 28 |
Card, |
| 29 |
Button, |
| 30 |
Dialog, |
| 31 |
DialogTitle, |
| 32 |
DialogContent, |
| 33 |
TextField, |
| 34 |
IconButton, |
| 35 |
Menu, |
| 36 |
MenuItem, |
| 37 |
ListItemIcon, |
| 38 |
ListItemText, |
| 39 |
DialogContentText, |
| 40 |
useTheme, |
| 41 |
} from "@mui/material"; |
| 42 |
import EditIcon from "@mui/icons-material/Edit"; |
| 43 |
import { Trans, useTranslation } from "react-i18next"; |
| 44 |
import DeleteOutlineIcon from "@mui/icons-material/DeleteOutlineOutlined"; |
| 45 |
import StarIcon from "@mui/icons-material/Star"; |
| 46 |
import StarBorderIcon from "@mui/icons-material/StarBorder"; |
| 47 |
import RefreshIcon from "@mui/icons-material/Refresh"; |
| 48 |
import InfoOutlinedIcon from "@mui/icons-material/InfoOutlined"; |
| 49 |
import CelebrationIcon from "@mui/icons-material/Celebration"; |
| 50 |
import CloseIcon from "@mui/icons-material/Close"; |
| 51 |
import ContentCopy from "@mui/icons-material/ContentCopy"; |
| 52 |
import Public from "@mui/icons-material/Public"; |
| 53 |
import AddIcon from "@mui/icons-material/Add"; |
| 54 |
import routes from "./routes"; |
| 55 |
import { copyToClipboard, formatBytes, formatDate, formatDateTime, formatShortDuration, openUrl } from "../app/utils"; |
| 56 |
import accountApi, { LimitBasis, Role, SubscriptionInterval, SubscriptionStatus } from "../app/AccountApi"; |
| 57 |
import { Pref, PrefGroup } from "./Pref"; |
| 58 |
import db from "../app/db"; |
| 59 |
import UpgradeDialog from "./UpgradeDialog"; |
| 60 |
import AccountContext from "./AccountContext"; |
| 61 |
import { usePrefCache } from "./PrefCache"; |
| 62 |
import DialogFooter from "./DialogFooter"; |
| 63 |
import { Paragraph } from "./styles"; |
| 64 |
import { EmailPrimaryElsewhereError, IncorrectPasswordError, UnauthorizedError } from "../app/errors"; |
| 65 |
import { ProChip } from "./SubscriptionPopup"; |
| 66 |
import session from "../app/Session"; |
| 67 |
|
| 68 |
const Account = () => { |
| 69 |
if (!session.exists()) { |
| 70 |
window.location.href = routes.app; |
| 71 |
return <></>; |
| 72 |
} |
| 73 |
return ( |
| 74 |
<Container maxWidth="md" sx={{ marginTop: 3, marginBottom: 3 }}> |
| 75 |
<Stack spacing={3}> |
| 76 |
<Basics /> |
| 77 |
<Stats /> |
| 78 |
<Tokens /> |
| 79 |
<Delete /> |
| 80 |
</Stack> |
| 81 |
</Container> |
| 82 |
); |
| 83 |
}; |
| 84 |
|
| 85 |
const Basics = () => { |
| 86 |
const { t } = useTranslation(); |
| 87 |
return ( |
| 88 |
<Card sx={{ p: 3 }} aria-label={t("account_basics_title")}> |
| 89 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 90 |
{t("account_basics_title")} |
| 91 |
</Typography> |
| 92 |
<PrefGroup> |
| 93 |
<Username /> |
| 94 |
<ChangePassword /> |
| 95 |
<Emails /> |
| 96 |
<PhoneNumbers /> |
| 97 |
<AccountType /> |
| 98 |
</PrefGroup> |
| 99 |
</Card> |
| 100 |
); |
| 101 |
}; |
| 102 |
|
| 103 |
const Username = () => { |
| 104 |
const { t } = useTranslation(); |
| 105 |
const { account } = useContext(AccountContext); |
| 106 |
const labelId = "prefUsername"; |
| 107 |
|
| 108 |
return ( |
| 109 |
<Pref labelId={labelId} title={t("account_basics_username_title")} description={t("account_basics_username_description")}> |
| 110 |
<div aria-labelledby={labelId}> |
| 111 |
{session.username()} |
| 112 |
{account?.role === Role.ADMIN && ( |
| 113 |
<> |
| 114 |
{" "} |
| 115 |
<Tooltip title={t("account_basics_username_admin_tooltip")}> |
| 116 |
<span style={{ cursor: "default" }}>👑</span> |
| 117 |
</Tooltip> |
| 118 |
</> |
| 119 |
)} |
| 120 |
</div> |
| 121 |
</Pref> |
| 122 |
); |
| 123 |
}; |
| 124 |
|
| 125 |
const ChangePassword = () => { |
| 126 |
const { t } = useTranslation(); |
| 127 |
const [dialogKey, setDialogKey] = useState(0); |
| 128 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 129 |
const { account } = useContext(AccountContext); |
| 130 |
const labelId = "prefChangePassword"; |
| 131 |
|
| 132 |
const handleDialogOpen = () => { |
| 133 |
setDialogKey((prev) => prev + 1); |
| 134 |
setDialogOpen(true); |
| 135 |
}; |
| 136 |
|
| 137 |
const handleDialogClose = () => { |
| 138 |
setDialogOpen(false); |
| 139 |
}; |
| 140 |
|
| 141 |
return ( |
| 142 |
<Pref labelId={labelId} title={t("account_basics_password_title")} description={t("account_basics_password_description")}> |
| 143 |
<div aria-labelledby={labelId}> |
| 144 |
<Typography color="gray" sx={{ float: "left", fontSize: "0.7rem", lineHeight: "3.5" }}> |
| 145 |
⬤⬤⬤⬤⬤⬤⬤⬤⬤⬤ |
| 146 |
</Typography> |
| 147 |
{!account?.provisioned ? ( |
| 148 |
<Tooltip title={t("account_basics_password_description")}> |
| 149 |
<IconButton onClick={handleDialogOpen} aria-label={t("account_basics_password_description")}> |
| 150 |
<EditIcon /> |
| 151 |
</IconButton> |
| 152 |
</Tooltip> |
| 153 |
) : ( |
| 154 |
<Tooltip title={t("account_basics_cannot_edit_or_delete_provisioned_user")}> |
| 155 |
<span> |
| 156 |
<IconButton disabled> |
| 157 |
<EditIcon /> |
| 158 |
</IconButton> |
| 159 |
</span> |
| 160 |
</Tooltip> |
| 161 |
)} |
| 162 |
</div> |
| 163 |
<ChangePasswordDialog key={`changePasswordDialog${dialogKey}`} open={dialogOpen} onClose={handleDialogClose} /> |
| 164 |
</Pref> |
| 165 |
); |
| 166 |
}; |
| 167 |
|
| 168 |
const ChangePasswordDialog = (props) => { |
| 169 |
const theme = useTheme(); |
| 170 |
const { t } = useTranslation(); |
| 171 |
const [error, setError] = useState(""); |
| 172 |
const [currentPassword, setCurrentPassword] = useState(""); |
| 173 |
const [newPassword, setNewPassword] = useState(""); |
| 174 |
const [confirmPassword, setConfirmPassword] = useState(""); |
| 175 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 176 |
|
| 177 |
const handleDialogSubmit = async () => { |
| 178 |
try { |
| 179 |
console.debug(`[Account] Changing password`); |
| 180 |
await accountApi.changePassword(currentPassword, newPassword); |
| 181 |
props.onClose(); |
| 182 |
} catch (e) { |
| 183 |
console.log(`[Account] Error changing password`, e); |
| 184 |
if (e instanceof IncorrectPasswordError) { |
| 185 |
setError(t("account_basics_password_dialog_current_password_incorrect")); |
| 186 |
} else if (e instanceof UnauthorizedError) { |
| 187 |
await session.resetAndRedirect(routes.login); |
| 188 |
} else { |
| 189 |
setError(e.message); |
| 190 |
} |
| 191 |
} |
| 192 |
}; |
| 193 |
|
| 194 |
return ( |
| 195 |
<Dialog open={props.open} onClose={props.onClose} fullScreen={fullScreen}> |
| 196 |
<DialogTitle>{t("account_basics_password_dialog_title")}</DialogTitle> |
| 197 |
<DialogContent> |
| 198 |
<TextField |
| 199 |
margin="dense" |
| 200 |
id="current-password" |
| 201 |
label={t("account_basics_password_dialog_current_password_label")} |
| 202 |
aria-label={t("account_basics_password_dialog_current_password_label")} |
| 203 |
type="password" |
| 204 |
value={currentPassword} |
| 205 |
onChange={(ev) => setCurrentPassword(ev.target.value)} |
| 206 |
fullWidth |
| 207 |
variant="standard" |
| 208 |
/> |
| 209 |
<TextField |
| 210 |
margin="dense" |
| 211 |
id="new-password" |
| 212 |
label={t("account_basics_password_dialog_new_password_label")} |
| 213 |
aria-label={t("account_basics_password_dialog_new_password_label")} |
| 214 |
type="password" |
| 215 |
value={newPassword} |
| 216 |
onChange={(ev) => setNewPassword(ev.target.value)} |
| 217 |
fullWidth |
| 218 |
variant="standard" |
| 219 |
/> |
| 220 |
<TextField |
| 221 |
margin="dense" |
| 222 |
id="confirm" |
| 223 |
label={t("account_basics_password_dialog_confirm_password_label")} |
| 224 |
aria-label={t("account_basics_password_dialog_confirm_password_label")} |
| 225 |
type="password" |
| 226 |
value={confirmPassword} |
| 227 |
onChange={(ev) => setConfirmPassword(ev.target.value)} |
| 228 |
fullWidth |
| 229 |
variant="standard" |
| 230 |
/> |
| 231 |
</DialogContent> |
| 232 |
<DialogFooter status={error}> |
| 233 |
<Button onClick={props.onClose}>{t("common_cancel")}</Button> |
| 234 |
<Button |
| 235 |
onClick={handleDialogSubmit} |
| 236 |
disabled={newPassword.length === 0 || currentPassword.length === 0 || newPassword !== confirmPassword} |
| 237 |
> |
| 238 |
{t("account_basics_password_dialog_button_submit")} |
| 239 |
</Button> |
| 240 |
</DialogFooter> |
| 241 |
</Dialog> |
| 242 |
); |
| 243 |
}; |
| 244 |
|
| 245 |
const AccountType = () => { |
| 246 |
const { t } = useTranslation(); |
| 247 |
const { dateFormat } = usePrefCache(); |
| 248 |
const { account } = useContext(AccountContext); |
| 249 |
const [upgradeDialogKey, setUpgradeDialogKey] = useState(0); |
| 250 |
const [upgradeDialogOpen, setUpgradeDialogOpen] = useState(false); |
| 251 |
const [showPortalError, setShowPortalError] = useState(false); |
| 252 |
|
| 253 |
if (!account) { |
| 254 |
return <></>; |
| 255 |
} |
| 256 |
|
| 257 |
const handleUpgradeClick = () => { |
| 258 |
setUpgradeDialogKey((k) => k + 1); |
| 259 |
setUpgradeDialogOpen(true); |
| 260 |
}; |
| 261 |
|
| 262 |
const handleManageBilling = async () => { |
| 263 |
try { |
| 264 |
const response = await accountApi.createBillingPortalSession(); |
| 265 |
window.open(response.redirect_url, "billing_portal"); |
| 266 |
} catch (e) { |
| 267 |
console.log(`[Account] Error opening billing portal`, e); |
| 268 |
if (e instanceof UnauthorizedError) { |
| 269 |
await session.resetAndRedirect(routes.login); |
| 270 |
} else { |
| 271 |
setShowPortalError(true); |
| 272 |
} |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
// The account type is a base label ("Admin", "Basic", "Free", or the tier name) plus an optional |
| 277 |
// qualifier chip (admin tier status, or the billing interval). |
| 278 |
let accountType; |
| 279 |
let qualifierChip; |
| 280 |
if (account.role === Role.ADMIN) { |
| 281 |
accountType = t("account_basics_tier_admin"); |
| 282 |
qualifierChip = account.tier |
| 283 |
? t("account_basics_tier_admin_suffix_with_tier", { tier: account.tier.name }) |
| 284 |
: t("account_basics_tier_admin_suffix_no_tier"); |
| 285 |
} else if (!account.tier) { |
| 286 |
accountType = config.enable_payments ? t("account_basics_tier_free") : t("account_basics_tier_basic"); |
| 287 |
} else { |
| 288 |
accountType = account.tier.name; |
| 289 |
if (account.billing?.interval === SubscriptionInterval.MONTH) { |
| 290 |
qualifierChip = t("account_basics_tier_interval_monthly"); |
| 291 |
} else if (account.billing?.interval === SubscriptionInterval.YEAR) { |
| 292 |
qualifierChip = t("account_basics_tier_interval_yearly"); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return ( |
| 297 |
<Pref |
| 298 |
alignTop={account.billing?.status === SubscriptionStatus.PAST_DUE || account.billing?.cancel_at > 0} |
| 299 |
title={t("account_basics_tier_title")} |
| 300 |
description={t("account_basics_tier_description")} |
| 301 |
> |
| 302 |
<div> |
| 303 |
{accountType} |
| 304 |
{qualifierChip && <Chip size="small" label={qualifierChip} sx={{ ml: 1 }} />} |
| 305 |
{account.provisioned && <Chip size="small" label={t("account_basics_tier_provisioned")} sx={{ ml: 1 }} />} |
| 306 |
{account.billing?.paid_until && !account.billing?.cancel_at && ( |
| 307 |
<Tooltip |
| 308 |
title={t("account_basics_tier_paid_until", { |
| 309 |
date: formatDate(account.billing?.paid_until, dateFormat), |
| 310 |
})} |
| 311 |
> |
| 312 |
<span> |
| 313 |
<InfoIcon /> |
| 314 |
</span> |
| 315 |
</Tooltip> |
| 316 |
)} |
| 317 |
{config.enable_payments && account.role === Role.USER && !account.billing?.subscription && ( |
| 318 |
<Button |
| 319 |
variant="outlined" |
| 320 |
size="small" |
| 321 |
startIcon={<CelebrationIcon sx={{ color: "#55b86e" }} />} |
| 322 |
onClick={handleUpgradeClick} |
| 323 |
sx={{ ml: 1 }} |
| 324 |
> |
| 325 |
{t("account_basics_tier_upgrade_button")} |
| 326 |
</Button> |
| 327 |
)} |
| 328 |
{config.enable_payments && account.role === Role.USER && account.billing?.subscription && ( |
| 329 |
<Button variant="outlined" size="small" onClick={handleUpgradeClick} sx={{ ml: 1 }}> |
| 330 |
{t("account_basics_tier_change_button")} |
| 331 |
</Button> |
| 332 |
)} |
| 333 |
{config.enable_payments && account.role === Role.USER && account.billing?.customer && ( |
| 334 |
<Button variant="outlined" size="small" onClick={handleManageBilling} sx={{ ml: 1 }}> |
| 335 |
{t("account_basics_tier_manage_billing_button")} |
| 336 |
</Button> |
| 337 |
)} |
| 338 |
{config.enable_payments && ( |
| 339 |
<UpgradeDialog |
| 340 |
key={`upgradeDialogFromAccount${upgradeDialogKey}`} |
| 341 |
open={upgradeDialogOpen} |
| 342 |
onCancel={() => setUpgradeDialogOpen(false)} |
| 343 |
/> |
| 344 |
)} |
| 345 |
</div> |
| 346 |
{account.billing?.status === SubscriptionStatus.PAST_DUE && ( |
| 347 |
<Alert severity="error" sx={{ mt: 1 }}> |
| 348 |
{t("account_basics_tier_payment_overdue")} |
| 349 |
</Alert> |
| 350 |
)} |
| 351 |
{account.billing?.cancel_at > 0 && ( |
| 352 |
<Alert severity="warning" sx={{ mt: 1 }}> |
| 353 |
{t("account_basics_tier_canceled_subscription", { |
| 354 |
date: formatDate(account.billing.cancel_at, dateFormat), |
| 355 |
})} |
| 356 |
</Alert> |
| 357 |
)} |
| 358 |
<Portal> |
| 359 |
<Snackbar |
| 360 |
open={showPortalError} |
| 361 |
autoHideDuration={3000} |
| 362 |
onClose={() => setShowPortalError(false)} |
| 363 |
message={t("account_usage_cannot_create_portal_session")} |
| 364 |
/> |
| 365 |
</Portal> |
| 366 |
</Pref> |
| 367 |
); |
| 368 |
}; |
| 369 |
|
| 370 |
const Emails = () => { |
| 371 |
const { t } = useTranslation(); |
| 372 |
const { account } = useContext(AccountContext); |
| 373 |
const [dialogKey, setDialogKey] = useState(0); |
| 374 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 375 |
const [snack, setSnack] = useState(""); // Non-empty shows a transient snackbar message |
| 376 |
const [menuAnchor, setMenuAnchor] = useState(null); // Chip element the actions menu is anchored to |
| 377 |
const [menuEmail, setMenuEmail] = useState(null); // The email the open menu acts on |
| 378 |
const labelId = "prefVerifiedEmails"; |
| 379 |
|
| 380 |
const openMenu = (ev, email) => { |
| 381 |
setMenuAnchor(ev.currentTarget); |
| 382 |
setMenuEmail(email); |
| 383 |
}; |
| 384 |
const closeMenu = () => { |
| 385 |
setMenuAnchor(null); |
| 386 |
setMenuEmail(null); |
| 387 |
}; |
| 388 |
const runMenuAction = (fn) => { |
| 389 |
closeMenu(); |
| 390 |
fn(menuEmail.address); |
| 391 |
}; |
| 392 |
|
| 393 |
const handleDialogOpen = () => { |
| 394 |
setDialogKey((prev) => prev + 1); |
| 395 |
setDialogOpen(true); |
| 396 |
}; |
| 397 |
|
| 398 |
const handleDialogClose = () => { |
| 399 |
setDialogOpen(false); |
| 400 |
}; |
| 401 |
|
| 402 |
const handleCopy = (email) => { |
| 403 |
copyToClipboard(email); |
| 404 |
setSnack(t("account_basics_emails_copied_to_clipboard")); |
| 405 |
}; |
| 406 |
|
| 407 |
// runEmailAction wraps an email account action with the shared error handling used by the |
| 408 |
// delete/set-primary/resend handlers (redirect on unauthorized, surface a message otherwise). |
| 409 |
// The account view refreshes via the server's sync event, so there's no explicit refetch here. |
| 410 |
const runEmailAction = async (fn, errorMessage) => { |
| 411 |
try { |
| 412 |
await fn(); |
| 413 |
} catch (e) { |
| 414 |
console.log(`[Account] Email action failed`, e); |
| 415 |
if (e instanceof UnauthorizedError) { |
| 416 |
await session.resetAndRedirect(routes.login); |
| 417 |
} else if (e instanceof EmailPrimaryElsewhereError) { |
| 418 |
setSnack(t("account_basics_emails_primary_elsewhere")); |
| 419 |
} else { |
| 420 |
setSnack(errorMessage ?? e.message); |
| 421 |
} |
| 422 |
} |
| 423 |
}; |
| 424 |
|
| 425 |
const handleDelete = (email) => runEmailAction(() => accountApi.deleteEmail(email)); |
| 426 |
const handleSetPrimary = (email) => runEmailAction(() => accountApi.setPrimaryEmail(email)); |
| 427 |
const handleResend = (email) => |
| 428 |
runEmailAction(async () => { |
| 429 |
await accountApi.resendEmailVerification(email); |
| 430 |
setSnack(t("account_basics_emails_resent")); |
| 431 |
}); |
| 432 |
|
| 433 |
if (!config.enable_emails) { |
| 434 |
return null; |
| 435 |
} |
| 436 |
|
| 437 |
if (account?.limits.emails === 0) { |
| 438 |
return ( |
| 439 |
<Pref |
| 440 |
title={ |
| 441 |
<> |
| 442 |
{t("account_basics_emails_title")} |
| 443 |
{config.enable_payments && <ProChip />} |
| 444 |
</> |
| 445 |
} |
| 446 |
description={t("account_basics_emails_description")} |
| 447 |
> |
| 448 |
<em>{t("account_usage_emails_none")}</em> |
| 449 |
</Pref> |
| 450 |
); |
| 451 |
} |
| 452 |
|
| 453 |
const emails = account?.emails ?? []; |
| 454 |
// Verified addresses, primary always first |
| 455 |
const verifiedEmails = emails.filter((e) => !e.pending).sort((a, b) => (b.primary ? 1 : 0) - (a.primary ? 1 : 0)); |
| 456 |
const pendingEmails = emails.filter((e) => e.pending); |
| 457 |
const primaryEmail = verifiedEmails.find((e) => e.primary)?.address ?? ""; |
| 458 |
// Recovery nudges (skipped for provisioned users -- they can't reset, and the Add-email dialog |
| 459 |
// explains the limitation): prompt for a first email when there are none, or for a primary when |
| 460 |
// there are emails but none is primary. |
| 461 |
const recoveryRelevant = config.enable_reset_password && !account?.provisioned; |
| 462 |
const hasNoEmails = verifiedEmails.length === 0 && pendingEmails.length === 0; |
| 463 |
const showNoEmailWarning = recoveryRelevant && hasNoEmails; |
| 464 |
const showNoPrimaryWarning = recoveryRelevant && !hasNoEmails && primaryEmail === ""; |
| 465 |
|
| 466 |
return ( |
| 467 |
<Pref labelId={labelId} alignTop title={t("account_basics_emails_title")} description={t("account_basics_emails_description")}> |
| 468 |
<div aria-labelledby={labelId}> |
| 469 |
<Box sx={{ display: "flex", flexWrap: "wrap", alignItems: "center", gap: 0.75 }}> |
| 470 |
{verifiedEmails.map((email) => ( |
| 471 |
<Chip |
| 472 |
key={email.address} |
| 473 |
icon={email.primary ? <StarIcon /> : undefined} |
| 474 |
label={ |
| 475 |
<Tooltip |
| 476 |
title={email.primary ? t("account_basics_emails_chip_actions_primary") : t("account_basics_emails_chip_actions_verified")} |
| 477 |
> |
| 478 |
<span>{email.address}</span> |
| 479 |
</Tooltip> |
| 480 |
} |
| 481 |
variant="outlined" |
| 482 |
onClick={(ev) => openMenu(ev, email)} |
| 483 |
onDelete={() => handleDelete(email.address)} |
| 484 |
sx={email.primary ? { "& .MuiChip-icon": { color: "#fbc02d" } } : undefined} |
| 485 |
/> |
| 486 |
))} |
| 487 |
{pendingEmails.map((email) => ( |
| 488 |
<Chip |
| 489 |
key={email.address} |
| 490 |
label={ |
| 491 |
<Tooltip title={t("account_basics_emails_chip_actions_unverified")}> |
| 492 |
<span> |
| 493 |
{email.address} <em>({t("account_basics_emails_unverified")})</em> |
| 494 |
</span> |
| 495 |
</Tooltip> |
| 496 |
} |
| 497 |
variant="outlined" |
| 498 |
onClick={(ev) => openMenu(ev, email)} |
| 499 |
onDelete={() => handleDelete(email.address)} |
| 500 |
sx={{ opacity: 0.7 }} |
| 501 |
/> |
| 502 |
))} |
| 503 |
{verifiedEmails.length === 0 && pendingEmails.length === 0 && <em>{t("account_basics_emails_no_emails_yet")}</em>} |
| 504 |
<IconButton onClick={handleDialogOpen} aria-label={t("account_basics_emails_dialog_title")}> |
| 505 |
<AddIcon /> |
| 506 |
</IconButton> |
| 507 |
</Box> |
| 508 |
{showNoEmailWarning && ( |
| 509 |
<Alert severity="warning" sx={{ mt: 1 }}> |
| 510 |
{t("account_basics_emails_no_recovery_warning")} |
| 511 |
</Alert> |
| 512 |
)} |
| 513 |
{showNoPrimaryWarning && ( |
| 514 |
<Alert severity="warning" sx={{ mt: 1 }}> |
| 515 |
{t("account_basics_emails_no_primary_warning")} |
| 516 |
</Alert> |
| 517 |
)} |
| 518 |
</div> |
| 519 |
<Menu anchorEl={menuAnchor} open={Boolean(menuAnchor)} onClose={closeMenu}> |
| 520 |
<MenuItem onClick={() => runMenuAction(handleCopy)}> |
| 521 |
<ListItemIcon> |
| 522 |
<ContentCopy fontSize="small" /> |
| 523 |
</ListItemIcon> |
| 524 |
<ListItemText>{t("common_copy_to_clipboard")}</ListItemText> |
| 525 |
</MenuItem> |
| 526 |
{menuEmail && !menuEmail.pending && !menuEmail.primary && ( |
| 527 |
<MenuItem onClick={() => runMenuAction(handleSetPrimary)}> |
| 528 |
<ListItemIcon> |
| 529 |
<StarBorderIcon fontSize="small" /> |
| 530 |
</ListItemIcon> |
| 531 |
<ListItemText>{t("account_basics_emails_set_primary")}</ListItemText> |
| 532 |
</MenuItem> |
| 533 |
)} |
| 534 |
{menuEmail && menuEmail.pending && ( |
| 535 |
<MenuItem onClick={() => runMenuAction(handleResend)}> |
| 536 |
<ListItemIcon> |
| 537 |
<RefreshIcon fontSize="small" /> |
| 538 |
</ListItemIcon> |
| 539 |
<ListItemText>{t("account_basics_emails_resend")}</ListItemText> |
| 540 |
</MenuItem> |
| 541 |
)} |
| 542 |
<MenuItem onClick={() => runMenuAction(handleDelete)}> |
| 543 |
<ListItemIcon> |
| 544 |
<DeleteOutlineIcon fontSize="small" /> |
| 545 |
</ListItemIcon> |
| 546 |
<ListItemText>{t("account_basics_emails_delete")}</ListItemText> |
| 547 |
</MenuItem> |
| 548 |
</Menu> |
| 549 |
<AddEmailDialog key={`addEmailDialog${dialogKey}`} open={dialogOpen} onClose={handleDialogClose} /> |
| 550 |
<Portal> |
| 551 |
<Snackbar open={snack !== ""} autoHideDuration={3000} onClose={() => setSnack("")} message={snack} /> |
| 552 |
</Portal> |
| 553 |
</Pref> |
| 554 |
); |
| 555 |
}; |
| 556 |
|
| 557 |
const AddEmailDialog = (props) => { |
| 558 |
const theme = useTheme(); |
| 559 |
const { t } = useTranslation(); |
| 560 |
const [error, setError] = useState(""); |
| 561 |
const [email, setEmail] = useState(""); |
| 562 |
const [sending, setSending] = useState(false); |
| 563 |
const [sent, setSent] = useState(false); |
| 564 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 565 |
|
| 566 |
// handleSubmit starts verification: the server emails a magic link. The pending address shows |
| 567 |
// up in the account list as "(unverified)" once the account refreshes. |
| 568 |
const handleSubmit = async () => { |
| 569 |
try { |
| 570 |
setSending(true); |
| 571 |
setError(""); // Clear any error from a previous attempt |
| 572 |
await accountApi.startEmailVerification(email); |
| 573 |
setSent(true); |
| 574 |
} catch (e) { |
| 575 |
console.log(`[Account] Error starting email verification`, e); |
| 576 |
if (e instanceof UnauthorizedError) { |
| 577 |
await session.resetAndRedirect(routes.login); |
| 578 |
} else { |
| 579 |
setError(e.message); |
| 580 |
} |
| 581 |
} finally { |
| 582 |
setSending(false); |
| 583 |
} |
| 584 |
}; |
| 585 |
|
| 586 |
return ( |
| 587 |
<Dialog open={props.open} onClose={props.onClose} fullScreen={fullScreen}> |
| 588 |
<DialogTitle>{t("account_basics_emails_dialog_title")}</DialogTitle> |
| 589 |
<DialogContent> |
| 590 |
{sent ? ( |
| 591 |
<DialogContentText>{t("account_basics_emails_dialog_check_inbox")}</DialogContentText> |
| 592 |
) : ( |
| 593 |
<> |
| 594 |
<DialogContentText>{t("account_basics_emails_dialog_description")}</DialogContentText> |
| 595 |
<TextField |
| 596 |
autoFocus |
| 597 |
margin="dense" |
| 598 |
label={t("account_basics_emails_dialog_email_label")} |
| 599 |
aria-label={t("account_basics_emails_dialog_email_label")} |
| 600 |
placeholder={t("account_basics_emails_dialog_email_placeholder")} |
| 601 |
type="email" |
| 602 |
value={email} |
| 603 |
onChange={(ev) => setEmail(ev.target.value)} |
| 604 |
fullWidth |
| 605 |
variant="standard" |
| 606 |
/> |
| 607 |
</> |
| 608 |
)} |
| 609 |
</DialogContent> |
| 610 |
<DialogFooter status={error}> |
| 611 |
{sent ? ( |
| 612 |
<Button onClick={props.onClose}>{t("common_close")}</Button> |
| 613 |
) : ( |
| 614 |
<> |
| 615 |
<Button onClick={props.onClose}>{t("common_cancel")}</Button> |
| 616 |
<Button onClick={handleSubmit} disabled={sending || !/^[^\s,;]+@[^\s,;]+$/.test(email)}> |
| 617 |
{t("account_basics_emails_dialog_verify_button")} |
| 618 |
</Button> |
| 619 |
</> |
| 620 |
)} |
| 621 |
</DialogFooter> |
| 622 |
</Dialog> |
| 623 |
); |
| 624 |
}; |
| 625 |
|
| 626 |
const PhoneNumbers = () => { |
| 627 |
const { t } = useTranslation(); |
| 628 |
const { account } = useContext(AccountContext); |
| 629 |
const [dialogKey, setDialogKey] = useState(0); |
| 630 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 631 |
const [snackOpen, setSnackOpen] = useState(false); |
| 632 |
const labelId = "prefPhoneNumbers"; |
| 633 |
|
| 634 |
const handleDialogOpen = () => { |
| 635 |
setDialogKey((prev) => prev + 1); |
| 636 |
setDialogOpen(true); |
| 637 |
}; |
| 638 |
|
| 639 |
const handleDialogClose = () => { |
| 640 |
setDialogOpen(false); |
| 641 |
}; |
| 642 |
|
| 643 |
const handleCopy = (phoneNumber) => { |
| 644 |
copyToClipboard(phoneNumber); |
| 645 |
setSnackOpen(true); |
| 646 |
}; |
| 647 |
|
| 648 |
const handleDelete = async (phoneNumber) => { |
| 649 |
try { |
| 650 |
await accountApi.deletePhoneNumber(phoneNumber); |
| 651 |
} catch (e) { |
| 652 |
console.log(`[Account] Error deleting phone number`, e); |
| 653 |
if (e instanceof UnauthorizedError) { |
| 654 |
await session.resetAndRedirect(routes.login); |
| 655 |
} |
| 656 |
} |
| 657 |
}; |
| 658 |
|
| 659 |
if (!config.enable_calls) { |
| 660 |
return null; |
| 661 |
} |
| 662 |
|
| 663 |
if (account?.limits.calls === 0) { |
| 664 |
return ( |
| 665 |
<Pref |
| 666 |
title={ |
| 667 |
<> |
| 668 |
{t("account_basics_phone_numbers_title")} |
| 669 |
{config.enable_payments && <ProChip />} |
| 670 |
</> |
| 671 |
} |
| 672 |
description={t("account_basics_phone_numbers_description")} |
| 673 |
> |
| 674 |
<em>{t("account_usage_calls_none")}</em> |
| 675 |
</Pref> |
| 676 |
); |
| 677 |
} |
| 678 |
|
| 679 |
return ( |
| 680 |
<Pref labelId={labelId} title={t("account_basics_phone_numbers_title")} description={t("account_basics_phone_numbers_description")}> |
| 681 |
<div aria-labelledby={labelId}> |
| 682 |
<Box sx={{ display: "flex", flexWrap: "wrap", alignItems: "center", gap: 0.75 }}> |
| 683 |
{account?.phone_numbers?.map((phoneNumber) => ( |
| 684 |
<Chip |
| 685 |
label={ |
| 686 |
<Tooltip title={t("common_copy_to_clipboard")}> |
| 687 |
<span>{phoneNumber}</span> |
| 688 |
</Tooltip> |
| 689 |
} |
| 690 |
variant="outlined" |
| 691 |
onClick={() => handleCopy(phoneNumber)} |
| 692 |
onDelete={() => handleDelete(phoneNumber)} |
| 693 |
/> |
| 694 |
))} |
| 695 |
{!account?.phone_numbers && <em>{t("account_basics_phone_numbers_no_phone_numbers_yet")}</em>} |
| 696 |
<IconButton onClick={handleDialogOpen}> |
| 697 |
<AddIcon /> |
| 698 |
</IconButton> |
| 699 |
</Box> |
| 700 |
</div> |
| 701 |
<AddPhoneNumberDialog key={`addPhoneNumberDialog${dialogKey}`} open={dialogOpen} onClose={handleDialogClose} /> |
| 702 |
<Portal> |
| 703 |
<Snackbar |
| 704 |
open={snackOpen} |
| 705 |
autoHideDuration={3000} |
| 706 |
onClose={() => setSnackOpen(false)} |
| 707 |
message={t("account_basics_phone_numbers_copied_to_clipboard")} |
| 708 |
/> |
| 709 |
</Portal> |
| 710 |
</Pref> |
| 711 |
); |
| 712 |
}; |
| 713 |
|
| 714 |
const AddPhoneNumberDialog = (props) => { |
| 715 |
const theme = useTheme(); |
| 716 |
const { t } = useTranslation(); |
| 717 |
const [error, setError] = useState(""); |
| 718 |
const [phoneNumber, setPhoneNumber] = useState(""); |
| 719 |
const [channel, setChannel] = useState("sms"); |
| 720 |
const [code, setCode] = useState(""); |
| 721 |
const [sending, setSending] = useState(false); |
| 722 |
const [verificationCodeSent, setVerificationCodeSent] = useState(false); |
| 723 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 724 |
|
| 725 |
const verifyPhone = async () => { |
| 726 |
try { |
| 727 |
setSending(true); |
| 728 |
await accountApi.verifyPhoneNumber(phoneNumber, channel); |
| 729 |
setVerificationCodeSent(true); |
| 730 |
} catch (e) { |
| 731 |
console.log(`[Account] Error sending verification`, e); |
| 732 |
if (e instanceof UnauthorizedError) { |
| 733 |
await session.resetAndRedirect(routes.login); |
| 734 |
} else { |
| 735 |
setError(e.message); |
| 736 |
} |
| 737 |
} finally { |
| 738 |
setSending(false); |
| 739 |
} |
| 740 |
}; |
| 741 |
|
| 742 |
const checkVerifyPhone = async () => { |
| 743 |
try { |
| 744 |
setSending(true); |
| 745 |
await accountApi.addPhoneNumber(phoneNumber, code); |
| 746 |
props.onClose(); |
| 747 |
} catch (e) { |
| 748 |
console.log(`[Account] Error confirming verification`, e); |
| 749 |
if (e instanceof UnauthorizedError) { |
| 750 |
await session.resetAndRedirect(routes.login); |
| 751 |
} else { |
| 752 |
setError(e.message); |
| 753 |
} |
| 754 |
} finally { |
| 755 |
setSending(false); |
| 756 |
} |
| 757 |
}; |
| 758 |
|
| 759 |
const handleDialogSubmit = async () => { |
| 760 |
if (!verificationCodeSent) { |
| 761 |
await verifyPhone(); |
| 762 |
} else { |
| 763 |
await checkVerifyPhone(); |
| 764 |
} |
| 765 |
}; |
| 766 |
|
| 767 |
const handleCancel = () => { |
| 768 |
if (verificationCodeSent) { |
| 769 |
setVerificationCodeSent(false); |
| 770 |
setCode(""); |
| 771 |
} else { |
| 772 |
props.onClose(); |
| 773 |
} |
| 774 |
}; |
| 775 |
|
| 776 |
return ( |
| 777 |
<Dialog open={props.open} onClose={props.onClose} fullScreen={fullScreen}> |
| 778 |
<DialogTitle>{t("account_basics_phone_numbers_dialog_title")}</DialogTitle> |
| 779 |
<DialogContent> |
| 780 |
<DialogContentText>{t("account_basics_phone_numbers_dialog_description")}</DialogContentText> |
| 781 |
{!verificationCodeSent && ( |
| 782 |
<div style={{ display: "flex" }}> |
| 783 |
<TextField |
| 784 |
margin="dense" |
| 785 |
label={t("account_basics_phone_numbers_dialog_number_label")} |
| 786 |
aria-label={t("account_basics_phone_numbers_dialog_number_label")} |
| 787 |
placeholder={t("account_basics_phone_numbers_dialog_number_placeholder")} |
| 788 |
type="tel" |
| 789 |
value={phoneNumber} |
| 790 |
onChange={(ev) => setPhoneNumber(ev.target.value)} |
| 791 |
slotProps={{ htmlInput: { inputMode: "tel", pattern: "+[0-9]*" } }} |
| 792 |
variant="standard" |
| 793 |
sx={{ flexGrow: 1 }} |
| 794 |
/> |
| 795 |
<FormControl sx={{ flexWrap: "nowrap" }}> |
| 796 |
<RadioGroup row sx={{ flexGrow: 1, marginTop: "8px", marginLeft: "5px" }}> |
| 797 |
<FormControlLabel |
| 798 |
value="sms" |
| 799 |
control={<Radio checked={channel === "sms"} onChange={(e) => setChannel(e.target.value)} />} |
| 800 |
label={t("account_basics_phone_numbers_dialog_channel_sms")} |
| 801 |
/> |
| 802 |
<FormControlLabel |
| 803 |
value="call" |
| 804 |
control={<Radio checked={channel === "call"} onChange={(e) => setChannel(e.target.value)} />} |
| 805 |
label={t("account_basics_phone_numbers_dialog_channel_call")} |
| 806 |
sx={{ marginRight: 0 }} |
| 807 |
/> |
| 808 |
</RadioGroup> |
| 809 |
</FormControl> |
| 810 |
</div> |
| 811 |
)} |
| 812 |
{verificationCodeSent && ( |
| 813 |
<TextField |
| 814 |
margin="dense" |
| 815 |
label={t("account_basics_phone_numbers_dialog_code_label")} |
| 816 |
aria-label={t("account_basics_phone_numbers_dialog_code_label")} |
| 817 |
placeholder={t("account_basics_phone_numbers_dialog_code_placeholder")} |
| 818 |
type="text" |
| 819 |
value={code} |
| 820 |
onChange={(ev) => setCode(ev.target.value)} |
| 821 |
fullWidth |
| 822 |
slotProps={{ htmlInput: { inputMode: "numeric", pattern: "[0-9]*" } }} |
| 823 |
variant="standard" |
| 824 |
/> |
| 825 |
)} |
| 826 |
</DialogContent> |
| 827 |
<DialogFooter status={error}> |
| 828 |
<Button onClick={handleCancel}>{verificationCodeSent ? t("common_back") : t("common_cancel")}</Button> |
| 829 |
<Button onClick={handleDialogSubmit} disabled={sending || !/^\+\d+$/.test(phoneNumber)}> |
| 830 |
{!verificationCodeSent && channel === "sms" && t("account_basics_phone_numbers_dialog_verify_button_sms")} |
| 831 |
{!verificationCodeSent && channel === "call" && t("account_basics_phone_numbers_dialog_verify_button_call")} |
| 832 |
{verificationCodeSent && t("account_basics_phone_numbers_dialog_check_verification_button")} |
| 833 |
</Button> |
| 834 |
</DialogFooter> |
| 835 |
</Dialog> |
| 836 |
); |
| 837 |
}; |
| 838 |
|
| 839 |
const Stats = () => { |
| 840 |
const { t, i18n } = useTranslation(); |
| 841 |
const { account } = useContext(AccountContext); |
| 842 |
|
| 843 |
if (!account) { |
| 844 |
return <></>; |
| 845 |
} |
| 846 |
|
| 847 |
const normalize = (value, max) => Math.min((value / max) * 100, 100); |
| 848 |
|
| 849 |
return ( |
| 850 |
<Card sx={{ p: 3 }} aria-label={t("account_usage_title")}> |
| 851 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 852 |
{t("account_usage_title")} |
| 853 |
</Typography> |
| 854 |
<PrefGroup> |
| 855 |
{(account.role === Role.ADMIN || account.limits.reservations > 0) && ( |
| 856 |
<Pref title={t("account_usage_reservations_title")}> |
| 857 |
<div> |
| 858 |
<Typography variant="body2" sx={{ float: "left" }}> |
| 859 |
{account.stats.reservations.toLocaleString()} |
| 860 |
</Typography> |
| 861 |
<Typography variant="body2" sx={{ float: "right" }}> |
| 862 |
{account.role === Role.USER |
| 863 |
? t("account_usage_of_limit", { |
| 864 |
limit: account.limits.reservations.toLocaleString(), |
| 865 |
}) |
| 866 |
: t("account_usage_unlimited")} |
| 867 |
</Typography> |
| 868 |
</div> |
| 869 |
<LinearProgress |
| 870 |
variant="determinate" |
| 871 |
value={ |
| 872 |
account.role === Role.USER && account.limits.reservations > 0 |
| 873 |
? normalize(account.stats.reservations, account.limits.reservations) |
| 874 |
: 100 |
| 875 |
} |
| 876 |
/> |
| 877 |
</Pref> |
| 878 |
)} |
| 879 |
<Pref |
| 880 |
title={ |
| 881 |
<> |
| 882 |
{t("account_usage_messages_title")} |
| 883 |
<Tooltip title={t("account_usage_limits_reset_daily")}> |
| 884 |
<span> |
| 885 |
<InfoIcon /> |
| 886 |
</span> |
| 887 |
</Tooltip> |
| 888 |
</> |
| 889 |
} |
| 890 |
> |
| 891 |
<div> |
| 892 |
<Typography variant="body2" sx={{ float: "left" }}> |
| 893 |
{account.stats.messages.toLocaleString()} |
| 894 |
</Typography> |
| 895 |
<Typography variant="body2" sx={{ float: "right" }}> |
| 896 |
{account.role === Role.USER |
| 897 |
? t("account_usage_of_limit", { |
| 898 |
limit: account.limits.messages.toLocaleString(), |
| 899 |
}) |
| 900 |
: t("account_usage_unlimited")} |
| 901 |
</Typography> |
| 902 |
</div> |
| 903 |
<LinearProgress |
| 904 |
variant="determinate" |
| 905 |
value={account.role === Role.USER ? normalize(account.stats.messages, account.limits.messages) : 100} |
| 906 |
/> |
| 907 |
</Pref> |
| 908 |
{config.enable_emails && ( |
| 909 |
<Pref |
| 910 |
title={ |
| 911 |
<> |
| 912 |
{t("account_usage_emails_title")} |
| 913 |
<Tooltip title={t("account_usage_limits_reset_daily")}> |
| 914 |
<span> |
| 915 |
<InfoIcon /> |
| 916 |
</span> |
| 917 |
</Tooltip> |
| 918 |
</> |
| 919 |
} |
| 920 |
> |
| 921 |
<div> |
| 922 |
<Typography variant="body2" sx={{ float: "left" }}> |
| 923 |
{account.stats.emails.toLocaleString()} |
| 924 |
</Typography> |
| 925 |
<Typography variant="body2" sx={{ float: "right" }}> |
| 926 |
{account.role === Role.USER |
| 927 |
? t("account_usage_of_limit", { |
| 928 |
limit: account.limits.emails.toLocaleString(), |
| 929 |
}) |
| 930 |
: t("account_usage_unlimited")} |
| 931 |
</Typography> |
| 932 |
</div> |
| 933 |
<LinearProgress |
| 934 |
variant="determinate" |
| 935 |
value={account.role === Role.USER ? normalize(account.stats.emails, account.limits.emails) : 100} |
| 936 |
/> |
| 937 |
</Pref> |
| 938 |
)} |
| 939 |
{config.enable_calls && (account.role === Role.ADMIN || account.limits.calls > 0) && ( |
| 940 |
<Pref |
| 941 |
title={ |
| 942 |
<> |
| 943 |
{t("account_usage_calls_title")} |
| 944 |
<Tooltip title={t("account_usage_limits_reset_daily")}> |
| 945 |
<span> |
| 946 |
<InfoIcon /> |
| 947 |
</span> |
| 948 |
</Tooltip> |
| 949 |
</> |
| 950 |
} |
| 951 |
> |
| 952 |
<div> |
| 953 |
<Typography variant="body2" sx={{ float: "left" }}> |
| 954 |
{account.stats.calls.toLocaleString()} |
| 955 |
</Typography> |
| 956 |
<Typography variant="body2" sx={{ float: "right" }}> |
| 957 |
{account.role === Role.USER |
| 958 |
? t("account_usage_of_limit", { |
| 959 |
limit: account.limits.calls.toLocaleString(), |
| 960 |
}) |
| 961 |
: t("account_usage_unlimited")} |
| 962 |
</Typography> |
| 963 |
</div> |
| 964 |
<LinearProgress |
| 965 |
variant="determinate" |
| 966 |
value={account.role === Role.USER && account.limits.calls > 0 ? normalize(account.stats.calls, account.limits.calls) : 100} |
| 967 |
/> |
| 968 |
</Pref> |
| 969 |
)} |
| 970 |
<Pref |
| 971 |
alignTop |
| 972 |
title={t("account_usage_attachment_storage_title")} |
| 973 |
description={t("account_usage_attachment_storage_description", { |
| 974 |
filesize: formatBytes(account.limits.attachment_file_size), |
| 975 |
expiry: formatShortDuration(account.limits.attachment_expiry_duration * 1000, i18n.resolvedLanguage), |
| 976 |
})} |
| 977 |
> |
| 978 |
<div> |
| 979 |
<Typography variant="body2" sx={{ float: "left" }}> |
| 980 |
{formatBytes(account.stats.attachment_total_size)} |
| 981 |
</Typography> |
| 982 |
<Typography variant="body2" sx={{ float: "right" }}> |
| 983 |
{account.role === Role.USER |
| 984 |
? t("account_usage_of_limit", { |
| 985 |
limit: formatBytes(account.limits.attachment_total_size), |
| 986 |
}) |
| 987 |
: t("account_usage_unlimited")} |
| 988 |
</Typography> |
| 989 |
</div> |
| 990 |
<LinearProgress |
| 991 |
variant="determinate" |
| 992 |
value={account.role === Role.USER ? normalize(account.stats.attachment_total_size, account.limits.attachment_total_size) : 100} |
| 993 |
/> |
| 994 |
</Pref> |
| 995 |
{config.enable_reservations && account.role === Role.USER && account.limits.reservations === 0 && ( |
| 996 |
<Pref |
| 997 |
title={ |
| 998 |
<> |
| 999 |
{t("account_usage_reservations_title")} |
| 1000 |
{config.enable_payments && <ProChip />} |
| 1001 |
</> |
| 1002 |
} |
| 1003 |
> |
| 1004 |
<em>{t("account_usage_reservations_none")}</em> |
| 1005 |
</Pref> |
| 1006 |
)} |
| 1007 |
{config.enable_calls && account.role === Role.USER && account.limits.calls === 0 && ( |
| 1008 |
<Pref |
| 1009 |
title={ |
| 1010 |
<> |
| 1011 |
{t("account_usage_calls_title")} |
| 1012 |
{config.enable_payments && <ProChip />} |
| 1013 |
</> |
| 1014 |
} |
| 1015 |
> |
| 1016 |
<em>{t("account_usage_calls_none")}</em> |
| 1017 |
</Pref> |
| 1018 |
)} |
| 1019 |
</PrefGroup> |
| 1020 |
{account.role === Role.USER && account.limits.basis === LimitBasis.IP && ( |
| 1021 |
<Typography variant="body1" sx={{ pt: 3 }}> |
| 1022 |
{t("account_usage_basis_ip_description")} |
| 1023 |
</Typography> |
| 1024 |
)} |
| 1025 |
</Card> |
| 1026 |
); |
| 1027 |
}; |
| 1028 |
|
| 1029 |
const InfoIcon = () => ( |
| 1030 |
<InfoOutlinedIcon |
| 1031 |
sx={{ |
| 1032 |
verticalAlign: "middle", |
| 1033 |
width: "18px", |
| 1034 |
marginLeft: "4px", |
| 1035 |
color: "gray", |
| 1036 |
}} |
| 1037 |
/> |
| 1038 |
); |
| 1039 |
|
| 1040 |
const Tokens = () => { |
| 1041 |
const { t } = useTranslation(); |
| 1042 |
const { account } = useContext(AccountContext); |
| 1043 |
const [dialogKey, setDialogKey] = useState(0); |
| 1044 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 1045 |
const tokens = account?.tokens || []; |
| 1046 |
|
| 1047 |
const handleCreateClick = () => { |
| 1048 |
setDialogKey((prev) => prev + 1); |
| 1049 |
setDialogOpen(true); |
| 1050 |
}; |
| 1051 |
|
| 1052 |
const handleDialogClose = () => { |
| 1053 |
setDialogOpen(false); |
| 1054 |
}; |
| 1055 |
|
| 1056 |
return ( |
| 1057 |
<Card sx={{ padding: 1 }} aria-label={t("prefs_users_title")}> |
| 1058 |
<CardContent sx={{ paddingBottom: 1 }}> |
| 1059 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 1060 |
{t("account_tokens_title")} |
| 1061 |
</Typography> |
| 1062 |
<Paragraph> |
| 1063 |
<Trans |
| 1064 |
i18nKey="account_tokens_description" |
| 1065 |
components={{ |
| 1066 |
Link: <Link href="/docs/publish/#access-tokens" />, |
| 1067 |
}} |
| 1068 |
/> |
| 1069 |
</Paragraph> |
| 1070 |
<div style={{ width: "100%", overflowX: "auto" }}>{tokens?.length > 0 && <TokensTable tokens={tokens} />}</div> |
| 1071 |
</CardContent> |
| 1072 |
<CardActions> |
| 1073 |
<Button onClick={handleCreateClick}>{t("account_tokens_table_create_token_button")}</Button> |
| 1074 |
</CardActions> |
| 1075 |
<TokenDialog key={`tokenDialogCreate${dialogKey}`} open={dialogOpen} onClose={handleDialogClose} /> |
| 1076 |
</Card> |
| 1077 |
); |
| 1078 |
}; |
| 1079 |
|
| 1080 |
const TokensTable = (props) => { |
| 1081 |
const { t } = useTranslation(); |
| 1082 |
const { dateFormat, timeFormat } = usePrefCache(); |
| 1083 |
const [snackOpen, setSnackOpen] = useState(false); |
| 1084 |
const [upsertDialogKey, setUpsertDialogKey] = useState(0); |
| 1085 |
const [upsertDialogOpen, setUpsertDialogOpen] = useState(false); |
| 1086 |
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); |
| 1087 |
const [selectedToken, setSelectedToken] = useState(null); |
| 1088 |
|
| 1089 |
const tokens = (props.tokens || []).sort((a, b) => { |
| 1090 |
if (a.token === session.token()) { |
| 1091 |
return -1; |
| 1092 |
} |
| 1093 |
if (b.token === session.token()) { |
| 1094 |
return 1; |
| 1095 |
} |
| 1096 |
return a.token.localeCompare(b.token); |
| 1097 |
}); |
| 1098 |
|
| 1099 |
const handleEditClick = (token) => { |
| 1100 |
setUpsertDialogKey((prev) => prev + 1); |
| 1101 |
setSelectedToken(token); |
| 1102 |
setUpsertDialogOpen(true); |
| 1103 |
}; |
| 1104 |
|
| 1105 |
const handleDialogClose = () => { |
| 1106 |
setUpsertDialogOpen(false); |
| 1107 |
setDeleteDialogOpen(false); |
| 1108 |
setSelectedToken(null); |
| 1109 |
}; |
| 1110 |
|
| 1111 |
const handleDeleteClick = async (token) => { |
| 1112 |
setSelectedToken(token); |
| 1113 |
setDeleteDialogOpen(true); |
| 1114 |
}; |
| 1115 |
|
| 1116 |
const handleCopy = async (token) => { |
| 1117 |
copyToClipboard(token); |
| 1118 |
setSnackOpen(true); |
| 1119 |
}; |
| 1120 |
|
| 1121 |
return ( |
| 1122 |
<Table size="small" aria-label={t("account_tokens_title")}> |
| 1123 |
<TableHead> |
| 1124 |
<TableRow> |
| 1125 |
<TableCell sx={{ paddingLeft: 0 }}>{t("account_tokens_table_token_header")}</TableCell> |
| 1126 |
<TableCell>{t("account_tokens_table_label_header")}</TableCell> |
| 1127 |
<TableCell>{t("account_tokens_table_expires_header")}</TableCell> |
| 1128 |
<TableCell>{t("account_tokens_table_last_access_header")}</TableCell> |
| 1129 |
<TableCell /> |
| 1130 |
</TableRow> |
| 1131 |
</TableHead> |
| 1132 |
<TableBody> |
| 1133 |
{tokens.map((token) => { |
| 1134 |
const hasLastAccess = Number.isFinite(token.last_access) && token.last_access > 0; |
| 1135 |
const hasLastOrigin = !!token.last_origin; |
| 1136 |
|
| 1137 |
return ( |
| 1138 |
<TableRow key={token.token} sx={{ "&:last-child td, &:last-child th": { border: 0 } }}> |
| 1139 |
<TableCell |
| 1140 |
component="th" |
| 1141 |
scope="row" |
| 1142 |
sx={{ paddingLeft: 0, whiteSpace: "nowrap" }} |
| 1143 |
aria-label={t("account_tokens_table_token_header")} |
| 1144 |
> |
| 1145 |
<span> |
| 1146 |
<span style={{ fontFamily: "Monospace", fontSize: "0.9rem" }}>{token.token.slice(0, 12)}</span> |
| 1147 |
... |
| 1148 |
<Tooltip title={t("common_copy_to_clipboard")} placement="right"> |
| 1149 |
<IconButton onClick={() => handleCopy(token.token)}> |
| 1150 |
<ContentCopy /> |
| 1151 |
</IconButton> |
| 1152 |
</Tooltip> |
| 1153 |
</span> |
| 1154 |
</TableCell> |
| 1155 |
<TableCell aria-label={t("account_tokens_table_label_header")}> |
| 1156 |
{token.token === session.token() && <em>{t("account_tokens_table_current_session")}</em>} |
| 1157 |
{token.token !== session.token() && (token.label || "-")} |
| 1158 |
</TableCell> |
| 1159 |
<TableCell sx={{ whiteSpace: "nowrap" }} aria-label={t("account_tokens_table_expires_header")}> |
| 1160 |
{token.expires ? formatDateTime(token.expires, dateFormat, timeFormat) : <em>{t("account_tokens_table_never_expires")}</em>} |
| 1161 |
</TableCell> |
| 1162 |
<TableCell sx={{ whiteSpace: "nowrap" }} aria-label={t("account_tokens_table_last_access_header")}> |
| 1163 |
<div style={{ display: "flex", alignItems: "center" }}> |
| 1164 |
{hasLastAccess ? <span>{formatDateTime(token.last_access, dateFormat, timeFormat)}</span> : <em>-</em>} |
| 1165 |
{hasLastOrigin && ( |
| 1166 |
<Tooltip |
| 1167 |
title={t("account_tokens_table_last_origin_tooltip", { |
| 1168 |
ip: token.last_origin, |
| 1169 |
})} |
| 1170 |
> |
| 1171 |
<IconButton onClick={() => openUrl(`https://whatismyipaddress.com/ip/${token.last_origin}`)}> |
| 1172 |
<Public /> |
| 1173 |
</IconButton> |
| 1174 |
</Tooltip> |
| 1175 |
)} |
| 1176 |
</div> |
| 1177 |
</TableCell> |
| 1178 |
<TableCell align="right" sx={{ whiteSpace: "nowrap" }}> |
| 1179 |
{token.token !== session.token() && !token.provisioned && ( |
| 1180 |
<> |
| 1181 |
<Tooltip title={t("account_tokens_dialog_title_edit")}> |
| 1182 |
<IconButton onClick={() => handleEditClick(token)} aria-label={t("account_tokens_dialog_title_edit")}> |
| 1183 |
<EditIcon /> |
| 1184 |
</IconButton> |
| 1185 |
</Tooltip> |
| 1186 |
<Tooltip title={t("account_tokens_dialog_title_delete")}> |
| 1187 |
<IconButton onClick={() => handleDeleteClick(token)} aria-label={t("account_tokens_dialog_title_delete")}> |
| 1188 |
<CloseIcon /> |
| 1189 |
</IconButton> |
| 1190 |
</Tooltip> |
| 1191 |
</> |
| 1192 |
)} |
| 1193 |
{token.token === session.token() && ( |
| 1194 |
<Tooltip title={t("account_tokens_table_cannot_delete_or_edit")}> |
| 1195 |
<span> |
| 1196 |
<IconButton disabled> |
| 1197 |
<EditIcon /> |
| 1198 |
</IconButton> |
| 1199 |
<IconButton disabled> |
| 1200 |
<CloseIcon /> |
| 1201 |
</IconButton> |
| 1202 |
</span> |
| 1203 |
</Tooltip> |
| 1204 |
)} |
| 1205 |
{token.provisioned && ( |
| 1206 |
<Tooltip title={t("account_tokens_table_cannot_delete_or_edit_provisioned_token")}> |
| 1207 |
<span> |
| 1208 |
<IconButton disabled> |
| 1209 |
<EditIcon /> |
| 1210 |
</IconButton> |
| 1211 |
<IconButton disabled> |
| 1212 |
<CloseIcon /> |
| 1213 |
</IconButton> |
| 1214 |
</span> |
| 1215 |
</Tooltip> |
| 1216 |
)} |
| 1217 |
</TableCell> |
| 1218 |
</TableRow> |
| 1219 |
); |
| 1220 |
})} |
| 1221 |
</TableBody> |
| 1222 |
<Portal> |
| 1223 |
<Snackbar |
| 1224 |
open={snackOpen} |
| 1225 |
autoHideDuration={3000} |
| 1226 |
onClose={() => setSnackOpen(false)} |
| 1227 |
message={t("account_tokens_table_copied_to_clipboard")} |
| 1228 |
/> |
| 1229 |
</Portal> |
| 1230 |
<TokenDialog key={`tokenDialogEdit${upsertDialogKey}`} open={upsertDialogOpen} token={selectedToken} onClose={handleDialogClose} /> |
| 1231 |
<TokenDeleteDialog open={deleteDialogOpen} token={selectedToken} onClose={handleDialogClose} /> |
| 1232 |
</Table> |
| 1233 |
); |
| 1234 |
}; |
| 1235 |
|
| 1236 |
const TokenDialog = (props) => { |
| 1237 |
const theme = useTheme(); |
| 1238 |
const { t } = useTranslation(); |
| 1239 |
const [error, setError] = useState(""); |
| 1240 |
const [label, setLabel] = useState(props.token?.label || ""); |
| 1241 |
const [expires, setExpires] = useState(props.token ? -1 : 0); |
| 1242 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 1243 |
const editMode = !!props.token; |
| 1244 |
|
| 1245 |
const handleSubmit = async () => { |
| 1246 |
try { |
| 1247 |
if (editMode) { |
| 1248 |
await accountApi.updateToken(props.token.token, label, expires); |
| 1249 |
} else { |
| 1250 |
await accountApi.createToken(label, expires); |
| 1251 |
} |
| 1252 |
props.onClose(); |
| 1253 |
} catch (e) { |
| 1254 |
console.log(`[Account] Error creating token`, e); |
| 1255 |
if (e instanceof UnauthorizedError) { |
| 1256 |
await session.resetAndRedirect(routes.login); |
| 1257 |
} else { |
| 1258 |
setError(e.message); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
}; |
| 1262 |
|
| 1263 |
return ( |
| 1264 |
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}> |
| 1265 |
<DialogTitle>{editMode ? t("account_tokens_dialog_title_edit") : t("account_tokens_dialog_title_create")}</DialogTitle> |
| 1266 |
<DialogContent> |
| 1267 |
<TextField |
| 1268 |
margin="dense" |
| 1269 |
id="token-label" |
| 1270 |
label={t("account_tokens_dialog_label")} |
| 1271 |
aria-label={t("account_delete_dialog_label")} |
| 1272 |
type="text" |
| 1273 |
value={label} |
| 1274 |
onChange={(ev) => setLabel(ev.target.value)} |
| 1275 |
fullWidth |
| 1276 |
variant="standard" |
| 1277 |
/> |
| 1278 |
<FormControl fullWidth variant="standard" sx={{ mt: 1 }}> |
| 1279 |
<Select value={expires} onChange={(ev) => setExpires(ev.target.value)} aria-label={t("account_tokens_dialog_expires_label")}> |
| 1280 |
{editMode && <MenuItem value={-1}>{t("account_tokens_dialog_expires_unchanged")}</MenuItem>} |
| 1281 |
<MenuItem value={0}>{t("account_tokens_dialog_expires_never")}</MenuItem> |
| 1282 |
<MenuItem value={21600}>{t("account_tokens_dialog_expires_x_hours", { hours: 6 })}</MenuItem> |
| 1283 |
<MenuItem value={43200}>{t("account_tokens_dialog_expires_x_hours", { hours: 12 })}</MenuItem> |
| 1284 |
<MenuItem value={259200}>{t("account_tokens_dialog_expires_x_days", { days: 3 })}</MenuItem> |
| 1285 |
<MenuItem value={604800}>{t("account_tokens_dialog_expires_x_days", { days: 7 })}</MenuItem> |
| 1286 |
<MenuItem value={2592000}>{t("account_tokens_dialog_expires_x_days", { days: 30 })}</MenuItem> |
| 1287 |
<MenuItem value={7776000}>{t("account_tokens_dialog_expires_x_days", { days: 90 })}</MenuItem> |
| 1288 |
<MenuItem value={15552000}>{t("account_tokens_dialog_expires_x_days", { days: 180 })}</MenuItem> |
| 1289 |
</Select> |
| 1290 |
</FormControl> |
| 1291 |
</DialogContent> |
| 1292 |
<DialogFooter status={error}> |
| 1293 |
<Button onClick={props.onClose}>{t("account_tokens_dialog_button_cancel")}</Button> |
| 1294 |
<Button onClick={handleSubmit}> |
| 1295 |
{editMode ? t("account_tokens_dialog_button_update") : t("account_tokens_dialog_button_create")} |
| 1296 |
</Button> |
| 1297 |
</DialogFooter> |
| 1298 |
</Dialog> |
| 1299 |
); |
| 1300 |
}; |
| 1301 |
|
| 1302 |
const TokenDeleteDialog = (props) => { |
| 1303 |
const { t } = useTranslation(); |
| 1304 |
const [error, setError] = useState(""); |
| 1305 |
|
| 1306 |
const handleSubmit = async () => { |
| 1307 |
try { |
| 1308 |
await accountApi.deleteToken(props.token.token); |
| 1309 |
props.onClose(); |
| 1310 |
} catch (e) { |
| 1311 |
console.log(`[Account] Error deleting token`, e); |
| 1312 |
if (e instanceof UnauthorizedError) { |
| 1313 |
await session.resetAndRedirect(routes.login); |
| 1314 |
} else { |
| 1315 |
setError(e.message); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
}; |
| 1319 |
|
| 1320 |
return ( |
| 1321 |
<Dialog open={props.open} onClose={props.onClose}> |
| 1322 |
<DialogTitle>{t("account_tokens_delete_dialog_title")}</DialogTitle> |
| 1323 |
<DialogContent> |
| 1324 |
<DialogContentText> |
| 1325 |
<Trans i18nKey="account_tokens_delete_dialog_description" /> |
| 1326 |
</DialogContentText> |
| 1327 |
</DialogContent> |
| 1328 |
<DialogFooter status={error}> |
| 1329 |
<Button onClick={props.onClose}>{t("common_cancel")}</Button> |
| 1330 |
<Button onClick={handleSubmit} color="error"> |
| 1331 |
{t("account_tokens_delete_dialog_submit_button")} |
| 1332 |
</Button> |
| 1333 |
</DialogFooter> |
| 1334 |
</Dialog> |
| 1335 |
); |
| 1336 |
}; |
| 1337 |
|
| 1338 |
const Delete = () => { |
| 1339 |
const { t } = useTranslation(); |
| 1340 |
return ( |
| 1341 |
<Card sx={{ p: 3 }} aria-label={t("account_delete_title")}> |
| 1342 |
<Typography variant="h5" sx={{ marginBottom: 2 }}> |
| 1343 |
{t("account_delete_title")} |
| 1344 |
</Typography> |
| 1345 |
<PrefGroup> |
| 1346 |
<DeleteAccount /> |
| 1347 |
</PrefGroup> |
| 1348 |
</Card> |
| 1349 |
); |
| 1350 |
}; |
| 1351 |
|
| 1352 |
const DeleteAccount = () => { |
| 1353 |
const { t } = useTranslation(); |
| 1354 |
const [dialogKey, setDialogKey] = useState(0); |
| 1355 |
const [dialogOpen, setDialogOpen] = useState(false); |
| 1356 |
const { account } = useContext(AccountContext); |
| 1357 |
|
| 1358 |
const handleDialogOpen = () => { |
| 1359 |
setDialogKey((prev) => prev + 1); |
| 1360 |
setDialogOpen(true); |
| 1361 |
}; |
| 1362 |
|
| 1363 |
const handleDialogClose = () => { |
| 1364 |
setDialogOpen(false); |
| 1365 |
}; |
| 1366 |
|
| 1367 |
return ( |
| 1368 |
<Pref title={t("account_delete_title")} description={t("account_delete_description")}> |
| 1369 |
<div> |
| 1370 |
{!account?.provisioned ? ( |
| 1371 |
<Button fullWidth={false} variant="outlined" color="error" startIcon={<DeleteOutlineIcon />} onClick={handleDialogOpen}> |
| 1372 |
{t("account_delete_title")} |
| 1373 |
</Button> |
| 1374 |
) : ( |
| 1375 |
<Tooltip title={t("account_basics_cannot_edit_or_delete_provisioned_user")}> |
| 1376 |
<span> |
| 1377 |
<Button fullWidth={false} variant="outlined" color="error" startIcon={<DeleteOutlineIcon />} disabled> |
| 1378 |
{t("account_delete_title")} |
| 1379 |
</Button> |
| 1380 |
</span> |
| 1381 |
</Tooltip> |
| 1382 |
)} |
| 1383 |
</div> |
| 1384 |
<DeleteAccountDialog key={`deleteAccountDialog${dialogKey}`} open={dialogOpen} onClose={handleDialogClose} /> |
| 1385 |
</Pref> |
| 1386 |
); |
| 1387 |
}; |
| 1388 |
|
| 1389 |
const DeleteAccountDialog = (props) => { |
| 1390 |
const theme = useTheme(); |
| 1391 |
const { t } = useTranslation(); |
| 1392 |
const { account } = useContext(AccountContext); |
| 1393 |
const [error, setError] = useState(""); |
| 1394 |
const [password, setPassword] = useState(""); |
| 1395 |
const fullScreen = useMediaQuery(theme.breakpoints.down("sm")); |
| 1396 |
|
| 1397 |
const handleSubmit = async () => { |
| 1398 |
try { |
| 1399 |
await accountApi.delete(password); |
| 1400 |
await db().delete(); |
| 1401 |
console.debug(`[Account] Account deleted`); |
| 1402 |
await session.resetAndRedirect(routes.app, { fade: true }); |
| 1403 |
} catch (e) { |
| 1404 |
console.log(`[Account] Error deleting account`, e); |
| 1405 |
if (e instanceof IncorrectPasswordError) { |
| 1406 |
setError(t("account_basics_password_dialog_current_password_incorrect")); |
| 1407 |
} else if (e instanceof UnauthorizedError) { |
| 1408 |
await session.resetAndRedirect(routes.login); |
| 1409 |
} else { |
| 1410 |
setError(e.message); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
}; |
| 1414 |
|
| 1415 |
return ( |
| 1416 |
<Dialog open={props.open} onClose={props.onClose} fullScreen={fullScreen}> |
| 1417 |
<DialogTitle>{t("account_delete_title")}</DialogTitle> |
| 1418 |
<DialogContent> |
| 1419 |
<Typography variant="body1">{t("account_delete_dialog_description")}</Typography> |
| 1420 |
<TextField |
| 1421 |
margin="dense" |
| 1422 |
id="account-delete-confirm" |
| 1423 |
label={t("account_delete_dialog_label")} |
| 1424 |
aria-label={t("account_delete_dialog_label")} |
| 1425 |
type="password" |
| 1426 |
value={password} |
| 1427 |
onChange={(ev) => setPassword(ev.target.value)} |
| 1428 |
fullWidth |
| 1429 |
variant="standard" |
| 1430 |
/> |
| 1431 |
{account?.billing?.subscription && ( |
| 1432 |
<Alert severity="warning" sx={{ mt: 1 }}> |
| 1433 |
{t("account_delete_dialog_billing_warning")} |
| 1434 |
</Alert> |
| 1435 |
)} |
| 1436 |
</DialogContent> |
| 1437 |
<DialogFooter status={error}> |
| 1438 |
<Button onClick={props.onClose}>{t("account_delete_dialog_button_cancel")}</Button> |
| 1439 |
<Button onClick={handleSubmit} color="error" disabled={password.length === 0}> |
| 1440 |
{t("account_delete_dialog_button_submit")} |
| 1441 |
</Button> |
| 1442 |
</DialogFooter> |
| 1443 |
</Dialog> |
| 1444 |
); |
| 1445 |
}; |
| 1446 |
|
| 1447 |
export default Account; |