Skip to content

Open source CapCut alternative.

TypeScript

feat(site): manual credit grants carry an expiry

David Led15000eabee7
diff --git a/site/src/app/api/credits/grants/route.ts b/site/src/app/api/credits/grants/route.ts
index aa20e660..bd2a6fe8 100644
--- a/site/src/app/api/credits/grants/route.ts
+++ b/site/src/app/api/credits/grants/route.ts
@@ -3,18 +3,24 @@ import { z } from "zod";
import { creditMicrosToString, creditStringToMicros } from "@/lib/credits/amounts";
import { getCreditBalance, grantCredits } from "@/lib/credits/inference";
-import { maxCreditGrantDollars } from "@/lib/credits/top-up";
+import {
+ creditGrantExpiry,
+ maxCreditGrantDollars,
+ maxCreditGrantExpiryDays,
+} from "@/lib/credits/top-up";
import { notFoundResponse, withSuperUser } from "@/lib/donkey-api-auth";
import { prisma } from "@/lib/prisma";
// The target is identified by userId (grant to self) or by email (grant to
// another user). At least one is required. A single grant is capped to guard
-// against typos.
+// against typos. The caller states how long the grant lives; null keeps it
+// forever.
const creditGrantRequestSchema = z
.object({
amountDollars: z.coerce.number().int().positive().max(maxCreditGrantDollars),
description: z.string().trim().min(1).max(500).optional(),
email: z.string().trim().email().optional(),
+ expiresAfterDays: z.number().int().min(1).max(maxCreditGrantExpiryDays).nullable(),
sourceId: z.string().trim().min(1).max(160).optional(),
userId: z.string().trim().min(1).optional(),
})
@@ -61,8 +67,10 @@ export const POST = withSuperUser(async (request) => {
const grant = await grantCredits({
amountMicros,
description,
+ expiresAt: creditGrantExpiry(parsed.data.expiresAfterDays),
metadata: {
amountDollars: String(parsed.data.amountDollars),
+ expiresAfterDays: parsed.data.expiresAfterDays,
grantedByUserId: request.donkey.userId,
targetUserId: targetUser.id,
},
@@ -77,6 +85,7 @@ export const POST = withSuperUser(async (request) => {
creditMicrosGranted: amountMicros.toString(),
creditsGranted: creditMicrosToString(amountMicros),
grant: {
+ expiresAt: grant.expiresAt?.toISOString() ?? null,
id: grant.id,
source: grant.source,
sourceId: grant.sourceId,
diff --git a/site/src/app/su/credits/page.tsx b/site/src/app/su/credits/page.tsx
index e0a81781..95b5ef92 100644
--- a/site/src/app/su/credits/page.tsx
+++ b/site/src/app/su/credits/page.tsx
@@ -21,10 +21,13 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
+import { SETTINGS } from "@/lib/config/registry";
import {
+ creditGrantExpiryPresets,
creditTopUpDefaultDollars,
creditTopUpPresetsDollars,
maxCreditGrantDollars,
+ maxCreditGrantExpiryDays,
} from "@/lib/credits/top-up";
import { useAccount, useGrantCredits } from "@/queries/credits";
@@ -33,6 +36,17 @@ import { useAccount, useGrantCredits } from "@/queries/credits";
// maxCreditGrantDollars to guard against typos.
const presetDollars = creditTopUpPresetsDollars;
+// How long the grant lives: a preset, a custom day count, or never. The pick
+// starts on the lifetime the signup grant defaults to. An empty custom field
+// stands for never.
+const defaultExpiryDays = SETTINGS.signupCredits.default.expiresAfterDays;
+
+function describeExpiry(days: number | null): string {
+ if (days === null) return "never expires";
+ const preset = creditGrantExpiryPresets.find((p) => p.days === days);
+ return `expires in ${preset ? preset.label : `${days} days`}`;
+}
+
export default function SuCreditsPage() {
const account = useAccount();
const grant = useGrantCredits();
@@ -40,6 +54,7 @@ export default function SuCreditsPage() {
// super user edits the field we track their override here.
const [emailOverride, setEmailOverride] = useState<string | null>(null);
const [amount, setAmount] = useState(String(creditTopUpDefaultDollars));
+ const [expiry, setExpiry] = useState(defaultExpiryDays === null ? "" : String(defaultExpiryDays));
const [confirmOpen, setConfirmOpen] = useState(false);
const [lastResult, setLastResult] = useState<string | null>(null);
@@ -63,22 +78,34 @@ export default function SuCreditsPage() {
amountDollars > 0 &&
amountDollars <= maxCreditGrantDollars;
+ const expiresAfterDays = expiry.trim() === "" ? null : Number(expiry);
+ const expiryValid =
+ expiresAfterDays === null ||
+ (Number.isInteger(expiresAfterDays) &&
+ expiresAfterDays >= 1 &&
+ expiresAfterDays <= maxCreditGrantExpiryDays);
+ const expiryLabel = describeExpiry(expiresAfterDays);
+
const submit = () => {
- if (!amountValid) {
+ if (!amountValid || !expiryValid) {
return;
}
setLastResult(null);
grant.mutate(
{
amountDollars,
+ expiresAfterDays,
...(grantingToSelf
? { userId: account.data.userId }
: { email: overrideEmail }),
},
{
onSuccess: (result) => {
+ const expires = result.grant.expiresAt
+ ? `expires ${new Date(result.grant.expiresAt).toLocaleDateString()}`
+ : "never expires";
setLastResult(
- `Added $${amountDollars} to ${result.targetUser.email} — new balance $${result.balance.balance}.`,
+ `Added $${amountDollars} to ${result.targetUser.email} (${expires}) — new balance $${result.balance.balance}.`,
);
// Reset back to the default recipient (the current user).
setEmailOverride(null);
@@ -137,8 +164,41 @@ export default function SuCreditsPage() {
</div>
</div>
+ <div className="space-y-2">
+ <Label htmlFor="grant-expiry">Expires</Label>
+ <div className="flex flex-wrap items-center gap-2">
+ {creditGrantExpiryPresets.map((preset) => (
+ <Button
+ key={preset.days}
+ onClick={() => setExpiry(String(preset.days))}
+ type="button"
+ variant={expiry === String(preset.days) ? "default" : "outline"}
+ >
+ {preset.label}
+ </Button>
+ ))}
+ <Button
+ onClick={() => setExpiry("")}
+ type="button"
+ variant={expiry.trim() === "" ? "default" : "outline"}
+ >
+ Never
+ </Button>
+ <Input
+ className="max-w-[7rem]"
+ id="grant-expiry"
+ max={maxCreditGrantExpiryDays}
+ min={1}
+ onChange={(event) => setExpiry(event.target.value)}
+ placeholder="Days"
+ type="number"
+ value={expiry}
+ />
+ </div>
+ </div>
+
<Button
- disabled={grant.isPending || !amountValid}
+ disabled={grant.isPending || !amountValid || !expiryValid}
onClick={() => setConfirmOpen(true)}
>
{grant.isPending ? "Granting…" : `Grant $${amountDollars}`}
@@ -151,7 +211,8 @@ export default function SuCreditsPage() {
<DialogDescription>
Grant <span className="font-medium">${amountDollars}</span> in
credits to{" "}
- <span className="font-medium">{recipientLabel}</span>?
+ <span className="font-medium">{recipientLabel}</span>? The
+ grant {expiryLabel}.
</DialogDescription>
</DialogHeader>
<DialogFooter>
diff --git a/site/src/queries/credits.ts b/site/src/queries/credits.ts
index 5c0b5360..1c5c03c5 100644
--- a/site/src/queries/credits.ts
+++ b/site/src/queries/credits.ts
@@ -96,9 +96,15 @@ export function useGrantCredits() {
mutationFn: (input: {
amountDollars: number;
email?: string;
+ // Days until the grant expires; null keeps it forever.
+ expiresAfterDays: number | null;
userId?: string;
}) =>
- apiFetch<{ balance: CreditBalance; targetUser: { email: string } }>(
+ apiFetch<{
+ balance: CreditBalance;
+ grant: { expiresAt: string | null };
+ targetUser: { email: string };
+ }>(
"/api/credits/grants",
{ body: JSON.stringify(input), method: "POST" },
),