Skip to content

armgit / ente

Public Imported from GitHub
0

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

Refine staff user details components

Manav Rathi9c2d39efe619
diff --git a/infra/staff/src/App.tsx b/infra/staff/src/App.tsx
index d48f4d6bbf..33800a0ef0 100644
--- a/infra/staff/src/App.tsx
+++ b/infra/staff/src/App.tsx
@@ -8,12 +8,12 @@ import {
} from "@mui/material";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import "./App.css";
-import { FamilyTableComponent } from "./components/FamilyComponentTable";
+import { FamilyTable } from "./components/FamilyTable";
import { StorageBonusTableComponent } from "./components/StorageBonusTableComponent";
import { TokensTableComponent } from "./components/TokenTableComponent";
-import { UserComponent, type UserData } from "./components/UserComponent";
+import { UserDetails, type UserDetailsData } from "./components/UserDetails";
import duckieimage from "./components/duckie.png";
-import { getUser } from "./services/admin-user";
+import { getUser, type UserResponse } from "./services/admin-user";
import { StaffSessionProvider } from "./services/session";
export const App: React.FC = () => {
@@ -29,7 +29,7 @@ export const App: React.FC = () => {
const [error, setError] = useState("");
const [fetchSuccess, setFetchSuccess] = useState(false);
const [tabValue, setTabValue] = useState(0);
- const [userData, setUserData] = useState<UserData | null>(null);
+ const [userData, setUserData] = useState<UserDetailsData | null>(null);
useEffect(() => {
if (authToken) {
@@ -46,124 +46,17 @@ export const App: React.FC = () => {
const startTime = Date.now();
try {
const userSearchInput = input.trim();
- const userDataResponse = await getUser(
- { token: authToken },
+ const userDetailsData = buildUserDetailsData(
+ await getUser({ token: authToken }, userSearchInput),
userSearchInput,
);
- if (!userDataResponse.subscription) {
- throw new Error("Subscription data not found");
- }
- const { subscription } = userDataResponse;
- const userEmail = userDataResponse.user.email || userSearchInput;
- setSelectedUserEmail(userEmail);
- const emailMFAEnabled =
- userDataResponse.details?.profileData?.isEmailMFAEnabled ??
- false;
- const twoFactorEnabled =
- userDataResponse.details?.profileData?.isTwoFactorEnabled ??
- false;
- const passkeysEnabled =
- (userDataResponse.details?.profileData?.passkeyCount ?? 0) > 0;
- const canDisableEmailMFA =
- userDataResponse.details?.profileData?.canDisableEmailMFA ??
- false;
-
- const extractedUserData: UserData = {
- email: userEmail,
- user: [
- {
- kind: "text",
- label: "User ID",
- value: userDataResponse.user.ID || "None",
- },
- {
- kind: "email",
- label: "Email",
- value: userDataResponse.user.email || "None",
- },
- {
- kind: "text",
- label: "Creation time",
- value:
- new Date(
- userDataResponse.user.creationTime / 1000,
- ).toLocaleString() || "None",
- },
- ],
- storage: [
- {
- kind: "text",
- label: "Total",
- value: formatStorage(subscription.storage, true),
- },
- {
- kind: "text",
- label: "Consumed",
- value: formatStorage(userDataResponse.details?.usage),
- },
- {
- kind: "text",
- label: "Bonus",
- value: formatStorage(
- userDataResponse.details?.storageBonus,
- ),
- },
- ],
- subscription: [
- {
- kind: "text",
- label: "Product ID",
- value: subscription.productID || "None",
- },
- {
- kind: "text",
- label: "Provider",
- value: subscription.paymentProvider || "None",
- },
- {
- kind: "expiry",
- label: "Expiry time",
- value:
- new Date(
- subscription.expiryTime / 1000,
- ).toISOString() || "None",
- },
- ],
- security: [
- {
- kind: "emailMFA",
- label: "Email MFA",
- value: emailMFAEnabled ? "Enabled" : "Disabled",
- },
- {
- kind: "twoFactor",
- label: "Two factor 2FA",
- value: twoFactorEnabled ? "Enabled" : "Disabled",
- },
- {
- kind: "passkeys",
- label: "Passkeys",
- value: passkeysEnabled ? "Enabled" : "Disabled",
- },
- {
- kind: "text",
- label: "AuthCodes",
- value: `${userDataResponse.authCodes ?? 0}`,
- },
- ],
- securityState: {
- emailMFAEnabled,
- twoFactorEnabled,
- canDisableEmailMFA,
- },
- };
-
+ setSelectedUserEmail(userDetailsData.email);
const elapsedTime = Date.now() - startTime;
const delay = Math.max(3000 - elapsedTime, 0);
setTimeout(() => {
setLoading(false);
setFetchSuccess(true);
- setUserData(extractedUserData);
+ setUserData(userDetailsData);
}, delay);
} catch (error) {
console.error("Error fetching data:", error);
@@ -311,11 +204,9 @@ export const App: React.FC = () => {
}}
>
{tabValue === 0 && userData && (
- <UserComponent userData={userData} />
- )}
- {tabValue === 1 && userData && (
- <FamilyTableComponent />
+ <UserDetails userData={userData} />
)}
+ {tabValue === 1 && userData && <FamilyTable />}
{tabValue === 2 && userData && (
<StorageBonusTableComponent />
)}
@@ -342,6 +233,104 @@ const readUrlCredentials = () => {
return { email: urlParams.get("email"), token: urlParams.get("token") };
};
+const buildUserDetailsData = (
+ userResponse: UserResponse,
+ userSearchInput: string,
+): UserDetailsData => {
+ if (!userResponse.subscription) {
+ throw new Error("Subscription data not found");
+ }
+
+ const { subscription } = userResponse;
+ const emailMFAEnabled =
+ userResponse.details?.profileData?.isEmailMFAEnabled ?? false;
+ const twoFactorEnabled =
+ userResponse.details?.profileData?.isTwoFactorEnabled ?? false;
+ const passkeysEnabled =
+ (userResponse.details?.profileData?.passkeyCount ?? 0) > 0;
+ const canDisableEmailMFA =
+ userResponse.details?.profileData?.canDisableEmailMFA ?? false;
+
+ return {
+ email: userResponse.user.email || userSearchInput,
+ user: [
+ {
+ kind: "text",
+ label: "User ID",
+ value: userResponse.user.ID || "None",
+ },
+ {
+ kind: "email",
+ label: "Email",
+ value: userResponse.user.email || "None",
+ },
+ {
+ kind: "text",
+ label: "Creation time",
+ value:
+ new Date(
+ userResponse.user.creationTime / 1000,
+ ).toLocaleString() || "None",
+ },
+ ],
+ storage: [
+ {
+ kind: "text",
+ label: "Total",
+ value: formatStorage(subscription.storage, true),
+ },
+ {
+ kind: "text",
+ label: "Consumed",
+ value: formatStorage(userResponse.details?.usage),
+ },
+ {
+ kind: "text",
+ label: "Bonus",
+ value: formatStorage(userResponse.details?.storageBonus),
+ },
+ ],
+ subscription: [
+ {
+ kind: "text",
+ label: "Product ID",
+ value: subscription.productID || "None",
+ },
+ {
+ kind: "text",
+ label: "Provider",
+ value: subscription.paymentProvider || "None",
+ },
+ {
+ kind: "expiry",
+ label: "Expiry time",
+ value:
+ new Date(subscription.expiryTime / 1000).toISOString() ||
+ "None",
+ },
+ ],
+ security: [
+ { kind: "emailMFA", label: "Email MFA", enabled: emailMFAEnabled },
+ {
+ kind: "twoFactor",
+ label: "Two factor 2FA",
+ enabled: twoFactorEnabled,
+ },
+ { kind: "passkeys", label: "Passkeys", enabled: passkeysEnabled },
+ {
+ kind: "text",
+ label: "AuthCodes",
+ value: `${userResponse.authCodes ?? 0}`,
+ },
+ ],
+ securityState: {
+ emailMFAEnabled,
+ twoFactorEnabled,
+ canDisableEmailMFA,
+ },
+ };
+};
+
const formatStorage = (bytes: number | undefined, noneWhenZero = false) => {
if (bytes === undefined || (noneWhenZero && bytes === 0)) {
return "None";
diff --git a/infra/staff/src/components/FamilyComponentTable.tsx b/infra/staff/src/components/FamilyTable.tsx
similarity index 95%
rename from infra/staff/src/components/FamilyComponentTable.tsx
rename to infra/staff/src/components/FamilyTable.tsx
index b5d733ac1a..c73a23609e 100644
--- a/infra/staff/src/components/FamilyComponentTable.tsx
+++ b/infra/staff/src/components/FamilyTable.tsx
@@ -16,7 +16,7 @@ import { useInitialStaffSession } from "../services/session";
import { CloseFamily } from "./CloseFamily";
import { StatusBadge } from "./StatusBadge";
-export const FamilyTableComponent: React.FC = () => {
+export const FamilyTable: React.FC = () => {
const [familyMembers, setFamilyMembers] = useState<FamilyMember[]>([]);
const [closeFamilyOpen, setCloseFamilyOpen] = useState(false);
const [loading, setLoading] = useState(true);
@@ -44,7 +44,7 @@ export const FamilyTableComponent: React.FC = () => {
setCloseFamilyOpen(true);
};
- const handleCloseCloseFamily = () => {
+ const handleCloseFamilyDialog = () => {
setCloseFamilyOpen(false);
};
@@ -131,8 +131,8 @@ export const FamilyTableComponent: React.FC = () => {
{closeFamilyOpen && (
<CloseFamily
open={closeFamilyOpen}
- handleClose={handleCloseCloseFamily}
- handleCloseFamily={handleCloseCloseFamily}
+ handleClose={handleCloseFamilyDialog}
+ handleCloseFamily={handleCloseFamilyDialog}
/>
)}
</>
diff --git a/infra/staff/src/components/UserComponent.tsx b/infra/staff/src/components/UserDetails.tsx
similarity index 77%
rename from infra/staff/src/components/UserComponent.tsx
rename to infra/staff/src/components/UserDetails.tsx
index bb9efed338..1c0b46fca1 100644
--- a/infra/staff/src/components/UserComponent.tsx
+++ b/infra/staff/src/components/UserDetails.tsx
@@ -23,7 +23,7 @@ import { DisablePasskeys } from "./DisablePasskeys";
import { ToggleEmailMFA } from "./ToggleEmailMFA";
import { UpdateSubscription } from "./UpdateSubscription";
-export interface UserData {
+export interface UserDetailsData {
email: string;
user: UserTableRow[];
storage: UserTableRow[];
@@ -40,22 +40,22 @@ type UserTableRow =
| { kind: "text"; label: string; value: string }
| { kind: "email"; label: string; value: string }
| { kind: "expiry"; label: string; value: string }
- | { kind: "passkeys"; label: string; value: string }
- | { kind: "twoFactor"; label: string; value: string }
- | { kind: "emailMFA"; label: string; value: string };
+ | { kind: "passkeys"; label: string; enabled: boolean }
+ | { kind: "twoFactor"; label: string; enabled: boolean }
+ | { kind: "emailMFA"; label: string; enabled: boolean };
type UserSectionKey = "user" | "storage" | "subscription" | "security";
-interface UserComponentProps {
- userData: UserData;
+interface UserDetailsProps {
+ userData: UserDetailsData;
}
-export const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
+export const UserDetails: React.FC<UserDetailsProps> = ({ userData }) => {
const [deleteAccountOpen, setDeleteAccountOpen] = useState(false);
- const [email2FAEnabled, setEmail2FAEnabled] = useState(
+ const [emailMFAEnabled, setEmailMFAEnabled] = useState(
userData.securityState.emailMFAEnabled,
);
- const [email2FAOpen, setEmail2FAToggleOpen] = useState(false);
+ const [emailMFAOpen, setEmailMFAOpen] = useState(false);
const [disable2FAOpen, setDisable2FAOpen] = useState(false);
const [twoFactorEnabled, setTwoFactorEnabled] = useState(
userData.securityState.twoFactorEnabled,
@@ -70,7 +70,7 @@ export const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
// These switches are local after successful mutations, but same-user
// refetches should still resync them with the server.
// eslint-disable-next-line react-hooks/set-state-in-effect
- setEmail2FAEnabled(userData.securityState.emailMFAEnabled);
+ setEmailMFAEnabled(userData.securityState.emailMFAEnabled);
setTwoFactorEnabled(userData.securityState.twoFactorEnabled);
}, [
userData.securityState.emailMFAEnabled,
@@ -81,6 +81,12 @@ export const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
const handleDeleteAccountClick = () => setDeleteAccountOpen(true);
const handleEditSubscription = () => setUpdateSubscriptionOpen(true);
const handleDisablePasskeys = () => setDisablePasskeysOpen(true);
+ const handleTwoFactorChange = (enabled: boolean) => {
+ setTwoFactorEnabled(enabled);
+ if (!enabled) {
+ setDisable2FAOpen(true);
+ }
+ };
const sections: {
key: UserSectionKey;
@@ -96,6 +102,20 @@ export const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
},
{ key: "security", title: "Security", rows: userData.security },
];
+ const tableActions: UserTableActions = {
+ editEmail: handleEditEmail,
+ deleteAccount: handleDeleteAccountClick,
+ editSubscription: handleEditSubscription,
+ disablePasskeys: handleDisablePasskeys,
+ addOTT: () => setAddOTTOpen(true),
+ toggleEmailMFA: () => setEmailMFAOpen(true),
+ changeTwoFactor: handleTwoFactorChange,
+ };
+ const securityControls: SecurityControls = {
+ canDisableEmailMFA,
+ twoFactorEnabled,
+ emailMFAEnabled,
+ };
return (
<Grid container spacing={6} sx={{ justifyContent: "center" }}>
@@ -105,17 +125,8 @@ export const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
sectionKey={key}
title={title}
rows={rows}
- onEditEmail={handleEditEmail}
- onDeleteAccount={handleDeleteAccountClick}
- onEditSubscription={handleEditSubscription}
- onDisablePasskeys={handleDisablePasskeys}
- onAddOTT={() => setAddOTTOpen(true)}
- canDisableEmailMFA={canDisableEmailMFA}
- twoFactorEnabled={twoFactorEnabled}
- setTwoFactorEnabled={setTwoFactorEnabled}
- setDisable2FAOpen={setDisable2FAOpen}
- email2FAEnabled={email2FAEnabled}
- onToggleEmailMFA={() => setEmail2FAToggleOpen(true)}
+ actions={tableActions}
+ securityControls={securityControls}
/>
</Grid>
))}
@@ -130,9 +141,9 @@ export const UserComponent: React.FC<UserComponentProps> = ({ userData }) => {
handleDisable2FA={() => setTwoFactorEnabled(false)}
/>
<ToggleEmailMFA
- open={email2FAOpen}
- handleClose={() => setEmail2FAToggleOpen(false)}
- handleToggleEmailMFA={setEmail2FAEnabled}
+ open={emailMFAOpen}
+ handleClose={() => setEmailMFAOpen(false)}
+ handleToggleEmailMFA={setEmailMFAEnabled}
/>
<UpdateSubscription
open={updateSubscriptionOpen}
@@ -160,34 +171,32 @@ interface DataTableProps {
sectionKey: UserSectionKey;
title: string;
rows: UserTableRow[];
- onEditEmail: () => void;
- onDeleteAccount: () => void;
- onEditSubscription: () => void;
- onDisablePasskeys: () => void;
- onAddOTT: () => void;
+ actions: UserTableActions;
+ securityControls: SecurityControls;
+}
+
+interface UserTableActions {
+ editEmail: () => void;
+ deleteAccount: () => void;
+ editSubscription: () => void;
+ disablePasskeys: () => void;
+ addOTT: () => void;
+ toggleEmailMFA: () => void;
+ changeTwoFactor: (enabled: boolean) => void;
+}
+
+interface SecurityControls {
canDisableEmailMFA: boolean;
twoFactorEnabled: boolean;
- setTwoFactorEnabled: (enabled: boolean) => void;
- setDisable2FAOpen: (open: boolean) => void;
- email2FAEnabled: boolean;
- onToggleEmailMFA: () => void;
+ emailMFAEnabled: boolean;
}
const DataTable: React.FC<DataTableProps> = ({
sectionKey,
title,
rows,
- onEditEmail,
- onDeleteAccount,
- onEditSubscription,
- onDisablePasskeys,
- onAddOTT,
- canDisableEmailMFA,
- twoFactorEnabled,
- setTwoFactorEnabled,
- setDisable2FAOpen,
- email2FAEnabled,
- onToggleEmailMFA,
+ actions,
+ securityControls,
}) => (
<TableContainer
component={Paper}
@@ -223,7 +232,7 @@ const DataTable: React.FC<DataTableProps> = ({
<IconButton
edge="start"
aria-label="delete"
- onClick={onDeleteAccount}
+ onClick={actions.deleteAccount}
>
<DeleteIcon />
</IconButton>
@@ -232,7 +241,7 @@ const DataTable: React.FC<DataTableProps> = ({
<IconButton
edge="end"
aria-label="edit"
- onClick={onEditSubscription}
+ onClick={actions.editSubscription}
>
<EditIcon sx={{ color: "black", mr: "15px" }} />
</IconButton>
@@ -276,14 +285,8 @@ const DataTable: React.FC<DataTableProps> = ({
>
{renderTableCellContent(
row,
- onEditEmail,
- onDisablePasskeys,
- canDisableEmailMFA,
- twoFactorEnabled,
- setTwoFactorEnabled,
- setDisable2FAOpen,
- email2FAEnabled,
- onToggleEmailMFA,
+ actions,
+ securityControls,
)}
</TableCell>
</TableRow>
@@ -303,7 +306,7 @@ const DataTable: React.FC<DataTableProps> = ({
>
<Button
variant="contained"
- onClick={onAddOTT}
+ onClick={actions.addOTT}
sx={{ textTransform: "none" }}
>
Add OTT
@@ -318,14 +321,8 @@ const DataTable: React.FC<DataTableProps> = ({
const renderTableCellContent = (
row: UserTableRow,
- onEditEmail: () => void,
- onDisablePasskeys: () => void,
- canToggleEmailMFA: boolean,
- twoFactorEnabled: boolean,
- setTwoFactorEnabled: (enabled: boolean) => void,
- setDisable2FAOpen: (open: boolean) => void,
- email2FAEnabled: boolean,
- onToggleEmailMFA: () => void,
+ actions: UserTableActions,
+ securityControls: SecurityControls,
) => {
switch (row.kind) {
case "email":
@@ -341,20 +338,20 @@ const renderTableCellContent = (
<IconButton
edge="end"
aria-label="edit-email"
- onClick={onEditEmail}
+ onClick={actions.editEmail}
>
<EditIcon sx={{ color: "black" }} />
</IconButton>
</Box>
);
case "passkeys":
- return row.value === "Enabled" ? (
- <Button variant="outlined" onClick={onDisablePasskeys}>
+ return row.enabled ? (
+ <Button variant="outlined" onClick={actions.disablePasskeys}>
Remove Passkey
</Button>
) : (
<Typography sx={{ width: "100%", paddingLeft: "1px" }}>
- {row.value}
+ {enabledLabel(row.enabled)}
</Typography>
);
case "twoFactor":
@@ -369,18 +366,14 @@ const renderTableCellContent = (
}}
>
<Typography sx={{ marginRight: "1px" }}>
- {row.value}
+ {enabledLabel(row.enabled)}
</Typography>
- {row.value === "Enabled" && (
+ {row.enabled && (
<Switch
- checked={twoFactorEnabled}
- onChange={(e) => {
- const isChecked = e.target.checked;
- setTwoFactorEnabled(isChecked);
- if (!isChecked) {
- setDisable2FAOpen(true);
- }
- }}
+ checked={securityControls.twoFactorEnabled}
+ onChange={(e) =>
+ actions.changeTwoFactor(e.target.checked)
+ }
sx={{
"& .MuiSwitch-switchBase.Mui-checked": {
color: "#00B33C",
@@ -427,12 +420,12 @@ const renderTableCellContent = (
}}
>
<Typography sx={{ marginRight: "1px" }}>
- {row.value}
+ {enabledLabel(row.enabled)}
</Typography>
- {canToggleEmailMFA && (
+ {securityControls.canDisableEmailMFA && (
<Switch
- checked={email2FAEnabled}
- onChange={onToggleEmailMFA}
+ checked={securityControls.emailMFAEnabled}
+ onChange={actions.toggleEmailMFA}
sx={{
"& .MuiSwitch-switchBase.Mui-checked": {
color: "#00B33C",
@@ -452,3 +445,5 @@ const renderTableCellContent = (
return <Typography>{row.value}</Typography>;
}
};
+
+const enabledLabel = (enabled: boolean) => (enabled ? "Enabled" : "Disabled");