Skip to content

armgit / ente

Public Imported from GitHub
0

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

Share staff unit helpers

Manav Rathi6e4614a16485
diff --git a/infra/staff/src/App.tsx b/infra/staff/src/App.tsx
index 33800a0ef0..7da6948415 100644
--- a/infra/staff/src/App.tsx
+++ b/infra/staff/src/App.tsx
@@ -15,6 +15,11 @@ import { UserDetails, type UserDetailsData } from "./components/UserDetails";
import duckieimage from "./components/duckie.png";
import { getUser, type UserResponse } from "./services/admin-user";
import { StaffSessionProvider } from "./services/session";
+import {
+ dateFromMicroseconds,
+ formatStorageSize,
+ SUCCESS_COLOR,
+} from "./utils";
export const App: React.FC = () => {
const [urlCredentials] = useState(readUrlCredentials);
@@ -176,7 +181,7 @@ export const App: React.FC = () => {
centered
sx={{
"& .MuiTabs-indicator": {
- backgroundColor: "#00B33C",
+ backgroundColor: SUCCESS_COLOR,
height: "5px",
borderRadius: "20px",
},
@@ -268,8 +273,8 @@ const buildUserDetailsData = (
kind: "text",
label: "Creation time",
value:
- new Date(
- userResponse.user.creationTime / 1000,
+ dateFromMicroseconds(
+ userResponse.user.creationTime,
).toLocaleString() || "None",
},
],
@@ -277,17 +282,20 @@ const buildUserDetailsData = (
{
kind: "text",
label: "Total",
- value: formatStorage(subscription.storage, true),
+ value:
+ subscription.storage === 0
+ ? "None"
+ : formatStorageSize(subscription.storage),
},
{
kind: "text",
label: "Consumed",
- value: formatStorage(userResponse.details?.usage),
+ value: formatStorageSize(userResponse.details?.usage),
},
{
kind: "text",
label: "Bonus",
- value: formatStorage(userResponse.details?.storageBonus),
+ value: formatStorageSize(userResponse.details?.storageBonus),
},
],
subscription: [
@@ -305,8 +313,9 @@ const buildUserDetailsData = (
kind: "expiry",
label: "Expiry time",
value:
- new Date(subscription.expiryTime / 1000).toISOString() ||
- "None",
+ dateFromMicroseconds(
+ subscription.expiryTime,
+ ).toISOString() || "None",
},
],
security: [
@@ -330,13 +339,3 @@ const buildUserDetailsData = (
},
};
};
-
-const formatStorage = (bytes: number | undefined, noneWhenZero = false) => {
- if (bytes === undefined || (noneWhenZero && bytes === 0)) {
- return "None";
- }
- if (bytes >= 1024 ** 3) {
- return `${(bytes / 1024 ** 3).toFixed(2)} GB`;
- }
- return `${(bytes / 1024 ** 2).toFixed(2)} MB`;
-};
diff --git a/infra/staff/src/components/AddOTT.tsx b/infra/staff/src/components/AddOTT.tsx
index f98eb9fcf5..0736f80706 100644
--- a/infra/staff/src/components/AddOTT.tsx
+++ b/infra/staff/src/components/AddOTT.tsx
@@ -17,6 +17,7 @@ import {
import React, { useState } from "react";
import { addOTT } from "../services/admin-user";
import { useStaffSession } from "../services/session";
+import { microsecondsFromNow } from "../utils";
const APP_OPTIONS = [
{ label: "Photos", value: "photos" },
@@ -182,8 +183,7 @@ export const AddOTT: React.FC<AddOTTProps> = ({ open, onClose, userEmail }) => {
);
};
-const computeExpiryTimeMicros = () =>
- Date.now() * 1000 + 7 * 24 * 60 * 60 * 1_000_000;
+const computeExpiryTimeMicros = () => microsecondsFromNow(7);
const generateOTP = () => {
const values = new Uint32Array(1);
diff --git a/infra/staff/src/components/ChangeEmail.tsx b/infra/staff/src/components/ChangeEmail.tsx
index cf21af58a0..37c86a38c5 100644
--- a/infra/staff/src/components/ChangeEmail.tsx
+++ b/infra/staff/src/components/ChangeEmail.tsx
@@ -11,6 +11,7 @@ import {
import React, { useEffect, useState } from "react";
import { changeUserEmail, getSelectedUserID } from "../services/admin-user";
import { useStaffSessionRef } from "../services/session";
+import { SUCCESS_COLOR } from "../utils";
interface ChangeEmailProps {
open: boolean;
@@ -115,4 +116,4 @@ const dialogCloseButtonSx = { position: "absolute", right: 10, top: 10 };
const fieldLabelSx = { display: "block", mb: "4px", textAlign: "left" };
-const submitSx = { bgcolor: "#00B33C", color: "white" };
+const submitSx = { bgcolor: SUCCESS_COLOR, color: "white" };
diff --git a/infra/staff/src/components/FamilyTable.tsx b/infra/staff/src/components/FamilyTable.tsx
index c73a23609e..b31139b8f2 100644
--- a/infra/staff/src/components/FamilyTable.tsx
+++ b/infra/staff/src/components/FamilyTable.tsx
@@ -13,6 +13,7 @@ import {
import React, { useEffect, useState } from "react";
import { getFamilyMembers, type FamilyMember } from "../services/admin-user";
import { useInitialStaffSession } from "../services/session";
+import { formatBytesToGB } from "../utils";
import { CloseFamily } from "./CloseFamily";
import { StatusBadge } from "./StatusBadge";
@@ -102,12 +103,12 @@ export const FamilyTable: React.FC = () => {
</StatusBadge>
</TableCell>
<TableCell>
- {formatUsageToGB(member.usage)}
+ {formatBytesToGB(member.usage)}
</TableCell>
<TableCell>
{member.status !== "SELF"
? (member.storageLimit &&
- formatUsageToGB(
+ formatBytesToGB(
member.storageLimit,
)) ||
"NA"
@@ -138,6 +139,3 @@ export const FamilyTable: React.FC = () => {
</>
);
};
-
-const formatUsageToGB = (usage: number): string =>
- `${(usage / (1024 * 1024 * 1024)).toFixed(2)} GB`;
diff --git a/infra/staff/src/components/StatusBadge.tsx b/infra/staff/src/components/StatusBadge.tsx
index 5d758fcdef..71629ebb4e 100644
--- a/infra/staff/src/components/StatusBadge.tsx
+++ b/infra/staff/src/components/StatusBadge.tsx
@@ -1,5 +1,6 @@
import { Box } from "@mui/material";
import type { ReactNode } from "react";
+import { SUCCESS_COLOR } from "../utils";
type StatusBadgeTone = "success" | "neutral";
@@ -29,4 +30,4 @@ export const StatusBadge = ({
);
const statusBadgeColor = (tone: StatusBadgeTone) =>
- tone === "success" ? "#00B33C" : "#494949";
+ tone === "success" ? SUCCESS_COLOR : "#494949";
diff --git a/infra/staff/src/components/StorageBonusTableComponent.tsx b/infra/staff/src/components/StorageBonusTableComponent.tsx
index e74040ebc7..211ee7a02a 100644
--- a/infra/staff/src/components/StorageBonusTableComponent.tsx
+++ b/infra/staff/src/components/StorageBonusTableComponent.tsx
@@ -10,6 +10,7 @@ import {
import React, { useEffect, useState } from "react";
import { getStorageBonuses, type StorageBonus } from "../services/admin-user";
import { useInitialStaffSession } from "../services/session";
+import { dateFromMicroseconds, formatBytesToGB } from "../utils";
import { StatusBadge } from "./StatusBadge";
export const StorageBonusTableComponent: React.FC = () => {
@@ -76,7 +77,7 @@ export const StorageBonusTableComponent: React.FC = () => {
{storageBonuses.map((bonus, index) => (
<TableRow key={index}>
<TableCell>
- {formatStorage(bonus.storage)}
+ {formatBytesToGB(bonus.storage)}
</TableCell>
<TableCell>{bonus.type}</TableCell>
<TableCell>
@@ -99,14 +100,11 @@ export const StorageBonusTableComponent: React.FC = () => {
};
const formatCreatedAt = (createdAt: number): string =>
- new Date(createdAt / 1000).toLocaleDateString();
+ dateFromMicroseconds(createdAt).toLocaleDateString();
const formatValidTill = (validTill: number): string => {
if (validTill === 0) {
return "Forever";
}
- return new Date(validTill / 1000).toLocaleDateString();
+ return dateFromMicroseconds(validTill).toLocaleDateString();
};
-
-const formatStorage = (storage: number): string =>
- `${(storage / 1024 ** 3).toFixed(2)} GB`;
diff --git a/infra/staff/src/components/TokenTableComponent.tsx b/infra/staff/src/components/TokenTableComponent.tsx
index fffc7fafc1..3009af733c 100644
--- a/infra/staff/src/components/TokenTableComponent.tsx
+++ b/infra/staff/src/components/TokenTableComponent.tsx
@@ -11,6 +11,7 @@ import {
import React, { useEffect, useMemo, useState } from "react";
import { getTokens, type TokenData } from "../services/admin-user";
import { useInitialStaffSession } from "../services/session";
+import { dateFromMicroseconds } from "../utils";
import { StatusBadge } from "./StatusBadge";
export const TokensTableComponent: React.FC = () => {
@@ -147,4 +148,4 @@ export const TokensTableComponent: React.FC = () => {
};
const formatDate = (timestamp: number): string =>
- new Date(timestamp / 1000).toLocaleDateString();
+ dateFromMicroseconds(timestamp).toLocaleDateString();
diff --git a/infra/staff/src/components/UpdateSubscription.tsx b/infra/staff/src/components/UpdateSubscription.tsx
index fc673a943f..ee28899a9f 100644
--- a/infra/staff/src/components/UpdateSubscription.tsx
+++ b/infra/staff/src/components/UpdateSubscription.tsx
@@ -22,6 +22,13 @@ import {
updateUserSubscription,
} from "../services/admin-user";
import { useInitialStaffSession, useStaffSession } from "../services/session";
+import {
+ bytesToGB,
+ dateFromMicroseconds,
+ dateToMicroseconds,
+ gbToBytes,
+ SUCCESS_COLOR,
+} from "../utils";
interface UpdateSubscriptionProps {
open: boolean;
@@ -61,12 +68,14 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
try {
const subscription =
await getSelectedSubscription(initialSession);
- const expiryTime = new Date(subscription.expiryTime / 1000);
+ const expiryTime = dateFromMicroseconds(
+ subscription.expiryTime,
+ );
setValues({
productId: subscription.productID || "",
provider: subscription.paymentProvider || "",
- storage: subscription.storage / (1024 * 1024 * 1024) || 0,
+ storage: bytesToGB(subscription.storage) || 0,
transactionId: subscription.originalTransactionID || "",
expiryTime,
userId: subscription.userID || "",
@@ -115,12 +124,12 @@ export const UpdateSubscription: React.FC<UpdateSubscriptionProps> = ({
try {
let expiryTime = null;
if (values.expiryTime) {
- expiryTime = values.expiryTime.getTime() * 1000;
+ expiryTime = dateToMicroseconds(values.expiryTime);
}
await updateUserSubscription(session, {
userId: values.userId,
- storage: values.storage * 1024 ** 3,
+ storage: gbToBytes(values.storage),
expiryTime,
productId: values.productId,
paymentProvider: values.provider,
@@ -315,4 +324,4 @@ const fieldWrapperSx = { mb: 1 };
const fieldLabelSx = { display: "block", mb: "4px", textAlign: "left" };
-const submitSx = { bgcolor: "#00B33C", color: "white" };
+const submitSx = { bgcolor: SUCCESS_COLOR, color: "white" };
diff --git a/infra/staff/src/components/UserDetails.tsx b/infra/staff/src/components/UserDetails.tsx
index 1c0b46fca1..0b3bf2b8b0 100644
--- a/infra/staff/src/components/UserDetails.tsx
+++ b/infra/staff/src/components/UserDetails.tsx
@@ -15,6 +15,7 @@ import {
Typography,
} from "@mui/material";
import React, { useEffect, useState } from "react";
+import { SUCCESS_COLOR } from "../utils";
import { AddOTT } from "./AddOTT";
import { ChangeEmail } from "./ChangeEmail";
import { DeleteAccount } from "./DeleteAccount";
@@ -374,17 +375,7 @@ const renderTableCellContent = (
onChange={(e) =>
actions.changeTwoFactor(e.target.checked)
}
- sx={{
- "& .MuiSwitch-switchBase.Mui-checked": {
- color: "#00B33C",
- "&:hover": {
- backgroundColor:
- "rgba(0, 179, 60, 0.08)",
- },
- },
- "& .MuiSwitch-switchBase.Mui-checked + .MuiSwitch-track":
- { backgroundColor: "#00B33C" },
- }}
+ sx={successSwitchSx}
/>
)}
</Box>
@@ -398,7 +389,7 @@ const renderTableCellContent = (
sx={{
color:
isValidExpiryTime && expiryTime > currentTime
- ? "#00B33C"
+ ? SUCCESS_COLOR
: "red",
}}
>
@@ -426,17 +417,7 @@ const renderTableCellContent = (
<Switch
checked={securityControls.emailMFAEnabled}
onChange={actions.toggleEmailMFA}
- sx={{
- "& .MuiSwitch-switchBase.Mui-checked": {
- color: "#00B33C",
- "&:hover": {
- backgroundColor:
- "rgba(0, 179, 60, 0.08)",
- },
- },
- "& .MuiSwitch-switchBase.Mui-checked + .MuiSwitch-track":
- { backgroundColor: "#00B33C" },
- }}
+ sx={successSwitchSx}
/>
)}
</Box>
@@ -447,3 +428,13 @@ const renderTableCellContent = (
};
const enabledLabel = (enabled: boolean) => (enabled ? "Enabled" : "Disabled");
+
+const successSwitchSx = {
+ "& .MuiSwitch-switchBase.Mui-checked": {
+ color: "#00B33C",
+ "&:hover": { backgroundColor: "rgba(0, 179, 60, 0.08)" },
+ },
+ "& .MuiSwitch-switchBase.Mui-checked + .MuiSwitch-track": {
+ backgroundColor: "#00B33C",
+ },
+} as const;
diff --git a/infra/staff/src/utils.ts b/infra/staff/src/utils.ts
new file mode 100644
index 0000000000..046f348b74
--- /dev/null
+++ b/infra/staff/src/utils.ts
@@ -0,0 +1,32 @@
+const MICROSECONDS_IN_MILLISECOND = 1000;
+const MICROSECONDS_IN_DAY = 24 * 60 * 60 * 1_000_000;
+const BYTES_IN_GB = 1024 ** 3;
+const BYTES_IN_MB = 1024 ** 2;
+
+export const SUCCESS_COLOR = "#00B33C";
+
+export const dateFromMicroseconds = (microseconds: number) =>
+ new Date(microseconds / MICROSECONDS_IN_MILLISECOND);
+
+export const dateToMicroseconds = (date: Date) =>
+ date.getTime() * MICROSECONDS_IN_MILLISECOND;
+
+export const microsecondsFromNow = (days: number) =>
+ Date.now() * MICROSECONDS_IN_MILLISECOND + days * MICROSECONDS_IN_DAY;
+
+export const bytesToGB = (bytes: number) => bytes / BYTES_IN_GB;
+
+export const gbToBytes = (gb: number) => gb * BYTES_IN_GB;
+
+export const formatBytesToGB = (bytes: number): string =>
+ `${bytesToGB(bytes).toFixed(2)} GB`;
+
+export const formatStorageSize = (bytes: number | undefined) => {
+ if (bytes === undefined) {
+ return "None";
+ }
+ if (bytes >= BYTES_IN_GB) {
+ return `${bytesToGB(bytes).toFixed(2)} GB`;
+ }
+ return `${(bytes / BYTES_IN_MB).toFixed(2)} MB`;
+};