Skip to content

Open source CapCut alternative.

TypeScript

feat(site): show extra vision calls on the settings dashboard

David Lef9deddd5b230
diff --git a/site/src/app/api/billing/usage/route.ts b/site/src/app/api/billing/usage/route.ts
index f9e3fe08..7f445295 100644
--- a/site/src/app/api/billing/usage/route.ts
+++ b/site/src/app/api/billing/usage/route.ts
@@ -1,6 +1,7 @@
import { NextResponse } from "next/server";
import { inferenceUsageRoutes } from "@/lib/credits/inference";
+import { visionCallGrantRemaining } from "@/lib/credits/vision-grants";
import {
donkeySessionUserId,
unauthorizedResponse,
@@ -19,7 +20,7 @@ export const GET = withDonkeyAuth(async (request) => {
return unauthorizedResponse();
}
- const [subscription, recent] = await Promise.all([
+ const [subscription, recent, extraRemaining] = await Promise.all([
prisma.visionApiSubscription.findUnique({ where: { userId } }),
prisma.inferenceUsageEvent.findMany({
orderBy: { createdAt: "desc" },
@@ -38,12 +39,16 @@ export const GET = withDonkeyAuth(async (request) => {
userId,
},
}),
+ // One-time vision-call grants (signup bonus, top-ups) on top of any plan.
+ visionCallGrantRemaining(userId),
]);
const limit = subscription?.monthlyCallQuota ?? 0;
const used = subscription?.periodCallCount ?? 0;
return NextResponse.json({
+ // Extra calls from grants, spent after the subscription quota runs out.
+ extraRemaining: Number(extraRemaining),
limit,
periodEnd: subscription?.currentPeriodEnd?.toISOString() ?? null,
periodStart: subscription?.currentPeriodStart?.toISOString() ?? null,
diff --git a/site/src/app/app/settings/_components/ApiKeysManager.tsx b/site/src/app/app/settings/_components/ApiKeysManager.tsx
index 0de3a1c0..c2440916 100644
--- a/site/src/app/app/settings/_components/ApiKeysManager.tsx
+++ b/site/src/app/app/settings/_components/ApiKeysManager.tsx
@@ -8,7 +8,7 @@ import {
useDeleteApiKey,
type CreatedApiKey,
} from "@/queries/apiKeys";
-import { useSubscription } from "@/queries/billing";
+import { useSubscription, useUsage } from "@/queries/billing";
import { Button } from "@/components/ui/button";
import {
Card,
@@ -39,6 +39,7 @@ import {
export function ApiKeysManager() {
const subscription = useSubscription();
+ const usage = useUsage();
const keys = useApiKeys();
const createKey = useCreateApiKey();
const deleteKey = useDeleteApiKey();
@@ -46,7 +47,11 @@ export function ApiKeysManager() {
const [name, setName] = useState("");
const [createdKey, setCreatedKey] = useState<CreatedApiKey | null>(null);
- const hasActiveSubscription = subscription.data?.isActive ?? false;
+ // Keys can always be created, but only return data when the user has capacity:
+ // an active subscription or remaining extra calls.
+ const hasCapacity =
+ (subscription.data?.isActive ?? false) ||
+ (usage.data?.extraRemaining ?? 0) > 0;
const handleCreate = () => {
const trimmed = name.trim();
@@ -70,11 +75,7 @@ export function ApiKeysManager() {
</CardDescription>
</CardHeader>
<CardContent className="space-y-6">
- {!hasActiveSubscription ? (
- <p className="text-sm text-muted-foreground">
- An active subscription is required to create API keys.
- </p>
- ) : (
+ <div className="space-y-3">
<div className="flex items-end gap-3">
<div className="grid gap-1.5">
<Label htmlFor="key-name">New key name</Label>
@@ -92,7 +93,13 @@ export function ApiKeysManager() {
{createKey.isPending ? "Creating…" : "Create key"}
</Button>
</div>
- )}
+ {!hasCapacity ? (
+ <p className="text-sm text-muted-foreground">
+ Keys won&apos;t return data until you have available calls —
+ subscribe or use your free calls.
+ </p>
+ ) : null}
+ </div>
{keys.isLoading ? (
<Skeleton className="h-24 w-full" />
diff --git a/site/src/app/app/settings/_components/SubscriptionCard.tsx b/site/src/app/app/settings/_components/SubscriptionCard.tsx
index ceb6b663..ad006b97 100644
--- a/site/src/app/app/settings/_components/SubscriptionCard.tsx
+++ b/site/src/app/app/settings/_components/SubscriptionCard.tsx
@@ -72,7 +72,7 @@ export function SubscriptionCard() {
</div>
) : (
<p>
- Subscribe to generate API keys and start calling the Vision API.
+ Subscribe for a monthly quota, or get started with your free calls.
</p>
)}
</CardContent>
diff --git a/site/src/app/app/settings/_components/UsageCard.tsx b/site/src/app/app/settings/_components/UsageCard.tsx
index fa515842..feb2dd5e 100644
--- a/site/src/app/app/settings/_components/UsageCard.tsx
+++ b/site/src/app/app/settings/_components/UsageCard.tsx
@@ -36,9 +36,11 @@ export function UsageCard() {
}
const data = usage.data;
+ const limit = data?.limit ?? 0;
+ const extra = data?.extraRemaining ?? 0;
const pct =
- data && data.limit > 0
- ? Math.min(100, Math.round((data.used / data.limit) * 100))
+ data && limit > 0
+ ? Math.min(100, Math.round((data.used / limit) * 100))
: 0;
return (
@@ -46,13 +48,15 @@ export function UsageCard() {
<CardHeader>
<CardTitle>Usage this period</CardTitle>
<CardDescription>
- {data && data.limit > 0
- ? `${data.used.toLocaleString()} of ${data.limit.toLocaleString()} calls used · ${data.remaining.toLocaleString()} remaining`
- : "No active plan — subscribe to start making calls."}
+ {data && limit > 0
+ ? `${data.used.toLocaleString()} of ${limit.toLocaleString()} calls used · ${data.remaining.toLocaleString()} remaining`
+ : extra > 0
+ ? "No subscription — using your extra calls."
+ : "No active plan — subscribe to start making calls."}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
- {data && data.limit > 0 ? (
+ {data && limit > 0 ? (
<div
aria-label="Quota used"
className="h-2 w-full overflow-hidden rounded-full bg-muted"
@@ -64,6 +68,17 @@ export function UsageCard() {
</div>
) : null}
+ {extra > 0 ? (
+ <div className="flex items-baseline justify-between rounded-md border bg-muted/40 px-3 py-2">
+ <span className="text-sm font-medium text-foreground">
+ Extra calls
+ </span>
+ <span className="text-sm text-muted-foreground">
+ {extra.toLocaleString()} available
+ </span>
+ </div>
+ ) : null}
+
<div>
<h3 className="mb-2 text-sm font-medium">Recent calls</h3>
{data && data.recent.length > 0 ? (
diff --git a/site/src/queries/billing.ts b/site/src/queries/billing.ts
index be50fdfc..604477aa 100644
--- a/site/src/queries/billing.ts
+++ b/site/src/queries/billing.ts
@@ -25,6 +25,9 @@ export type VisionUsage = {
used: number;
limit: number;
remaining: number;
+ // Extra calls from one-time grants (signup bonus, top-ups), spent after the
+ // subscription quota is exhausted.
+ extraRemaining: number;
periodStart: string | null;
periodEnd: string | null;
recent: {