Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Refine staff source cleanup

Manav Rathibbe2278e954d
diff --git a/infra/staff/src/App.css b/infra/staff/src/App.css
index 4b86d5d7c8..37eb3a7bc4 100644
--- a/infra/staff/src/App.css
+++ b/infra/staff/src/App.css
@@ -73,6 +73,10 @@
font-weight: bold;
}
+.empty-state-image {
+ margin-top: 150px;
+}
+
@media screen and (max-width: 475px) {
.link-text {
font-size: 30px;
diff --git a/infra/staff/src/App.tsx b/infra/staff/src/App.tsx
index 0ade251e29..1ad27329fa 100644
--- a/infra/staff/src/App.tsx
+++ b/infra/staff/src/App.tsx
@@ -84,7 +84,7 @@ export const App: React.FC = () => {
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
- "X-AUTH-TOKEN": authToken ?? getToken(),
+ "X-Auth-Token": authToken ?? getToken(),
},
});
if (!response.ok) {
@@ -235,7 +235,7 @@ export const App: React.FC = () => {
console.error("Fetch data error:", error),
);
}}
- style={{ padding: "0 16px" }}
+ sx={{ px: 2 }}
>
FETCH
</Button>
@@ -310,7 +310,7 @@ export const App: React.FC = () => {
<img
src={duckieimage}
alt="duckie"
- style={{ marginTop: "150px" }}
+ className="empty-state-image"
/>
)}
</div>
diff --git a/infra/staff/src/components/AddOTT.tsx b/infra/staff/src/components/AddOTT.tsx
index f2f1bf39fb..58aa61377b 100644
--- a/infra/staff/src/components/AddOTT.tsx
+++ b/infra/staff/src/components/AddOTT.tsx
@@ -15,7 +15,11 @@ import {
Typography,
} from "@mui/material";
import React, { useEffect, useState } from "react";
-import { apiOrigin, requireToken } from "../services/support";
+import {
+ apiOrigin,
+ requireToken,
+ responseErrorMessage,
+} from "../services/support";
const APP_OPTIONS = [
{ label: "Photos", value: "photos" },
@@ -67,7 +71,7 @@ export const AddOTT: React.FC<AddOTTProps> = ({ open, onClose, userEmail }) => {
const token = requireToken();
- const otp = generateOtp();
+ const otp = generateOTP();
const expiryTime = computeExpiryTimeMicros();
setExpiryPreview(expiryTime);
@@ -86,8 +90,12 @@ export const AddOTT: React.FC<AddOTTProps> = ({ open, onClose, userEmail }) => {
});
if (!response.ok) {
- const message = await response.text();
- throw new Error(message || "Failed to create OTT");
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Failed to create OTT",
+ ),
+ );
}
setCreatedOtp(otp);
@@ -198,7 +206,8 @@ export const AddOTT: React.FC<AddOTTProps> = ({ open, onClose, userEmail }) => {
const computeExpiryTimeMicros = () =>
Date.now() * 1000 + 7 * 24 * 60 * 60 * 1_000_000;
-const generateOtp = () =>
- Math.floor(Math.random() * 1_000_000)
- .toString()
- .padStart(6, "0");
+const generateOTP = () => {
+ const values = new Uint32Array(1);
+ crypto.getRandomValues(values);
+ return `${values[0]! % 1_000_000}`.padStart(6, "0");
+};
diff --git a/infra/staff/src/components/ChangeEmail.tsx b/infra/staff/src/components/ChangeEmail.tsx
index 65529688f9..cc8dfcc2f3 100644
--- a/infra/staff/src/components/ChangeEmail.tsx
+++ b/infra/staff/src/components/ChangeEmail.tsx
@@ -1,5 +1,6 @@
import CloseIcon from "@mui/icons-material/Close";
import {
+ Box,
Button,
Dialog,
DialogActions,
@@ -13,12 +14,9 @@ import {
apiOrigin,
getCurrentAdminUserId,
requireToken,
+ responseErrorMessage,
} from "../services/support";
-interface ErrorResponse {
- message: string;
-}
-
interface ChangeEmailProps {
open: boolean;
onClose: () => void;
@@ -64,20 +62,18 @@ export const ChangeEmail: React.FC<ChangeEmailProps> = ({ open, onClose }) => {
method: "PUT",
headers: {
"Content-Type": "application/json",
- "X-AUTH-TOKEN": token,
+ "X-Auth-Token": token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
- let errorMessage = "Network response was not ok";
- try {
- const errorData = (await response.json()) as ErrorResponse;
- errorMessage = errorData.message;
- } catch (error) {
- console.error("Error parsing error response:", error);
- }
- throw new Error(errorMessage);
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Network response was not ok",
+ ),
+ );
}
onClose();
@@ -100,28 +96,22 @@ export const ChangeEmail: React.FC<ChangeEmailProps> = ({ open, onClose }) => {
paper: { style: { width: "444px", height: "300px" } },
}}
>
- <DialogTitle style={{ marginBottom: "20px", marginTop: "20px" }}>
+ <DialogTitle sx={dialogTitleSx}>
Change Email
- <Button
- onClick={onClose}
- style={{ position: "absolute", right: 10, top: 10 }}
- >
- <CloseIcon style={{ color: "black" }} />
+ <Button onClick={onClose} sx={dialogCloseButtonSx}>
+ <CloseIcon sx={{ color: "black" }} />
</Button>
</DialogTitle>
<DialogContent>
<form onSubmit={handleSubmit}>
- <div style={{ marginBottom: "16px" }}>
- <label
+ <Box sx={{ mb: 2 }}>
+ <Box
+ component="label"
htmlFor="newEmail"
- style={{
- textAlign: "left",
- display: "block",
- marginBottom: "4px",
- }}
+ sx={fieldLabelSx}
>
Email
- </label>
+ </Box>
<TextField
id="newEmail"
name="newEmail"
@@ -129,19 +119,10 @@ export const ChangeEmail: React.FC<ChangeEmailProps> = ({ open, onClose }) => {
onChange={handleChange}
fullWidth
/>
- </div>
-
- <DialogActions
- style={{ justifyContent: "center", marginTop: "40px" }}
- >
- <Button
- type="submit"
- variant="contained"
- style={{
- backgroundColor: "#00B33C",
- color: "white",
- }}
- >
+ </Box>
+
+ <DialogActions sx={{ justifyContent: "center", mt: 5 }}>
+ <Button type="submit" variant="contained" sx={submitSx}>
Change Email
</Button>
</DialogActions>
@@ -150,3 +131,11 @@ export const ChangeEmail: React.FC<ChangeEmailProps> = ({ open, onClose }) => {
</Dialog>
);
};
+
+const dialogTitleSx = { mb: "20px", mt: "20px" };
+
+const dialogCloseButtonSx = { position: "absolute", right: 10, top: 10 };
+
+const fieldLabelSx = { display: "block", mb: "4px", textAlign: "left" };
+
+const submitSx = { bgcolor: "#00B33C", color: "white" };
diff --git a/infra/staff/src/components/CloseFamily.tsx b/infra/staff/src/components/CloseFamily.tsx
index fde2c97f28..bbdd42c0b1 100644
--- a/infra/staff/src/components/CloseFamily.tsx
+++ b/infra/staff/src/components/CloseFamily.tsx
@@ -1,18 +1,11 @@
-import {
- Button,
- Dialog,
- DialogActions,
- DialogContent,
- DialogContentText,
- DialogTitle,
- Paper,
-} from "@mui/material";
import React, { useState } from "react";
import {
apiOrigin,
getCurrentAdminUserId,
requireToken,
+ responseErrorMessage,
} from "../services/support";
+import { ConfirmationDialog } from "./ConfirmationDialog";
interface CloseFamilyProps {
open: boolean;
@@ -32,20 +25,25 @@ export const CloseFamily: React.FC<CloseFamilyProps> = ({
setLoading(true);
const token = requireToken();
const userId = await getCurrentAdminUserId();
-
- const closeFamilyUrl = `${apiOrigin}/admin/user/close-family`;
- const closeFamilyResponse = await fetch(closeFamilyUrl, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- "X-Auth-Token": token,
+ const response = await fetch(
+ `${apiOrigin}/admin/user/close-family`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "X-Auth-Token": token,
+ },
+ body: JSON.stringify({ userId }),
},
- body: JSON.stringify({ userId }),
- });
+ );
- if (!closeFamilyResponse.ok) {
- const errorResponse = await closeFamilyResponse.text();
- throw new Error(`Failed to close family: ${errorResponse}`);
+ if (!response.ok) {
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Failed to close family",
+ ),
+ );
}
handleCloseFamily();
@@ -62,61 +60,24 @@ export const CloseFamily: React.FC<CloseFamilyProps> = ({
};
return (
- <Dialog
+ <ConfirmationDialog
open={open}
onClose={handleClose}
- aria-labelledby="alert-dialog-title"
- aria-describedby="alert-dialog-description"
- PaperComponent={Paper}
- sx={{
- width: "499px",
- height: "286px",
- margin: "auto",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- }}
- slotProps={{
- backdrop: {
- style: { backgroundColor: "rgba(255, 255, 255, 0.9)" },
- },
- }}
- >
- <DialogTitle id="alert-dialog-title">Close Family?</DialogTitle>
- <DialogContent>
- <DialogContentText id="alert-dialog-description">
- Are you sure you want to close family relations for this
- account?
- </DialogContentText>
- </DialogContent>
- <DialogActions sx={{ justifyContent: "center" }}>
- <Button
- onClick={handleClose}
- sx={{
- bgcolor: "white",
- color: "black",
- "&:hover": { bgcolor: "#FAFAFA" },
- }}
- >
- Cancel
- </Button>
- <Button
- onClick={() => {
+ title="Close Family?"
+ actions={[
+ {
+ label: "Close",
+ loadingLabel: "Closing...",
+ loading,
+ onClick: () => {
handleClosure().catch((error: unknown) =>
console.error(error),
);
- }}
- sx={{
- bgcolor: "#F4473D",
- color: "white",
- "&:hover": { bgcolor: "#E53935" },
- }}
- disabled={loading}
- >
- {loading ? "Closing..." : "Close"}
- </Button>
- </DialogActions>
- </Dialog>
+ },
+ },
+ ]}
+ >
+ Are you sure you want to close family relations for this account?
+ </ConfirmationDialog>
);
};
diff --git a/infra/staff/src/components/ConfirmationDialog.tsx b/infra/staff/src/components/ConfirmationDialog.tsx
new file mode 100644
index 0000000000..4edfbea21d
--- /dev/null
+++ b/infra/staff/src/components/ConfirmationDialog.tsx
@@ -0,0 +1,101 @@
+import {
+ Button,
+ Dialog,
+ DialogActions,
+ DialogContent,
+ DialogContentText,
+ DialogTitle,
+ Paper,
+ type SxProps,
+ type Theme,
+} from "@mui/material";
+import type { ReactNode } from "react";
+
+type DialogActionTone = "danger" | "success";
+
+interface ConfirmationDialogAction {
+ label: string;
+ loadingLabel?: string;
+ loading?: boolean;
+ tone?: DialogActionTone;
+ onClick: () => void;
+}
+
+interface ConfirmationDialogProps {
+ open: boolean;
+ title: string;
+ children: ReactNode;
+ onClose: () => void;
+ actions: ConfirmationDialogAction[];
+}
+
+export const ConfirmationDialog: React.FC<ConfirmationDialogProps> = ({
+ open,
+ title,
+ children,
+ onClose,
+ actions,
+}) => (
+ <Dialog
+ open={open}
+ onClose={onClose}
+ aria-labelledby="alert-dialog-title"
+ aria-describedby="alert-dialog-description"
+ PaperComponent={Paper}
+ sx={{
+ width: 499,
+ height: 286,
+ margin: "auto",
+ display: "flex",
+ flexDirection: "column",
+ alignItems: "center",
+ justifyContent: "center",
+ }}
+ slotProps={{
+ backdrop: {
+ style: { backgroundColor: "rgba(255, 255, 255, 0.9)" },
+ },
+ }}
+ >
+ <DialogTitle id="alert-dialog-title">{title}</DialogTitle>
+ <DialogContent>
+ <DialogContentText id="alert-dialog-description">
+ {children}
+ </DialogContentText>
+ </DialogContent>
+ <DialogActions sx={{ justifyContent: "center" }}>
+ <Button onClick={onClose} sx={cancelButtonSx}>
+ Cancel
+ </Button>
+ {actions.map(({ label, loadingLabel, loading, tone, onClick }) => (
+ <Button
+ key={label}
+ onClick={onClick}
+ sx={actionButtonSx[tone ?? "danger"]}
+ disabled={loading}
+ >
+ {loading && loadingLabel ? loadingLabel : label}
+ </Button>
+ ))}
+ </DialogActions>
+ </Dialog>
+);
+
+const cancelButtonSx: SxProps<Theme> = {
+ bgcolor: "white",
+ color: "black",
+ "&:hover": { bgcolor: "#FAFAFA" },
+};
+
+const actionButtonSx: Record<DialogActionTone, SxProps<Theme>> = {
+ danger: {
+ bgcolor: "#F4473D",
+ color: "white",
+ "&:hover": { bgcolor: "#E53935" },
+ },
+ success: {
+ bgcolor: "#4CAF50",
+ color: "white",
+ "&:hover": { bgcolor: "#45A049" },
+ },
+};
diff --git a/infra/staff/src/components/DeleteAccount.tsx b/infra/staff/src/components/DeleteAccount.tsx
index d439302d5f..e1fc6cadbf 100644
--- a/infra/staff/src/components/DeleteAccount.tsx
+++ b/infra/staff/src/components/DeleteAccount.tsx
@@ -1,15 +1,11 @@
-import {
- Button,
- Dialog,
- DialogActions,
- DialogContent,
- DialogContentText,
- DialogTitle,
- Paper,
-} from "@mui/material";
import React from "react";
import { getEmail } from "../services/session";
-import { apiOrigin, requireToken } from "../services/support";
+import {
+ apiOrigin,
+ requireToken,
+ responseErrorMessage,
+} from "../services/support";
+import { ConfirmationDialog } from "./ConfirmationDialog";
interface DeleteAccountProps {
open: boolean;
@@ -25,14 +21,17 @@ export const DeleteAccount: React.FC<DeleteAccountProps> = ({
const email = getEmail();
if (!email) throw new Error("Email not found");
const token = requireToken();
-
- const deleteUrl = `${apiOrigin}/admin/user/delete?email=${encodeURIComponent(email)}`;
- const response = await fetch(deleteUrl, {
- method: "DELETE",
- headers: { "X-Auth-Token": token },
- });
+ const response = await fetch(
+ `${apiOrigin}/admin/user/delete?email=${encodeURIComponent(email)}`,
+ { method: "DELETE", headers: { "X-Auth-Token": token } },
+ );
if (!response.ok) {
- throw new Error("Failed to delete user account");
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Failed to delete user account",
+ ),
+ );
}
handleClose();
} catch (error) {
@@ -45,59 +44,22 @@ export const DeleteAccount: React.FC<DeleteAccountProps> = ({
};
return (
- <Dialog
+ <ConfirmationDialog
open={open}
onClose={handleClose}
- aria-labelledby="alert-dialog-title"
- aria-describedby="alert-dialog-description"
- PaperComponent={Paper}
- sx={{
- width: "499px",
- height: "286px",
- margin: "auto",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- }}
- slotProps={{
- backdrop: {
- style: { backgroundColor: "rgba(255, 255, 255, 0.9)" },
- },
- }}
- >
- <DialogTitle id="alert-dialog-title">Delete Account?</DialogTitle>
- <DialogContent>
- <DialogContentText id="alert-dialog-description">
- Are you sure you want to delete the account?
- </DialogContentText>
- </DialogContent>
- <DialogActions sx={{ justifyContent: "center" }}>
- <Button
- onClick={handleClose}
- sx={{
- bgcolor: "white",
- color: "black",
- "&:hover": { bgcolor: "#FAFAFA" },
- }}
- >
- Cancel
- </Button>
- <Button
- onClick={() => {
+ title="Delete Account?"
+ actions={[
+ {
+ label: "Delete",
+ onClick: () => {
handleDelete().catch((error: unknown) =>
console.error("Fetch data error:", error),
);
- }}
- sx={{
- bgcolor: "#F4473D",
- color: "white",
- "&:hover": { bgcolor: "#E53935" },
- }}
- >
- Delete
- </Button>
- </DialogActions>
- </Dialog>
+ },
+ },
+ ]}
+ >
+ Are you sure you want to delete the account?
+ </ConfirmationDialog>
);
};
diff --git a/infra/staff/src/components/Disable2FA.tsx b/infra/staff/src/components/Disable2FA.tsx
index bc6c168af5..682eebafda 100644
--- a/infra/staff/src/components/Disable2FA.tsx
+++ b/infra/staff/src/components/Disable2FA.tsx
@@ -1,18 +1,11 @@
-import {
- Button,
- Dialog,
- DialogActions,
- DialogContent,
- DialogContentText,
- DialogTitle,
- Paper,
-} from "@mui/material";
import React, { useState } from "react";
import {
apiOrigin,
getCurrentAdminUserId,
requireToken,
+ responseErrorMessage,
} from "../services/support";
+import { ConfirmationDialog } from "./ConfirmationDialog";
interface Disable2FAProps {
open: boolean;
@@ -32,20 +25,25 @@ export const Disable2FA: React.FC<Disable2FAProps> = ({
setLoading(true);
const token = requireToken();
const userID = await getCurrentAdminUserId();
-
- const disableUrl = `${apiOrigin}/admin/user/disable-2fa`;
- const disableResponse = await fetch(disableUrl, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- "X-Auth-Token": token,
+ const response = await fetch(
+ `${apiOrigin}/admin/user/disable-2fa`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ "X-Auth-Token": token,
+ },
+ body: JSON.stringify({ userID }),
},
- body: JSON.stringify({ userID }),
- });
+ );
- if (!disableResponse.ok) {
- const errorResponse = await disableResponse.text();
- throw new Error(`Failed to disable 2FA: ${errorResponse}`);
+ if (!response.ok) {
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Failed to disable 2FA",
+ ),
+ );
}
handleDisable2FA();
handleClose();
@@ -61,60 +59,24 @@ export const Disable2FA: React.FC<Disable2FAProps> = ({
};
return (
- <Dialog
+ <ConfirmationDialog
open={open}
onClose={handleClose}
- aria-labelledby="alert-dialog-title"
- aria-describedby="alert-dialog-description"
- PaperComponent={Paper}
- sx={{
- width: "499px",
- height: "286px",
- margin: "auto",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- }}
- slotProps={{
- backdrop: {
- style: { backgroundColor: "rgba(255, 255, 255, 0.9)" },
- },
- }}
- >
- <DialogTitle id="alert-dialog-title">Disable 2FA?</DialogTitle>
- <DialogContent>
- <DialogContentText id="alert-dialog-description">
- Are you sure you want to disable 2FA for this account?
- </DialogContentText>
- </DialogContent>
- <DialogActions sx={{ justifyContent: "center" }}>
- <Button
- onClick={handleClose}
- sx={{
- bgcolor: "white",
- color: "black",
- "&:hover": { bgcolor: "#FAFAFA" },
- }}
- >
- Cancel
- </Button>
- <Button
- onClick={() => {
+ title="Disable 2FA?"
+ actions={[
+ {
+ label: "Disable",
+ loadingLabel: "Disabling...",
+ loading,
+ onClick: () => {
handleDisable().catch((error: unknown) =>
console.error(error),
);
- }}
- sx={{
- bgcolor: "#F4473D",
- color: "white",
- "&:hover": { bgcolor: "#E53935" },
- }}
- disabled={loading}
- >
- {loading ? "Disabling..." : "Disable"}
- </Button>
- </DialogActions>
- </Dialog>
+ },
+ },
+ ]}
+ >
+ Are you sure you want to disable 2FA for this account?
+ </ConfirmationDialog>
);
};
diff --git a/infra/staff/src/components/DisablePasskeys.tsx b/infra/staff/src/components/DisablePasskeys.tsx
index 027a98f2f7..900b7b8397 100644
--- a/infra/staff/src/components/DisablePasskeys.tsx
+++ b/infra/staff/src/components/DisablePasskeys.tsx
@@ -1,18 +1,11 @@
-import {
- Button,
- Dialog,
- DialogActions,
- DialogContent,
- DialogContentText,
- DialogTitle,
- Paper,
-} from "@mui/material";
import React, { useState } from "react";
import {
apiOrigin,
getCurrentAdminUserId,
requireToken,
+ responseErrorMessage,
} from "../services/support";
+import { ConfirmationDialog } from "./ConfirmationDialog";
interface DisablePasskeysProps {
open: boolean;
@@ -34,7 +27,7 @@ export const DisablePasskeys: React.FC<DisablePasskeysProps> = ({
const userId = await getCurrentAdminUserId();
const disablePasskeysUrl = `${apiOrigin}/admin/user/disable-passkeys`;
- const disablePasskeysResponse = await fetch(disablePasskeysUrl, {
+ const response = await fetch(disablePasskeysUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -43,9 +36,13 @@ export const DisablePasskeys: React.FC<DisablePasskeysProps> = ({
body: JSON.stringify({ userId }),
});
- if (!disablePasskeysResponse.ok) {
- const errorResponse = await disablePasskeysResponse.text();
- throw new Error(`Failed to disable passkeys: ${errorResponse}`);
+ if (!response.ok) {
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Failed to disable passkeys",
+ ),
+ );
}
handleDisablePasskeys();
@@ -62,60 +59,24 @@ export const DisablePasskeys: React.FC<DisablePasskeysProps> = ({
};
return (
- <Dialog
+ <ConfirmationDialog
open={open}
onClose={handleClose}
- aria-labelledby="alert-dialog-title"
- aria-describedby="alert-dialog-description"
- PaperComponent={Paper}
- sx={{
- width: "499px",
- height: "286px",
- margin: "auto",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- }}
- slotProps={{
- backdrop: {
- style: { backgroundColor: "rgba(255, 255, 255, 0.9)" },
- },
- }}
- >
- <DialogTitle id="alert-dialog-title">Disable Passkeys?</DialogTitle>
- <DialogContent>
- <DialogContentText id="alert-dialog-description">
- Are you sure you want to disable passkeys for this account?
- </DialogContentText>
- </DialogContent>
- <DialogActions sx={{ justifyContent: "center" }}>
- <Button
- onClick={handleClose}
- sx={{
- bgcolor: "white",
- color: "black",
- "&:hover": { bgcolor: "#FAFAFA" },
- }}
- >
- Cancel
- </Button>
- <Button
- onClick={() => {
+ title="Disable Passkeys?"
+ actions={[
+ {
+ label: "Disable",
+ loadingLabel: "Disabling...",
+ loading,
+ onClick: () => {
handleDisabling().catch((error: unknown) =>
console.error(error),
);
- }}
- sx={{
- bgcolor: "#F4473D",
- color: "white",
- "&:hover": { bgcolor: "#E53935" },
- }}
- disabled={loading}
- >
- {loading ? "Disabling..." : "Disable"}
- </Button>
- </DialogActions>
- </Dialog>
+ },
+ },
+ ]}
+ >
+ Are you sure you want to disable passkeys for this account?
+ </ConfirmationDialog>
);
};
diff --git a/infra/staff/src/components/FamilyComponentTable.tsx b/infra/staff/src/components/FamilyComponentTable.tsx
index 4a010f3403..5852a3d607 100644
--- a/infra/staff/src/components/FamilyComponentTable.tsx
+++ b/infra/staff/src/components/FamilyComponentTable.tsx
@@ -1,4 +1,5 @@
import {
+ Box,
Button,
CircularProgress,
Paper,
@@ -73,7 +74,7 @@ export const FamilyTableComponent: React.FC = () => {
<>
<TableContainer
component={Paper}
- style={{ marginTop: "20px", backgroundColor: "#F1F1F3" }}
+ sx={{ mt: "20px", bgcolor: "#F1F1F3" }}
>
<Table aria-label="family-table">
<TableHead>
@@ -137,7 +138,7 @@ export const FamilyTableComponent: React.FC = () => {
</TableBody>
</Table>
</TableContainer>
- <div style={{ marginTop: "20px" }}>
+ <Box sx={{ mt: "20px" }}>
<Button
variant="contained"
color="error"
@@ -145,7 +146,7 @@ export const FamilyTableComponent: React.FC = () => {
>
Close Family
</Button>
- </div>
+ </Box>
{closeFamilyOpen && (
<CloseFamily
diff --git a/infra/staff/src/components/StorageBonusTableComponent.tsx b/infra/staff/src/components/StorageBonusTableComponent.tsx
index 8cd5b70d13..78b5a7223d 100644
--- a/infra/staff/src/components/StorageBonusTableComponent.tsx
+++ b/infra/staff/src/components/StorageBonusTableComponent.tsx
@@ -58,66 +58,64 @@ export const StorageBonusTableComponent: React.FC = () => {
}
return (
- <div style={{ marginTop: "20px", marginBottom: "20px" }}>
- <TableContainer
- component={Paper}
- style={{ backgroundColor: "#F1F1F3" }}
- >
- <Table aria-label="storage-bonus-table">
- <TableHead>
- <TableRow>
+ <TableContainer
+ component={Paper}
+ sx={{ mt: "20px", mb: "20px", bgcolor: "#F1F1F3" }}
+ >
+ <Table aria-label="storage-bonus-table">
+ <TableHead>
+ <TableRow>
+ <TableCell>
+ <b>Storage</b>
+ </TableCell>
+ <TableCell>
+ <b>Type</b>
+ </TableCell>
+ <TableCell>
+ <b>Created At</b>
+ </TableCell>
+ <TableCell>
+ <b>Valid Till</b>
+ </TableCell>
+ <TableCell>
+ <b>Is Revoked</b>
+ </TableCell>
+ </TableRow>
+ </TableHead>
+ <TableBody>
+ {storageBonuses.map((bonus, index) => (
+ <TableRow key={index}>
<TableCell>
- <b>Storage</b>
+ {formatStorage(bonus.storage)}
</TableCell>
+ <TableCell>{bonus.type}</TableCell>
<TableCell>
- <b>Type</b>
+ {formatCreatedAt(bonus.createdAt)}
</TableCell>
<TableCell>
- <b>Created At</b>
+ {formatValidTill(bonus.validTill)}
</TableCell>
<TableCell>
- <b>Valid Till</b>
- </TableCell>
- <TableCell>
- <b>Is Revoked</b>
+ <span
+ style={{
+ backgroundColor: bonus.isRevoked
+ ? "#494949"
+ : "transparent",
+ color: bonus.isRevoked
+ ? "white"
+ : "inherit",
+ padding: "4px 8px",
+ borderRadius: "10px",
+ }}
+ >
+ {bonus.isRevoked ? "Yes" : "No"}
+ </span>
</TableCell>
</TableRow>
- </TableHead>
- <TableBody>
- {storageBonuses.map((bonus, index) => (
- <TableRow key={index}>
- <TableCell>
- {formatStorage(bonus.storage)}
- </TableCell>
- <TableCell>{bonus.type}</TableCell>
- <TableCell>
- {formatCreatedAt(bonus.createdAt)}
- </TableCell>
- <TableCell>
- {formatValidTill(bonus.validTill)}
- </TableCell>
- <TableCell>
- <span
- style={{
- backgroundColor: bonus.isRevoked
- ? "#494949"
- : "transparent",
- color: bonus.isRevoked
- ? "white"
- : "inherit",
- padding: "4px 8px",
- borderRadius: "10px",
- }}
- >
- {bonus.isRevoked ? "Yes" : "No"}
- </span>
- </TableCell>
- </TableRow>
- ))}
- </TableBody>
- </Table>
- </TableContainer>
- </div>
+ ))}
+ </TableBody>
+ </Table>
+ </TableContainer>
);
};
diff --git a/infra/staff/src/components/ToggleEmailMFA.tsx b/infra/staff/src/components/ToggleEmailMFA.tsx
index 19536a8560..3ee27334c2 100644
--- a/infra/staff/src/components/ToggleEmailMFA.tsx
+++ b/infra/staff/src/components/ToggleEmailMFA.tsx
@@ -1,18 +1,11 @@
-import {
- Button,
- Dialog,
- DialogActions,
- DialogContent,
- DialogContentText,
- DialogTitle,
- Paper,
-} from "@mui/material";
import React, { useState } from "react";
import {
apiOrigin,
getCurrentAdminUserId,
requireToken,
+ responseErrorMessage,
} from "../services/support";
+import { ConfirmationDialog } from "./ConfirmationDialog";
interface ToggleEmailMFAProps {
open: boolean;
@@ -34,7 +27,7 @@ export const ToggleEmailMFA: React.FC<ToggleEmailMFAProps> = ({
const userId = await getCurrentAdminUserId();
const toggleEmailMFAUrl = `${apiOrigin}/admin/user/update-email-mfa`;
- const toggleEmailMFAResponse = await fetch(toggleEmailMFAUrl, {
+ const response = await fetch(toggleEmailMFAUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -43,9 +36,13 @@ export const ToggleEmailMFA: React.FC<ToggleEmailMFAProps> = ({
body: JSON.stringify({ userID: userId, emailMFA: enable }),
});
- if (!toggleEmailMFAResponse.ok) {
- const errorResponse = await toggleEmailMFAResponse.text();
- throw new Error(`Failed to update Email MFA: ${errorResponse}`);
+ if (!response.ok) {
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Failed to update Email MFA",
+ ),
+ );
}
handleToggleEmailMFA(enable);
@@ -62,75 +59,35 @@ export const ToggleEmailMFA: React.FC<ToggleEmailMFAProps> = ({
};
return (
- <Dialog
+ <ConfirmationDialog
open={open}
onClose={handleClose}
- aria-labelledby="alert-dialog-title"
- aria-describedby="alert-dialog-description"
- PaperComponent={Paper}
- sx={{
- width: "499px",
- height: "286px",
- margin: "auto",
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "center",
- }}
- slotProps={{
- backdrop: {
- style: { backgroundColor: "rgba(255, 255, 255, 0.9)" },
- },
- }}
- >
- <DialogTitle id="alert-dialog-title">Toggle Email MFA</DialogTitle>
- <DialogContent>
- <DialogContentText id="alert-dialog-description">
- Do you want to enable or disable Email MFA for this account?
- </DialogContentText>
- </DialogContent>
- <DialogActions sx={{ justifyContent: "center" }}>
- <Button
- onClick={handleClose}
- sx={{
- bgcolor: "white",
- color: "black",
- "&:hover": { bgcolor: "#FAFAFA" },
- }}
- >
- Cancel
- </Button>
- <Button
- onClick={() => {
+ title="Toggle Email MFA"
+ actions={[
+ {
+ label: "Enable Email MFA",
+ loadingLabel: "Processing...",
+ loading,
+ tone: "success",
+ onClick: () => {
handleToggle(true).catch((error: unknown) =>
console.error(error),
);
- }}
- sx={{
- bgcolor: "#4CAF50",
- color: "white",
- "&:hover": { bgcolor: "#45A049" },
- }}
- disabled={loading}
- >
- {loading ? "Processing..." : "Enable Email MFA"}
- </Button>
- <Button
- onClick={() => {
+ },
+ },
+ {
+ label: "Disable Email MFA",
+ loadingLabel: "Processing...",
+ loading,
+ onClick: () => {
handleToggle(false).catch((error: unknown) =>
console.error(error),
);
- }}
- sx={{
- bgcolor: "#F4473D",
- color: "white",
- "&:hover": { bgcolor: "#E53935" },
- }}
- disabled={loading}
- >
- {loading ? "Processing..." : "Disable Email MFA"}
- </Button>
- </DialogActions>
- </Dialog>
+ },
+ },
+ ]}
+ >
+ Do you want to enable or disable Email MFA for this account?
+ </ConfirmationDialog>
);
};
diff --git a/infra/staff/src/components/TokenTableComponent.tsx b/infra/staff/src/components/TokenTableComponent.tsx
index 28b1b00d87..b88466c8ff 100644
--- a/infra/staff/src/components/TokenTableComponent.tsx
+++ b/infra/staff/src/components/TokenTableComponent.tsx
@@ -78,96 +78,90 @@ export const TokensTableComponent: React.FC = () => {
}
return (
- <div style={{ marginTop: "20px", marginBottom: "20px" }}>
- <TableContainer
- component={Paper}
- style={{ backgroundColor: "#F1F1F3" }}
- >
- <Table aria-label="tokens-table">
- <TableHead>
- <TableRow>
- <TableCell
- sortDirection={
- orderBy === "creationTime" ? order : false
+ <TableContainer
+ component={Paper}
+ sx={{ mt: "20px", mb: "20px", bgcolor: "#F1F1F3" }}
+ >
+ <Table aria-label="tokens-table">
+ <TableHead>
+ <TableRow>
+ <TableCell
+ sortDirection={
+ orderBy === "creationTime" ? order : false
+ }
+ >
+ <TableSortLabel
+ active={orderBy === "creationTime"}
+ direction={
+ orderBy === "creationTime" ? order : "asc"
+ }
+ onClick={() =>
+ handleRequestSort("creationTime")
}
>
- <TableSortLabel
- active={orderBy === "creationTime"}
- direction={
- orderBy === "creationTime"
- ? order
- : "asc"
- }
- onClick={() =>
- handleRequestSort("creationTime")
- }
- >
- Created At
- </TableSortLabel>
- </TableCell>
- <TableCell
- sortDirection={
- orderBy === "lastUsedTime" ? order : false
+ Created At
+ </TableSortLabel>
+ </TableCell>
+ <TableCell
+ sortDirection={
+ orderBy === "lastUsedTime" ? order : false
+ }
+ >
+ <TableSortLabel
+ active={orderBy === "lastUsedTime"}
+ direction={
+ orderBy === "lastUsedTime" ? order : "asc"
+ }
+ onClick={() =>
+ handleRequestSort("lastUsedTime")
}
>
- <TableSortLabel
- active={orderBy === "lastUsedTime"}
- direction={
- orderBy === "lastUsedTime"
- ? order
- : "asc"
- }
- onClick={() =>
- handleRequestSort("lastUsedTime")
- }
- >
- Last Used At
- </TableSortLabel>
- </TableCell>
+ Last Used At
+ </TableSortLabel>
+ </TableCell>
+ <TableCell>
+ <b>User Agent</b>
+ </TableCell>
+ <TableCell>
+ <b>App</b>
+ </TableCell>
+ <TableCell>
+ <b>Is Deleted</b>
+ </TableCell>
+ </TableRow>
+ </TableHead>
+ <TableBody>
+ {sortedTokens.map((token, index) => (
+ <TableRow key={index}>
<TableCell>
- <b>User Agent</b>
+ {formatDate(token.creationTime)}
</TableCell>
<TableCell>
- <b>App</b>
+ {formatDate(token.lastUsedTime)}
</TableCell>
+ <TableCell>{token.ua || "-"}</TableCell>
+ <TableCell>{token.app}</TableCell>
<TableCell>
- <b>Is Deleted</b>
+ <span
+ style={{
+ backgroundColor: token.isDeleted
+ ? "#494949"
+ : "transparent",
+ color: token.isDeleted
+ ? "white"
+ : "inherit",
+ padding: "4px 8px",
+ borderRadius: "10px",
+ }}
+ >
+ {token.isDeleted ? "Yes" : "No"}
+ </span>
</TableCell>
</TableRow>
- </TableHead>
- <TableBody>
- {sortedTokens.map((token, index) => (
- <TableRow key={index}>
- <TableCell>
- {formatDate(token.creationTime)}
- </TableCell>
- <TableCell>
- {formatDate(token.lastUsedTime)}
- </TableCell>
- <TableCell>{token.ua || "-"}</TableCell>
- <TableCell>{token.app}</TableCell>
- <TableCell>
- <span
- style={{
- backgroundColor: token.isDeleted
- ? "#494949"
- : "transparent",
- color: token.isDeleted
- ? "white"
- : "inherit",
- padding: "4px 8px",
- borderRadius: "10px",
- }}
- >
- {token.isDeleted ? "Yes" : "No"}
- </span>
- </TableCell>
- </TableRow>
- ))}
- </TableBody>
- </Table>
- </TableContainer>
- </div>
+ ))}
+ </TableBody>
+ </Table>
+ </TableContainer>
);
};
diff --git a/infra/staff/src/components/UpdateSubscription.tsx b/infra/staff/src/components/UpdateSubscription.tsx
index 1311816469..4d2ab20914 100644
--- a/infra/staff/src/components/UpdateSubscription.tsx
+++ b/infra/staff/src/components/UpdateSubscription.tsx
@@ -1,6 +1,7 @@
import CalendarTodayIcon from "@mui/icons-material/CalendarToday";
import CloseIcon from "@mui/icons-material/Close";
import {
+ Box,
Button,
Dialog,
DialogActions,
@@ -20,6 +21,7 @@ import {
apiOrigin,
getCurrentAdminUser,
requireToken,
+ responseErrorMessage,
} from "../services/support";
interface Subscription {
@@ -165,13 +167,18 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
method: "PUT",
headers: {
"Content-Type": "application/json",
- "X-AUTH-TOKEN": token,
+ "X-Auth-Token": token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
- throw new Error("Network response was not ok");
+ throw new Error(
+ await responseErrorMessage(
+ response,
+ "Network response was not ok",
+ ),
+ );
}
onClose();
} catch (error) {
@@ -196,58 +203,37 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
},
}}
>
- <DialogTitle style={{ marginBottom: "20px", marginTop: "20px" }}>
+ <DialogTitle sx={dialogTitleSx}>
Update Subscription
- <Button
- onClick={onClose}
- style={{ position: "absolute", right: 10, top: 10 }}
- >
- <CloseIcon style={{ color: "black" }} />
+ <Button onClick={onClose} sx={dialogCloseButtonSx}>
+ <CloseIcon sx={{ color: "black" }} />
</Button>
</DialogTitle>
<DialogContent>
<form onSubmit={handleSubmit}>
<Grid container spacing={4}>
+ <SubscriptionTextField
+ id="productId"
+ label="Product ID"
+ value={values.productId}
+ onChange={handleChange}
+ />
<Grid size={6}>
- <div style={{ marginBottom: "8px" }}>
- <label
- htmlFor="productId"
- style={{
- textAlign: "left",
- display: "block",
- marginBottom: "4px",
- }}
- >
- Product ID
- </label>
- <TextField
- id="productId"
- name="productId"
- value={values.productId}
- onChange={handleChange}
- fullWidth
- />
- </div>
- </Grid>
- <Grid size={6}>
- <div style={{ marginBottom: "8px" }}>
- <label
+ <Box sx={fieldWrapperSx}>
+ <Box
+ component="label"
htmlFor="provider"
- style={{
- textAlign: "left",
- display: "block",
- marginBottom: "4px",
- }}
+ sx={fieldLabelSx}
>
Provider
- </label>
+ </Box>
<Select
id="provider"
name="provider"
value={values.provider}
onChange={handleChangeProvider}
fullWidth
- style={{ textAlign: "left" }}
+ sx={{ textAlign: "left" }}
>
<MenuItem value="stripe">Stripe</MenuItem>
<MenuItem value="appstore">
@@ -257,63 +243,30 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
<MenuItem value="bitpay">BitPay</MenuItem>
<MenuItem value="None">None</MenuItem>
</Select>
- </div>
+ </Box>
</Grid>
+ <SubscriptionTextField
+ id="storage"
+ label="Storage (GB)"
+ type="number"
+ value={values.storage}
+ onChange={handleChange}
+ />
+ <SubscriptionTextField
+ id="transactionId"
+ label="Transaction ID"
+ value={values.transactionId}
+ onChange={handleChange}
+ />
<Grid size={6}>
- <div style={{ marginBottom: "8px" }}>
- <label
- htmlFor="storage"
- style={{
- textAlign: "left",
- display: "block",
- marginBottom: "4px",
- }}
- >
- Storage (GB)
- </label>
- <TextField
- id="storage"
- name="storage"
- type="number"
- value={values.storage}
- onChange={handleChange}
- fullWidth
- />
- </div>
- </Grid>
- <Grid size={6}>
- <div style={{ marginBottom: "8px" }}>
- <label
- htmlFor="transactionId"
- style={{
- textAlign: "left",
- display: "block",
- marginBottom: "4px",
- }}
- >
- Transaction ID
- </label>
- <TextField
- id="transactionId"
- name="transactionId"
- value={values.transactionId}
- onChange={handleChange}
- fullWidth
- />
- </div>
- </Grid>
- <Grid size={6}>
- <div style={{ marginBottom: "8px" }}>
- <label
+ <Box sx={fieldWrapperSx}>
+ <Box
+ component="label"
htmlFor="expiryTime"
- style={{
- textAlign: "left",
- display: "block",
- marginBottom: "4px",
- }}
+ sx={fieldLabelSx}
>
Expiry Time
- </label>
+ </Box>
<TextField
id="expiryTime"
name="expiryTime"
@@ -355,18 +308,11 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
inline
/>
)}
- </div>
+ </Box>
</Grid>
</Grid>
- <DialogActions style={{ justifyContent: "center" }}>
- <Button
- type="submit"
- variant="contained"
- style={{
- backgroundColor: "#00B33C",
- color: "white",
- }}
- >
+ <DialogActions sx={{ justifyContent: "center" }}>
+ <Button type="submit" variant="contained" sx={submitSx}>
Update
</Button>
</DialogActions>
@@ -375,3 +321,47 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
</Dialog>
);
};
+
+type SubscriptionTextFieldId = "productId" | "storage" | "transactionId";
+
+interface SubscriptionTextFieldProps {
+ id: SubscriptionTextFieldId;
+ label: string;
+ type?: string;
+ value: string | number;
+ onChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
+}
+
+const SubscriptionTextField = ({
+ id,
+ label,
+ type,
+ value,
+ onChange,
+}: SubscriptionTextFieldProps) => (
+ <Grid size={6}>
+ <Box sx={fieldWrapperSx}>
+ <Box component="label" htmlFor={id} sx={fieldLabelSx}>
+ {label}
+ </Box>
+ <TextField
+ id={id}
+ name={id}
+ type={type}
+ value={value}
+ onChange={onChange}
+ fullWidth
+ />
+ </Box>
+ </Grid>
+);
+
+const dialogTitleSx = { mb: "20px", mt: "20px" };
+
+const dialogCloseButtonSx = { position: "absolute", right: 10, top: 10 };
+
+const fieldWrapperSx = { mb: 1 };
+
+const fieldLabelSx = { display: "block", mb: "4px", textAlign: "left" };
+
+const submitSx = { bgcolor: "#00B33C", color: "white" };
diff --git a/infra/staff/src/components/UserComponent.tsx b/infra/staff/src/components/UserComponent.tsx
index 93bd0dbffc..8eeaedf4fb 100644
--- a/infra/staff/src/components/UserComponent.tsx
+++ b/infra/staff/src/components/UserComponent.tsx
@@ -204,7 +204,7 @@ const DataTable: React.FC<DataTableProps> = ({
aria-label="edit"
onClick={onEditSubscription}
>
- <EditIcon style={{ color: "black", marginRight: "15px" }} />
+ <EditIcon sx={{ color: "black", mr: "15px" }} />
</IconButton>
)}
</Box>
@@ -226,8 +226,8 @@ const DataTable: React.FC<DataTableProps> = ({
<TableCell
component="th"
scope="row"
- style={{
- padding: "16px",
+ sx={{
+ p: "16px",
borderBottom:
index === 1 || index === 0
? "1px solid rgba(224, 224, 224, 1)"
@@ -238,8 +238,8 @@ const DataTable: React.FC<DataTableProps> = ({
</TableCell>
<TableCell
align="right"
- style={{
- padding: "10px",
+ sx={{
+ p: "10px",
borderBottom:
index === 1 || index === 0
? "1px solid rgba(224, 224, 224, 1)"
@@ -266,13 +266,13 @@ const DataTable: React.FC<DataTableProps> = ({
<TableCell
component="th"
scope="row"
- style={{ padding: "16px", borderBottom: "none" }}
+ sx={{ p: "16px", borderBottom: "none" }}
>
Add OTT
</TableCell>
<TableCell
align="right"
- style={{ padding: "10px", borderBottom: "none" }}
+ sx={{ p: "10px", borderBottom: "none" }}
>
<Button
variant="contained"
@@ -317,7 +317,7 @@ const renderTableCellContent = (
aria-label="edit-email"
onClick={onEditEmail}
>
- <EditIcon style={{ color: "black" }} />
+ <EditIcon sx={{ color: "black" }} />
</IconButton>
</Box>
);
diff --git a/infra/staff/src/services/support.ts b/infra/staff/src/services/support.ts
index e16dd9774a..5516db42dd 100644
--- a/infra/staff/src/services/support.ts
+++ b/infra/staff/src/services/support.ts
@@ -30,6 +30,30 @@ export const requireToken = () => {
return token;
};
+export const responseErrorMessage = async (
+ response: Response,
+ fallback: string,
+) => {
+ const text = await response.text();
+ if (!text) {
+ return fallback;
+ }
+
+ try {
+ const body = JSON.parse(text) as unknown;
+ if (body && typeof body === "object" && "message" in body) {
+ const { message } = body as { message?: unknown };
+ if (typeof message === "string" && message) {
+ return message;
+ }
+ }
+ } catch {
+ return text;
+ }
+
+ return text;
+};
+
export const getCurrentAdminUser = async <T>(): Promise<T> => {
const email = requireEmail();
const token = requireToken();
@@ -37,7 +61,11 @@ export const getCurrentAdminUser = async <T>(): Promise<T> => {
const response = await fetch(url, {
headers: { "Content-Type": "application/json", "X-Auth-Token": token },
});
- if (!response.ok) throw new Error("Failed to fetch user data");
+ if (!response.ok) {
+ throw new Error(
+ await responseErrorMessage(response, "Failed to fetch user data"),
+ );
+ }
return (await response.json()) as T;
};