Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

add: billing limits states + api layer

Ben Fornefeld93a6b9b86808
diff --git a/apps/web/src/components/Dashboard/BillingAlerts.tsx b/apps/web/src/components/Dashboard/BillingAlerts.tsx
new file mode 100644
index 000000000..6f69061a5
--- /dev/null
+++ b/apps/web/src/components/Dashboard/BillingAlerts.tsx
@@ -0,0 +1,171 @@
+'use client'
+
+import { useState, useEffect, useCallback } from 'react'
+import { useToast } from '../ui/use-toast'
+import { Button } from '../Button'
+import { getBillingUrl } from '@/app/(dashboard)/dashboard/utils'
+import { Team, useUser } from '@/utils/useUser'
+
+interface BillingThreshold {
+ amount_gte: number | null
+ alert_amount_percentage: number | null
+}
+
+export const BillingAlerts = ({
+ team,
+ domain,
+ email,
+}: {
+ team: Team
+ domain: string
+ email: string
+}) => {
+ const { toast } = useToast()
+ const [threshold, setThreshold] = useState<BillingThreshold>({
+ amount_gte: null,
+ alert_amount_percentage: null,
+ })
+ const { user } = useUser()
+
+ const fetchBillingThreshold = useCallback(async () => {
+ if (!user) return
+
+ try {
+ const res = await fetch(
+ getBillingUrl(domain, `/teams/${team.id}/billing-thresholds`),
+ {
+ headers: {
+ 'X-Team-API-Key': team.apiKeys[0],
+ },
+ }
+ )
+
+ if (!res.ok) {
+ toast({
+ title: 'Failed to fetch billing alerts',
+ description: 'Unable to load your billing alert settings',
+ })
+ return
+ }
+
+ const data = await res.json()
+ setThreshold(data)
+ } catch (error) {
+ console.error('Error fetching billing threshold:', error)
+ toast({
+ title: 'Error',
+ description: 'Failed to load billing alert settings',
+ })
+ }
+ }, [user, domain, team, toast])
+
+ const updateBillingThreshold = useCallback(async () => {
+ if (!user) return
+
+ try {
+ const res = await fetch(
+ getBillingUrl(domain, `/teams/${team.id}/billing-thresholds`),
+ {
+ method: 'PATCH',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-Team-API-Key': team.apiKeys[0],
+ },
+ body: JSON.stringify(threshold),
+ }
+ )
+
+ if (!res.ok) {
+ toast({
+ title: 'Failed to update billing alerts',
+ description: 'Unable to save your billing alert settings',
+ })
+ return
+ }
+
+ toast({
+ title: 'Billing alerts updated',
+ description: 'Your billing alert settings have been saved',
+ })
+ } catch (error) {
+ console.error('Error updating billing threshold:', error)
+ toast({
+ title: 'Error',
+ description: 'Failed to save billing alert settings',
+ })
+ }
+ }, [user, domain, team, threshold, toast])
+
+ useEffect(() => {
+ fetchBillingThreshold()
+ }, [fetchBillingThreshold])
+
+ return (
+ <>
+ <div className="flex flex-col lg:flex-row gap-6">
+ <div className="space-y-2 flex-1">
+ <h3 className="font-medium">Set a Budget Alert</h3>
+ <p className="text-sm text-white/70">
+ When your usage reaches this percentage of your budget, you&apos;ll
+ receive an early warning notification to <b>{email}</b>.
+ </p>
+
+ <div className="relative !mt-4">
+ <input
+ type="number"
+ min="1"
+ max="100"
+ className="w-full border border-white/10 text-sm focus:outline-none outline-none rounded-md p-2 pr-12"
+ value={threshold.alert_amount_percentage || ''}
+ onChange={(e) =>
+ setThreshold({
+ ...threshold,
+ alert_amount_percentage: Number(e.target.value) || null,
+ })
+ }
+ placeholder="Enter percentage"
+ />
+ <div className="absolute right-3 top-1/2 -translate-y-1/2 text-sm text-white/50">
+ %
+ </div>
+ </div>
+ </div>
+
+ <div className="space-y-2 flex-1">
+ <h3 className="font-medium">Enable Budget Limit</h3>
+ <p className="text-sm text-white/70">
+ If your organization exceeds this threshold in a given billing
+ period,
+ </p>
+ <p className="text-sm text-red-400">
+ Caution: This helps you monitor spending before reaching your budget
+ limit.
+ </p>
+ <div className="relative !mt-4">
+ <input
+ type="number"
+ className="w-full border border-white/10 text-sm focus:outline-none outline-none rounded-md p-2 pr-12"
+ value={threshold.amount_gte || ''}
+ onChange={(e) =>
+ setThreshold({
+ ...threshold,
+ amount_gte: Number(e.target.value) || null,
+ })
+ }
+ placeholder="Enter amount"
+ />
+ <div className="absolute right-3 top-1/2 -translate-y-1/2 text-sm text-white/50">
+ USD
+ </div>
+ </div>
+ </div>
+ </div>
+ <Button
+ onClick={updateBillingThreshold}
+ className="mt-6 mb-4 float-right"
+ >
+ Save Budget Controls
+ </Button>
+ </>
+ )
+}
diff --git a/apps/web/src/components/Dashboard/Usage.tsx b/apps/web/src/components/Dashboard/Usage.tsx
index 97e86c70a..c29d0cc7e 100644
--- a/apps/web/src/components/Dashboard/Usage.tsx
+++ b/apps/web/src/components/Dashboard/Usage.tsx
@@ -6,6 +6,7 @@ import { Team } from '@/utils/useUser'
import { getBillingUrl } from '@/app/(dashboard)/dashboard/utils'
import { cn } from '@/lib/utils'
import { Skeleton } from '../ui/skeleton'
+import { BillingAlerts } from './BillingAlerts'
type Usage = {
month: number
@@ -115,7 +116,7 @@ export const UsageContent = ({
{/* First Row - Costs and Budget Controls */}
<div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-3">
{/* Cost Card - Full width mobile, half tablet, third desktop */}
- <Card className="bg-inherit/10 border border-white/20 col-span-1 lg:col-span-2 xl:col-span-1">
+ <Card className="bg-inherit/10 border border-white/20 col-span-2 xl:col-span-1">
<CardHeader className="pb-2">
<CardTitle>Costs in USD</CardTitle>
{!isLoading && costThisMonth && (
@@ -138,43 +139,12 @@ export const UsageContent = ({
</Card>
{/* Budget Controls */}
- <Card className="bg-inherit/10 border border-white/20 col-span-1 lg:col-span-2">
+ <Card className="bg-inherit/10 border border-white/20 col-span-2">
<CardHeader className="pb-2">
<CardTitle>Budget Controls</CardTitle>
</CardHeader>
<CardContent>
- <div className="flex flex-col lg:flex-row gap-6">
- <div className="space-y-2 flex-1">
- <h3 className="font-medium">Set a Budget Alert</h3>
- <p className="text-sm text-white/70">
- If your organization exceeds this threshold in a given
- calendar month (UTC), an alert notification will be sent to{' '}
- <b>{team.email}</b>
- </p>
- <input
- type="text"
- value="$35.00"
- className="w-full border !mt-4 border-white/10 text-sm focus:outline-none outline-none rounded-md p-2"
- />
- </div>
-
- <div className="space-y-2 flex-1">
- <h3 className="font-medium">Enable Budget Limit</h3>
- <p className="text-sm text-white/70">
- If your organization exceeds this budget in a given calendar
- month (UTC), subsequent API requests will be rejected.
- </p>
- <p className="text-sm text-red-400">
- Caution: Enabling a Budget Limit could result in interruptions
- to your service.
- </p>
- <input
- type="text"
- value="$50.00"
- className="w-full border !mt-4 border-white/10 text-sm focus:outline-none outline-none rounded-md p-2"
- />
- </div>
- </div>
+ <BillingAlerts team={team} domain={domain} email={team.email} />
</CardContent>
</Card>
</div>