Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): dedup settings money formatting and top-up constants

David Le4f41456bb3ff
diff --git a/site/src/app/app/settings/_components/CreditsCard.tsx b/site/src/app/app/settings/_components/CreditsCard.tsx
index 87ec6977..c5c1540d 100644
--- a/site/src/app/app/settings/_components/CreditsCard.tsx
+++ b/site/src/app/app/settings/_components/CreditsCard.tsx
@@ -13,6 +13,11 @@ import {
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Skeleton } from "@/components/ui/skeleton";
+import { formatUsd } from "@/lib/credits/format-usd";
+import {
+ creditTopUpMinDollars,
+ creditTopUpPresetsDollars,
+} from "@/lib/credits/top-up";
import { ApiError } from "@/queries/apiClient";
import {
useCreditAutoReload,
@@ -21,19 +26,6 @@ import {
useUpdateCreditAutoReload,
} from "@/queries/credits";
-const presets = [5, 25, 50, 100];
-
-function formatDollars(value: string): string {
- const parsed = Number.parseFloat(value);
- if (!Number.isFinite(parsed)) {
- return "$0.00";
- }
- return parsed.toLocaleString("en-US", {
- currency: "USD",
- style: "currency",
- });
-}
-
export function CreditsCard() {
const balance = useCreditBalance();
const checkout = useStartCreditCheckout();
@@ -46,7 +38,8 @@ export function CreditsCard() {
};
const customValue = Number.parseInt(customAmount, 10);
- const customValid = Number.isFinite(customValue) && customValue >= 5;
+ const customValid =
+ Number.isFinite(customValue) && customValue >= creditTopUpMinDollars;
return (
<Card>
@@ -62,7 +55,7 @@ export function CreditsCard() {
<Skeleton className="h-9 w-32" />
) : (
<div className="text-3xl font-semibold tabular-nums">
- {formatDollars(balance.data?.balance ?? "0")}
+ {formatUsd(balance.data?.balance ?? "0")}
</div>
)}
<p className="mt-1 text-sm text-muted-foreground">Available balance</p>
@@ -71,7 +64,7 @@ export function CreditsCard() {
<div className="space-y-3">
<Label>Buy credits</Label>
<div className="flex flex-wrap gap-2">
- {presets.map((amount) => (
+ {creditTopUpPresetsDollars.map((amount) => (
<Button
disabled={checkout.isPending}
key={amount}
@@ -112,7 +105,9 @@ export function CreditsCard() {
) : null}
</div>
- <AutoReloadSection onNeedsCard={() => startCheckout(presets[1])} />
+ <AutoReloadSection
+ onNeedsCard={() => startCheckout(creditTopUpPresetsDollars[1])}
+ />
</CardContent>
</Card>
);
diff --git a/site/src/app/app/settings/_components/ProCard.tsx b/site/src/app/app/settings/_components/ProCard.tsx
index 11284036..a4915426 100644
--- a/site/src/app/app/settings/_components/ProCard.tsx
+++ b/site/src/app/app/settings/_components/ProCard.tsx
@@ -16,17 +16,7 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
-
-function formatDollars(value: string | null): string {
- if (value === null) {
- return "—";
- }
- const parsed = Number.parseFloat(value);
- if (!Number.isFinite(parsed)) {
- return "—";
- }
- return parsed.toLocaleString("en-US", { currency: "USD", style: "currency" });
-}
+import { formatUsd } from "@/lib/credits/format-usd";
export function ProCard() {
const pro = useProSubscription();
@@ -68,8 +58,8 @@ export function ProCard() {
{isActive && data ? (
<div className="space-y-1">
<div className="text-foreground">
- {formatDollars(data.allowanceRemaining)} of{" "}
- {formatDollars(data.monthlyAllowance)} included left this month
+ {formatUsd(data.allowanceRemaining)} of{" "}
+ {formatUsd(data.monthlyAllowance)} included left this month
</div>
<div>
Renews:{" "}
diff --git a/site/src/lib/credits/format-usd.ts b/site/src/lib/credits/format-usd.ts
new file mode 100644
index 00000000..dff9995c
--- /dev/null
+++ b/site/src/lib/credits/format-usd.ts
@@ -0,0 +1,13 @@
+// Shared USD formatter for the settings cards so money renders identically
+// everywhere. null/undefined → an em dash (no value yet); a non-numeric string
+// → "$0.00"; otherwise a localized currency string.
+export function formatUsd(value: string | null | undefined): string {
+ if (value === null || value === undefined) {
+ return "—";
+ }
+ const parsed = Number.parseFloat(value);
+ if (!Number.isFinite(parsed)) {
+ return "$0.00";
+ }
+ return parsed.toLocaleString("en-US", { currency: "USD", style: "currency" });
+}