Skip to content

Open source alternative to Semrush and Ahrefs

Reshape billing into a compact, editable summary (#1072)

Ben Senescu6b538c8fd4ed
diff --git a/src/client/features/billing/AutoReloadNotice.tsx b/src/client/features/billing/AutoReloadNotice.tsx
index fe7dae58..f7e963ce 100644
--- a/src/client/features/billing/AutoReloadNotice.tsx
+++ b/src/client/features/billing/AutoReloadNotice.tsx
@@ -70,7 +70,7 @@ export function AutoReloadNotice({
Auto-reload failed on {formatDate(saved.failure.at)}:{" "}
{CARD_FAILURES[saved.failure.reason] ?? "the charge failed"}.{" "}
{canRetry
- ? "Update your card, then retry below."
+ ? "Update your card, then retry."
: "Update your card so it works the next time you run low."}
</>
) : (
diff --git a/src/client/features/billing/AutoReloadSettings.tsx b/src/client/features/billing/AutoReloadSettings.tsx
index 8f879509..01352de6 100644
--- a/src/client/features/billing/AutoReloadSettings.tsx
+++ b/src/client/features/billing/AutoReloadSettings.tsx
@@ -2,12 +2,7 @@ import { useMutation } from "@tanstack/react-query";
import { useState } from "react";
import { toast } from "sonner";
import { Button } from "@/client/components/ui/button";
-import {
- Field,
- FieldDescription,
- FieldError,
- FieldLabel,
-} from "@/client/components/ui/field";
+import { Field, FieldError, FieldLabel } from "@/client/components/ui/field";
import {
InputGroup,
InputGroupAddon,
@@ -17,6 +12,7 @@ import {
AutoReloadNotice,
UpdateCardButton,
} from "@/client/features/billing/AutoReloadNotice";
+import { BillingRow } from "@/client/features/billing/BillingRow";
import { billingAccountQueryOptions } from "@/client/features/billing/billingAccountQuery";
import { parseUsdAmount } from "@/client/features/billing/HostedBillingContentUtils";
import { useCreditBalance } from "@/client/features/billing/useCreditBalance";
@@ -41,12 +37,12 @@ type AutoReload = NonNullable<
>;
/**
- * The auto-reload section of the Buy credits card. Off: the two amounts and
- * one button to turn it on. On: a status line, the amounts with Save, and
- * Turn off, which acts at once. Turning on can charge the first reload right
- * away (the server buys it before enabling); the button says so. A spent
- * budget shows a notice with a one-click raise. Owner-only at the call site,
- * as the server enforces.
+ * The auto-reload row of the billing summary. Collapsed, it says what is set
+ * and offers Turn on or Edit; the amounts open only when asked for. Turning
+ * on can charge the first reload right away (the server buys it before
+ * enabling); the button says so. A failed reload or a spent budget shows a
+ * notice even while collapsed, the latter with a one-click raise.
+ * Owner-only at the call site, as the server enforces.
*/
export function AutoReloadSettings() {
const {
@@ -59,12 +55,15 @@ export function AutoReloadSettings() {
// The saved control while it is on; null when off or never set up.
const on = saved?.enabled ? saved : null;
- const [fields, setFields] = useState<Record<keyof AutoReloadValues, string>>({
+ const savedFields = () => ({
amountUsd: String(saved?.amountUsd ?? AUTO_RELOAD_DEFAULTS.amountUsd),
monthlyLimitUsd: String(
saved?.monthlyLimitUsd ?? AUTO_RELOAD_DEFAULTS.monthlyLimitUsd,
),
});
+ const [editing, setEditing] = useState(false);
+ const [fields, setFields] =
+ useState<Record<keyof AutoReloadValues, string>>(savedFields);
const amount = parseUsdAmount(fields.amountUsd, {
fallback: saved?.amountUsd ?? AUTO_RELOAD_DEFAULTS.amountUsd,
@@ -84,10 +83,9 @@ export function AutoReloadSettings() {
on.amountUsd !== values.amountUsd ||
on.monthlyLimitUsd !== values.monthlyLimitUsd;
// The server decides whether saving buys the first reload now (also the
- // retry after a failed one), and the page shows that same decision. A
- // retry is worth submitting with nothing changed.
+ // retry after a failed one), and the page shows that same decision.
const budgetUnderSpend = isBudgetUnderSpend(saved, values.monthlyLimitUsd);
- const canSubmit = isValid && !budgetUnderSpend && (isDirty || chargesNow);
+ const canSubmit = isValid && !budgetUnderSpend;
const hint = hintCopy({
saved,
on,
@@ -107,6 +105,7 @@ export function AutoReloadSettings() {
void queryClient.invalidateQueries({
queryKey: billingAccountQueryOptions().queryKey,
});
+ setEditing(false);
toast.success(input.enabled ? "Auto-reload is on" : "Auto-reload is off");
},
});
@@ -121,116 +120,151 @@ export function AutoReloadSettings() {
mutation.mutate({ enabled: true, ...next });
};
+ const openForm = () => {
+ setFields(savedFields());
+ mutation.reset();
+ setEditing(true);
+ };
+
const saveLabel = chargesNow
? `${on ? "Retry" : "Turn on"} and buy $${values.amountUsd} now`
: on
- ? "Save changes"
+ ? "Save"
: "Turn on auto-reload";
- return (
- <div className="space-y-3">
- <div>
- <p className="text-sm font-medium">Auto-reload</p>
- <p className="text-xs text-muted-foreground">
- When your top-up credits drop to ${AUTO_RELOAD_THRESHOLD_USD} or less,
- we charge your card for more, up to a monthly budget you set.
- </p>
- </div>
-
- {on ? (
- <p className="text-xs text-muted-foreground">
- <span className="font-medium text-foreground">On</span> &middot; $
- {on.spentUsd.toFixed(2)} of ${on.monthlyLimitUsd} spent this month
- {refillDate ? `, resets ${refillDate}` : ""}
- </p>
- ) : null}
-
- {saved ? (
- <AutoReloadNotice
- saved={saved}
- state={autoReloadState}
- canRetry={chargesNow}
- resetDate={refillDate}
- // The raise is the plain save; when saving would charge the card,
- // the main button says so and is the way to do it.
- onRaise={
- on && !chargesNow
- ? (monthlyLimitUsd) =>
- save({ amountUsd: on.amountUsd, monthlyLimitUsd })
- : undefined
- }
- pending={mutation.isPending}
- />
- ) : null}
+ const notice = saved ? (
+ <AutoReloadNotice
+ saved={saved}
+ state={autoReloadState}
+ canRetry={chargesNow}
+ resetDate={refillDate}
+ // The raise is the plain save; when saving would charge the card,
+ // the form's button says so and is the way to do it.
+ onRaise={
+ on && !chargesNow
+ ? (monthlyLimitUsd) =>
+ save({ amountUsd: on.amountUsd, monthlyLimitUsd })
+ : undefined
+ }
+ pending={mutation.isPending}
+ />
+ ) : null;
- <div className="grid gap-3 sm:grid-cols-2">
- <AmountField
- id="auto-reload-amount"
- label="Add each time"
- value={fields.amountUsd}
- error={
- amount.isValid
- ? undefined
- : `Enter between $${TOP_UP_MIN_USD}–$${TOP_UP_MAX_USD}.`
- }
- onChange={(value) =>
- setFields((prev) => ({ ...prev, amountUsd: value }))
- }
- />
- <AmountField
- id="auto-reload-budget"
- label="Monthly budget"
- value={fields.monthlyLimitUsd}
- error={
- budget.isValid
- ? undefined
- : `Enter between $${amount.parsed}–$${AUTO_RELOAD_MAX_MONTHLY_USD}.`
- }
- description="The most you spend on top-up credits a month, including ones you buy yourself."
- onChange={(value) =>
- setFields((prev) => ({ ...prev, monthlyLimitUsd: value }))
- }
- />
- </div>
+ return (
+ <BillingRow
+ label="Auto-reload"
+ summary={
+ on ? (
+ <>
+ <span className="font-medium text-foreground">On</span> &middot;
+ Adds ${on.amountUsd} when top-ups drop to $
+ {AUTO_RELOAD_THRESHOLD_USD} &middot; ${on.spentUsd.toFixed(2)} of $
+ {on.monthlyLimitUsd} monthly budget used
+ {refillDate ? `, resets ${refillDate}` : ""}
+ </>
+ ) : (
+ <>
+ Off &middot; Buy more automatically when top-ups drop to $
+ {AUTO_RELOAD_THRESHOLD_USD}
+ </>
+ )
+ }
+ action={
+ editing ? null : (
+ <Button variant="outline" size="sm" onClick={openForm}>
+ {!on ? "Turn on" : chargesNow ? "Retry" : "Edit"}
+ </Button>
+ )
+ }
+ >
+ {notice}
+ {editing ? (
+ <div className="max-w-xl space-y-3">
+ <div className="grid gap-3 sm:grid-cols-2">
+ <AmountField
+ id="auto-reload-amount"
+ label="Add each time"
+ value={fields.amountUsd}
+ error={
+ amount.isValid
+ ? undefined
+ : `Enter between $${TOP_UP_MIN_USD}–$${TOP_UP_MAX_USD}.`
+ }
+ onChange={(value) =>
+ setFields((prev) => ({ ...prev, amountUsd: value }))
+ }
+ />
+ <AmountField
+ id="auto-reload-budget"
+ label="Monthly budget"
+ value={fields.monthlyLimitUsd}
+ error={
+ budget.isValid
+ ? undefined
+ : `Enter between $${amount.parsed}–$${AUTO_RELOAD_MAX_MONTHLY_USD}.`
+ }
+ onChange={(value) =>
+ setFields((prev) => ({ ...prev, monthlyLimitUsd: value }))
+ }
+ />
+ </div>
+ <p className="text-xs text-muted-foreground">
+ The budget caps what you spend on top-up credits each month,
+ including ones you buy yourself.
+ </p>
- {hint ? <p className="text-xs text-amber-600">{hint}</p> : null}
+ {hint ? <p className="text-xs text-amber-600">{hint}</p> : null}
- <div className="flex gap-2">
- <Button
- variant="secondary"
- className="flex-1"
- pending={saving}
- disabled={mutation.isPending || !canSubmit}
- onClick={() => save(values)}
- >
- {saveLabel}
- </Button>
- {on ? (
- <Button
- variant="ghost"
- pending={turningOff}
- disabled={mutation.isPending}
- onClick={() => mutation.mutate({ enabled: false })}
- >
- Turn off
- </Button>
- ) : null}
- </div>
+ <div className="flex flex-wrap items-center gap-2">
+ <Button
+ size="sm"
+ pending={saving}
+ disabled={mutation.isPending || !canSubmit}
+ onClick={() =>
+ // Nothing to change and nothing to retry: just close.
+ isDirty || chargesNow ? save(values) : setEditing(false)
+ }
+ >
+ {saveLabel}
+ </Button>
+ <Button
+ variant="ghost"
+ size="sm"
+ disabled={mutation.isPending}
+ onClick={() => setEditing(false)}
+ >
+ Cancel
+ </Button>
+ {on ? (
+ <Button
+ variant="ghost"
+ size="sm"
+ className="ml-auto text-muted-foreground"
+ pending={turningOff}
+ disabled={mutation.isPending}
+ onClick={() => mutation.mutate({ enabled: false })}
+ >
+ Turn off auto-reload
+ </Button>
+ ) : null}
+ </div>
- {mutation.isError ? (
- <div className="space-y-2">
- <p role="alert" className="text-sm text-destructive">
- {getStandardErrorMessage(
- mutation.error,
- "We couldn't save auto-reload. Please try again.",
- )}
- </p>
- {getErrorCode(mutation.error) === "PAYMENT_METHOD_REQUIRED" ? (
- <UpdateCardButton />
+ {mutation.isError ? (
+ <div className="space-y-2">
+ <p role="alert" className="text-sm text-destructive">
+ {getStandardErrorMessage(
+ mutation.error,
+ "We couldn't save auto-reload. Please try again.",
+ )}
+ </p>
+ {getErrorCode(mutation.error) === "PAYMENT_METHOD_REQUIRED" ? (
+ <UpdateCardButton />
+ ) : null}
+ </div>
) : null}
</div>
) : null}
- </div>
+ </BillingRow>
);
}
@@ -285,14 +319,12 @@ function AmountField({
label,
value,
error,
- description,
onChange,
}: {
id: string;
label: string;
value: string;
error: string | undefined;
- description?: string;
onChange: (value: string) => void;
}) {
return (
@@ -312,11 +344,7 @@ function AmountField({
onChange={(event) => onChange(event.target.value)}
/>
</InputGroup>
- {error ? (
- <FieldError>{error}</FieldError>
- ) : description ? (
- <FieldDescription className="text-xs">{description}</FieldDescription>
- ) : null}
+ {error ? <FieldError>{error}</FieldError> : null}
</Field>
);
}
diff --git a/src/client/features/billing/BillingRow.tsx b/src/client/features/billing/BillingRow.tsx
new file mode 100644
index 00000000..401160d4
--- /dev/null
+++ b/src/client/features/billing/BillingRow.tsx
@@ -0,0 +1,31 @@
+import type { ReactNode } from "react";
+
+/**
+ * One line of the billing summary: a label, what is set now, and the action
+ * that changes it. A form opened by the action goes in `children`, under the
+ * summary, so the list stays short until someone edits.
+ */
+export function BillingRow({
+ label,
+ summary,
+ action,
+ children,
+}: {
+ label: string;
+ summary: ReactNode;
+ action?: ReactNode;
+ children?: ReactNode;
+}) {
+ return (
+ <div className="grid gap-x-6 gap-y-3 px-4 py-4 sm:grid-cols-[10rem_1fr_auto] sm:items-center">
+ <p className="text-sm font-medium">{label}</p>
+ <div className="min-w-0 text-sm text-muted-foreground">{summary}</div>
+ {action ? <div className="sm:justify-self-end">{action}</div> : null}
+ {children ? (
+ <div className="min-w-0 space-y-3 empty:hidden sm:col-span-2 sm:col-start-2">
+ {children}
+ </div>
+ ) : null}
+ </div>
+ );
+}
diff --git a/src/routes/_app/billing.tsx b/src/routes/_app/billing.tsx
index 3e705d93..4586e055 100644
--- a/src/routes/_app/billing.tsx
+++ b/src/routes/_app/billing.tsx
@@ -5,14 +5,8 @@ import { ErrorState } from "@/client/components/ErrorState";
import { PageHeader } from "@/client/components/PageHeader";
import { QueryError } from "@/client/components/QueryState";
import { Button } from "@/client/components/ui/button";
-import {
- Card,
- CardContent,
- CardHeader,
- CardTitle,
- CardDescription,
-} from "@/client/components/ui/card";
-import { Field, FieldError } from "@/client/components/ui/field";
+import { Card, CardContent } from "@/client/components/ui/card";
+import { Field } from "@/client/components/ui/field";
import {
InputGroup,
InputGroupAddon,
@@ -29,6 +23,7 @@ import {
prefetchUpgradeCheckout,
} from "@/client/features/billing/checkout";
import { AutoReloadSettings } from "@/client/features/billing/AutoReloadSettings";
+import { BillingRow } from "@/client/features/billing/BillingRow";
import { BillingTabs } from "@/client/features/billing/BillingTabs";
import { parseTopUpAmount } from "@/client/features/billing/HostedBillingContentUtils";
import { billingUsageQueryOptions } from "@/client/features/billing/useBillingUsage";
@@ -36,7 +31,6 @@ import { useCreditBalance } from "@/client/features/billing/useCreditBalance";
import {
BASE_PLAN_OFFER,
getPlanName,
- monthlyCreditsFeature,
} from "@/client/features/billing/plan-offers";
import {
createBillingPortalSession,
@@ -80,6 +74,7 @@ function BillingPage() {
const { tab } = Route.useSearch();
const navigate = useNavigate();
const [topUpAmount, setTopUpAmount] = useState("20");
+ const [buyingCredits, setBuyingCredits] = useState(false);
const [pendingAction, setPendingAction] = useState<BillingAction | null>(
null,
);
@@ -155,9 +150,7 @@ function BillingPage() {
<PageHeader title="Billing" />
{accountQuery.isPending ? (
- <div className="grid gap-5 md:grid-cols-2">
- <SubscriptionCardSkeleton />
- </div>
+ <SubscriptionCardSkeleton />
) : !account ? (
<QueryError
cause={accountQuery.error}
@@ -176,215 +169,210 @@ function BillingPage() {
/>
) : null}
- <div className="grid gap-5 md:grid-cols-2">
- {/* Subscription card */}
- <Card>
- <CardContent className="flex h-full flex-col justify-between gap-4">
- <div>
- <div className="text-2xl font-semibold tabular-nums">
- ${totalRemaining.toFixed(2)}{" "}
- <span className="text-sm font-normal text-muted-foreground">
- remaining
- </span>
- </div>
- {!isFreePlan ? (
- <div className="mt-1 flex gap-3 text-xs text-muted-foreground">
- <span className="tabular-nums">
- Monthly ${monthlyRemaining.toFixed(2)}
- </span>
- <span>&middot;</span>
- <span className="tabular-nums">
- Top-ups ${topUpRemaining.toFixed(2)}
- </span>
- </div>
- ) : null}
- {isOutOfCredits ? (
- <p className="mt-2 text-xs text-destructive">
- {isFreePlan
- ? "You’ve used your free credits. Upgrade your plan to continue."
- : `You’ve used this month’s credits. Buy more below to keep going${
- refillDate
- ? `, or wait for them to refill on ${refillDate}`
- : ""
- }.`}
- </p>
- ) : isLowCredits ? (
- <p className="mt-2 text-xs text-amber-600">
- You&rsquo;re running low on credits.{" "}
- {isFreePlan
- ? `Upgrade to get $${BASE_PLAN_OFFER.monthlyCreditsUsd}/month.`
- : "Buy more credits below."}
- </p>
- ) : null}
- </div>
-
- <div className="text-sm">
- <span className="font-medium">Plan</span>{" "}
- <span className="text-muted-foreground">
- {account.paidPlanId
- ? getPlanName(account.paidPlanId)
- : "Free Plan"}
+ <Card className="max-w-4xl gap-0 divide-y divide-border py-0">
+ <div className="px-4 py-4">
+ <div className="text-2xl font-semibold tabular-nums">
+ ${totalRemaining.toFixed(2)}{" "}
+ <span className="text-sm font-normal text-muted-foreground">
+ remaining
+ </span>
+ </div>
+ {!isFreePlan ? (
+ <div className="mt-1 flex gap-3 text-xs text-muted-foreground">
+ <span className="tabular-nums">
+ Monthly ${monthlyRemaining.toFixed(2)}
+ </span>
+ <span>&middot;</span>
+ <span className="tabular-nums">
+ Top-ups ${topUpRemaining.toFixed(2)}
</span>
- {account.paidPlanId ? (
- <span className="text-muted-foreground">
- {" "}
- &middot; ${account.monthlyCreditsUsd.toFixed(2)} of Usage
- Credits each month
- </span>
- ) : null}
</div>
+ ) : null}
+ {isOutOfCredits ? (
+ <p className="mt-2 text-xs text-destructive">
+ {isFreePlan
+ ? "You’ve used your free credits. Upgrade your plan to continue."
+ : `You’ve used this month’s credits. Buy more below to keep going${
+ refillDate
+ ? `, or wait for them to refill on ${refillDate}`
+ : ""
+ }.`}
+ </p>
+ ) : isLowCredits ? (
+ <p className="mt-2 text-xs text-amber-600">
+ You&rsquo;re running low on credits.{" "}
+ {isFreePlan
+ ? `Upgrade to get $${BASE_PLAN_OFFER.monthlyCreditsUsd}/month.`
+ : "Buy more credits below."}
+ </p>
+ ) : null}
+ </div>
- {!canManageBilling ? (
- <p className="border-t border-border pt-3 text-sm text-muted-foreground">
- Only the organization owner can change the plan or buy
- credits. Ask them if you need more.
- </p>
- ) : isFreePlan ? (
- <div className="space-y-3 border-t border-border pt-3">
- <div className="flex items-baseline justify-between gap-4">
- <span className="text-sm font-medium">
- {BASE_PLAN_OFFER.name}
- </span>
- <span className="text-sm font-medium tabular-nums">
- ${BASE_PLAN_OFFER.priceUsd}/month
- </span>
- </div>
- <ul className="space-y-1.5">
- {[
- "Access to all OpenSEO features",
- monthlyCreditsFeature(BASE_PLAN_OFFER),
- ].map((item) => (
- <li
- key={item}
- className="flex gap-2 text-xs text-muted-foreground"
- >
- <span className="mt-[1px] shrink-0 text-muted-foreground/60">
- &mdash;
- </span>
- {item}
- </li>
- ))}
- </ul>
- <Button
- variant="secondary"
- className="w-full"
- pending={pendingAction === "plan"}
- disabled={pendingAction !== null}
- onClick={() =>
- void runAction(
- "plan",
- openUpgradeCheckout,
- "We couldn't start the checkout. Please try again.",
- )
- }
- >
- {pendingAction === "plan"
- ? "Redirecting to Stripe..."
- : "Upgrade Plan"}
- </Button>
- {actionError("plan")}
- </div>
+ <BillingRow
+ label="Plan"
+ summary={
+ account.paidPlanId ? (
+ <>
+ <span className="text-foreground">
+ {getPlanName(account.paidPlanId)}
+ </span>{" "}
+ &middot; ${account.monthlyCreditsUsd.toFixed(2)} of credits
+ each month
+ </>
) : (
- <div className="space-y-3">
- <Button
- variant="secondary"
- className="w-full"
- pending={pendingAction === "plan"}
- disabled={pendingAction !== null}
- onClick={() =>
- void runAction(
- "plan",
- openPortal,
- "We couldn't open the billing portal. Please try again.",
- )
- }
- >
- {pendingAction === "plan"
- ? "Opening Stripe..."
- : "Manage subscription"}
- </Button>
- {actionError("plan")}
- </div>
- )}
- </CardContent>
- </Card>
-
- {/* Buy credits card — paid plan only, owner-only. The sync turns
- auto-reload off when a plan ends; until that lands, an owner
- who left the plan still sees it, so they can turn it off. */}
- {isFreePlan && canManageBilling && account.autoReload?.enabled ? (
- <Card>
- <CardContent>
- <AutoReloadSettings />
- </CardContent>
- </Card>
- ) : null}
- {!isFreePlan && canManageBilling ? (
- <Card>
- <CardHeader>
- <CardTitle>Buy credits</CardTitle>
- <CardDescription>
- Top-up credits never expire and are used after your monthly
- credits.
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-3">
- <Field data-invalid={showTopUpError}>
- <InputGroup>
- <InputGroupAddon>$</InputGroupAddon>
- <InputGroupInput
- type="number"
- min={TOP_UP_MIN_USD}
- max={TOP_UP_MAX_USD}
- step={1}
- inputMode="numeric"
- aria-label="Top-up amount in USD"
- aria-invalid={showTopUpError}
- value={topUpAmount}
- onChange={(e) => setTopUpAmount(e.target.value)}
- />
- </InputGroup>
- {showTopUpError ? (
- <FieldError>
- Enter between ${TOP_UP_MIN_USD}–${TOP_UP_MAX_USD}.
- </FieldError>
+ <>
+ <span className="text-foreground">Free Plan</span>
+ {canManageBilling ? (
+ <>
+ {" "}
+ &middot; Upgrade to {BASE_PLAN_OFFER.name} ($
+ {BASE_PLAN_OFFER.priceUsd}/month) for every feature and
+ ${BASE_PLAN_OFFER.monthlyCreditsUsd} of credits each
+ month
+ </>
) : null}
- </Field>
-
+ </>
+ )
+ }
+ action={
+ !canManageBilling ? null : (
<Button
- variant="secondary"
- className="w-full"
- pending={pendingAction === "topUp"}
- disabled={
- !isValidTopUp || pendingAction !== null || budgetSpent
- }
+ variant={isFreePlan ? "default" : "outline"}
+ size="sm"
+ pending={pendingAction === "plan"}
+ disabled={pendingAction !== null}
onClick={() =>
- void runAction(
- "topUp",
- startTopUpCheckout,
- "We couldn't start the checkout. Please try again.",
- )
+ isFreePlan
+ ? void runAction(
+ "plan",
+ openUpgradeCheckout,
+ "We couldn't start the checkout. Please try again.",
+ )
+ : void runAction(
+ "plan",
+ openPortal,
+ "We couldn't open the billing portal. Please try again.",
+ )
}
>
- {pendingAction === "topUp"
+ {pendingAction === "plan"
? "Redirecting to Stripe..."
- : "Buy credits"}
+ : isFreePlan
+ ? "Upgrade Plan"
+ : "Manage subscription"}
</Button>
- {budgetSpent ? (
- <p className="text-xs text-muted-foreground">
- You&rsquo;ve hit your monthly auto-reload budget. Raise it
- below to buy more.
- </p>
- ) : null}
- {actionError("topUp")}
+ )
+ }
+ >
+ {!canManageBilling ? (
+ <p className="text-sm text-muted-foreground">
+ Only the organization owner can change the plan or buy
+ credits. Ask them if you need more.
+ </p>
+ ) : (
+ actionError("plan")
+ )}
+ </BillingRow>
- <div className="border-t border-border pt-4">
- <AutoReloadSettings />
+ {/* Owner-only. Buying needs a paid plan; free plans see what one
+ adds, so the base plan's credits aren't read as a cap. */}
+ {canManageBilling ? (
+ <BillingRow
+ label="Top-up credits"
+ summary={
+ isFreePlan
+ ? "Need more than your monthly credits? On a paid plan, buy as many as you need. They never expire."
+ : "Never expire and are used after your monthly credits."
+ }
+ action={
+ isFreePlan || buyingCredits ? null : (
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={() => setBuyingCredits(true)}
+ >
+ Buy credits
+ </Button>
+ )
+ }
+ >
+ {!isFreePlan && buyingCredits ? (
+ <div className="space-y-3">
+ <div className="flex flex-wrap items-start gap-2">
+ <Field data-invalid={showTopUpError} className="w-36">
+ <InputGroup>
+ <InputGroupAddon>$</InputGroupAddon>
+ <InputGroupInput
+ type="number"
+ min={TOP_UP_MIN_USD}
+ max={TOP_UP_MAX_USD}
+ step={1}
+ inputMode="numeric"
+ aria-label="Top-up amount in USD"
+ aria-invalid={showTopUpError}
+ autoFocus
+ value={topUpAmount}
+ onChange={(e) => setTopUpAmount(e.target.value)}
+ />
+ </InputGroup>
+ </Field>
+ <Button
+ className="h-10"
+ pending={pendingAction === "topUp"}
+ disabled={
+ !isValidTopUp || pendingAction !== null || budgetSpent
+ }
+ onClick={() =>
+ void runAction(
+ "topUp",
+ startTopUpCheckout,
+ "We couldn't start the checkout. Please try again.",
+ )
+ }
+ >
+ {pendingAction === "topUp"
+ ? "Redirecting to Stripe..."
+ : `Buy $${parsedTopUpAmount} of credits`}
+ </Button>
+ <Button
+ variant="ghost"
+ className="h-10"
+ disabled={pendingAction !== null}
+ onClick={() => setBuyingCredits(false)}
+ >
+ Cancel
+ </Button>
+ </div>
+ {showTopUpError ? (
+ <p className="text-xs text-destructive">
+ Enter between ${TOP_UP_MIN_USD}–${TOP_UP_MAX_USD}.
+ </p>
+ ) : null}
+ {budgetSpent ? (
+ <p className="text-xs text-muted-foreground">
+ You&rsquo;ve hit your monthly auto-reload budget. Raise
+ it below to buy more.
+ </p>
+ ) : null}
+ {actionError("topUp")}
</div>
- </CardContent>
- </Card>
+ ) : null}
+ </BillingRow>
+ ) : null}
+
+ {/* The sync turns auto-reload off when a plan ends; until that
+ lands, an owner who left the plan still sees it, so they can
+ turn it off. */}
+ {canManageBilling &&
+ (!isFreePlan || account.autoReload?.enabled) ? (
+ <AutoReloadSettings />
+ ) : canManageBilling ? (
+ <BillingRow
+ label="Auto-reload"
+ summary="On a paid plan, buy more automatically when you run low, up to a monthly budget you set."
+ />
) : null}
- </div>
+ </Card>
</>
)}
@@ -410,7 +398,7 @@ function BillingPage() {
);
}
-/** The subscription card's shape while the billing account loads. */
+/** The billing summary's shape while the billing account loads. */
function SubscriptionCardSkeleton() {
return (
<Card aria-busy>